public static async Task <Solution> RefactorAsync( Document document, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken = default(CancellationToken)) { Solution solution = document.Solution(); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); IMethodSymbol methodSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration, cancellationToken); string propertyName = methodDeclaration.Identifier.ValueText; ImmutableArray <DocumentReferenceInfo> infos = await SyntaxFinder.FindReferencesByDocumentAsync(methodSymbol, solution, allowCandidate : false, cancellationToken : cancellationToken).ConfigureAwait(false); foreach (DocumentReferenceInfo info in infos) { var rewriter = new ReplaceMethodWithPropertySyntaxRewriter(info.References, propertyName, methodDeclaration); SyntaxNode newRoot = rewriter.Visit(info.Root); solution = solution.WithDocumentSyntaxRoot(info.Document.Id, newRoot); } if (!infos.Any(f => f.Document.Id == document.Id)) { SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); SyntaxNode newRoot = root.ReplaceNode(methodDeclaration, ToPropertyDeclaration(methodDeclaration)); solution = solution.WithDocumentSyntaxRoot(document.Id, newRoot); } return(solution); }
public static async Task <Solution> RefactorAsync( Document document, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken = default(CancellationToken)) { Solution solution = document.Project.Solution; SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); IMethodSymbol methodSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration, cancellationToken); IEnumerable <ReferencedSymbol> referencedSymbols = await SymbolFinder.FindReferencesAsync(methodSymbol, solution, cancellationToken).ConfigureAwait(false); ReferenceLocation[] locations = referencedSymbols .SelectMany(f => f.Locations) .Where(f => !f.IsCandidateLocation && !f.IsImplicit) .ToArray(); string propertyName = GetPropertyName(methodDeclaration); bool isMethodReplaced = false; foreach (IGrouping <DocumentId, ReferenceLocation> grouping in locations .GroupBy(f => f.Document.Id)) { Document document2 = solution.GetDocument(grouping.Key); SyntaxNode root = await document2.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); TextSpan[] spans = grouping.Select(f => f.Location.SourceSpan).ToArray(); MethodDeclarationSyntax methodDeclaration2 = null; if (document.Id == document2.Id) { isMethodReplaced = true; methodDeclaration2 = methodDeclaration; } var rewriter = new ReplaceMethodWithPropertySyntaxRewriter(spans, propertyName, methodDeclaration2); SyntaxNode newRoot = rewriter.Visit(root); solution = solution.WithDocumentSyntaxRoot(grouping.Key, newRoot); } if (!isMethodReplaced) { document = solution.GetDocument(document.Id); SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var rewriter = new ReplaceMethodWithPropertySyntaxRewriter(new TextSpan[0], propertyName, methodDeclaration); SyntaxNode newRoot = rewriter.Visit(root); solution = solution.WithDocumentSyntaxRoot(document.Id, newRoot); } return(solution); }