예제 #1
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic = context.Diagnostics.FirstOrDefault(d => FixableDiagnosticIds.Contains(d.Id));

            if (diagnostic == null)
            {
                return;
            }

            // Register a code action that will invoke the fix.
            context.RegisterCodeFix(
                CreateCodeAction(context.Document, diagnostic),
                diagnostic);
        }
예제 #2
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken);

            // TODO: should we instead try to register for all diagnostics matching our fixable ones instead of just the first one?
            var diagnostic = context.Diagnostics.FirstOrDefault(d => FixableDiagnosticIds.Contains(d.Id));

            if (diagnostic == null)
            {
                return;
            }

            // Register a code action that will invoke the fix.
            context.RegisterCodeFix(
                CreateCodeAction(context.Document, diagnostic),
                diagnostic);
        }
예제 #3
0
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var project   = context.Document.Project;
            var workspace = project.Solution.Workspace;

            // check if we are allowed to add it
            if (!workspace.CanApplyChange(ApplyChangesKind.AddAdditionalDocument))
            {
                return;
            }

            var settingsFile = GetSettingsFile(project);

            // creating additional document from Roslyn is broken (https://github.com/dotnet/roslyn/issues/4655) the nsubstitute.json file have to be created by users manually
            // if there is no settings file do not provide refactorings
            if (settingsFile == null)
            {
                return;
            }

            var root = await context.Document.GetSyntaxRootAsync();

            var model = await context.Document.GetSemanticModelAsync();

            foreach (var diagnostic in context.Diagnostics.Where(diagnostic =>
                                                                 FixableDiagnosticIds.Contains(diagnostic.Id)))
            {
                var syntaxNode = root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
                var symbolInfo = model.GetSymbolInfo(syntaxNode);

                foreach (var innerSymbol in GetSuppressibleSymbol(symbolInfo.Symbol))
                {
                    context.RegisterCodeFix(
                        CodeAction.Create(
                            CreateCodeFixTitle(diagnostic, innerSymbol),
                            cancellationToken => GetTransformedSolutionAsync(context, diagnostic, settingsFile, innerSymbol)),
                        diagnostic);
                }
            }
        }
예제 #4
0
 private ImmutableDictionary <string, ImmutableArray <string> > LoadFixableDiagnosticIdsByPrefix()
 {
     return(FixableDiagnosticIds
            .GroupBy(f => f, DiagnosticIdComparer.Prefix)
            .ToImmutableDictionary(f => DiagnosticIdPrefix.GetPrefix(f.Key), f => f.ToImmutableArray()));
 }
예제 #5
0
 /// <summary>
 /// Returns <see langword="null"/> since the generator does not support batch fixing.
 /// </summary>
 /// <returns></returns>
 // See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md for more information on Fix All Providers
 //public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
 public override FixAllProvider GetFixAllProvider()
 => new StuntFixAllProvider(FixableDiagnosticIds.First(), CreateCodeAction);