Skip to content

Commit

Permalink
First cut dartfix protocol
Browse files Browse the repository at this point in the history
and address comments in https://dart-review.googlesource.com/c/sdk/+/76320

Change-Id: I5c7ab40810d4116b1d36de90b7234d0a932bae82
Reviewed-on: https://dart-review.googlesource.com/76400
Commit-Queue: Dan Rubel <danrubel@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
Dan Rubel authored and commit-bot@chromium.org committed Sep 25, 2018
1 parent 4cf2d39 commit c624f8b
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/analysis_server/doc/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,7 @@ <h2 class="domain"><a name="domain_edit">edit domain</a></h2>




<h3>Requests</h3><dl><dt class="request"><a name="request_edit.format">edit.format</a></dt><dd><div class="box"><pre>request: {
"id": String
"method": "edit.format"
Expand Down
4 changes: 4 additions & 0 deletions pkg/analysis_server/lib/protocol/protocol_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ const String DIAGNOSTIC_REQUEST_GET_DIAGNOSTICS = 'diagnostic.getDiagnostics';
const String DIAGNOSTIC_REQUEST_GET_SERVER_PORT = 'diagnostic.getServerPort';
const String DIAGNOSTIC_RESPONSE_GET_DIAGNOSTICS_CONTEXTS = 'contexts';
const String DIAGNOSTIC_RESPONSE_GET_SERVER_PORT_PORT = 'port';
const String EDIT_REQUEST_DARTFIX = 'edit.dartfix';
const String EDIT_REQUEST_DARTFIX_INCLUDED = 'included';
const String EDIT_REQUEST_FORMAT = 'edit.format';
const String EDIT_REQUEST_FORMAT_FILE = 'file';
const String EDIT_REQUEST_FORMAT_LINE_LENGTH = 'lineLength';
Expand Down Expand Up @@ -171,6 +173,8 @@ const String EDIT_REQUEST_ORGANIZE_DIRECTIVES = 'edit.organizeDirectives';
const String EDIT_REQUEST_ORGANIZE_DIRECTIVES_FILE = 'file';
const String EDIT_REQUEST_SORT_MEMBERS = 'edit.sortMembers';
const String EDIT_REQUEST_SORT_MEMBERS_FILE = 'file';
const String EDIT_RESPONSE_DARTFIX_DESCRIPTION = 'description';
const String EDIT_RESPONSE_DARTFIX_FIXES = 'fixes';
const String EDIT_RESPONSE_FORMAT_EDITS = 'edits';
const String EDIT_RESPONSE_FORMAT_SELECTION_LENGTH = 'selectionLength';
const String EDIT_RESPONSE_FORMAT_SELECTION_OFFSET = 'selectionOffset';
Expand Down
212 changes: 212 additions & 0 deletions pkg/analysis_server/lib/protocol/protocol_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6165,6 +6165,218 @@ class DiagnosticGetServerPortResult implements ResponseResult {
}
}

/**
* edit.dartfix params
*
* {
* "included": List<FilePath>
* }
*
* Clients may not extend, implement or mix-in this class.
*/
class EditDartfixParams implements RequestParams {
List<String> _included;

/**
* A list of the files and directories for which edits should be suggested.
* If a request is made for a file which does not exist, or which is not
* currently subject to analysis (e.g. because it is not associated with any
* analysis root specified to analysis.setAnalysisRoots), an error of type
* FORMAT_INVALID_FILE will be generated.
*/
List<String> get included => _included;

/**
* A list of the files and directories for which edits should be suggested.
* If a request is made for a file which does not exist, or which is not
* currently subject to analysis (e.g. because it is not associated with any
* analysis root specified to analysis.setAnalysisRoots), an error of type
* FORMAT_INVALID_FILE will be generated.
*/
void set included(List<String> value) {
assert(value != null);
this._included = value;
}

EditDartfixParams(List<String> included) {
this.included = included;
}

factory EditDartfixParams.fromJson(
JsonDecoder jsonDecoder, String jsonPath, Object json) {
if (json == null) {
json = {};
}
if (json is Map) {
List<String> included;
if (json.containsKey("included")) {
included = jsonDecoder.decodeList(
jsonPath + ".included", json["included"], jsonDecoder.decodeString);
} else {
throw jsonDecoder.mismatch(jsonPath, "included");
}
return new EditDartfixParams(included);
} else {
throw jsonDecoder.mismatch(jsonPath, "edit.dartfix params", json);
}
}

factory EditDartfixParams.fromRequest(Request request) {
return new EditDartfixParams.fromJson(
new RequestDecoder(request), "params", request.params);
}

@override
Map<String, dynamic> toJson() {
Map<String, dynamic> result = {};
result["included"] = included;
return result;
}

@override
Request toRequest(String id) {
return new Request(id, "edit.dartfix", toJson());
}

@override
String toString() => json.encode(toJson());

@override
bool operator ==(other) {
if (other is EditDartfixParams) {
return listEqual(
included, other.included, (String a, String b) => a == b);
}
return false;
}

@override
int get hashCode {
int hash = 0;
hash = JenkinsSmiHash.combine(hash, included.hashCode);
return JenkinsSmiHash.finish(hash);
}
}

/**
* edit.dartfix result
*
* {
* "description": List<String>
* "fixes": List<SourceFileEdit>
* }
*
* Clients may not extend, implement or mix-in this class.
*/
class EditDartfixResult implements ResponseResult {
List<String> _description;

List<SourceFileEdit> _fixes;

/**
* A list of human readable changes made by applying the fixes.
*/
List<String> get description => _description;

/**
* A list of human readable changes made by applying the fixes.
*/
void set description(List<String> value) {
assert(value != null);
this._description = value;
}

/**
* The suggested fixes.
*/
List<SourceFileEdit> get fixes => _fixes;

/**
* The suggested fixes.
*/
void set fixes(List<SourceFileEdit> value) {
assert(value != null);
this._fixes = value;
}

EditDartfixResult(List<String> description, List<SourceFileEdit> fixes) {
this.description = description;
this.fixes = fixes;
}

factory EditDartfixResult.fromJson(
JsonDecoder jsonDecoder, String jsonPath, Object json) {
if (json == null) {
json = {};
}
if (json is Map) {
List<String> description;
if (json.containsKey("description")) {
description = jsonDecoder.decodeList(jsonPath + ".description",
json["description"], jsonDecoder.decodeString);
} else {
throw jsonDecoder.mismatch(jsonPath, "description");
}
List<SourceFileEdit> fixes;
if (json.containsKey("fixes")) {
fixes = jsonDecoder.decodeList(
jsonPath + ".fixes",
json["fixes"],
(String jsonPath, Object json) =>
new SourceFileEdit.fromJson(jsonDecoder, jsonPath, json));
} else {
throw jsonDecoder.mismatch(jsonPath, "fixes");
}
return new EditDartfixResult(description, fixes);
} else {
throw jsonDecoder.mismatch(jsonPath, "edit.dartfix result", json);
}
}

factory EditDartfixResult.fromResponse(Response response) {
return new EditDartfixResult.fromJson(
new ResponseDecoder(REQUEST_ID_REFACTORING_KINDS.remove(response.id)),
"result",
response.result);
}

@override
Map<String, dynamic> toJson() {
Map<String, dynamic> result = {};
result["description"] = description;
result["fixes"] =
fixes.map((SourceFileEdit value) => value.toJson()).toList();
return result;
}

@override
Response toResponse(String id) {
return new Response(id, result: toJson());
}

@override
String toString() => json.encode(toJson());

@override
bool operator ==(other) {
if (other is EditDartfixResult) {
return listEqual(
description, other.description, (String a, String b) => a == b) &&
listEqual(fixes, other.fixes,
(SourceFileEdit a, SourceFileEdit b) => a == b);
}
return false;
}

@override
int get hashCode {
int hash = 0;
hash = JenkinsSmiHash.combine(hash, description.hashCode);
hash = JenkinsSmiHash.combine(hash, fixes.hashCode);
return JenkinsSmiHash.finish(hash);
}
}

/**
* edit.format params
*
Expand Down
1 change: 1 addition & 0 deletions pkg/analysis_server/test/integration/coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ server calls. This file is validated by `coverage_test.dart`.
- [x] diagnostic.getServerPort

## edit domain
- [ ] edit.dartfix
- [x] edit.format
- [x] edit.getAssists
- [x] edit.getAvailableRefactorings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,39 @@ abstract class IntegrationTestMixin {
decoder, 'result', result);
}

/**
* Analyze the specified sources for recommended changes and return a set of
* suggested edits for those sources. These edits may include changes to
* sources outside the set of specified sources if a change in a specified
* source requires it.
*
* Parameters
*
* included: List<FilePath>
*
* A list of the files and directories for which edits should be suggested.
* If a request is made for a file which does not exist, or which is not
* currently subject to analysis (e.g. because it is not associated with
* any analysis root specified to analysis.setAnalysisRoots), an error of
* type FORMAT_INVALID_FILE will be generated.
*
* Returns
*
* description: List<String>
*
* A list of human readable changes made by applying the fixes.
*
* fixes: List<SourceFileEdit>
*
* The suggested fixes.
*/
Future<EditDartfixResult> sendEditDartfix(List<String> included) async {
var params = new EditDartfixParams(included).toJson();
var result = await server.send("edit.dartfix", params);
ResponseDecoder decoder = new ResponseDecoder(null);
return new EditDartfixResult.fromJson(decoder, 'result', result);
}

/**
* Return the set of fixes that are available for the errors at a given
* offset in a given file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2121,6 +2121,28 @@ final Matcher isDiagnosticGetServerPortParams = isNull;
final Matcher isDiagnosticGetServerPortResult = new LazyMatcher(() =>
new MatchesJsonObject("diagnostic.getServerPort result", {"port": isInt}));

/**
* edit.dartfix params
*
* {
* "included": List<FilePath>
* }
*/
final Matcher isEditDartfixParams = new LazyMatcher(() => new MatchesJsonObject(
"edit.dartfix params", {"included": isListOf(isFilePath)}));

/**
* edit.dartfix result
*
* {
* "description": List<String>
* "fixes": List<SourceFileEdit>
* }
*/
final Matcher isEditDartfixResult = new LazyMatcher(() => new MatchesJsonObject(
"edit.dartfix result",
{"description": isListOf(isString), "fixes": isListOf(isSourceFileEdit)}));

/**
* edit.format params
*
Expand Down
14 changes: 14 additions & 0 deletions pkg/analysis_server/tool/spec/generated/java/AnalysisServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,20 @@ public interface AnalysisServer {
*/
public void diagnostic_getServerPort(GetServerPortConsumer consumer);

/**
* {@code edit.dartfix}
*
* Analyze the specified sources for recommended changes and return a set of suggested edits for
* those sources. These edits may include changes to sources outside the set of specified sources
* if a change in a specified source requires it.
*
* @param included A list of the files and directories for which edits should be suggested. If a
* request is made for a file which does not exist, or which is not currently subject to
* analysis (e.g. because it is not associated with any analysis root specified to
* analysis.setAnalysisRoots), an error of type FORMAT_INVALID_FILE will be generated.
*/
public void edit_dartfix(List<String> included, DartfixConsumer consumer);

/**
* {@code edit.format}
*
Expand Down
40 changes: 40 additions & 0 deletions pkg/analysis_server/tool/spec/spec_input.html
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,46 @@ <h4>Options</h4>
</field>
</result>
</request>
<request method="dartfix" experimental="true">
<p>
Analyze the specified sources for recommended changes
and return a set of suggested edits for those sources.
These edits may include changes to sources outside the set
of specified sources if a change in a specified source requires it.
</p>
<params>
<field name="included">
<list>
<ref>FilePath</ref>
</list>
<p>
A list of the files and directories for which edits should be suggested.
If a request is made for a file which does not exist, or which is not
currently subject to analysis (e.g. because it is not associated with
any analysis root specified to analysis.setAnalysisRoots), an error of
type <tt>FORMAT_INVALID_FILE</tt> will be generated.
</p>
</field>
</params>
<result>
<field name="description">
<list>
<ref>String</ref>
</list>
<p>
A list of human readable changes made by applying the fixes.
</p>
</field>
<field name="fixes">
<list>
<ref>SourceFileEdit</ref>
</list>
<p>
The suggested fixes.
</p>
</field>
</result>
</request>
<request method="getFixes">
<p>
Return the set of fixes that are available for the errors at
Expand Down
3 changes: 3 additions & 0 deletions pkg/analyzer_cli/lib/src/fix/driver.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) 2018, 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 'dart:async';

import 'package:analyzer_cli/src/fix/options.dart';
Expand Down
Loading

0 comments on commit c624f8b

Please sign in to comment.