本文整理汇总了C#中Microsoft.CodeAnalysis.CodeFixes.FixAllContext.GetDocumentDiagnosticsAsync方法的典型用法代码示例。如果您正苦于以下问题:C# FixAllContext.GetDocumentDiagnosticsAsync方法的具体用法?C# FixAllContext.GetDocumentDiagnosticsAsync怎么用?C# FixAllContext.GetDocumentDiagnosticsAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.CodeFixes.FixAllContext
的用法示例。
在下文中一共展示了FixAllContext.GetDocumentDiagnosticsAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FixAllInDocumentAsync
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();
}
示例2: FixAllInDocumentAsync
protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document)
{
var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false);
if (diagnostics.IsEmpty)
{
return null;
}
var newDocument = document;
var root = await newDocument.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
// First annotate all expressions that need parenthesis with a temporary annotation.
// With this annotation we can find the nodes that need parenthesis even if
// the source span changes.
foreach (var diagnostic in diagnostics)
{
SyntaxNode node = root.FindNode(diagnostic.Location.SourceSpan);
if (node.IsMissing)
{
continue;
}
root = root.ReplaceNode(node, node.WithAdditionalAnnotations(NeedsParenthesisAnnotation));
}
return root.ReplaceNodes(root.GetAnnotatedNodes(NeedsParenthesisAnnotation), this.AddParentheses);
}
示例3: FixAllInDocumentAsync
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]);
}
示例4: FixAllInDocumentAsync
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());
}
示例5: GetDiagnosticsInDocAsync
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;
}
示例6: GetDocumentDiagnosticsToFixAsync
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);
}
示例7: CreateNewDocumentSyntaxRootAsync
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));
}
示例8: GetFixedProjectAsync
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;
}
示例9: FixAllInDocumentAsync
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);
}
示例10: FixDocumentAsync
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);
}
示例11: FixAllInDocumentAsync
protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document)
{
var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false);
SyntaxNode root = await document.GetSyntaxRootAsync().ConfigureAwait(false);
var nodesToRemove = diagnostics.Select(d => root.FindNode(d.Location.SourceSpan, findInsideTrivia: true))
.Where(node => node != null && !node.IsMissing)
.OfType<RegionDirectiveTriviaSyntax>()
.SelectMany(node => node.GetRelatedDirectives())
.Where(node => !node.IsMissing);
return root.RemoveNodes(nodesToRemove, SyntaxRemoveOptions.AddElasticMarker);
}
示例12: FixAllInDocumentAsync
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(fixAllContext.CancellationToken).ConfigureAwait(false);
var nodes = diagnostics.Select(diagnostic => syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true));
return syntaxRoot.ReplaceNodes(nodes, (originalNode, rewrittenNode) => GetReplacementNode(rewrittenNode));
}
示例13: GetFixedDocumentAsync
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;
}
示例14: GetFixedDocumentAsync
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, getInnermostNodeForTie: true).FirstAncestorOrSelfOfType<InvocationExpressionSyntax>()).Where(n => !n.IsMissing).ToList();
var newRoot = root.ReplaceNodes(nodes, (original, rewritten) => rewritten.WithAdditionalAnnotations(stringFormatAnnotation));
while (true)
{
var annotatedNodes = newRoot.GetAnnotatedNodes(stringFormatAnnotation);
var node = annotatedNodes.FirstOrDefault();
if (node == null) break;
newRoot = StringFormatCodeFixProvider.CreateNewStringInterpolation(newRoot, (InvocationExpressionSyntax)node);
}
return newRoot;
}
示例15: GetFixedDocumentAsync
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);
var newRoot = root.ReplaceNodes(nodes, (original, rewritten) => original.WithAdditionalAnnotations(nestedIfAnnotation));
while (true)
{
var annotatedNodes = newRoot.GetAnnotatedNodes(nestedIfAnnotation);
var condition = (BinaryExpressionSyntax)annotatedNodes.FirstOrDefault();
if (condition == null) break;
var ifStatement = condition.FirstAncestorOfType<IfStatementSyntax>();
newRoot = SplitIntoNestedIfCodeFixProvider.CreateNestedIf(condition, newRoot);
}
return newRoot;
}