Skip to content

Commit

Permalink
CodeFixContext
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Feb 14, 2022
1 parent d8e95dc commit 6fa1003
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Logging;
using OmniSharp.Mef;
using OmniSharp.Models.FixUsings;
using OmniSharp.Options;
using OmniSharp.Roslyn.Utilities;
using OmniSharp.Services;

Expand All @@ -14,17 +15,17 @@ namespace OmniSharp.Roslyn.CSharp.Services.Refactoring
public class FixUsingService : IRequestHandler<FixUsingsRequest, FixUsingsResponse>
{
private readonly OmniSharpWorkspace _workspace;
private readonly ILoggerFactory _loggerFactory;
private readonly OmniSharpOptions _options;
private readonly IEnumerable<ICodeActionProvider> _providers;

[ImportingConstructor]
public FixUsingService(
OmniSharpWorkspace workspace,
ILoggerFactory loggerFactory,
OmniSharpOptions options,
[ImportMany] IEnumerable<ICodeActionProvider> codeActionProviders)
{
_workspace = workspace;
_loggerFactory = loggerFactory;
_options = options;
_providers = codeActionProviders;
}

Expand All @@ -35,7 +36,7 @@ public async Task<FixUsingsResponse> Handle(FixUsingsRequest request)
var oldDocument = _workspace.GetDocument(request.FileName);
if (oldDocument != null)
{
var fixUsingsResponse = await new FixUsingsWorker(_providers)
var fixUsingsResponse = await new FixUsingsWorker(_providers, _options)
.FixUsingsAsync(oldDocument);

response.AmbiguousResults = fixUsingsResponse.AmbiguousResults;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using Microsoft.Extensions.Logging;
using OmniSharp.Abstractions.Models.V1.FixAll;
using OmniSharp.Mef;
using OmniSharp.Options;
using OmniSharp.Roslyn.CodeActions;
using OmniSharp.Roslyn.CSharp.Services.Refactoring.V2;
using OmniSharp.Roslyn.CSharp.Workers.Diagnostics;
using OmniSharp.Services;
Expand All @@ -26,12 +28,14 @@ public class RunFixAllCodeActionService : BaseCodeActionService<RunFixAllRequest
{
private readonly ILogger<RunFixAllCodeActionService> _logger;
private readonly FixAllDiagnosticProvider _fixAllDiagnosticProvider;
private readonly OmniSharpOptions _options;

[ImportingConstructor]
public RunFixAllCodeActionService(ICsDiagnosticWorker diagnosticWorker,
[ImportMany] IEnumerable<ICodeActionProvider> providers,
CachingCodeFixProviderForProjects codeFixProvider,
OmniSharpWorkspace workspace,
OmniSharpOptions options,
ILoggerFactory loggerFactory) :
base(
workspace,
Expand All @@ -42,6 +46,7 @@ public RunFixAllCodeActionService(ICsDiagnosticWorker diagnosticWorker,
{
_logger = loggerFactory.CreateLogger<RunFixAllCodeActionService>();
_fixAllDiagnosticProvider = new FixAllDiagnosticProvider(diagnosticWorker);
_options = options;
}

public async override Task<RunFixAllResponse> Handle(RunFixAllRequest request)
Expand Down Expand Up @@ -134,15 +139,20 @@ private async Task<Document> FixSpecificDiagnosticIdAsync(Document document, str
}

_logger.LogTrace("{0} is still present in the document. Getting fixes.", diagnosticId);

CodeAction action = null;
var context = new CodeFixContext(document, primaryDiagnostic,
var context = CodeFixContextFactory.Create(
document,
primaryDiagnostic.Location.SourceSpan,
ImmutableArray.Create(primaryDiagnostic),
(a, _) =>
{
if (action == null)
{
action = a;
}
},
_options,
cancellationToken);

await codeFixProvider.RegisterCodeFixesAsync(context).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,7 @@ private void OnWorkspaceChanged(object sender, WorkspaceChangeEventArgs changeEv
}

private AnalyzerOptions CreateAnalyzerOptions(Project project)
{
var ideOptions = new OmniSharpIdeAnalyzerOptions(
ImplementTypeOptions: new OmniSharpImplementTypeOptions(
(OmniSharpImplementTypeInsertionBehavior)_options.ImplementTypeOptions.InsertionBehavior,
(OmniSharpImplementTypePropertyGenerationBehavior)_options.ImplementTypeOptions.PropertyGenerationBehavior));

return OmniSharpWorkspaceAnalyzerOptionsFactory.Create(project.Solution, project.AnalyzerOptions, ideOptions);
}
=> OmniSharpWorkspaceAnalyzerOptionsFactory.Create(project.Solution, project.AnalyzerOptions);

public async Task<IEnumerable<Diagnostic>> AnalyzeDocumentAsync(Document document, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static class FormattingWorker
{
public static async Task<IEnumerable<LinePositionSpanTextChange>> GetFormattingChangesAfterKeystroke(Document document, int position, char character, OmniSharpOptions omnisharpOptions)
{
if (await GetDocumentationCommentChanges(document, position, character, omnisharpOptions) is LinePositionSpanTextChange change)
if (await GetDocumentationCommentChanges(document, position, character) is LinePositionSpanTextChange change)
{
return new[] { change };
}
Expand Down Expand Up @@ -202,7 +202,7 @@ private static OmniSharpBinaryOperatorSpacingOptions BinaryOperatorSpacingOption
_ => OmniSharpBinaryOperatorSpacingOptions.Single,
};

private static async Task<LinePositionSpanTextChange?> GetDocumentationCommentChanges(Document document, int position, char character, OmniSharpOptions omnisharpOptions)
private static async Task<LinePositionSpanTextChange?> GetDocumentationCommentChanges(Document document, int position, char character)
{
if (character != '\n' && character != '/')
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using OmniSharp.Models;
using OmniSharp.Options;
using OmniSharp.Roslyn.CodeActions;
using OmniSharp.Roslyn.CSharp.Services.CodeActions;
using OmniSharp.Services;

Expand All @@ -27,15 +29,17 @@ public class FixUsingsWorker
private readonly IEnumerable<ICodeActionProvider> _providers;
private readonly CodeFixProvider _addImportProvider;
private readonly CodeFixProvider _removeUnnecessaryUsingsProvider;
private readonly OmniSharpOptions _options;

public FixUsingsWorker(IEnumerable<ICodeActionProvider> providers)
public FixUsingsWorker(IEnumerable<ICodeActionProvider> providers, OmniSharpOptions options)
{
_providers = providers;

var codeFixProviders = providers.SelectMany(p => p.CodeFixProviders);

_addImportProvider = FindCodeFixProviderByTypeFullName(codeFixProviders, CodeActionHelper.AddImportProviderName);
_removeUnnecessaryUsingsProvider = FindCodeFixProviderByTypeFullName(codeFixProviders, CodeActionHelper.RemoveUnnecessaryUsingsProviderName);
_options = options;
}

private static CodeFixProvider FindCodeFixProviderByTypeFullName(IEnumerable<CodeFixProvider> providers, string fullName)
Expand Down Expand Up @@ -238,9 +242,10 @@ private async Task<ImmutableArray<CodeActionOperation>> GetCodeFixOperationsAsyn
ImmutableArray<Diagnostic> diagnostics)
{
var codeFixes = new List<CodeAction>();
var context = new CodeFixContext(
var context = CodeFixContextFactory.Create(
document, span, diagnostics,
registerCodeFix: (a, d) => codeFixes.Add(a),
_options,
cancellationToken: CancellationToken.None);

// Note: We're intentionally not checking CodeFixProvider.FixableDiagnosticIds here.
Expand Down
41 changes: 41 additions & 0 deletions src/OmniSharp.Roslyn/Utilities/CodeFixContextFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.CodeActions;
using Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.ImplementType;
using OmniSharp.Options;
using Microsoft.CodeAnalysis;
using System.Collections.Immutable;
using System.Threading;
using Microsoft.CodeAnalysis.Text;

namespace OmniSharp.Roslyn.CodeActions
{
internal static class CodeFixContextFactory
{
public static CodeFixContext Create(
Document document,
TextSpan span,
ImmutableArray<Diagnostic> diagnostics,
Action<CodeAction, ImmutableArray<Diagnostic>> registerCodeFix,
OmniSharpOptions options,
CancellationToken cancellationToken)
{
var implementTypeOptions = new OmniSharpImplementTypeOptions(
(OmniSharpImplementTypeInsertionBehavior)options.ImplementTypeOptions.InsertionBehavior,
(OmniSharpImplementTypePropertyGenerationBehavior)options.ImplementTypeOptions.PropertyGenerationBehavior);

return OmniSharpCodeFixContextFactory.Create(
document,
span,
diagnostics,
registerCodeFix,
implementTypeOptions,
cancellationToken);
}
}
}

0 comments on commit 6fa1003

Please sign in to comment.