Skip to content

Commit 75d15bb

Browse files
DanTupCommit Queue
authored and
Commit Queue
committed
[analysis_server] Support formatter page width from analysis_options in legacy protocol
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 #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 #56864 (comment). See #56864 Change-Id: Ic2be00087f78eb62c409dbc8f558d2f24b521f78 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/388920 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 262b759 commit 75d15bb

File tree

9 files changed

+80
-10
lines changed

9 files changed

+80
-10
lines changed

pkg/analysis_server/doc/api.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2321,7 +2321,9 @@ <h4>parameters:</h4><dl><dt class="field"><b>file: <a href="#type_FilePath">File
23212321
</dd><dt class="field"><b>lineLength: int<span style="color:#999999"> (optional)</span></b></dt><dd>
23222322

23232323
<p>
2324-
The line length to be used by the formatter.
2324+
The line length to be used by the formatter. This value is ignored if a
2325+
<tt>formatter.page_width</tt> has been configured in the relevant
2326+
<tt>analysis_options.yaml</tt> file.
23252327
</p>
23262328
</dd></dl><h4>returns:</h4><dl><dt class="field"><b>edits: List&lt;<a href="#type_SourceEdit">SourceEdit</a>&gt;</b></dt><dd>
23272329

pkg/analysis_server/lib/protocol/protocol_generated.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5830,7 +5830,9 @@ class EditFormatParams implements RequestParams {
58305830
/// The length of the current selection in the file.
58315831
int selectionLength;
58325832

5833-
/// The line length to be used by the formatter.
5833+
/// The line length to be used by the formatter. This value is ignored if a
5834+
/// formatter.page_width has been configured in the relevant
5835+
/// analysis_options.yaml file.
58345836
int? lineLength;
58355837

58365838
EditFormatParams(this.file, this.selectionOffset, this.selectionLength,

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:async';
77
import 'package:analysis_server/protocol/protocol.dart';
88
import 'package:analysis_server/protocol/protocol_generated.dart';
99
import 'package:analysis_server/src/handler/legacy/legacy_handler.dart';
10+
import 'package:analyzer/dart/analysis/results.dart';
1011
import 'package:analyzer_plugin/protocol/protocol_common.dart';
1112
import 'package:dart_style/src/dart_formatter.dart';
1213
import 'package:dart_style/src/exceptions.dart';
@@ -50,10 +51,17 @@ class EditFormatHandler extends LegacyHandler {
5051
);
5152

5253
var driver = server.getAnalysisDriver(file);
53-
var library = await driver?.getResolvedLibrary(file);
54+
var unit = await driver?.getResolvedUnit(file);
55+
var (pageWidth, effectiveLanguageVersion) = switch (unit) {
56+
ResolvedUnitResult() => (
57+
unit.analysisOptions.formatterOptions.pageWidth,
58+
unit.libraryElement2.effectiveLanguageVersion,
59+
),
60+
(_) => (null, null),
61+
};
5462
var formatter = DartFormatter(
55-
pageWidth: params.lineLength,
56-
languageVersion: library.effectiveLanguageVersion);
63+
pageWidth: pageWidth ?? params.lineLength,
64+
languageVersion: effectiveLanguageVersion);
5765
SourceCode formattedResult;
5866
try {
5967
formattedResult = formatter.formatSource(code);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:analysis_server/protocol/protocol_generated.dart';
1010
import 'package:analysis_server/src/legacy_analysis_server.dart';
1111
import 'package:analysis_server/src/protocol/protocol_internal.dart';
1212
import 'package:analyzer/dart/analysis/results.dart';
13+
import 'package:analyzer/dart/element/element2.dart';
1314
import 'package:analyzer/error/error.dart';
1415
import 'package:analyzer/src/dart/error/syntactic_errors.g.dart';
1516
import 'package:analyzer/src/util/performance/operation_performance.dart';
@@ -100,6 +101,14 @@ abstract class LegacyHandler {
100101
}
101102
}
102103

104+
extension LibraryElement2Extension on LibraryElement2 {
105+
// TODO(dantup): Remove this once we can use `languageVersion.effective`
106+
// in element-model-migrated files without triggering the lint.
107+
Version get effectiveLanguageVersion {
108+
return languageVersion.effective;
109+
}
110+
}
111+
103112
extension SomeResolvedLibraryResultExtension on SomeResolvedLibraryResult? {
104113
Version get effectiveLanguageVersion {
105114
var self = this;

pkg/analysis_server/test/edit/format_test.dart

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,49 @@ class FormatTest extends PubPackageAnalysisServerTest {
2323
await setRoots(included: [workspaceRootPath], excluded: []);
2424
}
2525

26-
Future<void> test_format_longLine() async {
26+
Future<void> test_format_longLine_analysisOptions() async {
27+
writeTestPackageAnalysisOptionsFile(r'''
28+
formatter:
29+
page_width: 100
30+
''');
31+
var content = '''
32+
fun(firstParam, secondParam, thirdParam, fourthParam) {
33+
if (firstParam.noNull && secondParam.noNull && thirdParam.noNull && fourthParam.noNull) {}
34+
}
35+
''';
36+
addTestFile(content);
37+
await waitForTasksFinished();
38+
var formatResult = await _formatAt(0, 3);
39+
40+
expect(formatResult.edits, isNotNull);
41+
expect(formatResult.edits, hasLength(0));
42+
43+
expect(formatResult.selectionOffset, equals(0));
44+
expect(formatResult.selectionLength, equals(3));
45+
}
46+
47+
Future<void> test_format_longLine_analysisOptions_overridesParameter() async {
48+
writeTestPackageAnalysisOptionsFile(r'''
49+
formatter:
50+
page_width: 100
51+
''');
52+
var content = '''
53+
fun(firstParam, secondParam, thirdParam, fourthParam) {
54+
if (firstParam.noNull && secondParam.noNull && thirdParam.noNull && fourthParam.noNull) {}
55+
}
56+
''';
57+
addTestFile(content);
58+
await waitForTasksFinished();
59+
var formatResult = await _formatAt(0, 3, lineLength: 50);
60+
61+
expect(formatResult.edits, isNotNull);
62+
expect(formatResult.edits, hasLength(0));
63+
64+
expect(formatResult.selectionOffset, equals(0));
65+
expect(formatResult.selectionLength, equals(3));
66+
}
67+
68+
Future<void> test_format_longLine_parameter() async {
2769
var content = '''
2870
fun(firstParam, secondParam, thirdParam, fourthParam) {
2971
if (firstParam.noNull && secondParam.noNull && thirdParam.noNull && fourthParam.noNull) {}

pkg/analysis_server/test/integration/support/integration_test_methods.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,9 @@ abstract class IntegrationTest {
15661566
///
15671567
/// lineLength: int (optional)
15681568
///
1569-
/// The line length to be used by the formatter.
1569+
/// The line length to be used by the formatter. This value is ignored if a
1570+
/// formatter.page_width has been configured in the relevant
1571+
/// analysis_options.yaml file.
15701572
///
15711573
/// Returns
15721574
///

pkg/analysis_server/tool/spec/generated/java/AnalysisServer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,8 @@ public interface AnalysisServer {
511511
* @param file The file containing the code to be formatted.
512512
* @param selectionOffset The offset of the current selection in the file.
513513
* @param selectionLength The length of the current selection in the file.
514-
* @param lineLength The line length to be used by the formatter.
514+
* @param lineLength The line length to be used by the formatter. This value is ignored if a
515+
* formatter.page_width has been configured in the relevant analysis_options.yaml file.
515516
*/
516517
public void edit_format(String file, int selectionOffset, int selectionLength, int lineLength, FormatConsumer consumer);
517518

pkg/analysis_server/tool/spec/spec_input.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,9 @@ <h4>Options</h4>
21862186
<field name="lineLength" optional="true">
21872187
<ref>int</ref>
21882188
<p>
2189-
The line length to be used by the formatter.
2189+
The line length to be used by the formatter. This value is ignored if a
2190+
<tt>formatter.page_width</tt> has been configured in the relevant
2191+
<tt>analysis_options.yaml</tt> file.
21902192
</p>
21912193
</field>
21922194
</params>

pkg/analysis_server_client/lib/src/protocol/protocol_generated.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5253,7 +5253,9 @@ class EditFormatParams implements RequestParams {
52535253
/// The length of the current selection in the file.
52545254
int selectionLength;
52555255

5256-
/// The line length to be used by the formatter.
5256+
/// The line length to be used by the formatter. This value is ignored if a
5257+
/// formatter.page_width has been configured in the relevant
5258+
/// analysis_options.yaml file.
52575259
int? lineLength;
52585260

52595261
EditFormatParams(this.file, this.selectionOffset, this.selectionLength,

0 commit comments

Comments
 (0)