Skip to content

Commit 7e36f3b

Browse files
author
Dart CI
committed
Version 2.18.0-161.0.dev
Merge commit '75d622a25d5928e62ee10ee12534648dd95963d0' into 'dev'
2 parents 8d7d7ed + 75d622a commit 7e36f3b

File tree

5 files changed

+57
-34
lines changed

5 files changed

+57
-34
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ abstract class ServerErrorCodes {
214214
static const FileHasErrors = ErrorCodes(-32008);
215215
static const ClientFailedToApplyEdit = ErrorCodes(-32009);
216216
static const RenameNotValid = ErrorCodes(-32010);
217-
static const RefactorFailed = ErrorCodes(-32011);
218217
static const FeatureDisabled = ErrorCodes(-32012);
219218

220219
/// A file that is expected to be analyzed, but failed.

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import 'dart:async';
66

77
import 'package:analysis_server/lsp_protocol/protocol.dart';
8-
import 'package:analysis_server/src/lsp/constants.dart';
98
import 'package:analysis_server/src/lsp/handlers/commands/abstract_refactor.dart';
109
import 'package:analysis_server/src/lsp/handlers/handlers.dart';
1110
import 'package:analysis_server/src/lsp/mapping.dart';
@@ -48,7 +47,11 @@ class PerformRefactorCommandHandler extends AbstractRefactorCommandHandler {
4847
final status = await refactoring.checkAllConditions();
4948

5049
if (status.hasError) {
51-
return error(ServerErrorCodes.RefactorFailed, status.message!);
50+
// Show the error to the user but don't fail the request, as the
51+
// LSP Client may show a failed request in a way that looks like a
52+
// server error.
53+
server.showErrorMessageToUser(status.message!);
54+
return success(null);
5255
}
5356

5457
if (cancellationToken.isCancellationRequested ||

pkg/analysis_server/test/lsp/code_actions_refactor_test.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,52 @@ void doFoo(void Function() a) => a();
475475
expect(response.message, contains('Cannot extract closure as method'));
476476
}
477477

478+
/// Test if the client does not call refactor.validate it still gets a
479+
/// sensible `showMessage` call and not a failed request.
480+
Future<void> test_validLocation_failsInitialValidation_noValidation() async {
481+
const content = '''
482+
f() {
483+
var a = 0;
484+
doFoo([[() => print(a)]]);
485+
print(a);
486+
}
487+
488+
void doFoo(void Function() a) => a();
489+
490+
''';
491+
newFile(mainFilePath, withoutMarkers(content));
492+
await initialize(
493+
// We expect an error notification so don't fail on it.
494+
failTestOnAnyErrorNotification: false,
495+
);
496+
497+
final codeActions = await getCodeActions(mainFileUri.toString(),
498+
range: rangeFromMarkers(content));
499+
final codeAction =
500+
findCommand(codeActions, Commands.performRefactor, extractMethodTitle)!;
501+
502+
final command = codeAction.map(
503+
(command) => command,
504+
(codeAction) => codeAction.command!,
505+
);
506+
507+
// Call the refactor without any validation and expected an error message
508+
// without a request failure.
509+
final errorNotification = await expectErrorNotification(() async {
510+
final response = await executeCommand(
511+
Command(
512+
title: command.title,
513+
command: command.command,
514+
arguments: command.arguments),
515+
);
516+
expect(response, isNull);
517+
});
518+
expect(
519+
errorNotification.message,
520+
contains('Cannot extract closure as method'),
521+
);
522+
}
523+
478524
Future<void> test_validLocation_passesInitialValidation() async {
479525
const content = '''
480526
f() {

pkg/analysis_server/test/mocks_lsp.dart

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import 'dart:convert';
88
import 'package:analysis_server/lsp_protocol/protocol.dart' as lsp;
99
import 'package:analysis_server/src/lsp/channel/lsp_channel.dart';
1010

11-
const _jsonEncoder = JsonEncoder.withIndent(' ');
12-
1311
/// A mock [LspServerCommunicationChannel] for testing [LspAnalysisServer].
1412
class MockLspServerChannel implements LspServerCommunicationChannel {
1513
final StreamController<lsp.Message> _clientToServer =
@@ -160,22 +158,14 @@ class MockLspServerChannel implements LspServerCommunicationChannel {
160158
/// received response. The returned future will throw an exception if a server
161159
/// error is reported before the response has been received.
162160
///
163-
/// Unlike [sendLspRequest], this method assumes that the [request] has
161+
/// Unlike [sendRequestToServer], this method assumes that the [request] has
164162
/// already been sent to the server.
165163
Future<lsp.ResponseMessage> waitForResponse(
166-
lsp.RequestMessage request, {
167-
bool throwOnError = true,
168-
}) async {
164+
lsp.RequestMessage request) async {
169165
final response = await _serverToClient.stream.firstWhere((message) =>
170-
(message is lsp.ResponseMessage && message.id == request.id) ||
171-
(throwOnError && _isShowErrorMessageNotification(message)));
172-
173-
if (response is lsp.ResponseMessage) {
174-
return response;
175-
} else {
176-
throw 'An error occurred while waiting for a response to ${request.method}: '
177-
'${_jsonEncoder.convert(response.toJson())}';
178-
}
166+
message is lsp.ResponseMessage && message.id == request.id);
167+
168+
return response as lsp.ResponseMessage;
179169
}
180170

181171
/// Round trips the object to JSON and back to ensure it behaves the same as
@@ -187,19 +177,4 @@ class MockLspServerChannel implements LspServerCommunicationChannel {
187177
return constructor(
188178
jsonDecode(jsonEncode(message.toJson())) as Map<String, Object?>);
189179
}
190-
191-
/// Checks whether [message] is a `window/showMessage` notification with a
192-
/// type of [lsp.MessageType.Error].
193-
bool _isShowErrorMessageNotification(lsp.Message message) {
194-
if (message is! lsp.NotificationMessage) {
195-
return false;
196-
}
197-
if (message.method != lsp.Method.window_showMessage) {
198-
return false;
199-
}
200-
final params = lsp.ShowMessageParams.fromJson(
201-
message.params as Map<String, Object?>,
202-
);
203-
return params.type == lsp.MessageType.Error;
204-
}
205180
}

tools/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ CHANNEL dev
2727
MAJOR 2
2828
MINOR 18
2929
PATCH 0
30-
PRERELEASE 160
30+
PRERELEASE 161
3131
PRERELEASE_PATCH 0

0 commit comments

Comments
 (0)