Skip to content

Commit

Permalink
Only run Code Cleanup on documents from the VS workspace (#76019)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored Nov 22, 2024
2 parents a608cbd + ee58836 commit f6f08b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@
using Microsoft.CodeAnalysis.Editor;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
using Microsoft.VisualStudio.LanguageServices.Implementation.CodeCleanup;
using Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Utilities;

namespace Microsoft.VisualStudio.LanguageServices.CSharp.LanguageService
{
[Export(typeof(AbstractCodeCleanUpFixer))]
[ContentType(ContentTypeNames.CSharpContentType)]
internal sealed class CSharpCodeCleanUpFixer : AbstractCodeCleanUpFixer
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpCodeCleanUpFixer(IThreadingContext threadingContext, VisualStudioWorkspaceImpl workspace, IVsHierarchyItemManager vsHierarchyItemManager)
: base(threadingContext, workspace, vsHierarchyItemManager)
{
}
}
}
namespace Microsoft.VisualStudio.LanguageServices.CSharp.LanguageService;

[Export(typeof(AbstractCodeCleanUpFixer))]
[ContentType(ContentTypeNames.CSharpContentType)]
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class CSharpCodeCleanUpFixer(
IThreadingContext threadingContext,
VisualStudioWorkspaceImpl workspace,
IVsHierarchyItemManager vsHierarchyItemManager)
: AbstractCodeCleanUpFixer(threadingContext, workspace, vsHierarchyItemManager);
35 changes: 12 additions & 23 deletions src/VisualStudio/Core/Def/CodeCleanup/AbstractCodeCleanUpFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeCleanup;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Progress;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities;
Expand All @@ -34,21 +32,14 @@ namespace Microsoft.VisualStudio.LanguageServices.Implementation.CodeCleanup;
/// be implementing the <see cref="ICodeCleanUpFixer"/> interface, this abstract base class allows Roslyn to operate
/// on MEF instances of fixers known to be relevant in the context of Roslyn languages.
/// </summary>
internal abstract partial class AbstractCodeCleanUpFixer : ICodeCleanUpFixer
internal abstract partial class AbstractCodeCleanUpFixer(
IThreadingContext threadingContext,
VisualStudioWorkspaceImpl workspace,
IVsHierarchyItemManager vsHierarchyItemManager) : ICodeCleanUpFixer
{
private readonly IThreadingContext _threadingContext;
private readonly VisualStudioWorkspaceImpl _workspace;
private readonly IVsHierarchyItemManager _vsHierarchyItemManager;

protected AbstractCodeCleanUpFixer(
IThreadingContext threadingContext,
VisualStudioWorkspaceImpl workspace,
IVsHierarchyItemManager vsHierarchyItemManager)
{
_threadingContext = threadingContext;
_workspace = workspace;
_vsHierarchyItemManager = vsHierarchyItemManager;
}
private readonly IThreadingContext _threadingContext = threadingContext;
private readonly VisualStudioWorkspaceImpl _workspace = workspace;
private readonly IVsHierarchyItemManager _vsHierarchyItemManager = vsHierarchyItemManager;

public Task<bool> FixAsync(ICodeCleanUpScope scope, ICodeCleanUpExecutionContext context)
=> scope switch
Expand Down Expand Up @@ -151,19 +142,17 @@ private Task<bool> FixTextBufferAsync(TextBufferCodeCleanUpScope textBufferScope

// Let LSP handle code cleanup in the cloud scenario
if (buffer.IsInLspEditorContext())
{
return SpecializedTasks.False;
}

var document = buffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();
if (document == null)
{
return SpecializedTasks.False;
}

var workspace = buffer.GetWorkspace();
Contract.ThrowIfNull(workspace);
return FixAsync(workspace, ApplyFixAsync, context);
if (workspace is not VisualStudioWorkspace visualStudioWorkspace)
return SpecializedTasks.False;

return FixAsync(visualStudioWorkspace, ApplyFixAsync, context);

// Local function
async Task<Solution> ApplyFixAsync(IProgress<CodeAnalysisProgress> progress, CancellationToken cancellationToken)
Expand All @@ -177,7 +166,7 @@ async Task<Solution> ApplyFixAsync(IProgress<CodeAnalysisProgress> progress, Can
}

private async Task<bool> FixAsync(
Workspace workspace,
VisualStudioWorkspace workspace,
Func<IProgress<CodeAnalysisProgress>, CancellationToken, Task<Solution>> applyFixAsync,
ICodeCleanUpExecutionContext context)
{
Expand Down

0 comments on commit f6f08b4

Please sign in to comment.