Skip to content

Commit

Permalink
Add constructor impl/tests + additional function tests for getSignature
Browse files Browse the repository at this point in the history
Bug: #27034
Change-Id: Ie47f76d4197dbcdec0fcfdada7914fdf986731ec
Reviewed-on: https://dart-review.googlesource.com/66540
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Danny Tuppeny <dantup@google.com>
  • Loading branch information
DanTup authored and commit-bot@chromium.org committed Jul 25, 2018
1 parent ab0d27b commit e5a5815
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 134 deletions.
7 changes: 7 additions & 0 deletions pkg/analysis_server/doc/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -4250,6 +4250,13 @@ <h2 class="domain"><a name="types">Types</a></h2>
which does not match a file currently subject to
analysis.
</p>
</dd><dt class="value">GET_SIGNATURE_INVALID_OFFSET</dt><dd>

<p>
An "analysis.getSignature" request specified an offset
which is not a valid location within for the contents of
the file specified FilePath.
</p>
</dd><dt class="value">GET_SIGNATURE_UNKNOWN_FUNCTION</dt><dd>

<p>
Expand Down
10 changes: 10 additions & 0 deletions pkg/analysis_server/lib/protocol/protocol.dart
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,16 @@ class Response {
error: new RequestError(RequestErrorCode.GET_SIGNATURE_INVALID_FILE,
'Error during `analysis.getSignature`: invalid file.'));

/**
* Initialize a newly created instance to represent the
* GET_SIGNATURE_INVALID_OFFSET error condition.
*/
Response.getSignatureInvalidOffset(Request request)
: this(request.id,
error: new RequestError(
RequestErrorCode.GET_SIGNATURE_INVALID_OFFSET,
'Error during `analysis.getSignature`: invalid offset.'));

/**
* Initialize a newly created instance to represent the
* GET_SIGNATURE_UNKNOWN_FUNCTION error condition.
Expand Down
28 changes: 20 additions & 8 deletions pkg/analysis_server/lib/protocol/protocol_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2017,7 +2017,7 @@ class AnalysisGetSignatureParams implements RequestParams {
*
* {
* "name": String
* "dartdoc": String
* "dartdoc": optional String
* "parameters": List<ParameterInfo>
* "selectedParameterIndex": int
* }
Expand Down Expand Up @@ -2063,7 +2063,6 @@ class AnalysisGetSignatureResult implements ResponseResult {
* dartdoc.
*/
void set dartdoc(String value) {
assert(value != null);
this._dartdoc = value;
}

Expand Down Expand Up @@ -2097,8 +2096,9 @@ class AnalysisGetSignatureResult implements ResponseResult {
this._selectedParameterIndex = value;
}

AnalysisGetSignatureResult(String name, String dartdoc,
List<ParameterInfo> parameters, int selectedParameterIndex) {
AnalysisGetSignatureResult(
String name, List<ParameterInfo> parameters, int selectedParameterIndex,
{String dartdoc}) {
this.name = name;
this.dartdoc = dartdoc;
this.parameters = parameters;
Expand All @@ -2121,8 +2121,6 @@ class AnalysisGetSignatureResult implements ResponseResult {
if (json.containsKey("dartdoc")) {
dartdoc =
jsonDecoder.decodeString(jsonPath + ".dartdoc", json["dartdoc"]);
} else {
throw jsonDecoder.mismatch(jsonPath, "dartdoc");
}
List<ParameterInfo> parameters;
if (json.containsKey("parameters")) {
Expand All @@ -2143,7 +2141,8 @@ class AnalysisGetSignatureResult implements ResponseResult {
throw jsonDecoder.mismatch(jsonPath, "selectedParameterIndex");
}
return new AnalysisGetSignatureResult(
name, dartdoc, parameters, selectedParameterIndex);
name, parameters, selectedParameterIndex,
dartdoc: dartdoc);
} else {
throw jsonDecoder.mismatch(
jsonPath, "analysis.getSignature result", json);
Expand All @@ -2161,7 +2160,9 @@ class AnalysisGetSignatureResult implements ResponseResult {
Map<String, dynamic> toJson() {
Map<String, dynamic> result = {};
result["name"] = name;
result["dartdoc"] = dartdoc;
if (dartdoc != null) {
result["dartdoc"] = dartdoc;
}
result["parameters"] =
parameters.map((ParameterInfo value) => value.toJson()).toList();
result["selectedParameterIndex"] = selectedParameterIndex;
Expand Down Expand Up @@ -15606,6 +15607,7 @@ class RequestError implements HasToJson {
* GET_NAVIGATION_INVALID_FILE
* GET_REACHABLE_SOURCES_INVALID_FILE
* GET_SIGNATURE_INVALID_FILE
* GET_SIGNATURE_INVALID_OFFSET
* GET_SIGNATURE_UNKNOWN_FUNCTION
* IMPORT_ELEMENTS_INVALID_FILE
* INVALID_ANALYSIS_ROOT
Expand Down Expand Up @@ -15705,6 +15707,13 @@ class RequestErrorCode implements Enum {
static const RequestErrorCode GET_SIGNATURE_INVALID_FILE =
const RequestErrorCode._("GET_SIGNATURE_INVALID_FILE");

/**
* An "analysis.getSignature" request specified an offset which is not a
* valid location within for the contents of the file specified FilePath.
*/
static const RequestErrorCode GET_SIGNATURE_INVALID_OFFSET =
const RequestErrorCode._("GET_SIGNATURE_INVALID_OFFSET");

/**
* An "analysis.getSignature" request specified an offset that could not be
* matched to a function call.
Expand Down Expand Up @@ -15853,6 +15862,7 @@ class RequestErrorCode implements Enum {
GET_NAVIGATION_INVALID_FILE,
GET_REACHABLE_SOURCES_INVALID_FILE,
GET_SIGNATURE_INVALID_FILE,
GET_SIGNATURE_INVALID_OFFSET,
GET_SIGNATURE_UNKNOWN_FUNCTION,
IMPORT_ELEMENTS_INVALID_FILE,
INVALID_ANALYSIS_ROOT,
Expand Down Expand Up @@ -15902,6 +15912,8 @@ class RequestErrorCode implements Enum {
return GET_REACHABLE_SOURCES_INVALID_FILE;
case "GET_SIGNATURE_INVALID_FILE":
return GET_SIGNATURE_INVALID_FILE;
case "GET_SIGNATURE_INVALID_OFFSET":
return GET_SIGNATURE_INVALID_OFFSET;
case "GET_SIGNATURE_UNKNOWN_FUNCTION":
return GET_SIGNATURE_UNKNOWN_FUNCTION;
case "IMPORT_ELEMENTS_INVALID_FILE":
Expand Down
30 changes: 19 additions & 11 deletions pkg/analysis_server/lib/src/computer/computer_signature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,44 @@ import 'package:analyzer/src/dart/ast/utilities.dart';
* A computer for the signature at the specified offset of a Dart [CompilationUnit].
*/
class DartUnitSignatureComputer {
final CompilationUnit _unit;
final int _offset;
final AstNode _node;
DartUnitSignatureComputer(CompilationUnit _unit, int _offset)
: _node = new NodeLocator(_offset).searchWithin(_unit);

DartUnitSignatureComputer(this._unit, this._offset);
bool get offsetIsValid => _node != null;

/**
* Returns the computed signature information, maybe `null`.
*/
AnalysisGetSignatureResult compute() {
AstNode node = new NodeLocator(_offset).searchWithin(_unit);
if (node == null) {
if (_node == null) {
return null;
}

// Find the closest argument list.
while (node != null && !(node is ArgumentList)) {
node = node.parent;
var argsNode = _node;
while (argsNode != null && !(argsNode is ArgumentList)) {
argsNode = argsNode.parent;
}

if (node == null) {
if (argsNode == null) {
return null;
}

final args = node;
final args = argsNode;
String name;
ExecutableElement execElement;
if (args.parent is MethodInvocation) {
MethodInvocation method = args.parent;
name = method.methodName.name;
execElement = ElementLocator.locate(method) as ExecutableElement;
} else if (args.parent is InstanceCreationExpression) {
InstanceCreationExpression constructor = args.parent;
name = constructor.constructorName.type.name.name;
if (constructor.constructorName.name != null) {
name += ".${constructor.constructorName.name.name}";
}
execElement = ElementLocator.locate(constructor) as ExecutableElement;
}

if (execElement == null) {
Expand All @@ -53,8 +61,8 @@ class DartUnitSignatureComputer {
final parameters =
execElement.parameters.map((p) => _convertParam(p)).toList();

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

ParameterInfo _convertParam(ParameterElement param) {
Expand Down
19 changes: 12 additions & 7 deletions pkg/analysis_server/lib/src/domain_analysis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,21 @@ class AnalysisDomainHandler extends AbstractRequestHandler {
return;
}

// Prepare the signature.
var signature =
new DartUnitSignatureComputer(unit, params.offset).compute();
// Ensure the offset provided is a valid location in the file.
final computer = new DartUnitSignatureComputer(unit, params.offset);
if (!computer.offsetIsValid) {
server.sendResponse(Response.getSignatureInvalidOffset(request));
return;
}

// Send the response.
if (signature != null) {
server.sendResponse(signature.toResponse(request.id));
} else {
// Try to get a signature.
final signature = computer.compute();
if (signature == null) {
server.sendResponse(Response.getSignatureUnknownFunction(request));
return;
}

server.sendResponse(signature.toResponse(request.id));
}

@override
Expand Down

0 comments on commit e5a5815

Please sign in to comment.