Skip to content

Commit

Permalink
Add signature param defaultValue and remove (unused) selectedParamete…
Browse files Browse the repository at this point in the history
…rIndex

Bug: #27034
Change-Id: I3409ab544b79a7f64d6d41e5d091ca20418f248a
Reviewed-on: https://dart-review.googlesource.com/66562
Commit-Queue: Danny Tuppeny <dantup@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
DanTup authored and commit-bot@chromium.org committed Jul 25, 2018
1 parent e5a5815 commit d792809
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 67 deletions.
2 changes: 0 additions & 2 deletions pkg/analysis_server/lib/protocol/protocol_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ const String ANALYSIS_RESPONSE_GET_REACHABLE_SOURCES_SOURCES = 'sources';
const String ANALYSIS_RESPONSE_GET_SIGNATURE_DARTDOC = 'dartdoc';
const String ANALYSIS_RESPONSE_GET_SIGNATURE_NAME = 'name';
const String ANALYSIS_RESPONSE_GET_SIGNATURE_PARAMETERS = 'parameters';
const String ANALYSIS_RESPONSE_GET_SIGNATURE_SELECTED_PARAMETER_INDEX =
'selectedParameterIndex';
const String ANALYTICS_REQUEST_ENABLE = 'analytics.enable';
const String ANALYTICS_REQUEST_ENABLE_VALUE = 'value';
const String ANALYTICS_REQUEST_IS_ENABLED = 'analytics.isEnabled';
Expand Down
39 changes: 3 additions & 36 deletions pkg/analysis_server/lib/protocol/protocol_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2019,7 +2019,6 @@ class AnalysisGetSignatureParams implements RequestParams {
* "name": String
* "dartdoc": optional String
* "parameters": List<ParameterInfo>
* "selectedParameterIndex": int
* }
*
* Clients may not extend, implement or mix-in this class.
Expand All @@ -2031,8 +2030,6 @@ class AnalysisGetSignatureResult implements ResponseResult {

List<ParameterInfo> _parameters;

int _selectedParameterIndex;

/**
* The name of the function being invoked at the given offset.
*/
Expand Down Expand Up @@ -2081,28 +2078,11 @@ class AnalysisGetSignatureResult implements ResponseResult {
this._parameters = value;
}

/**
* The index of the paramter in the parameters collection at the specified
* offset.
*/
int get selectedParameterIndex => _selectedParameterIndex;

/**
* The index of the paramter in the parameters collection at the specified
* offset.
*/
void set selectedParameterIndex(int value) {
assert(value != null);
this._selectedParameterIndex = value;
}

AnalysisGetSignatureResult(
String name, List<ParameterInfo> parameters, int selectedParameterIndex,
AnalysisGetSignatureResult(String name, List<ParameterInfo> parameters,
{String dartdoc}) {
this.name = name;
this.dartdoc = dartdoc;
this.parameters = parameters;
this.selectedParameterIndex = selectedParameterIndex;
}

factory AnalysisGetSignatureResult.fromJson(
Expand Down Expand Up @@ -2132,17 +2112,7 @@ class AnalysisGetSignatureResult implements ResponseResult {
} else {
throw jsonDecoder.mismatch(jsonPath, "parameters");
}
int selectedParameterIndex;
if (json.containsKey("selectedParameterIndex")) {
selectedParameterIndex = jsonDecoder.decodeInt(
jsonPath + ".selectedParameterIndex",
json["selectedParameterIndex"]);
} else {
throw jsonDecoder.mismatch(jsonPath, "selectedParameterIndex");
}
return new AnalysisGetSignatureResult(
name, parameters, selectedParameterIndex,
dartdoc: dartdoc);
return new AnalysisGetSignatureResult(name, parameters, dartdoc: dartdoc);
} else {
throw jsonDecoder.mismatch(
jsonPath, "analysis.getSignature result", json);
Expand All @@ -2165,7 +2135,6 @@ class AnalysisGetSignatureResult implements ResponseResult {
}
result["parameters"] =
parameters.map((ParameterInfo value) => value.toJson()).toList();
result["selectedParameterIndex"] = selectedParameterIndex;
return result;
}

Expand All @@ -2183,8 +2152,7 @@ class AnalysisGetSignatureResult implements ResponseResult {
return name == other.name &&
dartdoc == other.dartdoc &&
listEqual(parameters, other.parameters,
(ParameterInfo a, ParameterInfo b) => a == b) &&
selectedParameterIndex == other.selectedParameterIndex;
(ParameterInfo a, ParameterInfo b) => a == b);
}
return false;
}
Expand All @@ -2195,7 +2163,6 @@ class AnalysisGetSignatureResult implements ResponseResult {
hash = JenkinsSmiHash.combine(hash, name.hashCode);
hash = JenkinsSmiHash.combine(hash, dartdoc.hashCode);
hash = JenkinsSmiHash.combine(hash, parameters.hashCode);
hash = JenkinsSmiHash.combine(hash, selectedParameterIndex.hashCode);
return JenkinsSmiHash.finish(hash);
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/analysis_server/lib/src/computer/computer_signature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class DartUnitSignatureComputer {
final parameters =
execElement.parameters.map((p) => _convertParam(p)).toList();

return new AnalysisGetSignatureResult(name, parameters, 0,
return new AnalysisGetSignatureResult(name, parameters,
dartdoc: DartUnitHoverComputer.computeDocumentation(execElement));
}

Expand All @@ -71,6 +71,7 @@ class DartUnitSignatureComputer {
? ParameterKind.OPTIONAL
: param.isPositional ? ParameterKind.REQUIRED : ParameterKind.NAMED,
param.displayName,
param.type.displayName);
param.type.displayName,
defaultValue: param.defaultValueCode);
}
}
60 changes: 60 additions & 0 deletions pkg/analysis_server/test/analysis/get_signature_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,46 @@ main() {
equals(new ParameterInfo(ParameterKind.NAMED, "length", "int")));
}

test_function_named_with_default_int() async {
addTestFile('''
/// one doc
one(String name, {int length = 1}) {}
main() {
one("Danny", /*^*/);
}
''');
var result = await prepareSignature('/*^*/');
expect(result.name, equals("one"));
expect(result.dartdoc, equals("one doc"));
expect(result.parameters, hasLength(2));
expect(result.parameters[0],
equals(new ParameterInfo(ParameterKind.REQUIRED, "name", "String")));
expect(
result.parameters[1],
equals(new ParameterInfo(ParameterKind.NAMED, "length", "int",
defaultValue: "1")));
}

test_function_named_with_default_string() async {
addTestFile('''
/// one doc
one(String name, {String email = "a@b.c"}) {}
main() {
one("Danny", /*^*/);
}
''');
var result = await prepareSignature('/*^*/');
expect(result.name, equals("one"));
expect(result.dartdoc, equals("one doc"));
expect(result.parameters, hasLength(2));
expect(result.parameters[0],
equals(new ParameterInfo(ParameterKind.REQUIRED, "name", "String")));
expect(
result.parameters[1],
equals(new ParameterInfo(ParameterKind.NAMED, "email", "String",
defaultValue: '"a@b.c"')));
}

test_function_nested_call_inner() async {
// eg. foo(bar(1, 2));
addTestFile('''
Expand Down Expand Up @@ -306,6 +346,26 @@ main() {
equals(new ParameterInfo(ParameterKind.OPTIONAL, "length", "int")));
}

test_function_optional_with_default() async {
addTestFile('''
/// one doc
one(String name, [int length = 11]) {}
main() {
one("Danny", /*^*/);
}
''');
var result = await prepareSignature('/*^*/');
expect(result.name, equals("one"));
expect(result.dartdoc, equals("one doc"));
expect(result.parameters, hasLength(2));
expect(result.parameters[0],
equals(new ParameterInfo(ParameterKind.REQUIRED, "name", "String")));
expect(
result.parameters[1],
equals(new ParameterInfo(ParameterKind.OPTIONAL, "length", "int",
defaultValue: "11")));
}

test_function_required() async {
addTestFile('''
/// one doc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,6 @@ abstract class IntegrationTestMixin {
*
* A list of information about each of the parameters of the function being
* invoked.
*
* selectedParameterIndex: int
*
* The index of the paramter in the parameters collection at the specified
* offset.
*/
Future<AnalysisGetSignatureResult> sendAnalysisGetSignature(
String file, int offset) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1064,11 +1064,13 @@ final Matcher isOverride =
* "kind": ParameterKind
* "name": String
* "type": String
* "defaultValue": optional String
* }
*/
final Matcher isParameterInfo = new LazyMatcher(() => new MatchesJsonObject(
"ParameterInfo",
{"kind": isParameterKind, "name": isString, "type": isString}));
{"kind": isParameterKind, "name": isString, "type": isString},
optionalFields: {"defaultValue": isString}));

/**
* ParameterKind
Expand Down Expand Up @@ -1723,17 +1725,12 @@ final Matcher isAnalysisGetSignatureParams = new LazyMatcher(() =>
* "name": String
* "dartdoc": optional String
* "parameters": List<ParameterInfo>
* "selectedParameterIndex": int
* }
*/
final Matcher isAnalysisGetSignatureResult = new LazyMatcher(
() => new MatchesJsonObject("analysis.getSignature result", {
"name": isString,
"parameters": isListOf(isParameterInfo),
"selectedParameterIndex": isInt
}, optionalFields: {
"dartdoc": isString
}));
final Matcher isAnalysisGetSignatureResult = new LazyMatcher(() =>
new MatchesJsonObject("analysis.getSignature result",
{"name": isString, "parameters": isListOf(isParameterInfo)},
optionalFields: {"dartdoc": isString}));

/**
* analysis.highlights params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,20 @@ public class ParameterInfo {
*/
private final String type;

/**
* The default value for this parameter. This value will be omitted if the parameter does not have
* a default value.
*/
private final String defaultValue;

/**
* Constructor for {@link ParameterInfo}.
*/
public ParameterInfo(String kind, String name, String type) {
public ParameterInfo(String kind, String name, String type, String defaultValue) {
this.kind = kind;
this.name = name;
this.type = type;
this.defaultValue = defaultValue;
}

@Override
Expand All @@ -66,7 +73,8 @@ public boolean equals(Object obj) {
return
ObjectUtilities.equals(other.kind, kind) &&
ObjectUtilities.equals(other.name, name) &&
ObjectUtilities.equals(other.type, type);
ObjectUtilities.equals(other.type, type) &&
ObjectUtilities.equals(other.defaultValue, defaultValue);
}
return false;
}
Expand All @@ -75,7 +83,8 @@ public static ParameterInfo fromJson(JsonObject jsonObject) {
String kind = jsonObject.get("kind").getAsString();
String name = jsonObject.get("name").getAsString();
String type = jsonObject.get("type").getAsString();
return new ParameterInfo(kind, name, type);
String defaultValue = jsonObject.get("defaultValue") == null ? null : jsonObject.get("defaultValue").getAsString();
return new ParameterInfo(kind, name, type, defaultValue);
}

public static List<ParameterInfo> fromJsonArray(JsonArray jsonArray) {
Expand All @@ -90,6 +99,14 @@ public static List<ParameterInfo> fromJsonArray(JsonArray jsonArray) {
return list;
}

/**
* The default value for this parameter. This value will be omitted if the parameter does not have
* a default value.
*/
public String getDefaultValue() {
return defaultValue;
}

/**
* The kind of the parameter.
*/
Expand Down Expand Up @@ -117,6 +134,7 @@ public int hashCode() {
builder.append(kind);
builder.append(name);
builder.append(type);
builder.append(defaultValue);
return builder.toHashCode();
}

Expand All @@ -125,6 +143,9 @@ public JsonObject toJson() {
jsonObject.addProperty("kind", kind);
jsonObject.addProperty("name", name);
jsonObject.addProperty("type", type);
if (defaultValue != null) {
jsonObject.addProperty("defaultValue", defaultValue);
}
return jsonObject;
}

Expand All @@ -137,7 +158,9 @@ public String toString() {
builder.append("name=");
builder.append(name + ", ");
builder.append("type=");
builder.append(type);
builder.append(type + ", ");
builder.append("defaultValue=");
builder.append(defaultValue);
builder.append("]");
return builder.toString();
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/analysis_server/tool/spec/spec_input.html
Original file line number Diff line number Diff line change
Expand Up @@ -709,10 +709,6 @@ <h4>Options</h4>
</list>
<p>A list of information about each of the parameters of the function being invoked.</p>
</field>
<field name="selectedParameterIndex">
<ref>int</ref>
<p>The index of the paramter in the parameters collection at the specified offset.</p>
</field>
</result>
</request>
<request method="reanalyze">
Expand Down

0 comments on commit d792809

Please sign in to comment.