@@ -17,6 +17,8 @@ namespace Microsoft.DotNet.CodeFormatting.Rules
17
17
{
18
18
// [RuleOrder(10)]
19
19
// 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.
20
22
internal sealed class HasUnderScoreInPrivateFieldNames : IFormattingRule
21
23
{
22
24
private static string [ ] AccessorModifiers = { "public" , "internal" , "protected" , "const" } ;
@@ -69,6 +71,8 @@ private async Task<Solution> RenameFields(Solution solution, DocumentId document
69
71
int count = privateFields . Count ( ) ;
70
72
for ( int i = 0 ; i < count ; i ++ )
71
73
{
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);
72
76
var model = await solution . GetDocument ( documentId ) . GetSemanticModelAsync ( cancellationToken ) ;
73
77
var root = await model . SyntaxTree . GetRootAsync ( cancellationToken ) as CSharpSyntaxNode ;
74
78
var symbol = model . GetDeclaredSymbol ( root . GetAnnotatedNodes ( AnnotationMarker ) . ElementAt ( i ) , cancellationToken ) ;
@@ -78,5 +82,49 @@ private async Task<Solution> RenameFields(Solution solution, DocumentId document
78
82
79
83
return solution ;
80
84
}
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
+ }
81
129
}
82
130
}
0 commit comments