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 {

Analysis Server API Specification

-

Version 1.6.2

+

Version 1.7.0

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.

+
lineLength ( optional int )
+ +

+ The line length to be used by the formatter. +

Returns

edits ( List<SourceEdit> )

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 ) @@ -1105,8 +1109,8 @@ abstract class IntegrationTestMixin { * * The length of the selection after formatting the code. */ - Future sendEditFormat(String file, int selectionOffset, int selectionLength) { - var params = new EditFormatParams(file, selectionOffset, selectionLength).toJson(); + Future sendEditFormat(String file, int selectionOffset, int selectionLength, {int lineLength}) { + var params = new EditFormatParams(file, selectionOffset, selectionLength, lineLength: lineLength).toJson(); return server.send("edit.format", params) .then((result) { ResponseDecoder decoder = new ResponseDecoder(null); diff --git a/pkg/analysis_server/test/integration/protocol_matchers.dart b/pkg/analysis_server/test/integration/protocol_matchers.dart index 42d9ee981c4..60515ea100f 100644 --- a/pkg/analysis_server/test/integration/protocol_matchers.dart +++ b/pkg/analysis_server/test/integration/protocol_matchers.dart @@ -643,6 +643,7 @@ final Matcher isSearchResultsParams = new LazyMatcher(() => new MatchesJsonObjec * "file": FilePath * "selectionOffset": int * "selectionLength": int + * "lineLength": optional int * } */ final Matcher isEditFormatParams = new LazyMatcher(() => new MatchesJsonObject( @@ -650,6 +651,8 @@ final Matcher isEditFormatParams = new LazyMatcher(() => new MatchesJsonObject( "file": isFilePath, "selectionOffset": isInt, "selectionLength": isInt + }, optionalFields: { + "lineLength": isInt })); /** diff --git a/pkg/analysis_server/tool/spec/generated/java/AnalysisServer.java b/pkg/analysis_server/tool/spec/generated/java/AnalysisServer.java index 2f39f5ef40d..1132dd89db8 100644 --- a/pkg/analysis_server/tool/spec/generated/java/AnalysisServer.java +++ b/pkg/analysis_server/tool/spec/generated/java/AnalysisServer.java @@ -269,8 +269,9 @@ public interface AnalysisServer { * @param file The file containing the code to be formatted. * @param selectionOffset The offset of the current selection in the file. * @param selectionLength The length of the current selection in the file. + * @param lineLength The line length to be used by the formatter. */ - public void edit_format(String file, int selectionOffset, int selectionLength, FormatConsumer consumer); + public void edit_format(String file, int selectionOffset, int selectionLength, int lineLength, FormatConsumer consumer); /** * {@code edit.getAssists} diff --git a/pkg/analysis_server/tool/spec/spec_input.html b/pkg/analysis_server/tool/spec/spec_input.html index 87c3340676e..b2e68fce935 100644 --- a/pkg/analysis_server/tool/spec/spec_input.html +++ b/pkg/analysis_server/tool/spec/spec_input.html @@ -5,7 +5,7 @@

Analysis Server API Specification

-

Version 1.6.2

+

Version 1.7.0

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 @@

--> + + int +

+ The line length to be used by the formatter. +

+