forked from dotnet/roslyn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeActionHelpers.cs
308 lines (272 loc) · 14.2 KB
/
CodeActionHelpers.cs
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
302
303
304
305
306
307
308
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.UnifiedSuggestions;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Roslyn.Utilities;
using static Microsoft.CodeAnalysis.CodeActions.CodeAction;
using CodeAction = Microsoft.CodeAnalysis.CodeActions.CodeAction;
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol;
namespace Microsoft.CodeAnalysis.LanguageServer.Handler.CodeActions
{
internal static class CodeActionHelpers
{
/// <summary>
/// Get, order, and filter code actions, and then transform them into VSCodeActions.
/// </summary>
/// <remarks>
/// Used by CodeActionsHandler.
/// </remarks>
public static async Task<VSCodeAction[]> GetVSCodeActionsAsync(
CodeActionParams request,
CodeActionsCache codeActionsCache,
Document document,
ICodeFixService codeFixService,
ICodeRefactoringService codeRefactoringService,
CancellationToken cancellationToken)
{
var actionSets = await GetActionSetsAsync(
document, codeFixService, codeRefactoringService, request.Range, cancellationToken).ConfigureAwait(false);
if (!actionSets.HasValue)
{
return Array.Empty<VSCodeAction>();
}
await codeActionsCache.UpdateActionSetsAsync(document, request.Range, actionSets.Value, cancellationToken).ConfigureAwait(false);
var documentText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
// Each suggested action set should have a unique set number, which is used for grouping code actions together.
var currentHighestSetNumber = 0;
using var _ = ArrayBuilder<VSCodeAction>.GetInstance(out var codeActions);
foreach (var set in actionSets)
{
var currentSetNumber = ++currentHighestSetNumber;
foreach (var suggestedAction in set.Actions)
{
// Filter out code actions with options since they'll show dialogs and we can't remote the UI and the options.
if (suggestedAction.OriginalCodeAction is CodeActionWithOptions)
{
continue;
}
codeActions.Add(GenerateVSCodeAction(
request, documentText,
suggestedAction: suggestedAction,
codeActionKind: GetCodeActionKindFromSuggestedActionCategoryName(set.CategoryName!),
setPriority: set.Priority,
applicableRange: set.ApplicableToSpan.HasValue ? ProtocolConversions.TextSpanToRange(set.ApplicableToSpan.Value, documentText) : null,
currentSetNumber: currentSetNumber,
currentHighestSetNumber: ref currentHighestSetNumber));
}
}
return codeActions.ToArray();
}
private static VSCodeAction GenerateVSCodeAction(
CodeActionParams request,
SourceText documentText,
IUnifiedSuggestedAction suggestedAction,
LSP.CodeActionKind codeActionKind,
UnifiedSuggestedActionSetPriority setPriority,
LSP.Range? applicableRange,
int currentSetNumber,
ref int currentHighestSetNumber,
string currentTitle = "")
{
if (!string.IsNullOrEmpty(currentTitle))
{
// Adding a delimiter for nested code actions, e.g. 'Suppress or Configure issues|Suppress IDEXXXX|in Source'
currentTitle += '|';
}
var codeAction = suggestedAction.OriginalCodeAction;
currentTitle += codeAction.Title;
// Nested code actions' unique identifiers consist of: parent code action unique identifier + '|' + title of code action
var nestedActions = GenerateNestedVSCodeActions(request, documentText, suggestedAction, codeActionKind, ref currentHighestSetNumber, currentTitle);
return new VSCodeAction
{
Title = codeAction.Title,
Kind = codeActionKind,
Diagnostics = request.Context.Diagnostics,
Children = nestedActions,
Priority = UnifiedSuggestedActionSetPriorityToPriorityLevel(setPriority),
Group = $"Roslyn{currentSetNumber}",
ApplicableRange = applicableRange,
Data = new CodeActionResolveData(currentTitle, request.Range, request.TextDocument)
};
static VSCodeAction[] GenerateNestedVSCodeActions(
CodeActionParams request,
SourceText documentText,
IUnifiedSuggestedAction suggestedAction,
CodeActionKind codeActionKind,
ref int currentHighestSetNumber,
string currentTitle)
{
if (suggestedAction is not UnifiedSuggestedActionWithNestedActions suggestedActionWithNestedActions)
{
return Array.Empty<VSCodeAction>();
}
using var _ = ArrayBuilder<VSCodeAction>.GetInstance(out var nestedActions);
foreach (var nestedActionSet in suggestedActionWithNestedActions.NestedActionSets)
{
// Nested code action sets should each have a unique set number that is not yet assigned to any set.
var nestedSetNumber = ++currentHighestSetNumber;
foreach (var nestedSuggestedAction in nestedActionSet.Actions)
{
nestedActions.Add(GenerateVSCodeAction(
request, documentText, nestedSuggestedAction, codeActionKind, nestedActionSet.Priority,
applicableRange: nestedActionSet.ApplicableToSpan.HasValue
? ProtocolConversions.TextSpanToRange(nestedActionSet.ApplicableToSpan.Value, documentText) : null,
nestedSetNumber, ref currentHighestSetNumber, currentTitle));
}
}
return nestedActions.ToArray();
}
}
/// <summary>
/// Get, order, and filter code actions.
/// </summary>
/// <remarks>
/// Used by CodeActionResolveHandler and RunCodeActionHandler.
/// </remarks>
public static async Task<ImmutableArray<CodeAction>> GetCodeActionsAsync(
CodeActionsCache codeActionsCache,
Document document,
LSP.Range selection,
ICodeFixService codeFixService,
ICodeRefactoringService codeRefactoringService,
CancellationToken cancellationToken)
{
var actionSets = await GetActionSetsAsync(
document, codeFixService, codeRefactoringService, selection, cancellationToken).ConfigureAwait(false);
if (!actionSets.HasValue)
{
actionSets = await GetActionSetsAsync(
document, codeFixService, codeRefactoringService, selection, cancellationToken).ConfigureAwait(false);
if (!actionSets.HasValue)
{
return ImmutableArray<CodeAction>.Empty;
}
await codeActionsCache.UpdateActionSetsAsync(document, selection, actionSets.Value, cancellationToken).ConfigureAwait(false);
}
var _ = ArrayBuilder<CodeAction>.GetInstance(out var codeActions);
foreach (var set in actionSets)
{
foreach (var suggestedAction in set.Actions)
{
// Filter out code actions with options since they'll show dialogs and we can't remote the UI and the options.
if (suggestedAction.OriginalCodeAction is CodeActionWithOptions)
{
continue;
}
codeActions.Add(GetNestedActionsFromActionSet(suggestedAction));
}
}
return codeActions.ToImmutable();
}
/// <summary>
/// Generates a code action with its nested actions properly set.
/// </summary>
private static CodeAction GetNestedActionsFromActionSet(IUnifiedSuggestedAction suggestedAction)
{
var codeAction = suggestedAction.OriginalCodeAction;
if (suggestedAction is not UnifiedSuggestedActionWithNestedActions suggestedActionWithNestedActions)
{
return codeAction;
}
using var _ = ArrayBuilder<CodeAction>.GetInstance(out var nestedActions);
foreach (var actionSet in suggestedActionWithNestedActions.NestedActionSets)
{
foreach (var action in actionSet.Actions)
{
nestedActions.Add(GetNestedActionsFromActionSet(action));
}
}
return new CodeActionWithNestedActions(
codeAction.Title, nestedActions.ToImmutable(), codeAction.IsInlinable, codeAction.Priority);
}
private static async Task<ImmutableArray<UnifiedSuggestedActionSet>?> GetActionSetsAsync(
Document document,
ICodeFixService codeFixService,
ICodeRefactoringService codeRefactoringService,
LSP.Range selection,
CancellationToken cancellationToken)
{
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var textSpan = ProtocolConversions.RangeToTextSpan(selection, text);
var codeFixes = await UnifiedSuggestedActionsSource.GetFilterAndOrderCodeFixesAsync(
document.Project.Solution.Workspace, codeFixService, document, textSpan, includeSuppressionFixes: true,
isBlocking: false, addOperationScope: _ => null, cancellationToken).ConfigureAwait(false);
var codeRefactorings = await UnifiedSuggestedActionsSource.GetFilterAndOrderCodeRefactoringsAsync(
document.Project.Solution.Workspace, codeRefactoringService, document, textSpan, isBlocking: false,
addOperationScope: _ => null, filterOutsideSelection: false, cancellationToken).ConfigureAwait(false);
var actionSets = UnifiedSuggestedActionsSource.FilterAndOrderActionSets(codeFixes, codeRefactorings, textSpan);
return actionSets;
}
private static CodeActionKind GetCodeActionKindFromSuggestedActionCategoryName(string categoryName)
=> categoryName switch
{
UnifiedPredefinedSuggestedActionCategoryNames.CodeFix => CodeActionKind.QuickFix,
UnifiedPredefinedSuggestedActionCategoryNames.Refactoring => CodeActionKind.Refactor,
UnifiedPredefinedSuggestedActionCategoryNames.StyleFix => CodeActionKind.QuickFix,
UnifiedPredefinedSuggestedActionCategoryNames.ErrorFix => CodeActionKind.QuickFix,
_ => throw ExceptionUtilities.UnexpectedValue(categoryName)
};
private static LSP.PriorityLevel? UnifiedSuggestedActionSetPriorityToPriorityLevel(UnifiedSuggestedActionSetPriority priority)
=> priority switch
{
UnifiedSuggestedActionSetPriority.Lowest => LSP.PriorityLevel.Lowest,
UnifiedSuggestedActionSetPriority.Low => LSP.PriorityLevel.Low,
UnifiedSuggestedActionSetPriority.Medium => LSP.PriorityLevel.Normal,
UnifiedSuggestedActionSetPriority.High => LSP.PriorityLevel.High,
_ => throw ExceptionUtilities.UnexpectedValue(priority)
};
public static CodeAction? GetCodeActionToResolve(string distinctTitle, ImmutableArray<CodeAction> codeActions)
{
// Searching for the matching code action. We compare against the unique identifier
// (e.g. "Suppress or Configure issues|Configure IDExxxx|Warning") instead of the
// code action's title (e.g. "Warning") since there's a chance that multiple code
// actions may have the same title (e.g. there could be multiple code actions with
// the title "Warning" that appear in the code action menu if there are multiple
// diagnostics on the same line).
foreach (var c in codeActions)
{
var action = CheckForMatchingAction(c, distinctTitle);
if (action != null)
{
return action;
}
}
return null;
}
private static CodeAction? CheckForMatchingAction(CodeAction codeAction, string goalTitle, string currentTitle = "")
{
// If the unique identifier of the current code action matches the unique identifier of the code action
// we're looking for, return the code action. If not, check to see if one of the current code action's
// nested actions may be a match.
if (!string.IsNullOrEmpty(currentTitle))
{
// Adding a delimiter for nested code actions, e.g. 'Suppress or Configure issues.Suppress IDEXXXX|in Source'
currentTitle += '|';
}
currentTitle += codeAction.Title;
if (currentTitle == goalTitle)
{
return codeAction;
}
foreach (var nestedAction in codeAction.NestedCodeActions)
{
var match = CheckForMatchingAction(nestedAction, goalTitle, currentTitle);
if (match != null)
{
return match;
}
}
return null;
}
}
}