75d15bbfa3
This adds the same support previously added to LSP to the legacy protocol. As with LSP, the value in analysis_options overrides the explicit argument (since the expectation is that the user has not specifically chosen this value for this file, but rather as a default for the project/globally). See https://github.com/dart-lang/sdk/issues/56864 [analyzer] Add support for "formatter" in analysis_options + use page_width in LSP This adds support for validation + completion for `formatter/page_width` in analysis_options, and uses this value in preference to the client-supplied value in the LSP server. It does not yet add support for the legacy protocol, and there are a few questions in https://github.com/dart-lang/sdk/issues/56864#issuecomment-2399289974. See https://github.com/dart-lang/sdk/issues/56864 Change-Id: Ic2be00087f78eb62c409dbc8f558d2f24b521f78 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/388920 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
160 lines
4.6 KiB
Dart
160 lines
4.6 KiB
Dart
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
import 'package:analysis_server/protocol/protocol_generated.dart';
|
|
import 'package:test/test.dart';
|
|
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
|
|
|
import '../analysis_server_base.dart';
|
|
import '../mocks.dart';
|
|
|
|
void main() {
|
|
defineReflectiveSuite(() {
|
|
defineReflectiveTests(FormatTest);
|
|
});
|
|
}
|
|
|
|
@reflectiveTest
|
|
class FormatTest extends PubPackageAnalysisServerTest {
|
|
@override
|
|
Future<void> setUp() async {
|
|
super.setUp();
|
|
await setRoots(included: [workspaceRootPath], excluded: []);
|
|
}
|
|
|
|
Future<void> test_format_longLine_analysisOptions() async {
|
|
writeTestPackageAnalysisOptionsFile(r'''
|
|
formatter:
|
|
page_width: 100
|
|
''');
|
|
var content = '''
|
|
fun(firstParam, secondParam, thirdParam, fourthParam) {
|
|
if (firstParam.noNull && secondParam.noNull && thirdParam.noNull && fourthParam.noNull) {}
|
|
}
|
|
''';
|
|
addTestFile(content);
|
|
await waitForTasksFinished();
|
|
var formatResult = await _formatAt(0, 3);
|
|
|
|
expect(formatResult.edits, isNotNull);
|
|
expect(formatResult.edits, hasLength(0));
|
|
|
|
expect(formatResult.selectionOffset, equals(0));
|
|
expect(formatResult.selectionLength, equals(3));
|
|
}
|
|
|
|
Future<void> test_format_longLine_analysisOptions_overridesParameter() async {
|
|
writeTestPackageAnalysisOptionsFile(r'''
|
|
formatter:
|
|
page_width: 100
|
|
''');
|
|
var content = '''
|
|
fun(firstParam, secondParam, thirdParam, fourthParam) {
|
|
if (firstParam.noNull && secondParam.noNull && thirdParam.noNull && fourthParam.noNull) {}
|
|
}
|
|
''';
|
|
addTestFile(content);
|
|
await waitForTasksFinished();
|
|
var formatResult = await _formatAt(0, 3, lineLength: 50);
|
|
|
|
expect(formatResult.edits, isNotNull);
|
|
expect(formatResult.edits, hasLength(0));
|
|
|
|
expect(formatResult.selectionOffset, equals(0));
|
|
expect(formatResult.selectionLength, equals(3));
|
|
}
|
|
|
|
Future<void> test_format_longLine_parameter() async {
|
|
var content = '''
|
|
fun(firstParam, secondParam, thirdParam, fourthParam) {
|
|
if (firstParam.noNull && secondParam.noNull && thirdParam.noNull && fourthParam.noNull) {}
|
|
}
|
|
''';
|
|
addTestFile(content);
|
|
await waitForTasksFinished();
|
|
var formatResult = await _formatAt(0, 3, lineLength: 100);
|
|
|
|
expect(formatResult.edits, isNotNull);
|
|
expect(formatResult.edits, hasLength(0));
|
|
|
|
expect(formatResult.selectionOffset, equals(0));
|
|
expect(formatResult.selectionLength, equals(3));
|
|
}
|
|
|
|
Future<void> test_format_noOp() async {
|
|
// Already formatted source
|
|
addTestFile('''
|
|
void f() {
|
|
int x = 3;
|
|
}
|
|
''');
|
|
await waitForTasksFinished();
|
|
var formatResult = await _formatAt(0, 3);
|
|
expect(formatResult.edits, isNotNull);
|
|
expect(formatResult.edits, hasLength(0));
|
|
}
|
|
|
|
Future<void> test_format_noSelection() async {
|
|
addTestFile('''
|
|
void f() { int x = 3; }
|
|
''');
|
|
await waitForTasksFinished();
|
|
var formatResult = await _formatAt(0, 0);
|
|
|
|
expect(formatResult.edits, isNotNull);
|
|
expect(formatResult.edits, hasLength(1));
|
|
|
|
var edit = formatResult.edits[0];
|
|
expect(edit.replacement, equals('''
|
|
void f() {
|
|
int x = 3;
|
|
}
|
|
'''));
|
|
expect(formatResult.selectionOffset, equals(0));
|
|
expect(formatResult.selectionLength, equals(0));
|
|
}
|
|
|
|
Future<void> test_format_simple() async {
|
|
addTestFile('''
|
|
void f() { int x = 3; }
|
|
''');
|
|
await waitForTasksFinished();
|
|
var formatResult = await _formatAt(0, 3);
|
|
|
|
expect(formatResult.edits, isNotNull);
|
|
expect(formatResult.edits, hasLength(1));
|
|
|
|
var edit = formatResult.edits[0];
|
|
expect(edit.replacement, equals('''
|
|
void f() {
|
|
int x = 3;
|
|
}
|
|
'''));
|
|
expect(formatResult.selectionOffset, equals(0));
|
|
expect(formatResult.selectionLength, equals(3));
|
|
}
|
|
|
|
Future<void> test_format_withErrors() async {
|
|
addTestFile('''
|
|
void f() { int x =
|
|
''');
|
|
await waitForTasksFinished();
|
|
var request = EditFormatParams(testFile.path, 0, 3)
|
|
.toRequest('0', clientUriConverter: server.uriConverter);
|
|
var response = await handleRequest(request);
|
|
expect(response, isResponseFailure('0'));
|
|
}
|
|
|
|
Future<EditFormatResult> _formatAt(int selectionOffset, int selectionLength,
|
|
{int? lineLength}) async {
|
|
var request = EditFormatParams(
|
|
testFile.path, selectionOffset, selectionLength,
|
|
lineLength: lineLength)
|
|
.toRequest('0', clientUriConverter: server.uriConverter);
|
|
var response = await handleSuccessfulRequest(request);
|
|
return EditFormatResult.fromResponse(response,
|
|
clientUriConverter: server.uriConverter);
|
|
}
|
|
}
|