Skip to content

Commit ba459f9

Browse files
DanTupcommit-bot@chromium.org
authored andcommitted
Allow dart.lineLength to control formatting for LSP
Noted at Dart-Code/Dart-Code#2286 (comment). Change-Id: If45bede84a0a4414a16d1809a1aa0f81d18cebfb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/154008 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Danny Tuppeny <[email protected]>
1 parent da9697f commit ba459f9

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class LspClientConfiguration {
88
final Map<String, dynamic> _settings = <String, dynamic>{};
99

1010
bool get enableSdkFormatter => _settings['enableSdkFormatter'] ?? true;
11+
int get lineLength => _settings['lineLength'];
1112

1213
void replace(Map<String, dynamic> newConfig) {
1314
_settings

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class FormatOnTypeHandler
2929
}
3030

3131
final unformattedSource = file.readAsStringSync();
32-
return success(generateEditsForFormatting(unformattedSource));
32+
return success(generateEditsForFormatting(
33+
unformattedSource, server.clientConfiguration.lineLength));
3334
}
3435

3536
@override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class FormattingHandler
2929
}
3030

3131
final unformattedSource = file.readAsStringSync();
32-
return success(generateEditsForFormatting(unformattedSource));
32+
return success(generateEditsForFormatting(
33+
unformattedSource, server.clientConfiguration.lineLength));
3334
}
3435

3536
@override

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:analyzer_plugin/protocol/protocol_common.dart' as plugin;
88
import 'package:analyzer_plugin/utilities/pair.dart';
99
import 'package:dart_style/dart_style.dart';
1010

11-
final DartFormatter formatter = DartFormatter();
11+
DartFormatter formatter = DartFormatter();
1212

1313
/// Transforms a sequence of LSP document change events to a sequence of source
1414
/// edits used by analysis plugins.
@@ -66,12 +66,17 @@ ErrorOr<Pair<String, List<plugin.SourceEdit>>> applyAndConvertEditsToServer(
6666
return ErrorOr.success(Pair(newContent, serverEdits));
6767
}
6868

69-
List<TextEdit> generateEditsForFormatting(String unformattedSource) {
69+
List<TextEdit> generateEditsForFormatting(
70+
String unformattedSource, int lineLength) {
7071
final lineInfo = LineInfo.fromContent(unformattedSource);
7172
final code =
7273
SourceCode(unformattedSource, uri: null, isCompilationUnit: true);
7374
SourceCode formattedResult;
7475
try {
76+
// If the lineLength has changed, recreate the formatter with the new setting.
77+
if (lineLength != formatter.pageWidth) {
78+
formatter = DartFormatter(pageWidth: lineLength);
79+
}
7580
formattedResult = formatter.formatSource(code);
7681
} on FormatterException {
7782
// If the document fails to parse, just return no edits to avoid the the

pkg/analysis_server/test/lsp/format_test.dart

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ void main() {
1818

1919
@reflectiveTest
2020
class FormatTest extends AbstractLspAnalysisServerTest {
21+
Future<void> expectFormattedContents(
22+
Uri uri, String original, String expected) async {
23+
final formatEdits = await formatDocument(uri.toString());
24+
final formattedContents = applyTextEdits(original, formatEdits);
25+
expect(formattedContents, equals(expected));
26+
}
27+
2128
Future<void> test_alreadyFormatted() async {
2229
const contents = '''main() {
2330
print('test');
@@ -149,6 +156,32 @@ class FormatTest extends AbstractLspAnalysisServerTest {
149156
expect(formatEdits, isNull);
150157
}
151158

159+
Future<void> test_lineLength() async {
160+
const contents = '''
161+
main() =>
162+
print(
163+
'123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789'
164+
);
165+
''';
166+
final expectedDefault = '''main() => print(
167+
'123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789');\n''';
168+
final expectedLongLines =
169+
'''main() => print('123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789');\n''';
170+
171+
// Initialize with config support, supplying an empty config when requested.
172+
await provideConfig(
173+
() => initialize(
174+
workspaceCapabilities: withDidChangeConfigurationDynamicRegistration(
175+
withConfigurationSupport(emptyWorkspaceClientCapabilities))),
176+
{}, // empty config
177+
);
178+
await openFile(mainFileUri, contents);
179+
180+
await expectFormattedContents(mainFileUri, contents, expectedDefault);
181+
await updateConfig({'lineLength': 500});
182+
await expectFormattedContents(mainFileUri, contents, expectedLongLines);
183+
}
184+
152185
Future<void> test_nonDartFile() async {
153186
await initialize();
154187
await openFile(pubspecFileUri, simplePubspecContent);
@@ -201,11 +234,7 @@ class FormatTest extends AbstractLspAnalysisServerTest {
201234
''';
202235
await initialize();
203236
await openFile(mainFileUri, contents);
204-
205-
final formatEdits = await formatDocument(mainFileUri.toString());
206-
expect(formatEdits, isNotNull);
207-
final formattedContents = applyTextEdits(contents, formatEdits);
208-
expect(formattedContents, equals(expected));
237+
await expectFormattedContents(mainFileUri, contents, expected);
209238
}
210239

211240
Future<void> test_unopenFile() async {
@@ -222,10 +251,6 @@ class FormatTest extends AbstractLspAnalysisServerTest {
222251
''';
223252
newFile(mainFilePath, content: contents);
224253
await initialize();
225-
226-
final formatEdits = await formatDocument(mainFileUri.toString());
227-
expect(formatEdits, isNotNull);
228-
final formattedContents = applyTextEdits(contents, formatEdits);
229-
expect(formattedContents, equals(expected));
254+
await expectFormattedContents(mainFileUri, contents, expected);
230255
}
231256
}

0 commit comments

Comments
 (0)