Skip to content
This repository was archived by the owner on Nov 8, 2018. It is now read-only.

Commit 359de14

Browse files
committed
Add RenameHelper
1 parent 41d66c0 commit 359de14

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

AsyncUsageAnalyzers/AsyncUsageAnalyzers/AsyncUsageAnalyzers.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<Compile Include="AnalyzerConstants.cs" />
4646
<Compile Include="AnalyzerExtensions.cs" />
4747
<Compile Include="GeneratedCodeAnalysisExtensions.cs" />
48+
<Compile Include="Helpers\RenameHelper.cs" />
4849
<Compile Include="NoCodeFixAttribute.cs" />
4950
<Compile Include="Properties\AssemblyInfo.cs" />
5051
<Compile Include="Resources.Designer.cs">
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace AsyncUsageAnalyzers.Helpers
2+
{
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Microsoft.CodeAnalysis;
6+
using Microsoft.CodeAnalysis.CodeActions;
7+
using Microsoft.CodeAnalysis.Rename;
8+
9+
internal static class RenameHelper
10+
{
11+
public static async Task<Solution> RenameSymbolAsync(Document document, SyntaxNode root, SyntaxToken declarationToken, string newName, CancellationToken cancellationToken)
12+
{
13+
var annotatedRoot = root.ReplaceToken(declarationToken, declarationToken.WithAdditionalAnnotations(RenameAnnotation.Create()));
14+
var annotatedSolution = document.Project.Solution.WithDocumentSyntaxRoot(document.Id, annotatedRoot);
15+
var annotatedDocument = annotatedSolution.GetDocument(document.Id);
16+
17+
annotatedRoot = await annotatedDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
18+
var annotatedToken = annotatedRoot.FindToken(declarationToken.SpanStart);
19+
20+
var semanticModel = await annotatedDocument.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
21+
var symbol = semanticModel?.GetDeclaredSymbol(annotatedToken.Parent, cancellationToken);
22+
23+
var newSolution = await Renamer.RenameSymbolAsync(annotatedSolution, symbol, newName, null, cancellationToken).ConfigureAwait(false);
24+
25+
// TODO: return annotatedSolution instead of newSolution if newSolution contains any new errors (for any project)
26+
return newSolution;
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)