public override async Task RegisterCodeFixesAsync(CodeFixContext context) { var document = context.Document; var cancellationToken = context.CancellationToken; var syntaxRoot = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var compilationUnit = (CompilationUnitSyntax)syntaxRoot; var options = await document.GetCSharpCodeFixOptionsProviderAsync(context.GetOptionsProvider(), cancellationToken).ConfigureAwait(false); var simplifierOptions = options.GetSimplifierOptions(); // Read the preferred placement option and verify if it can be applied to this code file. // There are cases where we will not be able to fix the diagnostic and the user will need to resolve // it manually. var(placement, preferPreservation) = DeterminePlacement(compilationUnit, options.UsingDirectivePlacement); if (preferPreservation) { return; } foreach (var diagnostic in context.Diagnostics) { context.RegisterCodeFix( CodeAction.Create( CSharpAnalyzersResources.Move_misplaced_using_directives, token => GetTransformedDocumentAsync(document, compilationUnit, GetAllUsingDirectives(compilationUnit), placement, simplifierOptions, token), nameof(CSharpAnalyzersResources.Move_misplaced_using_directives)), diagnostic); } }
private async Task <Document> FixOneAsync(CodeFixContext context, Diagnostic diagnostic, CancellationToken cancellationToken) { var options = await context.Document.GetCodeFixOptionsAsync(context.GetOptionsProvider(), cancellationToken).ConfigureAwait(false); var formattingOptions = options.GetFormattingOptions(SyntaxFormatting); var tree = await context.Document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); var updatedTree = await FormattingCodeFixHelper.FixOneAsync(tree, SyntaxFormatting, formattingOptions, diagnostic, cancellationToken).ConfigureAwait(false); return(context.Document.WithText(await updatedTree.GetTextAsync(cancellationToken).ConfigureAwait(false))); }
public sealed override Task RegisterCodeFixesAsync(CodeFixContext context) { var title = GetTitle(); context.RegisterCodeFix( CodeAction.Create( title, c => RemoveUnnecessaryImportsAsync(context.Document, context.GetOptionsProvider(), c), title), context.Diagnostics); return(Task.CompletedTask); }
public override Task RegisterCodeFixesAsync(CodeFixContext context) { var document = context.Document; var diagnostic = context.Diagnostics.First(); context.RegisterCodeFix( CodeAction.Create( CSharpCodeFixesResources.Place_statement_on_following_line, c => FixAllAsync(document, ImmutableArray.Create(diagnostic), context.GetOptionsProvider(), c), nameof(CSharpCodeFixesResources.Place_statement_on_following_line)), context.Diagnostics); return(Task.CompletedTask); }
public override Task RegisterCodeFixesAsync(CodeFixContext context) { if (context.Document.Project.Solution.Workspace.CanApplyChange(ApplyChangesKind.ChangeDocumentInfo)) { context.RegisterCodeFix( CodeAction.Create( AnalyzersResources.Change_namespace_to_match_folder_structure, cancellationToken => FixAllInDocumentAsync(context.Document, context.Diagnostics, context.GetOptionsProvider(), cancellationToken), nameof(AnalyzersResources.Change_namespace_to_match_folder_structure)), context.Diagnostics); } return(Task.CompletedTask); }
public override Task RegisterCodeFixesAsync(CodeFixContext context) { foreach (var diagnostic in context.Diagnostics) { context.RegisterCodeFix( CodeAction.Create( CodeFixesResources.Add_file_header, cancellationToken => GetTransformedDocumentAsync(context.Document, context.GetOptionsProvider(), cancellationToken), nameof(AbstractFileHeaderCodeFixProvider)), diagnostic); } return(Task.CompletedTask); }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { var cancellationToken = context.CancellationToken; var document = context.Document; var syntaxFacts = document.GetRequiredLanguageService <ISyntaxFactsService>(); var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); // Innermost: We are looking for an IdentifierName. IdentifierName is sometimes at the same span as its parent (e.g. SimpleBaseTypeSyntax). var diagnosticNode = root.FindNode(context.Span, getInnermostNodeForTie: true); if (!syntaxFacts.IsIdentifierName(diagnosticNode)) { return; } var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false); var symbolInfo = semanticModel.GetSymbolInfo(diagnosticNode, cancellationToken); if (SymbolCandidatesContainsSupportedSymbols(symbolInfo)) { var addImportService = document.GetRequiredLanguageService <IAddImportsService>(); var syntaxGenerator = document.GetRequiredLanguageService <SyntaxGenerator>(); var compilation = semanticModel.Compilation; var options = await document.GetAddImportPlacementOptionsAsync(addImportService, context.GetOptionsProvider(), cancellationToken).ConfigureAwait(false); var codeActionsBuilder = ImmutableArray.CreateBuilder <CodeAction>(symbolInfo.CandidateSymbols.Length); foreach (var symbol in symbolInfo.CandidateSymbols.Cast <ITypeSymbol>()) { var typeName = symbol.Name; var title = GetTextPreviewOfChange(typeName, symbol); codeActionsBuilder.Add(CodeAction.Create( title, cancellationToken => { var aliasDirective = syntaxGenerator.AliasImportDeclaration(typeName, symbol); var newRoot = addImportService.AddImport(compilation, root, diagnosticNode, aliasDirective, syntaxGenerator, options, cancellationToken); return(Task.FromResult(document.WithSyntaxRoot(newRoot))); }, title)); } var groupingTitle = string.Format(CodeFixesResources.Alias_ambiguous_type_0, diagnosticNode.ToString()); var groupingCodeAction = CodeAction.Create(groupingTitle, codeActionsBuilder.ToImmutable(), isInlinable: true); context.RegisterCodeFix(groupingCodeAction, context.Diagnostics.First()); } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { var root = await context.Document.GetRequiredSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); var diagnostic = context.Diagnostics.First(); var diagnosticSpan = diagnostic.Location.SourceSpan; var token = root.FindToken(diagnosticSpan.Start); var originalNode = token.GetAncestor <PropertyDeclarationSyntax>() ?? token.GetAncestor <MethodDeclarationSyntax>() ?? (SyntaxNode?)token.GetAncestor <FieldDeclarationSyntax>(); if (originalNode == null) { return; } context.RegisterCodeFix(new AddNewKeywordAction(context.Document, originalNode, context.GetOptionsProvider()), context.Diagnostics); }
public override Task RegisterCodeFixesAsync(CodeFixContext context) { var document = context.Document; var diagnostic = context.Diagnostics.First(); context.RegisterCodeFix(CodeAction.Create( CodeFixesResources.Add_blank_line_after_block, c => UpdateDocumentAsync(document, diagnostic, context.GetOptionsProvider(), c), nameof(CodeFixesResources.Add_blank_line_after_block)), context.Diagnostics); return(Task.CompletedTask); }