Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit 93fb6b7

Browse files
author
Lakshmi Priya Sekar
committed
Add hack for the rename bug in Roslyn
1 parent 519cd70 commit 93fb6b7

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/Microsoft.DotNet.CodeFormatting/Rules/HasUnderScoreInPrivateFieldNames.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ namespace Microsoft.DotNet.CodeFormatting.Rules
1717
{
1818
// [RuleOrder(10)]
1919
// TODO: Deactivated due to active bug in Roslyn.
20+
// There is a hack to run this rule, but it's slow.
21+
// If needed, enabled rule and enable the hack at the code below in RenameFields.
2022
internal sealed class HasUnderScoreInPrivateFieldNames : IFormattingRule
2123
{
2224
private static string[] AccessorModifiers = { "public", "internal", "protected", "const" };
@@ -69,6 +71,8 @@ private async Task<Solution> RenameFields(Solution solution, DocumentId document
6971
int count = privateFields.Count();
7072
for (int i = 0; i < count; i++)
7173
{
74+
// This is a hack to till the roslyn bug is fixed. Very slow, enable this statement only if the rule is enabled.
75+
// solution = await CleanSolutionAsync(solution, cancellationToken);
7276
var model = await solution.GetDocument(documentId).GetSemanticModelAsync(cancellationToken);
7377
var root = await model.SyntaxTree.GetRootAsync(cancellationToken) as CSharpSyntaxNode;
7478
var symbol = model.GetDeclaredSymbol(root.GetAnnotatedNodes(AnnotationMarker).ElementAt(i), cancellationToken);
@@ -78,5 +82,49 @@ private async Task<Solution> RenameFields(Solution solution, DocumentId document
7882

7983
return solution;
8084
}
85+
86+
private async Task<Solution> CleanSolutionAsync(Solution solution, CancellationToken cancellationToken)
87+
{
88+
var documentIdsToProcess = new List<DocumentId>();
89+
foreach (var document in solution.Projects.SelectMany(p => p.Documents))
90+
{
91+
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
92+
if (root.HasAnnotations("Rename") || true)
93+
{
94+
documentIdsToProcess.Add(document.Id);
95+
}
96+
}
97+
98+
foreach (var documentId in documentIdsToProcess)
99+
{
100+
var root = await solution.GetDocument(documentId).GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
101+
102+
while (true)
103+
{
104+
var renameNodes = root.DescendantNodes(descendIntoTrivia: true).Where(s => s.GetAnnotations("Rename").Any());
105+
if (!renameNodes.Any())
106+
{
107+
break;
108+
}
109+
110+
root = root.ReplaceNode(renameNodes.First(), renameNodes.First().WithoutAnnotations("Rename"));
111+
}
112+
113+
while (true)
114+
{
115+
var renameTokens = root.DescendantTokens(descendIntoTrivia: true).Where(s => s.GetAnnotations("Rename").Any());
116+
if (!renameTokens.Any())
117+
{
118+
break;
119+
}
120+
121+
root = root.ReplaceToken(renameTokens.First(), renameTokens.First().WithoutAnnotations("Rename"));
122+
}
123+
124+
solution = solution.WithDocumentSyntaxRoot(documentId, root);
125+
}
126+
127+
return solution;
128+
}
81129
}
82130
}

0 commit comments

Comments
 (0)