Skip to content

Commit d6cc4c3

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Don't return the Fix All code action for legacy server
Currently the commands for Fix All are only supported for a real LSP server, so we shouldn't return a Source CodeAction that would try to run the command when running in the legacy server. Change-Id: I3256ef5e19c3a45e7fc3f3ec726a59da30d3a30c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/429642 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 2a69434 commit d6cc4c3

File tree

7 files changed

+104
-15
lines changed

7 files changed

+104
-15
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,18 @@ abstract class AbstractCodeActionsProducer
274274
return null;
275275
}
276276
}
277+
278+
/// Checks whether the server supports a given command.
279+
bool serverSupportsCommand(String command) {
280+
var handler = server.executeCommandHandler;
281+
282+
// `null` should never happen, it's set by the constructor of
283+
// ExecuteCommandHandler which is invoked as part of initialization.
284+
assert(handler != null);
285+
if (handler == null) {
286+
return false;
287+
}
288+
289+
return handler.commandHandlers[command] != null;
290+
}
277291
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
104104
(() => Commands.serverSupportedCommands.contains(command))(),
105105
'serverSupportedCommands did not contain $command',
106106
);
107+
assert(
108+
(() => serverSupportsCommand(command))(),
109+
'This kind of server does not support $command',
110+
);
107111

108112
return _maybeWrapCommandInCodeActionLiteral(
109113
actionKind,
@@ -527,7 +531,8 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
527531
'Organize Imports',
528532
Commands.organizeImports,
529533
),
530-
if (shouldIncludeKind(DartCodeActionKind.FixAll))
534+
if (serverSupportsCommand(Commands.fixAll) &&
535+
shouldIncludeKind(DartCodeActionKind.FixAll))
531536
createAction(DartCodeActionKind.FixAll, 'Fix All', Commands.fixAll),
532537
];
533538
}

pkg/analysis_server/test/lsp/code_actions_mixin.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ mixin CodeActionsTestMixin
2323
LspVerifyEditHelpersMixin {
2424
final String simplePubspecContent = 'name: my_project';
2525

26+
/// Whether the server supports the "Fix All" command (currently LSP-only).
27+
bool get serverSupportsFixAll => true;
28+
2629
/// Initializes the server with some basic configuration and expects to find
2730
/// a [CodeAction] with [kind]/[command]/[title].
2831
Future<CodeAction> expectCodeAction(

pkg/analysis_server/test/lsp/code_actions_source_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'server_abstract.dart';
1717

1818
void main() {
1919
defineReflectiveSuite(() {
20+
defineReflectiveTests(SourceCodeActionsTest);
2021
defineReflectiveTests(SortMembersSourceCodeActionsTest);
2122
defineReflectiveTests(OrganizeImportsSourceCodeActionsTest);
2223
defineReflectiveTests(FixAllSourceCodeActionsTest);
@@ -345,3 +346,10 @@ class SortMembersSourceCodeActionsTest extends AbstractSourceCodeActionsTest
345346
SharedSourceCodeActionsTestMixin,
346347
// Tests are defined in a shared mixin.
347348
SharedSortMembersSourceCodeActionsTests {}
349+
350+
@reflectiveTest
351+
class SourceCodeActionsTest extends AbstractSourceCodeActionsTest
352+
with
353+
SharedSourceCodeActionsTestMixin,
354+
// Tests are defined in a shared mixin.
355+
SharedSourceCodeActionsTests {}

pkg/analysis_server/test/lsp_over_legacy/abstract_code_actions.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analysis_server/src/lsp/constants.dart';
6+
57
import '../lsp/code_actions_mixin.dart';
68
import 'abstract_lsp_over_legacy.dart';
79

810
class AbstractCodeActionsTest extends SharedLspOverLegacyTest
911
with CodeActionsTestMixin {
1012
@override
13+
bool get serverSupportsFixAll =>
14+
// Legacy doesn't currently support the fix all command, but read it
15+
// directly from the handler so if it changes in future the test starts
16+
// running (and fails, prompting updating).
17+
server.executeCommandHandler!.commandHandlers[Commands.fixAll] != null;
18+
19+
@override
1120
Future<void> initializeServer() async {
1221
await super.initializeServer();
22+
await server.lspInitialized;
1323

1424
// Most CodeActions tests set LSP capabilities so automatically send these
1525
// to the legacy server as part of initialization.

pkg/analysis_server/test/lsp_over_legacy/code_action_source_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,41 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analysis_server/src/lsp/constants.dart';
6+
import 'package:test/test.dart';
57
import 'package:test_reflective_loader/test_reflective_loader.dart';
68

79
import '../shared/shared_code_actions_source_tests.dart';
810
import 'abstract_code_actions.dart';
911

1012
void main() {
1113
defineReflectiveSuite(() {
14+
defineReflectiveTests(SourceCodeActionsTest);
1215
defineReflectiveTests(OrganizeImportsSourceCodeActionsTest);
1316
defineReflectiveTests(SortMembersSourceCodeActionsTest);
17+
defineReflectiveTests(FixAllSourceCodeActionsTest);
1418
});
1519
}
1620

21+
@reflectiveTest
22+
class FixAllSourceCodeActionsTest extends AbstractCodeActionsTest
23+
with SharedSourceCodeActionsTestMixin {
24+
/// The fix all command is currently LSP-only, so we shouldn't return code
25+
/// actions for it.
26+
Future<void> test_unavailable() async {
27+
await expectNoAction('// content', command: Commands.fixAll);
28+
29+
expect(
30+
serverSupportsFixAll,
31+
isFalse,
32+
reason:
33+
'Fix All is not currently available for the legacy server. '
34+
'If/when that changes, this test must be updated by extracting the '
35+
'Fix All tests from LSP into a shared mixin like the others',
36+
);
37+
}
38+
}
39+
1740
@reflectiveTest
1841
class OrganizeImportsSourceCodeActionsTest extends AbstractCodeActionsTest
1942
with
@@ -27,3 +50,10 @@ class SortMembersSourceCodeActionsTest extends AbstractCodeActionsTest
2750
SharedSourceCodeActionsTestMixin,
2851
// Tests are defined in a shared mixin.
2952
SharedSortMembersSourceCodeActionsTests {}
53+
54+
@reflectiveTest
55+
class SourceCodeActionsTest extends AbstractCodeActionsTest
56+
with
57+
SharedSourceCodeActionsTestMixin,
58+
// Tests are defined in a shared mixin.
59+
SharedSourceCodeActionsTests {}

pkg/analysis_server/test/shared/shared_code_actions_source_tests.dart

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,6 @@ int minified(int x, int y) => min(x, y);
126126
);
127127
}
128128

129-
Future<void> test_filtersCorrectly() async {
130-
createFile(testFilePath, '');
131-
await initializeServer();
132-
133-
ofKind(CodeActionKind kind) => getCodeActions(testFileUri, kinds: [kind]);
134-
135-
expect(await ofKind(CodeActionKind.Source), hasLength(3));
136-
expect(await ofKind(CodeActionKind.SourceOrganizeImports), hasLength(1));
137-
expect(await ofKind(DartCodeActionKind.SortMembers), hasLength(1));
138-
expect(await ofKind(DartCodeActionKind.FixAll), hasLength(1));
139-
expect(await ofKind(CodeActionKind('source.foo')), isEmpty);
140-
expect(await ofKind(CodeActionKind.Refactor), isEmpty);
141-
}
142-
143129
Future<void> test_noEdits() async {
144130
const content = '''
145131
import 'dart:async';
@@ -371,3 +357,36 @@ mixin SharedSourceCodeActionsTestMixin
371357
setSupportedCodeActionKinds([CodeActionKind.Source]);
372358
}
373359
}
360+
361+
/// Shared tests used by both LSP + Legacy server tests and/or integration.
362+
mixin SharedSourceCodeActionsTests
363+
on
364+
SharedTestInterface,
365+
SharedSourceCodeActionsTestMixin,
366+
CodeActionsTestMixin,
367+
LspRequestHelpersMixin,
368+
LspEditHelpersMixin,
369+
LspVerifyEditHelpersMixin,
370+
ClientCapabilitiesHelperMixin {
371+
Future<void> test_filtersCorrectly() async {
372+
createFile(testFilePath, '');
373+
await initializeServer();
374+
375+
ofKind(CodeActionKind kind) => getCodeActions(testFileUri, kinds: [kind]);
376+
377+
var serverSupportsFixAll = this.serverSupportsFixAll;
378+
379+
expect(
380+
await ofKind(CodeActionKind.Source),
381+
hasLength(serverSupportsFixAll ? 3 : 2),
382+
);
383+
expect(await ofKind(CodeActionKind.SourceOrganizeImports), hasLength(1));
384+
expect(await ofKind(DartCodeActionKind.SortMembers), hasLength(1));
385+
expect(
386+
await ofKind(DartCodeActionKind.FixAll),
387+
hasLength(serverSupportsFixAll ? 1 : 0),
388+
);
389+
expect(await ofKind(CodeActionKind('source.foo')), isEmpty);
390+
expect(await ofKind(CodeActionKind.Refactor), isEmpty);
391+
}
392+
}

0 commit comments

Comments
 (0)