Skip to content

Commit 036b599

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Allow the ApplyCodeAction command to be called via LSP-over-Legacy
This updates the `ApplyCodeActionCommandHandler` to be a "shared" command that can work for either kind of server (although it does not yet allow calling over DTD - the executeCommand handler still requires trusted callers). It also moves all of the tests into a shared mixin so they will be run for both server kinds. Change-Id: I831cc5a1a9feadc528e5b33b82e3469c60111dfa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/427440 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 050b740 commit 036b599

17 files changed

+333
-233
lines changed

pkg/analysis_server/lib/src/analysis_server.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import 'package:analyzer_plugin/src/utilities/client_uri_converter.dart';
8585
import 'package:collection/collection.dart';
8686
import 'package:http/http.dart' as http;
8787
import 'package:meta/meta.dart';
88+
import 'package:path/path.dart' as path;
8889
import 'package:watcher/watcher.dart';
8990

9091
/// The function for sending `openUri` request to the client.
@@ -455,6 +456,8 @@ abstract class AnalysisServer {
455456
/// Returns owners of files.
456457
OwnedFiles get ownedFiles => contextManager.ownedFiles;
457458

459+
path.Context get pathContext => resourceProvider.pathContext;
460+
458461
/// Whether or not the client supports showMessageRequest to show the user
459462
/// a message and allow them to respond by clicking buttons.
460463
///

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ library;
88
import 'dart:async';
99

1010
import 'package:analysis_server/lsp_protocol/protocol.dart';
11+
import 'package:analysis_server/src/analysis_server.dart';
1112
import 'package:analysis_server/src/lsp/client_capabilities.dart';
1213
import 'package:analysis_server/src/lsp/constants.dart';
13-
import 'package:analysis_server/src/lsp/lsp_analysis_server.dart';
1414
import 'package:analysis_server/src/lsp/mapping.dart';
1515
import 'package:analysis_server/src/protocol_server.dart'
1616
hide AnalysisOptions, Position;
@@ -32,7 +32,7 @@ typedef CodeActionWithPriorityAndIndex =
3232

3333
/// A base for classes that produce [CodeAction]s for the LSP handler.
3434
abstract class AbstractCodeActionsProducer
35-
with RequestHandlerMixin<LspAnalysisServer> {
35+
with RequestHandlerMixin<AnalysisServer> {
3636
final File file;
3737
final LineInfo lineInfo;
3838
final int offset;
@@ -50,7 +50,7 @@ abstract class AbstractCodeActionsProducer
5050
final AnalysisOptions analysisOptions;
5151

5252
@override
53-
final LspAnalysisServer server;
53+
final AnalysisServer server;
5454

5555
/// Whether [CodeAction]s can be [Command]s or not.
5656
///

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
library;
99

1010
import 'package:analysis_server/lsp_protocol/protocol.dart';
11+
import 'package:analysis_server/src/analysis_server.dart';
1112
import 'package:analysis_server/src/lsp/client_capabilities.dart';
1213
import 'package:analysis_server/src/lsp/error_or.dart';
1314
import 'package:analysis_server/src/lsp/extensions/code_action.dart';
@@ -17,7 +18,6 @@ import 'package:analysis_server/src/lsp/handlers/code_actions/dart.dart';
1718
import 'package:analysis_server/src/lsp/handlers/code_actions/plugins.dart';
1819
import 'package:analysis_server/src/lsp/handlers/code_actions/pubspec.dart';
1920
import 'package:analysis_server/src/lsp/handlers/handlers.dart';
20-
import 'package:analysis_server/src/lsp/lsp_analysis_server.dart';
2121
import 'package:analysis_server/src/lsp/mapping.dart';
2222
import 'package:analyzer/dart/analysis/analysis_options.dart';
2323
import 'package:analyzer/dart/analysis/results.dart';
@@ -30,13 +30,7 @@ import 'package:path/src/context.dart';
3030

3131
/// A helper class for computing [CodeAction]s that is used by both the
3232
/// [CodeActionHandler] and [ApplyCodeActionCommandHandler].
33-
class CodeActionComputer
34-
with
35-
HandlerHelperMixin<
36-
// TODO(dantup): Make this (and the code action producers) work with
37-
// either server.
38-
LspAnalysisServer
39-
> {
33+
class CodeActionComputer with HandlerHelperMixin<AnalysisServer> {
4034
/// The text document to compute code actions for.
4135
final TextDocumentIdentifier textDocument;
4236

@@ -78,7 +72,7 @@ class CodeActionComputer
7872
final LspClientCapabilities editorCapabilities;
7973

8074
@override
81-
final LspAnalysisServer server;
75+
final AnalysisServer server;
8276

8377
/// The kinds of [CodeAction]s that the caller supports.
8478
///

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
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/analysis_server.dart';
67
import 'package:analysis_server/src/lsp/constants.dart';
78
import 'package:analysis_server/src/lsp/error_or.dart';
89
import 'package:analysis_server/src/lsp/handlers/code_actions/code_action_computer.dart';
910
import 'package:analysis_server/src/lsp/handlers/commands/simple_edit_handler.dart';
1011
import 'package:analysis_server/src/lsp/handlers/handlers.dart';
11-
import 'package:analysis_server/src/lsp/lsp_analysis_server.dart';
1212
import 'package:analysis_server/src/lsp/progress.dart';
1313
import 'package:language_server_protocol/json_parsing.dart';
1414

@@ -24,7 +24,7 @@ import 'package:language_server_protocol/json_parsing.dart';
2424
/// the server in the same session and therefore the arguments are not specified
2525
/// and can easily change - the server only needs to be consistent with itself.
2626
class ApplyCodeActionCommandHandler
27-
extends SimpleEditCommandHandler<LspAnalysisServer> {
27+
extends SimpleEditCommandHandler<AnalysisServer> {
2828
ApplyCodeActionCommandHandler(super.server);
2929

3030
@override
@@ -117,11 +117,9 @@ class ApplyCodeActionCommandHandler
117117
var loggedAction = loggedActionParameter as String?;
118118

119119
// Verify the document is still fresh.
120-
if (textDocument.version case var version?) {
121-
var filePath = server.uriConverter.fromClientUri(textDocument.uri);
122-
if (server.getDocumentVersion(filePath) != version) {
123-
return fileModifiedError;
124-
}
120+
var filePath = server.uriConverter.fromClientUri(textDocument.uri);
121+
if (fileHasBeenModified(filePath, textDocument.version)) {
122+
return fileModifiedError;
125123
}
126124

127125
// Also record the action we're triggering.
@@ -149,6 +147,10 @@ class ApplyCodeActionCommandHandler
149147
);
150148
var actions = await computer.compute();
151149

150+
if (cancellationToken.isCancellationRequested) {
151+
return cancelled(cancellationToken);
152+
}
153+
152154
return actions.mapResult((actions) async {
153155
return switch (actions) {
154156
null || [] => error(

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ class ExecuteCommandHandler
3939
Commands.organizeImports: OrganizeImportsCommandHandler(server),
4040
Commands.sendWorkspaceEdit: SendWorkspaceEditCommandHandler(server),
4141
Commands.logAction: LogActionCommandHandler(server),
42+
Commands.applyCodeAction: ApplyCodeActionCommandHandler(server),
4243

4344
// Commands that currently require an underlying LSP server.
4445
if (server is LspAnalysisServer) ...{
45-
// TODO(dantup): Make code actions shared.
46-
Commands.applyCodeAction: ApplyCodeActionCommandHandler(server),
4746
Commands.fixAll: FixAllCommandHandler(server),
4847
Commands.fixAllInWorkspace: FixAllInWorkspaceCommandHandler(server),
4948
Commands.previewFixAllInWorkspace:

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ mixin HandlerHelperMixin<S extends AnalysisServer> {
113113

114114
bool fileHasBeenModified(String path, int? clientVersion) {
115115
var serverDocumentVersion = server.getDocumentVersion(path);
116-
return clientVersion != null && clientVersion != serverDocumentVersion;
116+
// Allow nulls on either side, because if a client doesn't support versions
117+
// somewhere we can't just reject everything.
118+
return clientVersion != null &&
119+
serverDocumentVersion != null &&
120+
clientVersion != serverDocumentVersion;
117121
}
118122

119123
ErrorOr<T> fileNotAnalyzedError<T>(String path) => error<T>(

pkg/analysis_server/lib/src/lsp/lsp_analysis_server.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ import 'package:analyzer_plugin/src/utilities/client_uri_converter.dart';
5050
import 'package:collection/collection.dart';
5151
import 'package:http/http.dart' as http;
5252
import 'package:meta/meta.dart';
53-
import 'package:path/path.dart' as path;
5453

5554
/// Instances of the class [LspAnalysisServer] implement an LSP-based server
5655
/// that listens on a [LspServerCommunicationChannel] for LSP messages and
@@ -265,8 +264,6 @@ class LspAnalysisServer extends AnalysisServer {
265264
};
266265
}
267266

268-
path.Context get pathContext => resourceProvider.pathContext;
269-
270267
@override
271268
set pluginManager(PluginManager value) {
272269
if (AnalysisServer.supportsPlugins) {

pkg/analysis_server/lib/src/services/refactoring/framework/refactoring_context.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
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/analysis_server.dart';
56
import 'package:analysis_server/src/lsp/client_capabilities.dart';
6-
import 'package:analysis_server/src/lsp/lsp_analysis_server.dart';
77
import 'package:analysis_server/src/services/search/search_engine.dart';
88
import 'package:analysis_server_plugin/edit/correction_utils.dart';
99
import 'package:analysis_server_plugin/src/correction/change_workspace.dart';
@@ -97,7 +97,7 @@ class AbstractRefactoringContext {
9797

9898
/// The context in which a refactoring was requested.
9999
class RefactoringContext extends AbstractRefactoringContext {
100-
final LspAnalysisServer server;
100+
final AnalysisServer server;
101101

102102
/// Initialize a newly created refactoring context.
103103
RefactoringContext({

pkg/analysis_server/test/lsp/code_actions_mixin.dart

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,6 @@ mixin CodeActionsTestMixin on AbstractLspAnalysisServerTest {
8282
return action.asCodeActionLiteral;
8383
}
8484

85-
/// Expects that command [commandName] was logged to the analytics manager.
86-
void expectCommandLogged(String commandName) {
87-
expect(
88-
server.analyticsManager
89-
.getRequestData(Method.workspace_executeCommand.toString())
90-
.additionalEnumCounts['command']!
91-
.keys,
92-
contains(commandName),
93-
);
94-
}
95-
9685
/// Initializes the server with some basic configuration and expects not to
9786
/// find a [CodeAction] with [kind]/[command]/[title].
9887
Future<void> expectNoAction(

0 commit comments

Comments
 (0)