7
7
using Microsoft . CodeAnalysis ;
8
8
using Microsoft . CodeAnalysis . CSharp ;
9
9
using Microsoft . CodeAnalysis . CSharp . Syntax ;
10
+ using Microsoft . CodeAnalysis . Simplification ;
10
11
11
12
namespace Microsoft . DotNet . CodeFormatting . Rules
12
13
{
@@ -17,6 +18,12 @@ private sealed class ExplicitThisRewriter : CSharpSyntaxRewriter
17
18
{
18
19
private readonly SemanticModel _semanticModel ;
19
20
private readonly CancellationToken _cancellationToken ;
21
+ private bool _addedAnnotations ;
22
+
23
+ internal bool AddedAnnotations
24
+ {
25
+ get { return _addedAnnotations ; }
26
+ }
20
27
21
28
internal ExplicitThisRewriter ( SemanticModel semanticModel , CancellationToken cancellationToken )
22
29
{
@@ -38,25 +45,14 @@ public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyn
38
45
var field = ( IFieldSymbol ) symbolInfo . Symbol ;
39
46
if ( field . DeclaredAccessibility == Accessibility . Private )
40
47
{
41
- return RemoveQualification ( node ) ;
48
+ _addedAnnotations = true ;
49
+ return node . WithAdditionalAnnotations ( Simplifier . Annotation ) ;
42
50
}
43
51
}
44
52
}
45
53
46
54
return node ;
47
55
}
48
-
49
- private static NameSyntax RemoveQualification ( MemberAccessExpressionSyntax memberSyntax )
50
- {
51
- var thisSyntax = memberSyntax . Expression ;
52
- var nameSyntax = memberSyntax . Name ;
53
- var triviaList = thisSyntax
54
- . GetLeadingTrivia ( )
55
- . AddRange ( thisSyntax . GetTrailingTrivia ( ) )
56
- . AddRange ( memberSyntax . OperatorToken . GetAllTrivia ( ) )
57
- . AddRange ( nameSyntax . GetLeadingTrivia ( ) ) ;
58
- return nameSyntax . WithLeadingTrivia ( triviaList ) ;
59
- }
60
56
}
61
57
62
58
public async Task < Document > ProcessAsync ( Document document , CancellationToken cancellationToken )
@@ -70,7 +66,12 @@ public async Task<Document> ProcessAsync(Document document, CancellationToken ca
70
66
var semanticModel = await document . GetSemanticModelAsync ( cancellationToken ) ;
71
67
var rewriter = new ExplicitThisRewriter ( semanticModel , cancellationToken ) ;
72
68
var newNode = rewriter . Visit ( syntaxNode ) ;
73
- return document . WithSyntaxRoot ( newNode ) ;
69
+ if ( ! rewriter . AddedAnnotations )
70
+ {
71
+ return document ;
72
+ }
73
+
74
+ return await Simplifier . ReduceAsync ( document . WithSyntaxRoot ( newNode ) , cancellationToken : cancellationToken ) ;
74
75
}
75
76
}
76
77
}
0 commit comments