private async Task <SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); DocumentEditor editor = await DocumentEditor.CreateAsync(document, fixAllContext.CancellationToken).ConfigureAwait(false); SyntaxNode root = editor.GetChangedRoot(); ImmutableList <SyntaxNode> nodesToChange = ImmutableList.Create <SyntaxNode>(); // Make sure all nodes we care about are tracked foreach (var diagnostic in await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false)) { var location = diagnostic.Location; var syntaxNode = root.FindNode(location.SourceSpan); if (syntaxNode != null) { editor.TrackNode(syntaxNode); nodesToChange = nodesToChange.Add(syntaxNode); } } foreach (var node in nodesToChange) { editor.ReplaceNode(node, node.WithLeadingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed)); } return(editor.GetChangedRoot()); }
private async static Task <SyntaxNode> GetFixedDocumentAsync(FixAllContext fixAllContext, Document document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); var root = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); var nodes = diagnostics.Select(d => root.FindNode(d.Location.SourceSpan)).Where(n => !n.IsMissing).ToList(); var newRoot = root; while (nodes.Any()) { newRoot = newRoot.ReplaceNodes(nodes, (original, rewritten) => original.WithAdditionalAnnotations(removeUnreachableCodeAnnotation)); while (true) { var annotatedNodes = newRoot.GetAnnotatedNodes(removeUnreachableCodeAnnotation); var node = annotatedNodes.FirstOrDefault(); if (node == null) { break; } newRoot = RemoveUnreachableCodeCodeFixProvider.RemoveUnreachableStatement(newRoot, node); } var newDoc = document.WithSyntaxRoot(newRoot); diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(newDoc).ConfigureAwait(false); newRoot = await newDoc.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); nodes = diagnostics.Select(d => newRoot.FindNode(d.Location.SourceSpan)).Where(n => !n.IsMissing).ToList(); } return(newRoot); }
public static async Task <ImmutableArray <Diagnostic> > GetDiagnosticsInScopeAsync(this FixAllContext context) { switch (context.Scope) { case FixAllScope.Document: return(await context.GetDocumentDiagnosticsAsync(context.Document).ConfigureAwait(false)); case FixAllScope.Project: return(await context.GetAllDiagnosticsAsync(context.Project).ConfigureAwait(false)); case FixAllScope.Solution: Solution solution = context.Solution; ProjectDependencyGraph dependencyGraph = solution.GetProjectDependencyGraph(); // Walk through each project in topological order, determining and applying the diagnostics for each // project. We do this in topological order so that the compilations for successive projects are readily // available as we just computed them for dependent projects. If we were to do it out of order, we might // start with a project that has a ton of dependencies, and we'd spend an inordinate amount of time just // building the compilations for it before we could proceed. // // By processing one project at a time, we can also let go of a project once done with it, allowing us to // reclaim lots of the memory so we don't overload the system while processing a large solution. // // Note: we have to filter down to projects of the same language as the FixAllContext points at a // CodeFixProvider, and we can't call into providers of different languages with diagnostics from a // different language. IEnumerable <Project?> sortedProjects = dependencyGraph.GetTopologicallySortedProjects(context.CancellationToken) .Select(solution.GetProject) .Where(p => p.Language == context.Project.Language); return((await Task.WhenAll(sortedProjects.Select(context.GetAllDiagnosticsAsync)).ConfigureAwait(false)).SelectMany(diag => diag).ToImmutableArray()); default: return(ImmutableArray <Diagnostic> .Empty); } }
public override async Task <CodeAction> GetFixAsync(FixAllContext context) { if (context.Scope == FixAllScope.Project || context.Scope == FixAllScope.Solution) { Dictionary <Document, List <SyntaxNode> > nodesToReplace = null; if (context.Scope == FixAllScope.Project) { nodesToReplace = await CollectProjectDiagnostics(context.Project, context); } else if (context.Scope == FixAllScope.Solution) { nodesToReplace = await CollectSolutionDiagnostics(context.Solution, context); } return(CodeAction.Create( title: title, createChangedSolution: c => ReplaceAllWithEnumerableInSolution(context.Solution, nodesToReplace, c), equivalenceKey: title)); } else { var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); var diagnostics = await context.GetDocumentDiagnosticsAsync(context.Document); var nodesToReplace = diagnostics.Select(o => root.FindNode(o.Location.SourceSpan, getInnermostNodeForTie: true)).ToList(); return(CodeAction.Create( title: title, createChangedDocument: c => ReplaceAllWithEnumerable(context.Document, nodesToReplace, c), equivalenceKey: title)); } }
public override async Task <CodeAction> GetFixAsync(FixAllContext fixAllContext) { var diagnosticsToFix = new List <KeyValuePair <Project, ImmutableArray <Diagnostic> > >(); string title = null; switch (fixAllContext.Scope) { case FixAllScope.Document: { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(fixAllContext.Document); diagnostics = diagnostics.Where(d => d.Id == diagnosticId).ToImmutableArray(); if (diagnostics.Length > 0) { diagnosticsToFix.Add(new KeyValuePair <Project, ImmutableArray <Diagnostic> >(fixAllContext.Project, diagnostics)); } title = string.Format(titleFormat, "document", fixAllContext.Document.Name); break; } case FixAllScope.Project: { Project project = fixAllContext.Project; var diagnostics = await fixAllContext.GetAllDiagnosticsAsync(project); if (diagnostics.Length > 0) { diagnosticsToFix.Add(new KeyValuePair <Project, ImmutableArray <Diagnostic> >(project, diagnostics)); } title = string.Format(titleFormat, "project", fixAllContext.Project.Name); break; } case FixAllScope.Solution: { foreach (var project in fixAllContext.Solution.Projects) { var diagnostics = await fixAllContext.GetAllDiagnosticsAsync(project); if (diagnostics.Length > 0) { diagnosticsToFix.Add(new KeyValuePair <Project, ImmutableArray <Diagnostic> >(project, diagnostics)); } } title = string.Format(titleFormat, "solution", ""); break; } case FixAllScope.Custom: return(null); default: break; } return(new FixAllProxiesCodeAction(title, fixAllContext.Solution, diagnosticsToFix)); }
public override async Task <CodeAction?> GetFixAsync(FixAllContext fixAllContext) { var diagnostics = fixAllContext.Scope switch { FixAllScope.Document when fixAllContext.Document is not null => await fixAllContext .GetDocumentDiagnosticsAsync(fixAllContext.Document) .ConfigureAwait(false), FixAllScope.Project => await fixAllContext .GetAllDiagnosticsAsync(fixAllContext.Project) .ConfigureAwait(false), FixAllScope.Solution => await GetSolutionDiagnosticsAsync(fixAllContext).ConfigureAwait(false), _ => default }; if (diagnostics.IsDefaultOrEmpty) { return(null); } return(new MyCodeAction( FixAllContextHelper.GetDefaultFixAllTitle(fixAllContext), cancellationToken => FixAllByDocumentAsync( fixAllContext.Project.Solution, diagnostics, fixAllContext.GetProgressTracker(), cancellationToken ) ));
protected override async Task <SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); if (diagnostics.IsEmpty) { return(null); } SyntaxNode syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); var replaceMap = new Dictionary <SyntaxNode, SyntaxNode>(); foreach (Diagnostic diagnostic in diagnostics) { var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, false, true) as ThisExpressionSyntax; if (node == null || node.IsMissing) { continue; } replaceMap[node.Parent] = GenerateReplacementNode(node); } return(syntaxRoot.ReplaceNodes(replaceMap.Keys, (originalNode, rewrittenNode) => replaceMap[originalNode])); }
protected override async Task <SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); if (diagnostics.IsEmpty) { return(null); } SyntaxNode syntaxRoot = await document.GetSyntaxRootAsync().ConfigureAwait(false); List <SyntaxNode> nodesNeedingQualification = new List <SyntaxNode>(diagnostics.Length); foreach (Diagnostic diagnostic in diagnostics) { var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, false, true) as SimpleNameSyntax; if (node == null || node.IsMissing) { continue; } nodesNeedingQualification.Add(node); } return(syntaxRoot.ReplaceNodes(nodesNeedingQualification, (originalNode, rewrittenNode) => SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpressionSyntax, (SimpleNameSyntax)rewrittenNode.WithoutTrivia().WithoutFormatting()) .WithTriviaFrom(rewrittenNode) .WithoutFormatting())); }
protected override async Task <SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); if (diagnostics.IsEmpty) { return(null); } var root = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); List <SyntaxNode> nodes = new List <SyntaxNode>(); foreach (var diagnostic in diagnostics) { SyntaxNode node = root.FindNode(diagnostic.Location.SourceSpan); if (node.IsMissing) { continue; } nodes.Add(node); } return(root.ReplaceNodes(nodes, (originalNode, rewrittenNode) => AddParentheses(originalNode, rewrittenNode))); }
private async Task <SyntaxNode> GetFixedDocumentAsync(FixAllContext fixAllContext, Document document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); var root = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); var nodes = diagnostics.Select(d => root.FindNode(d.Location.SourceSpan)).Where(n => !n.IsMissing); var newRoot = root.ReplaceNodes(nodes, (original, rewritten) => original.WithAdditionalAnnotations(introduceFieldAnnotation)); var semanticModel = await document.GetSemanticModelAsync(fixAllContext.CancellationToken).ConfigureAwait(false); while (true) { var annotatedNodes = newRoot.GetAnnotatedNodes(introduceFieldAnnotation); var node = annotatedNodes.FirstOrDefault(); if (node == null) { break; } var constructorMethod = (ConstructorDeclarationSyntax)node.Parent.Parent; var parameter = (ParameterSyntax)node; newRoot = IntroduceFieldFromConstructorCodeFixProvider.IntroduceFieldFromConstructor(newRoot, constructorMethod, parameter); node = newRoot.GetAnnotatedNodes(introduceFieldAnnotation).First(); newRoot = newRoot.ReplaceNode(node, node.WithoutAnnotations(introduceFieldAnnotation)); } return(newRoot); }
protected override async Task <SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); if (diagnostics.IsEmpty) { return(null); } var syntaxRoot = await document.GetSyntaxRootAsync().ConfigureAwait(false); List <SyntaxTrivia> trivias = new List <SyntaxTrivia>(); foreach (var diagnostic in diagnostics) { trivias.Add(syntaxRoot.FindTrivia(diagnostic.Location.SourceSpan.Start)); } var tokensWithTrivia = trivias.GroupBy(x => x.Token); Dictionary <SyntaxToken, SyntaxToken> replacements = new Dictionary <SyntaxToken, SyntaxToken>(); foreach (var tokenWithTrivia in tokensWithTrivia) { var token = tokenWithTrivia.Key; var newLeadingTrivia = FixTriviaList(token.LeadingTrivia, tokenWithTrivia); var newTrailingTrivia = FixTriviaList(token.TrailingTrivia, tokenWithTrivia); replacements.Add(token, token.WithLeadingTrivia(newLeadingTrivia).WithTrailingTrivia(newTrailingTrivia)); } return(syntaxRoot.ReplaceTokens(replacements.Keys, (oldToken, newToken) => replacements[oldToken])); }
protected override async Task <SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); if (diagnostics.IsEmpty) { return(null); } SyntaxNode syntaxRoot = await document.GetSyntaxRootAsync().ConfigureAwait(false); List <SyntaxNode> nodesNeedingBlocks = new List <SyntaxNode>(diagnostics.Length); foreach (Diagnostic diagnostic in diagnostics) { var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, false, true) as StatementSyntax; if (node == null || node.IsMissing) { continue; } // If the parent of the statement contains a conditional directive, stuff will be really hard to fix correctly, so don't offer a code fix. if (ContainsConditionalDirectiveTrivia(node.Parent)) { continue; } nodesNeedingBlocks.Add(node); } return(syntaxRoot.ReplaceNodes(nodesNeedingBlocks, (originalNode, rewrittenNode) => SyntaxFactory.Block((StatementSyntax)rewrittenNode))); }
protected override async Task <SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); if (diagnostics.IsEmpty) { return(null); } var text = await document.GetTextAsync().ConfigureAwait(false); List <TextChange> changes = new List <TextChange>(); foreach (var diagnostic in diagnostics) { var sourceSpan = diagnostic.Location.SourceSpan; changes.Add(new TextChange(new TextSpan(sourceSpan.Start, 1), string.Empty)); } changes.Sort((left, right) => left.Span.Start.CompareTo(right.Span.Start)); var tree = await document.GetSyntaxTreeAsync().ConfigureAwait(false); return(await tree.WithChangedText(text.WithChanges(changes)).GetRootAsync().ConfigureAwait(false)); }
private static async Task <ImmutableArray <CodeAction> > GetDocumentEditorActionsAsync(FixAllContext fixAllContext, Document document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document) .ConfigureAwait(false); var actions = new List <CodeAction>(); foreach (var diagnostic in diagnostics) { var codeFixContext = new CodeFixContext( document, diagnostic, (a, _) => { if (a.EquivalenceKey == fixAllContext.CodeActionEquivalenceKey) { actions.Add(a); } }, fixAllContext.CancellationToken); await fixAllContext.CodeFixProvider.RegisterCodeFixesAsync(codeFixContext) .ConfigureAwait(false); } return(actions.ToImmutableArray()); }
protected override async Task <SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); if (diagnostics.IsEmpty) { return(null); } var indentationOptions = IndentationOptions.FromDocument(document); SourceText sourceText = await document.GetTextAsync(fixAllContext.CancellationToken).ConfigureAwait(false); List <TextChange> changes = new List <TextChange>(); foreach (var diagnostic in diagnostics) { changes.Add(FixDiagnostic(indentationOptions, sourceText, diagnostic)); } changes.Sort((left, right) => left.Span.Start.CompareTo(right.Span.Start)); SyntaxTree tree = await document.GetSyntaxTreeAsync(fixAllContext.CancellationToken).ConfigureAwait(false); return(await tree.WithChangedText(sourceText.WithChanges(changes)).GetRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false)); }
protected override async Task <SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); if (diagnostics.IsEmpty) { return(null); } var syntaxRoot = await document.GetSyntaxRootAsync().ConfigureAwait(false); List <SyntaxNode> nodes = new List <SyntaxNode>(); foreach (var diagnostic in diagnostics) { var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true); node = GetRelevantNode(node); if (node != null) { nodes.Add(node); } } return(syntaxRoot.ReplaceNodes(nodes, (oldNode, newNode) => { var newTriviaList = newNode.GetLeadingTrivia(); newTriviaList = newTriviaList.Insert(0, SyntaxFactory.CarriageReturnLineFeed); return newNode.WithLeadingTrivia(newTriviaList); })); }
private async static Task <Document> GetFixedDocumentAsync(FixAllContext fixAllContext, Document document) { var codeFixer = fixAllContext.CodeFixProvider as IFixDocumentInternalsOnly; if (codeFixer == null) { throw new ArgumentException("This CodeFixAllProvider requires that your CodeFixProvider implements the IFixDocumentInternalsOnly."); } var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); if (diagnostics.Length == 0) { return(null); } var root = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); var nodes = diagnostics.Select(d => root.FindNode(d.Location.SourceSpan)).Where(n => !n.IsMissing); var annotations = new List <SyntaxAnnotation>(); var newRoot = root.ReplaceNodes(nodes, (original, rewritten) => { var annotation = new SyntaxAnnotation(SyntaxAnnotationKey); annotations.Add(annotation); var newNode = original.WithAdditionalAnnotations(annotation); return(newNode); }); var newDocument = document.WithSyntaxRoot(newRoot); newDocument = await FixCodeForAnnotatedNodesAsync(newDocument, codeFixer, annotations, fixAllContext.CancellationToken).ConfigureAwait(false); newDocument = await RemoveAnnotationsAsync(newDocument, annotations).ConfigureAwait(false); return(newDocument); }
protected override async Task <SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); if (diagnostics.IsEmpty) { return(null); } var newLine = fixAllContext.Document.Project.Solution.Workspace.Options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp); var text = await document.GetTextAsync().ConfigureAwait(false); List <TextChange> changes = new List <TextChange>(); foreach (var diagnostic in diagnostics) { changes.Add(new TextChange(diagnostic.Location.SourceSpan, newLine)); } changes.Sort((left, right) => left.Span.Start.CompareTo(right.Span.Start)); var tree = await document.GetSyntaxTreeAsync().ConfigureAwait(false); return(await tree.WithChangedText(text.WithChanges(changes)).GetRootAsync().ConfigureAwait(false)); }
public static async Task <ImmutableDictionary <Document, ImmutableArray <Diagnostic> > > GetDocumentDiagnosticsToFixAsync(FixAllContext fixAllContext) { var allDiagnostics = ImmutableArray <Diagnostic> .Empty; var projectsToFix = ImmutableArray <Project> .Empty; var document = fixAllContext.Document; var project = fixAllContext.Project; switch (fixAllContext.Scope) { case FixAllScope.Document: if (document != null) { var documentDiagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); return(ImmutableDictionary <Document, ImmutableArray <Diagnostic> > .Empty.SetItem(document, documentDiagnostics)); } break; case FixAllScope.Project: projectsToFix = ImmutableArray.Create(project); allDiagnostics = await GetAllDiagnosticsAsync(fixAllContext, project).ConfigureAwait(false); break; case FixAllScope.Solution: projectsToFix = project.Solution.Projects .Where(p => p.Language == project.Language) .ToImmutableArray(); var diagnostics = new ConcurrentDictionary <ProjectId, ImmutableArray <Diagnostic> >(); var tasks = new Task[projectsToFix.Length]; for (int i = 0; i < projectsToFix.Length; i++) { fixAllContext.CancellationToken.ThrowIfCancellationRequested(); var projectToFix = projectsToFix[i]; tasks[i] = Task.Run( async() => { var projectDiagnostics = await GetAllDiagnosticsAsync(fixAllContext, projectToFix).ConfigureAwait(false); diagnostics.TryAdd(projectToFix.Id, projectDiagnostics); }, fixAllContext.CancellationToken); } await Task.WhenAll(tasks).ConfigureAwait(false); allDiagnostics = allDiagnostics.AddRange(diagnostics.SelectMany(i => i.Value.Where(x => fixAllContext.DiagnosticIds.Contains(x.Id)))); break; } if (allDiagnostics.IsEmpty) { return(ImmutableDictionary <Document, ImmutableArray <Diagnostic> > .Empty); } return(await GetDocumentDiagnosticsToFixAsync(allDiagnostics, projectsToFix, fixAllContext.CancellationToken).ConfigureAwait(false)); }
private async Task <Document> UpdateDocumentAsync(FixAllContext fixAllContext, CancellationToken cancellationToken) { ImmutableArray <Diagnostic> diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(fixAllContext.Document).ConfigureAwait(false); SyntaxNode newRoot = await GetNewDocumentRootAsync(fixAllContext.Document, diagnostics, cancellationToken).ConfigureAwait(false); return(fixAllContext.Document.WithSyntaxRoot(newRoot)); }
public override async Task <CodeAction?> GetFixAsync(FixAllContext fixAllContext) { // FixAll for source document fixes are handled fine by the batch fixer. if (fixAllContext.CodeActionEquivalenceKey.EndsWith(SourceDocumentEquivalenceKeySuffix, StringComparison.Ordinal)) { return(await WellKnownFixAllProviders.BatchFixer.GetFixAsync(fixAllContext).ConfigureAwait(false)); } // We need custom FixAll handling for additional document fixes. Debug.Assert(fixAllContext.CodeActionEquivalenceKey.EndsWith(AdditionalDocumentEquivalenceKeySuffix, StringComparison.Ordinal)); var diagnosticsToFix = new List <KeyValuePair <Project, ImmutableArray <Diagnostic> > >(); switch (fixAllContext.Scope) { case FixAllScope.Document: { ImmutableArray <Diagnostic> diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(fixAllContext.Document).ConfigureAwait(false); diagnosticsToFix.Add(new KeyValuePair <Project, ImmutableArray <Diagnostic> >(fixAllContext.Project, diagnostics)); break; } case FixAllScope.Project: { Project project = fixAllContext.Project; ImmutableArray <Diagnostic> diagnostics = await fixAllContext.GetAllDiagnosticsAsync(project).ConfigureAwait(false); diagnosticsToFix.Add(new KeyValuePair <Project, ImmutableArray <Diagnostic> >(fixAllContext.Project, diagnostics)); break; } case FixAllScope.Solution: { foreach (Project project in fixAllContext.Solution.Projects) { ImmutableArray <Diagnostic> diagnostics = await fixAllContext.GetAllDiagnosticsAsync(project).ConfigureAwait(false); diagnosticsToFix.Add(new KeyValuePair <Project, ImmutableArray <Diagnostic> >(project, diagnostics)); } break; } case FixAllScope.Custom: return(null); default: Debug.Fail($"Unknown FixAllScope '{fixAllContext.Scope}'"); return(null); } return(new FixAllAdditionalDocumentChangeAction(fixAllContext.Scope, fixAllContext.Solution, diagnosticsToFix, fixAllContext.CodeActionEquivalenceKey)); }
private static async Task <SyntaxNode> CreateNewDocumentSyntaxRootAsync(FixAllContext fixAllContext, Document document, CancellationToken cancellationToken) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var nodesToFix = diagnostics.Select(diagnostic => DefaulIfNullExpressionHelper.GetTargetExpression(diagnostic, root)); return(root.ReplaceNodes(nodesToFix, (orignalNode, rewritten) => DefaulIfNullExpressionHelper.CreateRelacementNode(rewritten))); }
private static async Task <Solution> FixDocumentAsync(FixAllContext fixAllContext, Document document) { Solution solution = document.Project.Solution; var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); if (diagnostics.Length == 0 || fixAllContext.CodeActionEquivalenceKey != await SA1412CodeFixProvider.GetEquivalenceKeyForDocumentAsync(document).ConfigureAwait(false)) { return(solution); } return(await SA1412CodeFixProvider.GetTransformedSolutionAsync(document).ConfigureAwait(false)); }
public override async Task <CodeAction?> GetFixAsync(FixAllContext fixAllContext) { IEnumerable <Diagnostic> allDiagnostics; IEnumerable <(Document Document, SyntaxTree SyntaxTree)> allSyntaxTrees; switch (fixAllContext.Scope) { case FixAllScope.Document: { allDiagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(fixAllContext.Document).ConfigureAwait(false); allSyntaxTrees = new[] { (fixAllContext.Document, await fixAllContext.Document.GetSyntaxTreeAsync(fixAllContext.CancellationToken).ConfigureAwait(false)) };
public override async Task <CodeAction> GetFixAsync(FixAllContext fixAllContext) { if (fixAllContext.Scope == FixAllScope.Document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(fixAllContext.Document); return(CodeAction.Create( "Convert all DependencyProperties in a document", c => ConvertDocumentAsync(fixAllContext.Document, diagnostics, c) )); } else if (fixAllContext.Scope == FixAllScope.Project) { var documentDiagnostics = new Dictionary <Document, IEnumerable <Diagnostic> >(); foreach (var document in fixAllContext.Project.Documents) { documentDiagnostics.Add(document, await fixAllContext.GetDocumentDiagnosticsAsync(document)); } return(CodeAction.Create( "Convert all DependencyProperties in a solution", c => ConvertSolutionAsync(fixAllContext.Solution, documentDiagnostics, c) )); } else if (fixAllContext.Scope == FixAllScope.Solution) { var documentDiagnostics = new Dictionary <Document, IEnumerable <Diagnostic> >(); foreach (var document in fixAllContext.Solution.Projects.SelectMany(project => project.Documents)) { documentDiagnostics.Add(document, await fixAllContext.GetDocumentDiagnosticsAsync(document)); } return(CodeAction.Create( "Convert all DependencyProperties in a solution", c => ConvertSolutionAsync(fixAllContext.Solution, documentDiagnostics, c) )); } else { throw new NotSupportedException(); } }
public override async Task <CodeAction> GetFixAsync(FixAllContext context) { var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); var diagnostics = await context.GetDocumentDiagnosticsAsync(context.Document); var nodesToReplace = diagnostics.Select(o => root.FindNode(o.Location.SourceSpan, getInnermostNodeForTie: true)).ToList(); return(CodeAction.Create( title: title, createChangedDocument: c => ReplaceAllWithTurboContract(context.Document, nodesToReplace, c), equivalenceKey: title)); }
public override async Task <CodeAction?> GetFixAsync(FixAllContext fixAllContext) { var diagnosticsToFix = new List <KeyValuePair <Project, ImmutableArray <Diagnostic> > >(); switch (fixAllContext.Scope) { case FixAllScope.Document: { ImmutableArray <Diagnostic> diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(fixAllContext.Document).ConfigureAwait(false); diagnosticsToFix.Add(new KeyValuePair <Project, ImmutableArray <Diagnostic> >(fixAllContext.Project, diagnostics)); break; } case FixAllScope.Project: { Project project = fixAllContext.Project; ImmutableArray <Diagnostic> diagnostics = await fixAllContext.GetAllDiagnosticsAsync(project).ConfigureAwait(false); diagnosticsToFix.Add(new KeyValuePair <Project, ImmutableArray <Diagnostic> >(fixAllContext.Project, diagnostics)); break; } case FixAllScope.Solution: { foreach (Project project in fixAllContext.Solution.Projects) { ImmutableArray <Diagnostic> diagnostics = await fixAllContext.GetAllDiagnosticsAsync(project).ConfigureAwait(false); diagnosticsToFix.Add(new KeyValuePair <Project, ImmutableArray <Diagnostic> >(project, diagnostics)); } break; } case FixAllScope.Custom: return(null); default: Debug.Fail($"Unknown FixAllScope '{fixAllContext.Scope}'"); return(null); } if (fixAllContext.CodeActionEquivalenceKey == CodeAnalysisDiagnosticsResources.EnableAnalyzerReleaseTrackingRuleTitle) { var projectIds = diagnosticsToFix.Select(d => d.Key.Id).ToImmutableArray(); return(new FixAllAddAdditionalDocumentsAction(projectIds, fixAllContext.Solution)); } return(new FixAllAdditionalDocumentChangeAction(fixAllContext.Scope, fixAllContext.Solution, diagnosticsToFix, fixAllContext.CodeActionEquivalenceKey)); }
private async static Task <Solution> GetFixedProjectAsync(FixAllContext fixAllContext, Project project) { var solution = project.Solution; foreach (var document in project.Documents) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); var newRoot = await GetFixedDocumentAsync(document, diagnostics, fixAllContext.CancellationToken).ConfigureAwait(false); solution = solution.WithDocumentSyntaxRoot(document.Id, newRoot); } return(solution); }
private static async Task <DiagnosticsInDoc> GetDiagnosticsInDocAsync(FixAllContext fixAllContext, Document document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); if (!diagnostics.Any()) { return(DiagnosticsInDoc.Empty); } var root = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false); var doc = DiagnosticsInDoc.Create(document.Id, diagnostics, root); return(doc); }
protected override async Task <SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document) { var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false); if (diagnostics.IsEmpty) { return(null); } var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, fixAllContext.CancellationToken); Document updatedDocument = await FixEndOfFileAsync(document, diagnostics[0], settings.LayoutRules.NewlineAtEndOfFile, fixAllContext.CancellationToken).ConfigureAwait(false); return(await updatedDocument.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false)); }