Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Immutable;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -70,34 +71,34 @@ private async Task<bool> FixHierarchyContentAsync(IVsHierarchyCodeCleanupScope h
return await FixSolutionAsync(_workspace.CurrentSolution, context).ConfigureAwait(true);
}

var itemId = hierarchyContent.ItemId;
if (itemId == (uint)VSConstants.VSITEMID.Root)
// Map the hierarchy to a ProjectId. For hierarchies mapping to multitargeted projects, we first try to
// get the project in the most recent active context, but fall back to the first target framework if no
// active context is available.
var hierarchyToProjectMap = _workspace.Services.GetRequiredService<IHierarchyItemToProjectIdMap>();

await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(context.OperationContext.UserCancellationToken);
context.OperationContext.UserCancellationToken.ThrowIfCancellationRequested();

ProjectId projectId = null;
if (ErrorHandler.Succeeded(hierarchy.GetProperty((uint)VSConstants.VSITEMID.Root, (int)__VSHPROPID8.VSHPROPID_ActiveIntellisenseProjectContext, out var contextProjectNameObject))
&& contextProjectNameObject is string contextProjectName
&& hierarchy.TryGetProjectGuid(out var projectGuid))
{
// Map the hierarchy to a ProjectId. For hierarchies mapping to multitargeted projects, we first try to
// get the project in the most recent active context, but fall back to the first target framework if no
// active context is available.
var hierarchyToProjectMap = _workspace.Services.GetRequiredService<IHierarchyItemToProjectIdMap>();

await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(context.OperationContext.UserCancellationToken);
context.OperationContext.UserCancellationToken.ThrowIfCancellationRequested();

ProjectId projectId = null;
if (ErrorHandler.Succeeded(hierarchy.GetProperty((uint)VSConstants.VSITEMID.Root, (int)__VSHPROPID8.VSHPROPID_ActiveIntellisenseProjectContext, out var contextProjectNameObject))
&& contextProjectNameObject is string contextProjectName
&& hierarchy.TryGetProjectGuid(out var projectGuid))
{
projectId = _workspace.GetProjectWithGuidAndName(projectGuid, contextProjectName)?.Id;
}
projectId = _workspace.GetProjectWithGuidAndName(projectGuid, contextProjectName)?.Id;
}

if (projectId is null)
if (projectId is null)
{
var projectHierarchyItem = _vsHierarchyItemManager.GetHierarchyItem(hierarchyContent.Hierarchy, (uint)VSConstants.VSITEMID.Root);
if (!hierarchyToProjectMap.TryGetProjectId(projectHierarchyItem, targetFrameworkMoniker: null, out projectId))
{
var projectHierarchyItem = _vsHierarchyItemManager.GetHierarchyItem(hierarchyContent.Hierarchy, itemId);
if (!hierarchyToProjectMap.TryGetProjectId(projectHierarchyItem, targetFrameworkMoniker: null, out projectId))
{
return false;
}
return false;
}
}

var itemId = hierarchyContent.ItemId;
if (itemId == (uint)VSConstants.VSITEMID.Root)
{
await TaskScheduler.Default;

var project = _workspace.CurrentSolution.GetProject(projectId);
Expand All @@ -119,9 +120,18 @@ private async Task<bool> FixHierarchyContentAsync(IVsHierarchyCodeCleanupScope h
}
else
{
// document
// TODO: this one will be implemented later
// https://github.com/dotnet/roslyn/issues/30165
// Handle code cleanup for a single document
await TaskScheduler.Default;

var solution = _workspace.CurrentSolution;
var documentIds = solution.GetDocumentIdsWithFilePath(path);
var documentId = documentIds.FirstOrDefault(id => id.ProjectId == projectId);
if (documentId is null)
{
return false;
}

return await FixDocumentAsync(solution.GetDocument(documentId), context).ConfigureAwait(true);
}
}

Expand Down Expand Up @@ -152,6 +162,18 @@ async Task<Solution> ApplyFixAsync(ProgressTracker progressTracker, Cancellation
}
}

private Task<bool> FixDocumentAsync(Document document, ICodeCleanUpExecutionContext context)
{
return FixAsync(document.Project.Solution.Workspace, ApplyFixAsync, context, document.Name);

// Local function
async Task<Solution> ApplyFixAsync(ProgressTracker progressTracker, CancellationToken cancellationToken)
{
var newDocument = await FixDocumentAsync(document, context.EnabledFixIds, progressTracker, cancellationToken).ConfigureAwait(true);
return newDocument.Project.Solution;
}
}

private Task<bool> FixTextBufferAsync(TextBufferCodeCleanUpScope textBufferScope, ICodeCleanUpExecutionContext context)
{
var buffer = textBufferScope.SubjectBuffer;
Expand Down