forked from DotNetAnalyzers/StyleCopAnalyzers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSA1500CodeFixProvider.cs
More file actions
301 lines (248 loc) · 12.8 KB
/
SA1500CodeFixProvider.cs
File metadata and controls
301 lines (248 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using StyleCop.Analyzers.Helpers;
using StyleCop.Analyzers.Lightup;
using StyleCop.Analyzers.Settings.ObjectModel;
/// <summary>
/// Implements a code fix for <see cref="SA1500BracesForMultiLineStatementsMustNotShareLine"/>.
/// </summary>
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(SA1500CodeFixProvider))]
[Shared]
internal class SA1500CodeFixProvider : CodeFixProvider
{
/// <inheritdoc/>
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
ImmutableArray.Create(SA1500BracesForMultiLineStatementsMustNotShareLine.DiagnosticId);
/// <inheritdoc/>
public override FixAllProvider GetFixAllProvider()
{
return FixAll.Instance;
}
/// <inheritdoc/>
public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
foreach (Diagnostic diagnostic in context.Diagnostics)
{
context.RegisterCodeFix(
CodeAction.Create(
LayoutResources.SA1500CodeFix,
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken),
nameof(SA1500CodeFixProvider)),
diagnostic);
}
return SpecializedTasks.CompletedTask;
}
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
var braceToken = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
var tokenReplacements = GenerateBraceFixes(settings, ImmutableArray.Create(braceToken));
var newSyntaxRoot = syntaxRoot.ReplaceTokens(tokenReplacements.Keys, (originalToken, rewrittenToken) => tokenReplacements[originalToken]);
return document.WithSyntaxRoot(newSyntaxRoot);
}
private static Dictionary<SyntaxToken, SyntaxToken> GenerateBraceFixes(StyleCopSettings settings, ImmutableArray<SyntaxToken> braceTokens)
{
var tokenReplacements = new Dictionary<SyntaxToken, SyntaxToken>();
foreach (var braceToken in braceTokens)
{
var braceLine = LocationHelpers.GetLineSpan(braceToken).StartLinePosition.Line;
var braceReplacementToken = braceToken;
var indentationSteps = DetermineIndentationSteps(settings.Indentation, braceToken);
var previousToken = braceToken.GetPreviousToken();
if (IsAccessorWithSingleLineBlock(previousToken, braceToken))
{
var newTrailingTrivia = previousToken.TrailingTrivia
.WithoutTrailingWhitespace()
.Add(SyntaxFactory.Space);
AddReplacement(tokenReplacements, previousToken, previousToken.WithTrailingTrivia(newTrailingTrivia));
braceReplacementToken = braceReplacementToken.WithLeadingTrivia(braceToken.LeadingTrivia.WithoutLeadingWhitespace());
}
else
{
// Check if we need to apply a fix before the brace
if (LocationHelpers.GetLineSpan(previousToken).StartLinePosition.Line == braceLine)
{
if (!braceTokens.Contains(previousToken))
{
var sharedTrivia = braceReplacementToken.LeadingTrivia.WithoutTrailingWhitespace();
var previousTokenNewTrailingTrivia = previousToken.TrailingTrivia
.WithoutTrailingWhitespace()
.AddRange(sharedTrivia)
.Add(SyntaxFactory.CarriageReturnLineFeed);
AddReplacement(tokenReplacements, previousToken, previousToken.WithTrailingTrivia(previousTokenNewTrailingTrivia));
}
braceReplacementToken = braceReplacementToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(settings.Indentation, indentationSteps));
}
// Check if we need to apply a fix after the brace. No fix is needed when:
// - The closing brace is followed by a semi-colon or closing paren
// - The closing brace is the last token in the file
// - The closing brace is followed by the while expression of a do/while loop and the
// allowDoWhileOnClosingBrace setting is enabled.
var nextToken = braceToken.GetNextToken();
var nextTokenLine = nextToken.IsKind(SyntaxKind.None) ? -1 : LocationHelpers.GetLineSpan(nextToken).StartLinePosition.Line;
var isMultiDimensionArrayInitializer = braceToken.IsKind(SyntaxKind.OpenBraceToken) && braceToken.Parent.IsKind(SyntaxKind.ArrayInitializerExpression) && braceToken.Parent.Parent.IsKind(SyntaxKind.ArrayInitializerExpression);
var allowDoWhileOnClosingBrace = settings.LayoutRules.AllowDoWhileOnClosingBrace && nextToken.IsKind(SyntaxKind.WhileKeyword) && (braceToken.Parent?.IsKind(SyntaxKind.Block) ?? false) && (braceToken.Parent.Parent?.IsKind(SyntaxKind.DoStatement) ?? false);
if ((nextTokenLine == braceLine) &&
(!braceToken.IsKind(SyntaxKind.CloseBraceToken) || !IsValidFollowingToken(nextToken)) &&
!isMultiDimensionArrayInitializer &&
!allowDoWhileOnClosingBrace)
{
var sharedTrivia = nextToken.LeadingTrivia.WithoutTrailingWhitespace();
var newTrailingTrivia = braceReplacementToken.TrailingTrivia
.WithoutTrailingWhitespace()
.AddRange(sharedTrivia)
.Add(SyntaxFactory.CarriageReturnLineFeed);
if (!braceTokens.Contains(nextToken))
{
int newIndentationSteps = indentationSteps;
if (braceToken.IsKind(SyntaxKind.OpenBraceToken))
{
newIndentationSteps++;
}
if (nextToken.IsKind(SyntaxKind.CloseBraceToken))
{
newIndentationSteps = Math.Max(0, newIndentationSteps - 1);
}
AddReplacement(tokenReplacements, nextToken, nextToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(settings.Indentation, newIndentationSteps)));
}
braceReplacementToken = braceReplacementToken.WithTrailingTrivia(newTrailingTrivia);
}
}
AddReplacement(tokenReplacements, braceToken, braceReplacementToken);
}
return tokenReplacements;
}
private static bool IsAccessorWithSingleLineBlock(SyntaxToken previousToken, SyntaxToken braceToken)
{
if (!braceToken.IsKind(SyntaxKind.OpenBraceToken))
{
return false;
}
switch (previousToken.Kind())
{
case SyntaxKind.GetKeyword:
case SyntaxKind.SetKeyword:
case SyntaxKindEx.InitKeyword:
case SyntaxKind.AddKeyword:
case SyntaxKind.RemoveKeyword:
break;
default:
return false;
}
var token = braceToken;
var depth = 1;
while (depth > 0)
{
token = token.GetNextToken();
switch (token.Kind())
{
case SyntaxKind.CloseBraceToken:
depth--;
break;
case SyntaxKind.OpenBraceToken:
depth++;
break;
}
}
return LocationHelpers.GetLineSpan(braceToken).StartLinePosition.Line == LocationHelpers.GetLineSpan(token).StartLinePosition.Line;
}
private static bool IsValidFollowingToken(SyntaxToken nextToken)
{
switch (nextToken.Kind())
{
case SyntaxKind.SemicolonToken:
case SyntaxKind.CloseParenToken:
case SyntaxKind.CommaToken:
return true;
default:
return false;
}
}
private static int DetermineIndentationSteps(IndentationSettings indentationSettings, SyntaxToken token)
{
// For a closing brace use the indentation of the corresponding opening brace
if (token.IsKind(SyntaxKind.CloseBraceToken))
{
var depth = 1;
while (depth > 0)
{
token = token.GetPreviousToken();
switch (token.Kind())
{
case SyntaxKind.CloseBraceToken:
depth++;
break;
case SyntaxKind.OpenBraceToken:
depth--;
break;
}
}
}
var startLine = GetTokenStartLinePosition(token).Line;
while (!ContainsStartOfLine(token, startLine))
{
token = token.GetPreviousToken();
}
return IndentationHelper.GetIndentationSteps(indentationSettings, token);
}
private static bool ContainsStartOfLine(SyntaxToken token, int startLine)
{
var startLinePosition = GetTokenStartLinePosition(token);
return (startLinePosition.Line < startLine) || (startLinePosition.Character == 0);
}
private static LinePosition GetTokenStartLinePosition(SyntaxToken token)
{
return token.SyntaxTree.GetLineSpan(token.FullSpan).StartLinePosition;
}
private static void AddReplacement(Dictionary<SyntaxToken, SyntaxToken> tokenReplacements, SyntaxToken originalToken, SyntaxToken replacementToken)
{
if (tokenReplacements.ContainsKey(originalToken))
{
// This will only happen when a single keyword (like else) has invalid brace tokens before and after it.
tokenReplacements[originalToken] = tokenReplacements[originalToken].WithTrailingTrivia(replacementToken.TrailingTrivia);
}
else
{
tokenReplacements[originalToken] = replacementToken;
}
}
private class FixAll : DocumentBasedFixAllProvider
{
public static FixAllProvider Instance { get; } =
new FixAll();
protected override string CodeActionTitle =>
LayoutResources.SA1500CodeFix;
protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray<Diagnostic> diagnostics)
{
if (diagnostics.IsEmpty)
{
return null;
}
SyntaxNode syntaxRoot = await document.GetSyntaxRootAsync().ConfigureAwait(false);
var tokens = diagnostics
.Select(diagnostic => syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start))
.OrderBy(token => token.SpanStart)
.ToImmutableArray();
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
var tokenReplacements = GenerateBraceFixes(settings, tokens);
return syntaxRoot.ReplaceTokens(tokenReplacements.Keys, (originalToken, rewrittenToken) => tokenReplacements[originalToken]);
}
}
}
}