Skip to content

Commit 4f3a672

Browse files
DanTupCommit Queue
authored and
Commit Queue
committed
[analysis_server] Add a CodeAction typedef for CodeActionLiteral | Command
Following on from a previous change to use the term `CodeAction` (an item returned from a `textDocument/codeAction` request) to mean either a `CodeActionLiteral` (which contains inline edits and/or a command) or a base `Command`, this adds a typedef describing the same and updates all code that used `Either2<CodeActionLiteral, Command>` to instead use the typedef. It also renames a few additional methods/functions to be more consistent with these terms and adds a `CodeActionExtension` to simplify extracting fields like `command` and `title` from either kind of code action. Change-Id: Iad5f77ef21229fc5c9738527f98ccf7202f83bd8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/425863 Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 6d6159f commit 4f3a672

21 files changed

+282
-248
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ abstract class AbstractCodeActionsProducer
180180
OperationPerformance? performance,
181181
);
182182

183-
Future<List<Either2<CodeActionLiteral, Command>>> getRefactorActions(
183+
Future<List<CodeAction>> getRefactorActions(
184184
OperationPerformance? performance,
185185
);
186186

187-
Future<List<Either2<CodeActionLiteral, Command>>> getSourceActions();
187+
Future<List<CodeAction>> getSourceActions();
188188

189189
/// Return the contents of the [file], or `null` if the file does not exist or
190190
/// cannot be read.

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,12 @@ class AnalysisOptionsCodeActionsProducer extends AbstractCodeActionsProducer {
111111
}
112112

113113
@override
114-
Future<List<Either2<CodeActionLiteral, Command>>> getRefactorActions(
114+
Future<List<CodeAction>> getRefactorActions(
115115
OperationPerformance? performance,
116116
) async => [];
117117

118118
@override
119-
Future<List<Either2<CodeActionLiteral, Command>>> getSourceActions() async =>
120-
[];
119+
Future<List<CodeAction>> getSourceActions() async => [];
121120

122121
YamlMap? _getOptions(SourceFactory sourceFactory, String content) {
123122
var optionsProvider = AnalysisOptionsProvider(sourceFactory);

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

+16-21
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
5858
@override
5959
String get name => 'ServerDartActionsComputer';
6060

61-
/// Helper to create a [CodeActionLiteral] or [Command] for the given arguments in
62-
/// the current file based on client capabilities.
63-
Either2<CodeActionLiteral, Command> createCommand(
61+
/// Helper to create a [CodeAction] for the given arguments in the current
62+
/// file based on client capabilities.
63+
CodeAction createAction(
6464
CodeActionKind actionKind,
6565
String title,
6666
String command,
@@ -69,7 +69,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
6969
(() => Commands.serverSupportedCommands.contains(command))(),
7070
'serverSupportedCommands did not contain $command',
7171
);
72-
return _commandOrCodeAction(
72+
return _commandOrCodeActionLiteral(
7373
actionKind,
7474
Command(
7575
title: title,
@@ -87,7 +87,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
8787

8888
/// Helper to create refactors that execute commands provided with
8989
/// the current file, location and document version.
90-
Either2<CodeActionLiteral, Command> createRefactor(
90+
CodeAction createRefactor(
9191
CodeActionKind actionKind,
9292
String name,
9393
RefactoringKind refactorKind, [
@@ -99,7 +99,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
9999
'serverSupportedCommands did not contain $command',
100100
);
101101

102-
return _commandOrCodeAction(
102+
return _commandOrCodeActionLiteral(
103103
actionKind,
104104
Command(
105105
title: name,
@@ -263,7 +263,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
263263
}
264264

265265
@override
266-
Future<List<Either2<CodeActionLiteral, Command>>> getRefactorActions(
266+
Future<List<CodeAction>> getRefactorActions(
267267
OperationPerformance? performance,
268268
) async {
269269
// If the client does not support workspace/applyEdit, we won't be able to
@@ -272,7 +272,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
272272
return const [];
273273
}
274274

275-
var refactorActions = <Either2<CodeActionLiteral, Command>>[];
275+
var refactorActions = <CodeAction>[];
276276
var performanceTracker = RefactoringPerformance();
277277

278278
try {
@@ -293,9 +293,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
293293
performance: performanceTracker,
294294
);
295295
var actions = await processor.compute();
296-
refactorActions.addAll(
297-
actions.map(Either2<CodeActionLiteral, Command>.t1),
298-
);
296+
refactorActions.addAll(actions.map(CodeAction.t1));
299297

300298
// Extracts
301299
if (shouldIncludeKind(CodeActionKind.RefactorExtract)) {
@@ -471,7 +469,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
471469
/// Gets "Source" CodeActions, which are actions that apply to whole files of
472470
/// source such as Sort Members and Organise Imports.
473471
@override
474-
Future<List<Either2<CodeActionLiteral, Command>>> getSourceActions() async {
472+
Future<List<CodeAction>> getSourceActions() async {
475473
// If the client does not support workspace/applyEdit, we won't be able to
476474
// run any of these.
477475
if (!supportsApplyEdit) {
@@ -480,33 +478,30 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
480478

481479
return [
482480
if (shouldIncludeKind(DartCodeActionKind.SortMembers))
483-
createCommand(
481+
createAction(
484482
DartCodeActionKind.SortMembers,
485483
'Sort Members',
486484
Commands.sortMembers,
487485
),
488486
if (shouldIncludeKind(CodeActionKind.SourceOrganizeImports))
489-
createCommand(
487+
createAction(
490488
CodeActionKind.SourceOrganizeImports,
491489
'Organize Imports',
492490
Commands.organizeImports,
493491
),
494492
if (shouldIncludeKind(DartCodeActionKind.FixAll))
495-
createCommand(DartCodeActionKind.FixAll, 'Fix All', Commands.fixAll),
493+
createAction(DartCodeActionKind.FixAll, 'Fix All', Commands.fixAll),
496494
];
497495
}
498496

499497
/// Wraps a command in a CodeAction if the client supports it so that a
500498
/// CodeActionKind can be supplied.
501-
Either2<CodeActionLiteral, Command> _commandOrCodeAction(
502-
CodeActionKind kind,
503-
Command command,
504-
) {
499+
CodeAction _commandOrCodeActionLiteral(CodeActionKind kind, Command command) {
505500
return supportsLiterals
506-
? Either2<CodeActionLiteral, Command>.t1(
501+
? CodeAction.t1(
507502
CodeActionLiteral(title: command.title, kind: kind, command: command),
508503
)
509-
: Either2<CodeActionLiteral, Command>.t2(command);
504+
: CodeAction.t2(command);
510505
}
511506
}
512507

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,12 @@ class PluginCodeActionsProducer extends AbstractCodeActionsProducer {
7171
}
7272

7373
@override
74-
Future<List<Either2<CodeActionLiteral, Command>>> getRefactorActions(
74+
Future<List<CodeAction>> getRefactorActions(
7575
OperationPerformance? performance,
7676
) async => [];
7777

7878
@override
79-
Future<List<Either2<CodeActionLiteral, Command>>> getSourceActions() async =>
80-
[];
79+
Future<List<CodeAction>> getSourceActions() async => [];
8180

8281
CodeActionWithPriority _convertAssist(plugin.PrioritizedSourceChange assist) {
8382
return (

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,10 @@ class PubspecCodeActionsProducer extends AbstractCodeActionsProducer {
9797
}
9898

9999
@override
100-
Future<List<Either2<CodeActionLiteral, Command>>> getRefactorActions(
100+
Future<List<CodeAction>> getRefactorActions(
101101
OperationPerformance? performance,
102102
) async => [];
103103

104104
@override
105-
Future<List<Either2<CodeActionLiteral, Command>>> getSourceActions() async =>
106-
[];
105+
Future<List<CodeAction>> getSourceActions() async => [];
107106
}

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class CodeActionHandler
219219
];
220220
var sorter = _CodeActionSorter(params.range, shouldIncludeKind);
221221

222-
var allActions = <Either2<CodeActionLiteral, Command>>[
222+
var allActions = <CodeAction>[
223223
// Like-kinded actions are grouped (and prioritized) together
224224
// regardless of which producer they came from.
225225

@@ -306,9 +306,7 @@ class _CodeActionSorter {
306306

307307
_CodeActionSorter(this.range, this.shouldIncludeKind);
308308

309-
List<Either2<CodeActionLiteral, Command>> sort(
310-
List<CodeActionWithPriority> actions,
311-
) {
309+
List<CodeAction> sort(List<CodeActionWithPriority> actions) {
312310
var dedupedActions = _dedupeActions(actions, range.start);
313311

314312
// Add each index so we can do a stable sort on priority.
@@ -325,7 +323,7 @@ class _CodeActionSorter {
325323

326324
return dedupedActionsWithIndex
327325
.where((action) => shouldIncludeKind(action.action.kind))
328-
.map((action) => Either2<CodeActionLiteral, Command>.t1(action.action))
326+
.map((action) => CodeAction.t1(action.action))
329327
.toList();
330328
}
331329

pkg/analysis_server/test/integration/server/message_scheduler_test.dart

+2-5
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,7 @@ void f() {
118118
''';
119119

120120
newFile(mainFilePath, content);
121-
// await initialize();
122-
// var code = TestCode.parse(content);
123-
// await openFile(mainFileUri, code.code);
124-
var codeAction = await expectAction(
121+
var codeAction = await expectCodeActionLiteral(
125122
content,
126123
command: Commands.performRefactor,
127124
title: extractMethodTitle,
@@ -308,7 +305,7 @@ void f() {
308305
}
309306
''';
310307

311-
var codeAction = await expectAction(
308+
var codeAction = await expectCodeActionLiteral(
312309
content,
313310
command: Commands.performRefactor,
314311
title: extractMethodTitle,

pkg/analysis_server/test/lsp/code_actions_abstract.dart

+27-22
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analysis_server/lsp_protocol/protocol.dart';
6+
import 'package:analysis_server/src/lsp/constants.dart';
67
import 'package:analysis_server/src/services/correction/assist_internal.dart';
78
import 'package:analysis_server/src/services/correction/fix_internal.dart';
89
import 'package:analyzer/src/test_utilities/test_code_format.dart';
910
import 'package:collection/collection.dart';
1011
import 'package:test/test.dart';
1112

13+
import '../utils/lsp_protocol_extensions.dart';
1214
import '../utils/test_code_extensions.dart';
1315
import 'change_verifier.dart';
1416
import 'server_abstract.dart';
1517

1618
abstract class AbstractCodeActionsTest extends AbstractLspAnalysisServerTest {
1719
/// Initializes the server with some basic configuration and expects to find
18-
/// a [CodeActionLiteral] with [kind]/[command]/[title].
19-
Future<CodeActionLiteral> expectAction(
20+
/// a [CodeAction] with [kind]/[command]/[title].
21+
Future<CodeActionLiteral> expectCodeActionLiteral(
2022
String content, {
2123
CodeActionKind? kind,
2224
String? command,
@@ -44,7 +46,7 @@ abstract class AbstractCodeActionsTest extends AbstractLspAnalysisServerTest {
4446
triggerKind: triggerKind,
4547
);
4648

47-
var action = findAction(
49+
var action = findCodeActionLiteral(
4850
codeActions,
4951
kind: kind,
5052
command: command,
@@ -95,25 +97,31 @@ abstract class AbstractCodeActionsTest extends AbstractLspAnalysisServerTest {
9597
);
9698

9799
expect(
98-
findAction(codeActions, kind: kind, command: command, title: title),
100+
findCodeActionLiteral(
101+
codeActions,
102+
kind: kind,
103+
command: command,
104+
title: title,
105+
),
99106
isNull,
100107
);
101108
}
102109

103-
/// Finds the single action matching [title], [kind] and [command].
110+
/// Finds the single [CodeActionLiteral] matching [title], [kind] and
111+
/// [command].
104112
///
105-
/// If [command] and/or [commandArgs] are supplied, ensures the action has
106-
/// a matching command/args.
113+
/// If [command] and/or [commandArgs] are supplied, ensures the result is
114+
/// either that command, or a literal code action with that command.
107115
///
108-
/// Throws if zero or more than one actions match.
109-
CodeActionLiteral? findAction(
110-
List<Either2<CodeActionLiteral, Command>> actions, {
116+
/// Returns `null` if there is not exactly one match.
117+
CodeActionLiteral? findCodeActionLiteral(
118+
List<CodeAction> actions, {
111119
String? title,
112120
CodeActionKind? kind,
113121
String? command,
114122
List<Object>? commandArgs,
115123
}) {
116-
return findActions(
124+
return findCodeActionLiterals(
117125
actions,
118126
title: title,
119127
kind: kind,
@@ -122,7 +130,7 @@ abstract class AbstractCodeActionsTest extends AbstractLspAnalysisServerTest {
122130
).singleOrNull;
123131
}
124132

125-
List<CodeActionLiteral> findActions(
133+
List<CodeActionLiteral> findCodeActionLiterals(
126134
List<Either2<CodeActionLiteral, Command>> actions, {
127135
String? title,
128136
CodeActionKind? kind,
@@ -165,17 +173,14 @@ abstract class AbstractCodeActionsTest extends AbstractLspAnalysisServerTest {
165173
.toList();
166174
}
167175

168-
Either2<CodeActionLiteral, Command>? findCommand(
169-
List<Either2<CodeActionLiteral, Command>> actions,
176+
CodeAction? findCommand(
177+
List<CodeAction> actions,
170178
String commandID, [
171179
String? wantedTitle,
172180
]) {
173181
for (var codeAction in actions) {
174-
var id = codeAction.map(
175-
(action) => action.command?.command,
176-
(cmd) => cmd.command,
177-
);
178-
var title = codeAction.map((cmd) => cmd.title, (action) => action.title);
182+
var id = codeAction.command?.command;
183+
var title = codeAction.title;
179184
if (id == commandID && (wantedTitle == null || wantedTitle == title)) {
180185
return codeAction;
181186
}
@@ -201,7 +206,7 @@ abstract class AbstractCodeActionsTest extends AbstractLspAnalysisServerTest {
201206
/// Initializes the server with some basic configuration and expects to find
202207
/// a [CodeActionLiteral] with [kind]/[title] that applies edits resulting in
203208
/// [expected].
204-
Future<LspChangeVerifier> verifyActionEdits(
209+
Future<LspChangeVerifier> verifyCodeActionLiteralEdits(
205210
String content,
206211
String expected, {
207212
String? filePath,
@@ -222,7 +227,7 @@ ${LspChangeVerifier.editMarkerStart} ${relativePath(filePath)}
222227
$expected''';
223228
}
224229

225-
var action = await expectAction(
230+
var action = await expectCodeActionLiteral(
226231
filePath: filePath,
227232
content,
228233
kind: kind,
@@ -236,7 +241,7 @@ $expected''';
236241
// the edits attached directly to the code action.
237242
// Don't try to execute 'dart.logAction' because it will never produce
238243
// edits.
239-
if (command != null && command != 'dart.logAction') {
244+
if (command != null && command != Commands.logAction) {
240245
return await verifyCommandEdits(
241246
action.command!,
242247
expected,

0 commit comments

Comments
 (0)