private static async Task <Document> FixDocumentAsync(FixAllContext context) { var syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken) .ConfigureAwait(false); using (var sorted = new SortedMembers()) { var updated = new SortRewriter(sorted).Visit(syntaxRoot); return(context.Document.WithSyntaxRoot(updated)); } }
/// <inheritdoc/> public override async Task RegisterCodeFixesAsync(CodeFixContext context) { var syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken) .ConfigureAwait(false) as CompilationUnitSyntax; var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false); if (syntaxRoot == null) { return; } foreach (var diagnostic in context.Diagnostics) { var token = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start); if (string.IsNullOrEmpty(token.ValueText) || token.IsMissing || syntaxRoot.Members.Count != 1) { continue; } var property = (BasePropertyDeclarationSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan); if (!property.IsPropertyOrIndexer()) { continue; } using (var sorted = new SortedMembers()) { sorted.Sort(property, semanticModel, context.CancellationToken); var updated = new SortRewriter(sorted, semanticModel, context.CancellationToken).Visit(syntaxRoot); context.RegisterCodeFix( CodeAction.Create( "Move property.", _ => Task.FromResult(context.Document.WithSyntaxRoot(updated)), this.GetType().FullName), diagnostic); } } }
public SortRewriter(SortedMembers sorted, SemanticModel semanticModel, CancellationToken cancellationToken) { this.sorted = sorted; this.semanticModel = semanticModel; this.cancellationToken = cancellationToken; }
public SortRewriter(SortedMembers sorted) { this.sorted = sorted; }