diff --git a/pkg/analysis_server/doc/api.html b/pkg/analysis_server/doc/api.html index 2b31520aaa7..4353e57dfc1 100644 --- a/pkg/analysis_server/doc/api.html +++ b/pkg/analysis_server/doc/api.html @@ -43,7 +43,7 @@ dt.typeDefinition {
This document contains a specification of the API provided by the
analysis server. The API in this document is currently under
@@ -1525,6 +1525,7 @@ dt.typeDefinition {
"file": FilePath
"selectionOffset": int
"selectionLength": int
+ "lineLength": optional int
}
}
response: {
"id": String
@@ -1569,6 +1570,11 @@ dt.typeDefinition {
The length of the current selection in the file.
+ + The line length to be used by the formatter. +
diff --git a/pkg/analysis_server/lib/src/edit/edit_domain.dart b/pkg/analysis_server/lib/src/edit/edit_domain.dart
index 8bb2432dc88..ee26dd8b8dc 100644
--- a/pkg/analysis_server/lib/src/edit/edit_domain.dart
+++ b/pkg/analysis_server/lib/src/edit/edit_domain.dart
@@ -91,7 +91,7 @@ class EditDomainHandler implements RequestHandler {
isCompilationUnit: true,
selectionStart: start,
selectionLength: length);
- DartFormatter formatter = new DartFormatter();
+ DartFormatter formatter = new DartFormatter(pageWidth: params.lineLength);
SourceCode formattedResult;
try {
formattedResult = formatter.formatSource(code);
diff --git a/pkg/analysis_server/lib/src/generated_protocol.dart b/pkg/analysis_server/lib/src/generated_protocol.dart
index 57fcf609157..9a7d1f2852c 100644
--- a/pkg/analysis_server/lib/src/generated_protocol.dart
+++ b/pkg/analysis_server/lib/src/generated_protocol.dart
@@ -4494,6 +4494,7 @@ class SearchResultsParams implements HasToJson {
* "file": FilePath
* "selectionOffset": int
* "selectionLength": int
+ * "lineLength": optional int
* }
*/
class EditFormatParams implements HasToJson {
@@ -4503,6 +4504,8 @@ class EditFormatParams implements HasToJson {
int _selectionLength;
+ int _lineLength;
+
/**
* The file containing the code to be formatted.
*/
@@ -4542,10 +4545,23 @@ class EditFormatParams implements HasToJson {
this._selectionLength = value;
}
- EditFormatParams(String file, int selectionOffset, int selectionLength) {
+ /**
+ * The line length to be used by the formatter.
+ */
+ int get lineLength => _lineLength;
+
+ /**
+ * The line length to be used by the formatter.
+ */
+ void set lineLength(int value) {
+ this._lineLength = value;
+ }
+
+ EditFormatParams(String file, int selectionOffset, int selectionLength, {int lineLength}) {
this.file = file;
this.selectionOffset = selectionOffset;
this.selectionLength = selectionLength;
+ this.lineLength = lineLength;
}
factory EditFormatParams.fromJson(JsonDecoder jsonDecoder, String jsonPath, Object json) {
@@ -4571,7 +4587,11 @@ class EditFormatParams implements HasToJson {
} else {
throw jsonDecoder.missingKey(jsonPath, "selectionLength");
}
- return new EditFormatParams(file, selectionOffset, selectionLength);
+ int lineLength;
+ if (json.containsKey("lineLength")) {
+ lineLength = jsonDecoder._decodeInt(jsonPath + ".lineLength", json["lineLength"]);
+ }
+ return new EditFormatParams(file, selectionOffset, selectionLength, lineLength: lineLength);
} else {
throw jsonDecoder.mismatch(jsonPath, "edit.format params");
}
@@ -4587,6 +4607,9 @@ class EditFormatParams implements HasToJson {
result["file"] = file;
result["selectionOffset"] = selectionOffset;
result["selectionLength"] = selectionLength;
+ if (lineLength != null) {
+ result["lineLength"] = lineLength;
+ }
return result;
}
@@ -4602,7 +4625,8 @@ class EditFormatParams implements HasToJson {
if (other is EditFormatParams) {
return file == other.file &&
selectionOffset == other.selectionOffset &&
- selectionLength == other.selectionLength;
+ selectionLength == other.selectionLength &&
+ lineLength == other.lineLength;
}
return false;
}
@@ -4613,6 +4637,7 @@ class EditFormatParams implements HasToJson {
hash = _JenkinsSmiHash.combine(hash, file.hashCode);
hash = _JenkinsSmiHash.combine(hash, selectionOffset.hashCode);
hash = _JenkinsSmiHash.combine(hash, selectionLength.hashCode);
+ hash = _JenkinsSmiHash.combine(hash, lineLength.hashCode);
return _JenkinsSmiHash.finish(hash);
}
}
diff --git a/pkg/analysis_server/test/edit/format_test.dart b/pkg/analysis_server/test/edit/format_test.dart
index 8d9d0119aa2..20dbdb3a97a 100644
--- a/pkg/analysis_server/test/edit/format_test.dart
+++ b/pkg/analysis_server/test/edit/format_test.dart
@@ -31,7 +31,7 @@ class FormatTest extends AbstractAnalysisTest {
handler = new EditDomainHandler(server);
}
- Future test_formatNoOp() {
+ Future test_format_noOp() {
// Already formatted source
addTestFile('''
main() {
@@ -45,7 +45,7 @@ main() {
});
}
- Future test_formatNoSelection() async {
+ Future test_format_noSelection() async {
addTestFile('''
main() { int x = 3; }
''');
@@ -65,7 +65,7 @@ main() {
expect(formatResult.selectionLength, equals(0));
}
- Future test_formatSimple() {
+ Future test_format_simple() {
addTestFile('''
main() { int x = 3; }
''');
@@ -86,7 +86,25 @@ main() {
});
}
- Future test_withErrors() {
+ Future test_format_longLine() {
+ String content = '''
+fun(firstParam, secondParam, thirdParam, fourthParam) {
+ if (firstParam.noNull && secondParam.noNull && thirdParam.noNull && fourthParam.noNull) {}
+}
+''';
+ addTestFile(content);
+ return waitForTasksFinished().then((_) {
+ EditFormatResult formatResult = _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 test_format_withErrors() {
addTestFile('''
main() { int x =
''');
@@ -97,9 +115,9 @@ main() { int x =
});
}
- EditFormatResult _formatAt(int selectionOffset, int selectionLength) {
+ EditFormatResult _formatAt(int selectionOffset, int selectionLength, {int lineLength}) {
Request request = new EditFormatParams(
- testFile, selectionOffset, selectionLength).toRequest('0');
+ testFile, selectionOffset, selectionLength, lineLength: lineLength).toRequest('0');
Response response = handleSuccessfulRequest(request);
return new EditFormatResult.fromResponse(response);
}
diff --git a/pkg/analysis_server/test/integration/integration_test_methods.dart b/pkg/analysis_server/test/integration/integration_test_methods.dart
index eba2bda6f09..3b609f2d8d9 100644
--- a/pkg/analysis_server/test/integration/integration_test_methods.dart
+++ b/pkg/analysis_server/test/integration/integration_test_methods.dart
@@ -1090,6 +1090,10 @@ abstract class IntegrationTestMixin {
*
* The length of the current selection in the file.
*
+ * lineLength ( optional int )
+ *
+ * The line length to be used by the formatter.
+ *
* Returns
*
* edits ( List
This document contains a specification of the API provided by the
analysis server. The API in this document is currently under
@@ -1362,6 +1362,12 @@
+ The line length to be used by the formatter.
+ Analysis Server API Specification
- Version
+ Version