Skip to content

Commit f6cf08b

Browse files
DanTupbwilkerson
authored andcommitted
[analysis_server] Use a static identifier for plugin-supplied fixes/assists
Change-Id: Ifc6310aabe60a8c5671203b66b11fe98e0c5cbbe Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/416800 Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 77961ae commit f6cf08b

File tree

8 files changed

+75
-12
lines changed

8 files changed

+75
-12
lines changed

pkg/analysis_server/lib/src/lsp/handlers/code_actions/abstract_code_actions_producer.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,15 @@ abstract class AbstractCodeActionsProducer
7070
@protected
7171
CodeAction createAssistAction(
7272
protocol.SourceChange change,
73+
String? loggedAssistId,
7374
String path,
7475
LineInfo lineInfo,
7576
) {
7677
return CodeAction(
7778
title: change.message,
7879
kind: toCodeActionKind(change.id, CodeActionKind.Refactor),
7980
diagnostics: const [],
80-
command: createLogActionCommand(change.id),
81+
command: createLogActionCommand(loggedAssistId),
8182
edit: createWorkspaceEdit(
8283
server,
8384
capabilities,
@@ -112,6 +113,7 @@ abstract class AbstractCodeActionsProducer
112113
@protected
113114
CodeAction createFixAction(
114115
protocol.SourceChange change,
116+
String? loggedFixId,
115117
Diagnostic diagnostic,
116118
String path,
117119
LineInfo lineInfo,
@@ -120,7 +122,7 @@ abstract class AbstractCodeActionsProducer
120122
title: change.message,
121123
kind: toCodeActionKind(change.id, CodeActionKind.QuickFix),
122124
diagnostics: [diagnostic],
123-
command: createLogActionCommand(change.id),
125+
command: createLogActionCommand(loggedFixId),
124126
edit: createWorkspaceEdit(
125127
server,
126128
capabilities,

pkg/analysis_server/lib/src/lsp/handlers/code_actions/analysis_options.dart

+7-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ class AnalysisOptionsCodeActionsProducer extends AbstractCodeActionsProducer {
9595
var diagnostic = createDiagnostic(lineInfo, result, error);
9696
codeActions.addAll(
9797
fixes.map((fix) {
98-
var action = createFixAction(fix.change, diagnostic, path, lineInfo);
98+
var action = createFixAction(
99+
fix.change,
100+
fix.change.id,
101+
diagnostic,
102+
path,
103+
lineInfo,
104+
);
99105
return (action: action, priority: fix.kind.priority);
100106
}),
101107
);

pkg/analysis_server/lib/src/lsp/handlers/code_actions/dart.dart

+2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
164164
return assists.map((assist) {
165165
var action = createAssistAction(
166166
assist.change,
167+
assist.change.id,
167168
unitResult.path,
168169
unitResult.lineInfo,
169170
);
@@ -242,6 +243,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
242243
fixes.map((fix) {
243244
var action = createFixAction(
244245
fix.change,
246+
fix.change.id,
245247
diagnostic,
246248
path,
247249
lineInfo,

pkg/analysis_server/lib/src/lsp/handlers/code_actions/plugins.dart

+13-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ class PluginCodeActionsProducer extends AbstractCodeActionsProducer {
7878

7979
CodeActionWithPriority _convertAssist(plugin.PrioritizedSourceChange assist) {
8080
return (
81-
action: createAssistAction(assist.change, path, lineInfo),
81+
action: createAssistAction(
82+
assist.change,
83+
'assist from plugin',
84+
path,
85+
lineInfo,
86+
),
8287
priority: assist.priority,
8388
);
8489
}
@@ -95,7 +100,13 @@ class PluginCodeActionsProducer extends AbstractCodeActionsProducer {
95100
);
96101
return fixes.fixes.map(
97102
(fix) => (
98-
action: createFixAction(fix.change, diagnostic, path, lineInfo),
103+
action: createFixAction(
104+
fix.change,
105+
'fix from plugin',
106+
diagnostic,
107+
path,
108+
lineInfo,
109+
),
99110
priority: fix.priority,
100111
),
101112
);

pkg/analysis_server/lib/src/lsp/handlers/code_actions/pubspec.dart

+7-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ class PubspecCodeActionsProducer extends AbstractCodeActionsProducer {
8181
var diagnostic = createDiagnostic(lineInfo, result, error);
8282
codeActions.addAll(
8383
fixes.map((fix) {
84-
var action = createFixAction(fix.change, diagnostic, path, lineInfo);
84+
var action = createFixAction(
85+
fix.change,
86+
fix.change.id,
87+
diagnostic,
88+
path,
89+
lineInfo,
90+
);
8591
return (action: action, priority: fix.kind.priority);
8692
}),
8793
);

pkg/analysis_server/test/lsp/code_actions_abstract.dart

+34-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ abstract class AbstractCodeActionsTest extends AbstractLspAnalysisServerTest {
2020
String content, {
2121
CodeActionKind? kind,
2222
String? command,
23+
List<Object>? commandArgs,
2324
String? title,
2425
CodeActionTriggerKind? triggerKind,
2526
String? filePath,
@@ -47,6 +48,7 @@ abstract class AbstractCodeActionsTest extends AbstractLspAnalysisServerTest {
4748
codeActions,
4849
kind: kind,
4950
command: command,
51+
commandArgs: commandArgs,
5052
title: title,
5153
);
5254
if (action == null) {
@@ -100,18 +102,23 @@ abstract class AbstractCodeActionsTest extends AbstractLspAnalysisServerTest {
100102

101103
/// Finds the single action matching [title], [kind] and [command].
102104
///
105+
/// If [command] and/or [commandArgs] are supplied, ensures the action has
106+
/// a matching command/args.
107+
///
103108
/// Throws if zero or more than one actions match.
104109
CodeAction? findAction(
105110
List<Either2<Command, CodeAction>> actions, {
106111
String? title,
107112
CodeActionKind? kind,
108113
String? command,
114+
List<Object>? commandArgs,
109115
}) {
110116
return findActions(
111117
actions,
112118
title: title,
113119
kind: kind,
114120
command: command,
121+
commandArgs: commandArgs,
115122
).singleOrNull;
116123
}
117124

@@ -120,21 +127,38 @@ abstract class AbstractCodeActionsTest extends AbstractLspAnalysisServerTest {
120127
String? title,
121128
CodeActionKind? kind,
122129
String? command,
130+
List<Object>? commandArgs,
123131
}) {
124132
return actions
125133
.map((action) => action.map((cmd) => null, (action) => action))
126134
.where((action) => title == null || action?.title == title)
127135
.where((action) => kind == null || action?.kind == kind)
136+
// Some tests filter by only supplying a command, so if there is no
137+
// title given, filter by the command. If a title was given, don't
138+
// filter by the command and assert it below. This results in a better
139+
// failure message if the action existed by title but without the correct
140+
// command.
128141
.where(
129-
(action) => command == null || action?.command?.command == command,
142+
(action) =>
143+
title != null ||
144+
command == null ||
145+
action?.command?.command == command,
130146
)
131147
.map((action) {
132148
// Always expect a command (either to execute, or for logging)
133-
assert(action!.command != null);
134-
// Expect an edit if we weren't looking for a command-action.
135-
if (command == null) {
136-
assert(action!.edit != null);
149+
expect(action!.command, isNotNull);
150+
151+
if (command != null) {
152+
expect(action.command!.command, command);
153+
} else {
154+
// Expect an edit if we weren't looking for a command-action.
155+
expect(action.edit, isNotNull);
137156
}
157+
158+
if (commandArgs != null) {
159+
expect(action.command!.arguments, equals(commandArgs));
160+
}
161+
138162
return action;
139163
})
140164
.nonNulls
@@ -183,6 +207,7 @@ abstract class AbstractCodeActionsTest extends AbstractLspAnalysisServerTest {
183207
String? filePath,
184208
CodeActionKind? kind,
185209
String? command,
210+
List<Object>? commandArgs,
186211
String? title,
187212
ProgressToken? commandWorkDoneToken,
188213
bool openTargetFile = false,
@@ -202,13 +227,16 @@ $expected''';
202227
content,
203228
kind: kind,
204229
command: command,
230+
commandArgs: commandArgs,
205231
title: title,
206232
openTargetFile: openTargetFile,
207233
);
208234

209235
// Verify the edits either by executing the command we expected, or
210236
// the edits attached directly to the code action.
211-
if (command != null) {
237+
// Don't try to execute 'dart.logAction' because it will never produce
238+
// edits.
239+
if (command != null && command != 'dart.logAction') {
212240
return await verifyCommandEdits(
213241
action.command!,
214242
expected,

pkg/analysis_server/test/lsp/code_actions_assists_test.dart

+4
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ bar
216216
expectedContent,
217217
kind: CodeActionKind('refactor.fooToBar'),
218218
title: "Change 'foo' to 'bar'",
219+
command: 'dart.logAction',
220+
commandArgs: [
221+
{'action': 'assist from plugin'},
222+
],
219223
);
220224
}
221225

pkg/analysis_server/test/lsp/code_actions_fixes_test.dart

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ bar
9797
expectedContent,
9898
kind: CodeActionKind('quickfix.fooToBar'),
9999
title: "Change 'foo' to 'bar'",
100+
command: 'dart.logAction',
101+
commandArgs: [
102+
{'action': 'fix from plugin'},
103+
],
100104
);
101105
}
102106

0 commit comments

Comments
 (0)