Skip to content

Commit fadf8a0

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Support LSP executeCommand over DTD to support code actions
There are a few things in this CL that were difficult to separate testing of (because they're all part of the same feature): 1. sets `requiresTrustedCaller=false` on the `executeCommand` handler so that commands can be called over DTD. It adds the same flag to the command handlers themselves, so a command can control whether it can be called over DTD or not (right now we allow everything except `logAction` and `sendWorkspaceEdit` which are both commands used internally and not appropriate for DTD clients to call). 2. Removes the allow-list on DTD methods, allowing all LSP shared methods to be available over DTD 3. Extends the integration test classes to support reverse-requests so we can verify the edits being sent back to the editor when calling the code actions commands over DTD 4. It also fixes a few bugs where we read the callers capabilities instead of the editors capabilities (which until now would always be the same in those places, but with this change are not). Change-Id: I6d271ddad6dc1b00a98b10b735763a368c91af7a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/428784 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent a3b1b4f commit fadf8a0

27 files changed

+379
-68
lines changed

pkg/analysis_server/lib/src/handler/legacy/lsp_over_legacy_handler.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class LspOverLegacyHandler extends LegacyHandler {
6969
performance: performance,
7070
clientCapabilities: server.editorClientCapabilities,
7171
timeSinceRequest: request.timeSinceRequest,
72+
isTrustedCaller: true,
7273
);
7374

7475
ErrorOr<Object?> result;

pkg/analysis_server/lib/src/lsp/handlers/commands/apply_code_action.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class ApplyCodeActionCommandHandler
3333
@override
3434
bool get recordsOwnAnalytics => true;
3535

36+
@override
37+
bool get requiresTrustedCaller => false;
38+
3639
@override
3740
Future<ErrorOr<void>> handle(
3841
MessageInfo message,

pkg/analysis_server/lib/src/lsp/handlers/commands/fix_all.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class FixAllCommandHandler extends SimpleEditCommandHandler<LspAnalysisServer> {
2222
@override
2323
String get commandName => 'Fix All';
2424

25+
@override
26+
bool get requiresTrustedCaller => false;
27+
2528
@override
2629
Future<ErrorOr<void>> handle(
2730
MessageInfo message,

pkg/analysis_server/lib/src/lsp/handlers/commands/fix_all_in_workspace.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ abstract class AbstractFixAllInWorkspaceCommandHandler
2424
/// user can choose which changes to apply.
2525
bool get requireConfirmation;
2626

27+
@override
28+
bool get requiresTrustedCaller => false;
29+
2730
@override
2831
Future<ErrorOr<void>> handle(
2932
MessageInfo message,

pkg/analysis_server/lib/src/lsp/handlers/commands/log_action.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class LogActionCommandHandler
1515
@override
1616
bool get recordsOwnAnalytics => true;
1717

18+
@override
19+
// We only currently expect this to be called by the editor.
20+
bool get requiresTrustedCaller => true;
21+
1822
@override
1923
Future<ErrorOr<void>> handle(
2024
MessageInfo message,

pkg/analysis_server/lib/src/lsp/handlers/commands/organize_imports.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class OrganizeImportsCommandHandler extends SimpleEditCommandHandler {
1616
@override
1717
String get commandName => 'Organize Imports';
1818

19+
@override
20+
bool get requiresTrustedCaller => false;
21+
1922
@override
2023
Future<ErrorOr<void>> handle(
2124
MessageInfo message,

pkg/analysis_server/lib/src/lsp/handlers/commands/perform_refactor.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class PerformRefactorCommandHandler extends AbstractRefactorCommandHandler {
2828
@override
2929
bool get recordsOwnAnalytics => true;
3030

31+
@override
32+
bool get requiresTrustedCaller => false;
33+
3134
@override
3235
FutureOr<ErrorOr<void>> execute(
3336
String path,
@@ -40,6 +43,12 @@ class PerformRefactorCommandHandler extends AbstractRefactorCommandHandler {
4043
ProgressReporter reporter,
4144
int? docVersion,
4245
) async {
46+
var editorCapabilities = server.editorClientCapabilities;
47+
if (editorCapabilities == null) {
48+
// This should not happen unless a client misbehaves.
49+
return serverNotInitializedError;
50+
}
51+
4352
var actionName = 'dart.refactor.${kind.toLowerCase()}';
4453
server.analyticsManager.executedCommand(actionName);
4554

@@ -102,7 +111,7 @@ class PerformRefactorCommandHandler extends AbstractRefactorCommandHandler {
102111
return fileModifiedError;
103112
}
104113

105-
var edit = createWorkspaceEdit(server, clientCapabilities, change);
114+
var edit = createWorkspaceEdit(server, editorCapabilities, change);
106115
return await sendWorkspaceEditToClient(edit);
107116
} finally {
108117
manager.end(cancelableToken);

pkg/analysis_server/lib/src/lsp/handlers/commands/refactor_command_handler.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class RefactorCommandHandler extends SimpleEditCommandHandler<AnalysisServer> {
2727

2828
RefactorCommandHandler(super.server, this.commandName, this.generator);
2929

30+
@override
31+
bool get requiresTrustedCaller => false;
32+
3033
@override
3134
Future<ErrorOr<void>> handle(
3235
MessageInfo message,

pkg/analysis_server/lib/src/lsp/handlers/commands/send_workspace_edit.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class SendWorkspaceEditCommandHandler extends SimpleEditCommandHandler {
2222
@override
2323
String get commandName => 'Send Workspace Edit';
2424

25+
@override
26+
// This command is used internally to work around some limitations in LSP
27+
// and is not expected to ever be called by a DTD client.
28+
bool get requiresTrustedCaller => true;
29+
2530
@override
2631
Future<ErrorOr<void>> handle(
2732
MessageInfo message,

pkg/analysis_server/lib/src/lsp/handlers/commands/sort_members.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class SortMembersCommandHandler extends SimpleEditCommandHandler {
1717
@override
1818
String get commandName => 'Sort Members';
1919

20+
@override
21+
bool get requiresTrustedCaller => false;
22+
2023
@override
2124
Future<ErrorOr<void>> handle(
2225
MessageInfo message,

0 commit comments

Comments
 (0)