Generate analysis_server_client protocol_generated.dart and related protocol files

Change-Id: I9a9af0ee37eab87a5c3e83cba638d855a2e1121b
Reviewed-on: https://dart-review.googlesource.com/c/81341
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Dan Rubel <danrubel@google.com>
This commit is contained in:
Dan Rubel 2018-10-24 17:09:39 +00:00 committed by commit-bot@chromium.org
parent a902fc351f
commit 3dc911909b
8 changed files with 26902 additions and 13 deletions

View file

@ -27,11 +27,27 @@ const Map<String, String> specialElementFlags = const {
'deprecated': '0x20'
};
GeneratedFile target(bool responseRequiresRequestTime) {
GeneratedFile clientTarget(bool responseRequiresRequestTime) {
return new GeneratedFile(
'../analysis_server_client/lib/src/protocol/protocol_generated.dart',
(String pkgPath) async {
CodegenProtocolVisitor visitor = new CodegenProtocolVisitor(
'analysis_server_client',
responseRequiresRequestTime,
false,
readApi(pkgPath));
return visitor.collectCode(visitor.visitApi);
});
}
GeneratedFile serverTarget(bool responseRequiresRequestTime) {
return new GeneratedFile('lib/protocol/protocol_generated.dart',
(String pkgPath) async {
CodegenProtocolVisitor visitor = new CodegenProtocolVisitor(
path.basename(pkgPath), responseRequiresRequestTime, readApi(pkgPath));
path.basename(pkgPath),
responseRequiresRequestTime,
true,
readApi(pkgPath));
return visitor.collectCode(visitor.visitApi);
});
}
@ -79,6 +95,12 @@ class CodegenProtocolVisitor extends DartCodegenVisitor with CodeGenerator {
*/
final bool responseRequiresRequestTime;
/**
* A flag indicating whether this generated code is for the server
* (analysis_server) or for the client (analysis_server_client).
*/
final bool isServer;
/**
* Visitor used to produce doc comments.
*/
@ -91,8 +113,8 @@ class CodegenProtocolVisitor extends DartCodegenVisitor with CodeGenerator {
*/
final Map<String, ImpliedType> impliedTypes;
CodegenProtocolVisitor(
this.packageName, this.responseRequiresRequestTime, Api api)
CodegenProtocolVisitor(this.packageName, this.responseRequiresRequestTime,
this.isServer, Api api)
: toHtmlVisitor = new ToHtmlVisitor(api),
impliedTypes = computeImpliedTypes(api),
super(api) {
@ -403,14 +425,24 @@ class CodegenProtocolVisitor extends DartCodegenVisitor with CodeGenerator {
void emitImports() {
writeln("import 'dart:convert' hide JsonDecoder;");
writeln();
writeln("import 'package:analyzer/src/generated/utilities_general.dart';");
writeln("import 'package:$packageName/protocol/protocol.dart';");
writeln(
"import 'package:$packageName/src/protocol/protocol_internal.dart';");
for (String uri in api.types.importUris) {
write("import '");
write(uri);
writeln("';");
if (isServer) {
writeln(
"import 'package:analyzer/src/generated/utilities_general.dart';");
writeln("import 'package:$packageName/protocol/protocol.dart';");
writeln(
"import 'package:$packageName/src/protocol/protocol_internal.dart';");
for (String uri in api.types.importUris) {
write("import '");
write(uri);
writeln("';");
}
} else {
writeln("import 'package:$packageName/src/protocol/protocol_base.dart';");
writeln(
"import 'package:$packageName/src/protocol/protocol_common.dart';");
writeln(
"import 'package:$packageName/src/protocol/protocol_internal.dart';");
writeln("import 'package:$packageName/src/protocol/protocol_util.dart';");
}
}

View file

@ -30,7 +30,8 @@ main() async {
List<GeneratedContent> get allTargets {
List<GeneratedContent> targets = <GeneratedContent>[];
targets.add(codegen_analysis_server.target);
targets.add(codegen_dart_protocol.target(false));
targets.add(codegen_dart_protocol.clientTarget(false));
targets.add(codegen_dart_protocol.serverTarget(false));
targets.add(codegen_java_types.targetDir);
targets.add(codegen_inttest_methods.target);
targets.add(codegen_matchers.target);

View file

@ -2,4 +2,7 @@
// 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.
export 'package:analysis_server_client/src/protocol/protocol_base.dart';
export 'package:analysis_server_client/src/protocol/protocol_common.dart';
export 'package:analysis_server_client/src/protocol/protocol_constants.dart';
export 'package:analysis_server_client/src/protocol/protocol_generated.dart';

View file

@ -0,0 +1,674 @@
// 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.
/**
* Support for client code that needs to interact with the requests, responses
* and notifications that are part of the analysis server's wire protocol.
*/
import 'dart:convert' hide JsonDecoder;
import 'package:analysis_server_client/src/protocol/protocol_generated.dart';
import 'package:analysis_server_client/src/protocol/protocol_internal.dart';
/**
* An interface for enumerated types in the protocol.
*
* Clients may not extend, implement or mix-in this class.
*/
abstract class Enum {
/**
* The name of the enumerated value. This should match the name of the static
* getter which provides access to this enumerated value.
*/
String get name;
}
/**
* A notification that can be sent from the server about an event that occurred.
*
* Clients may not extend, implement or mix-in this class.
*/
class Notification {
/**
* The name of the JSON attribute containing the name of the event that
* triggered the notification.
*/
static const String EVENT = 'event';
/**
* The name of the JSON attribute containing the result values.
*/
static const String PARAMS = 'params';
/**
* The name of the event that triggered the notification.
*/
final String event;
/**
* A table mapping the names of notification parameters to their values, or
* `null` if there are no notification parameters.
*/
final Map<String, Object> params;
/**
* Initialize a newly created [Notification] to have the given [event] name.
* If [params] is provided, it will be used as the params; otherwise no
* params will be used.
*/
Notification(this.event, [this.params]);
/**
* Initialize a newly created instance based on the given JSON data.
*/
factory Notification.fromJson(Map json) {
return new Notification(json[Notification.EVENT],
json[Notification.PARAMS] as Map<String, Object>);
}
/**
* Return a table representing the structure of the Json object that will be
* sent to the client to represent this response.
*/
Map<String, Object> toJson() {
Map<String, Object> jsonObject = {};
jsonObject[EVENT] = event;
if (params != null) {
jsonObject[PARAMS] = params;
}
return jsonObject;
}
}
/**
* A request that was received from the client.
*
* Clients may not extend, implement or mix-in this class.
*/
class Request {
/**
* The name of the JSON attribute containing the id of the request.
*/
static const String ID = 'id';
/**
* The name of the JSON attribute containing the name of the request.
*/
static const String METHOD = 'method';
/**
* The name of the JSON attribute containing the request parameters.
*/
static const String PARAMS = 'params';
/**
* The name of the optional JSON attribute indicating the time (milliseconds
* since epoch) at which the client made the request.
*/
static const String CLIENT_REQUEST_TIME = 'clientRequestTime';
/**
* The unique identifier used to identify this request.
*/
final String id;
/**
* The method being requested.
*/
final String method;
/**
* A table mapping the names of request parameters to their values.
*/
final Map<String, Object> params;
/**
* The time (milliseconds since epoch) at which the client made the request
* or `null` if this information is not provided by the client.
*/
final int clientRequestTime;
/**
* Initialize a newly created [Request] to have the given [id] and [method]
* name. If [params] is supplied, it is used as the "params" map for the
* request. Otherwise an empty "params" map is allocated.
*/
Request(this.id, this.method,
[Map<String, Object> params, this.clientRequestTime])
: params = params ?? <String, Object>{};
/**
* Return a request parsed from the given json, or `null` if the [data] is
* not a valid json representation of a request. The [data] is expected to
* have the following format:
*
* {
* 'clientRequestTime': millisecondsSinceEpoch
* 'id': String,
* 'method': methodName,
* 'params': {
* paramter_name: value
* }
* }
*
* where both the parameters and clientRequestTime are optional.
*
* The parameters can contain any number of name/value pairs. The
* clientRequestTime must be an int representing the time at which the client
* issued the request (milliseconds since epoch).
*/
factory Request.fromJson(Map<String, Object> result) {
var id = result[Request.ID];
var method = result[Request.METHOD];
if (id is! String || method is! String) {
return null;
}
var time = result[Request.CLIENT_REQUEST_TIME];
if (time != null && time is! int) {
return null;
}
var params = result[Request.PARAMS];
if (params is Map || params == null) {
return new Request(id, method, params as Map<String, Object>, time);
} else {
return null;
}
}
/**
* Return a request parsed from the given [data], or `null` if the [data] is
* not a valid json representation of a request. The [data] is expected to
* have the following format:
*
* {
* 'clientRequestTime': millisecondsSinceEpoch
* 'id': String,
* 'method': methodName,
* 'params': {
* paramter_name: value
* }
* }
*
* where both the parameters and clientRequestTime are optional.
*
* The parameters can contain any number of name/value pairs. The
* clientRequestTime must be an int representing the time at which the client
* issued the request (milliseconds since epoch).
*/
factory Request.fromString(String data) {
try {
var result = json.decode(data);
if (result is Map) {
return new Request.fromJson(result as Map<String, dynamic>);
}
return null;
} catch (exception) {
return null;
}
}
@override
int get hashCode {
return id.hashCode;
}
@override
bool operator ==(Object other) {
return other is Request &&
id == other.id &&
method == other.method &&
clientRequestTime == other.clientRequestTime &&
_equalMaps(params, other.params);
}
/**
* Return a table representing the structure of the Json object that will be
* sent to the client to represent this response.
*/
Map<String, Object> toJson() {
Map<String, Object> jsonObject = <String, Object>{};
jsonObject[ID] = id;
jsonObject[METHOD] = method;
if (params.isNotEmpty) {
jsonObject[PARAMS] = params;
}
if (clientRequestTime != null) {
jsonObject[CLIENT_REQUEST_TIME] = clientRequestTime;
}
return jsonObject;
}
bool _equalLists(List first, List second) {
if (first == null) {
return second == null;
}
if (second == null) {
return false;
}
int length = first.length;
if (length != second.length) {
return false;
}
for (int i = 0; i < length; i++) {
if (!_equalObjects(first[i], second[i])) {
return false;
}
}
return true;
}
bool _equalMaps(Map first, Map second) {
if (first == null) {
return second == null;
}
if (second == null) {
return false;
}
if (first.length != second.length) {
return false;
}
for (var key in first.keys) {
if (!second.containsKey(key)) {
return false;
}
if (!_equalObjects(first[key], second[key])) {
return false;
}
}
return true;
}
bool _equalObjects(Object first, Object second) {
if (first == null) {
return second == null;
}
if (second == null) {
return false;
}
if (first is Map) {
if (second is Map) {
return _equalMaps(first, second);
}
return false;
}
if (first is List) {
if (second is List) {
return _equalLists(first, second);
}
return false;
}
return first == second;
}
}
/**
* An exception that occurred during the handling of a request that requires
* that an error be returned to the client.
*
* Clients may not extend, implement or mix-in this class.
*/
class RequestFailure implements Exception {
/**
* The response to be returned as a result of the failure.
*/
final Response response;
/**
* Initialize a newly created exception to return the given reponse.
*/
RequestFailure(this.response);
}
/**
* An object that can handle requests and produce responses for them.
*
* Clients may not extend, implement or mix-in this class.
*/
abstract class RequestHandler {
/**
* Attempt to handle the given [request]. If the request is not recognized by
* this handler, return `null` so that other handlers will be given a chance
* to handle it. Otherwise, return the response that should be passed back to
* the client.
*/
Response handleRequest(Request request);
}
/**
* A response to a request.
*
* Clients may not extend, implement or mix-in this class.
*/
class Response {
/**
* The [Response] instance that is returned when a real [Response] cannot
* be provided at the moment.
*/
static final Response DELAYED_RESPONSE = new Response('DELAYED_RESPONSE');
/**
* The name of the JSON attribute containing the id of the request for which
* this is a response.
*/
static const String ID = 'id';
/**
* The name of the JSON attribute containing the error message.
*/
static const String ERROR = 'error';
/**
* The name of the JSON attribute containing the result values.
*/
static const String RESULT = 'result';
/**
* The unique identifier used to identify the request that this response is
* associated with.
*/
final String id;
/**
* The error that was caused by attempting to handle the request, or `null` if
* there was no error.
*/
final RequestError error;
/**
* A table mapping the names of result fields to their values. Should be
* `null` if there is no result to send.
*/
Map<String, Object> result;
/**
* Initialize a newly created instance to represent a response to a request
* with the given [id]. If [_result] is provided, it will be used as the
* result; otherwise an empty result will be used. If an [error] is provided
* then the response will represent an error condition.
*/
Response(this.id, {Map<String, Object> result, this.error}) : result = result;
/**
* Create and return the `DEBUG_PORT_COULD_NOT_BE_OPENED` error response.
*/
Response.debugPortCouldNotBeOpened(Request request, dynamic error)
: this(request.id,
error: new RequestError(
RequestErrorCode.DEBUG_PORT_COULD_NOT_BE_OPENED, '$error'));
/**
* Initialize a newly created instance to represent the FILE_NOT_ANALYZED
* error condition.
*/
Response.fileNotAnalyzed(Request request, String file)
: this(request.id,
error: new RequestError(RequestErrorCode.FILE_NOT_ANALYZED,
'File is not analyzed: $file.'));
/**
* Initialize a newly created instance to represent the FORMAT_INVALID_FILE
* error condition.
*/
Response.formatInvalidFile(Request request)
: this(request.id,
error: new RequestError(RequestErrorCode.FORMAT_INVALID_FILE,
'Error during `${request.method}`: invalid file.'));
/**
* Initialize a newly created instance to represent the FORMAT_WITH_ERROR
* error condition.
*/
Response.formatWithErrors(Request request)
: this(request.id,
error: new RequestError(RequestErrorCode.FORMAT_WITH_ERRORS,
'Error during `edit.format`: source contains syntax errors.'));
/**
* Initialize a newly created instance based on the given JSON data.
*/
factory Response.fromJson(Map json) {
try {
Object id = json[Response.ID];
if (id is! String) {
return null;
}
Object error = json[Response.ERROR];
RequestError decodedError;
if (error is Map) {
decodedError = new RequestError.fromJson(
new ResponseDecoder(null), '.error', error);
}
Object result = json[Response.RESULT];
Map<String, Object> decodedResult;
if (result is Map) {
decodedResult = result as Map<String, Object>;
}
return new Response(id, error: decodedError, result: decodedResult);
} catch (exception) {
return null;
}
}
/**
* Initialize a newly created instance to represent the
* GET_ERRORS_INVALID_FILE error condition.
*/
Response.getErrorsInvalidFile(Request request)
: this(request.id,
error: new RequestError(RequestErrorCode.GET_ERRORS_INVALID_FILE,
'Error during `analysis.getErrors`: invalid file.'));
/**
* Initialize a newly created instance to represent the
* GET_IMPORTED_ELEMENTS_INVALID_FILE error condition.
*/
Response.getImportedElementsInvalidFile(Request request)
: this(request.id,
error: new RequestError(
RequestErrorCode.GET_IMPORTED_ELEMENTS_INVALID_FILE,
'Error during `analysis.getImportedElements`: invalid file.'));
/**
* Initialize a newly created instance to represent the
* GET_KYTHE_ENTRIES_INVALID_FILE error condition.
*/
Response.getKytheEntriesInvalidFile(Request request)
: this(request.id,
error: new RequestError(
RequestErrorCode.GET_KYTHE_ENTRIES_INVALID_FILE,
'Error during `analysis.getKytheEntries`: invalid file.'));
/**
* Initialize a newly created instance to represent the
* GET_NAVIGATION_INVALID_FILE error condition.
*/
Response.getNavigationInvalidFile(Request request)
: this(request.id,
error: new RequestError(
RequestErrorCode.GET_NAVIGATION_INVALID_FILE,
'Error during `analysis.getNavigation`: invalid file.'));
/**
* Initialize a newly created instance to represent the
* GET_REACHABLE_SOURCES_INVALID_FILE error condition.
*/
Response.getReachableSourcesInvalidFile(Request request)
: this(request.id,
error: new RequestError(
RequestErrorCode.GET_REACHABLE_SOURCES_INVALID_FILE,
'Error during `analysis.getReachableSources`: invalid file.'));
/**
* Initialize a newly created instance to represent the
* GET_SIGNATURE_INVALID_FILE error condition.
*/
Response.getSignatureInvalidFile(Request request)
: this(request.id,
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.
*/
Response.getSignatureUnknownFunction(Request request)
: this(request.id,
error: new RequestError(
RequestErrorCode.GET_SIGNATURE_UNKNOWN_FUNCTION,
'Error during `analysis.getSignature`: unknown function.'));
/**
* Initialize a newly created instance to represent the
* IMPORT_ELEMENTS_INVALID_FILE error condition.
*/
Response.importElementsInvalidFile(Request request)
: this(request.id,
error: new RequestError(
RequestErrorCode.IMPORT_ELEMENTS_INVALID_FILE,
'Error during `edit.importElements`: invalid file.'));
/**
* Initialize a newly created instance to represent an error condition caused
* by an analysis.reanalyze [request] that specifies an analysis root that is
* not in the current list of analysis roots.
*/
Response.invalidAnalysisRoot(Request request, String rootPath)
: this(request.id,
error: new RequestError(RequestErrorCode.INVALID_ANALYSIS_ROOT,
"Invalid analysis root: $rootPath"));
/**
* Initialize a newly created instance to represent an error condition caused
* by a [request] that specifies an execution context whose context root does
* not exist.
*/
Response.invalidExecutionContext(Request request, String contextId)
: this(request.id,
error: new RequestError(RequestErrorCode.INVALID_EXECUTION_CONTEXT,
"Invalid execution context: $contextId"));
/**
* Initialize a newly created instance to represent the
* INVALID_FILE_PATH_FORMAT error condition.
*/
Response.invalidFilePathFormat(Request request, path)
: this(request.id,
error: new RequestError(RequestErrorCode.INVALID_FILE_PATH_FORMAT,
'Invalid file path format: $path'));
/**
* Initialize a newly created instance to represent an error condition caused
* by a [request] that had invalid parameter. [path] is the path to the
* invalid parameter, in Javascript notation (e.g. "foo.bar" means that the
* parameter "foo" contained a key "bar" whose value was the wrong type).
* [expectation] is a description of the type of data that was expected.
*/
Response.invalidParameter(Request request, String path, String expectation)
: this(request.id,
error: new RequestError(RequestErrorCode.INVALID_PARAMETER,
"Invalid parameter '$path'. $expectation."));
/**
* Initialize a newly created instance to represent an error condition caused
* by a malformed request.
*/
Response.invalidRequestFormat()
: this('',
error: new RequestError(
RequestErrorCode.INVALID_REQUEST, 'Invalid request'));
/**
* Initialize a newly created instance to represent the
* ORGANIZE_DIRECTIVES_ERROR error condition.
*/
Response.organizeDirectivesError(Request request, String message)
: this(request.id,
error: new RequestError(
RequestErrorCode.ORGANIZE_DIRECTIVES_ERROR, message));
/**
* Initialize a newly created instance to represent the
* REFACTORING_REQUEST_CANCELLED error condition.
*/
Response.refactoringRequestCancelled(Request request)
: this(request.id,
error: new RequestError(
RequestErrorCode.REFACTORING_REQUEST_CANCELLED,
'The `edit.getRefactoring` request was cancelled.'));
/**
* Initialize a newly created instance to represent the SERVER_ERROR error
* condition.
*/
factory Response.serverError(Request request, exception, stackTrace) {
RequestError error =
new RequestError(RequestErrorCode.SERVER_ERROR, exception.toString());
if (stackTrace != null) {
error.stackTrace = stackTrace.toString();
}
return new Response(request.id, error: error);
}
/**
* Initialize a newly created instance to represent the
* SORT_MEMBERS_INVALID_FILE error condition.
*/
Response.sortMembersInvalidFile(Request request)
: this(request.id,
error: new RequestError(RequestErrorCode.SORT_MEMBERS_INVALID_FILE,
'Error during `edit.sortMembers`: invalid file.'));
/**
* Initialize a newly created instance to represent the
* SORT_MEMBERS_PARSE_ERRORS error condition.
*/
Response.sortMembersParseErrors(Request request, int numErrors)
: this(request.id,
error: new RequestError(RequestErrorCode.SORT_MEMBERS_PARSE_ERRORS,
'Error during `edit.sortMembers`: file has $numErrors scan/parse errors.'));
/**
* Initialize a newly created instance to represent an error condition caused
* by a [request] that cannot be handled by any known handlers.
*/
Response.unknownRequest(Request request)
: this(request.id,
error: new RequestError(
RequestErrorCode.UNKNOWN_REQUEST, 'Unknown request'));
/**
* Initialize a newly created instance to represent an error condition caused
* by a [request] for a service that is not supported.
*/
Response.unsupportedFeature(String requestId, String message)
: this(requestId,
error: new RequestError(
RequestErrorCode.UNSUPPORTED_FEATURE, message));
/**
* Return a table representing the structure of the Json object that will be
* sent to the client to represent this response.
*/
Map<String, Object> toJson() {
Map<String, Object> jsonObject = <String, Object>{};
jsonObject[ID] = id;
if (error != null) {
jsonObject[ERROR] = error.toJson();
}
if (result != null) {
jsonObject[RESULT] = result;
}
return jsonObject;
}
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,479 @@
// 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:collection';
import 'dart:convert' hide JsonDecoder;
import 'package:analysis_server_client/src/protocol/protocol_base.dart';
import 'package:analysis_server_client/src/protocol/protocol_generated.dart';
import 'package:analysis_server_client/src/protocol/protocol_common.dart';
final Map<String, RefactoringKind> REQUEST_ID_REFACTORING_KINDS =
new HashMap<String, RefactoringKind>();
/**
* Adds the given [sourceEdits] to the list in [sourceFileEdit].
*/
void addAllEditsForSource(
SourceFileEdit sourceFileEdit, Iterable<SourceEdit> edits) {
edits.forEach(sourceFileEdit.add);
}
/**
* Adds the given [sourceEdit] to the list in [sourceFileEdit].
*/
void addEditForSource(SourceFileEdit sourceFileEdit, SourceEdit sourceEdit) {
List<SourceEdit> edits = sourceFileEdit.edits;
int index = 0;
while (index < edits.length && edits[index].offset > sourceEdit.offset) {
index++;
}
edits.insert(index, sourceEdit);
}
/**
* Adds [edit] to the [FileEdit] for the given [file].
*/
void addEditToSourceChange(
SourceChange change, String file, int fileStamp, SourceEdit edit) {
SourceFileEdit fileEdit = change.getFileEdit(file);
if (fileEdit == null) {
fileEdit = new SourceFileEdit(file, fileStamp);
change.addFileEdit(fileEdit);
}
fileEdit.add(edit);
}
/**
* Get the result of applying the edit to the given [code]. Access via
* SourceEdit.apply().
*/
String applyEdit(String code, SourceEdit edit) {
if (edit.length < 0) {
throw new RangeError('length is negative');
}
return code.replaceRange(edit.offset, edit.end, edit.replacement);
}
/**
* Get the result of applying a set of [edits] to the given [code]. Edits
* are applied in the order they appear in [edits]. Access via
* SourceEdit.applySequence().
*/
String applySequenceOfEdits(String code, Iterable<SourceEdit> edits) {
edits.forEach((SourceEdit edit) {
code = edit.apply(code);
});
return code;
}
/**
* Returns the [FileEdit] for the given [file], maybe `null`.
*/
SourceFileEdit getChangeFileEdit(SourceChange change, String file) {
for (SourceFileEdit fileEdit in change.edits) {
if (fileEdit.file == file) {
return fileEdit;
}
}
return null;
}
/**
* Compare the lists [listA] and [listB], using [itemEqual] to compare
* list elements.
*/
bool listEqual<T1, T2>(
List<T1> listA, List<T2> listB, bool itemEqual(T1 a, T2 b)) {
if (listA == null) {
return listB == null;
}
if (listB == null) {
return false;
}
if (listA.length != listB.length) {
return false;
}
for (int i = 0; i < listA.length; i++) {
if (!itemEqual(listA[i], listB[i])) {
return false;
}
}
return true;
}
/**
* Compare the maps [mapA] and [mapB], using [valueEqual] to compare map
* values.
*/
bool mapEqual<K, V>(Map<K, V> mapA, Map<K, V> mapB, bool valueEqual(V a, V b)) {
if (mapA == null) {
return mapB == null;
}
if (mapB == null) {
return false;
}
if (mapA.length != mapB.length) {
return false;
}
for (var key in mapA.keys) {
if (!mapB.containsKey(key)) {
return false;
}
if (!valueEqual(mapA[key], mapB[key])) {
return false;
}
}
return true;
}
/**
* Translate the input [map], applying [keyCallback] to all its keys, and
* [valueCallback] to all its values.
*/
Map<KR, VR> mapMap<KP, VP, KR, VR>(Map<KP, VP> map,
{KR keyCallback(KP key), VR valueCallback(VP value)}) {
Map<KR, VR> result = new HashMap<KR, VR>();
map.forEach((key, value) {
KR resultKey;
VR resultValue;
if (keyCallback != null) {
resultKey = keyCallback(key);
} else {
resultKey = key as KR;
}
if (valueCallback != null) {
resultValue = valueCallback(value);
} else {
resultValue = value as VR;
}
result[resultKey] = resultValue;
});
return result;
}
RefactoringProblemSeverity maxRefactoringProblemSeverity(
RefactoringProblemSeverity a, RefactoringProblemSeverity b) {
if (b == null) {
return a;
}
if (a == null) {
return b;
} else if (a == RefactoringProblemSeverity.INFO) {
return b;
} else if (a == RefactoringProblemSeverity.WARNING) {
if (b == RefactoringProblemSeverity.ERROR ||
b == RefactoringProblemSeverity.FATAL) {
return b;
}
} else if (a == RefactoringProblemSeverity.ERROR) {
if (b == RefactoringProblemSeverity.FATAL) {
return b;
}
}
return a;
}
/**
* Create a [RefactoringFeedback] corresponding the given [kind].
*/
RefactoringFeedback refactoringFeedbackFromJson(
JsonDecoder jsonDecoder, String jsonPath, Object json, Map feedbackJson) {
RefactoringKind kind = jsonDecoder.refactoringKind;
if (kind == RefactoringKind.EXTRACT_LOCAL_VARIABLE) {
return new ExtractLocalVariableFeedback.fromJson(
jsonDecoder, jsonPath, json);
}
if (kind == RefactoringKind.EXTRACT_METHOD) {
return new ExtractMethodFeedback.fromJson(jsonDecoder, jsonPath, json);
}
if (kind == RefactoringKind.EXTRACT_WIDGET) {
return new ExtractWidgetFeedback.fromJson(jsonDecoder, jsonPath, json);
}
if (kind == RefactoringKind.INLINE_LOCAL_VARIABLE) {
return new InlineLocalVariableFeedback.fromJson(
jsonDecoder, jsonPath, json);
}
if (kind == RefactoringKind.INLINE_METHOD) {
return new InlineMethodFeedback.fromJson(jsonDecoder, jsonPath, json);
}
if (kind == RefactoringKind.RENAME) {
return new RenameFeedback.fromJson(jsonDecoder, jsonPath, json);
}
return null;
}
/**
* Create a [RefactoringOptions] corresponding the given [kind].
*/
RefactoringOptions refactoringOptionsFromJson(JsonDecoder jsonDecoder,
String jsonPath, Object json, RefactoringKind kind) {
if (kind == RefactoringKind.EXTRACT_LOCAL_VARIABLE) {
return new ExtractLocalVariableOptions.fromJson(
jsonDecoder, jsonPath, json);
}
if (kind == RefactoringKind.EXTRACT_METHOD) {
return new ExtractMethodOptions.fromJson(jsonDecoder, jsonPath, json);
}
if (kind == RefactoringKind.EXTRACT_WIDGET) {
return new ExtractWidgetOptions.fromJson(jsonDecoder, jsonPath, json);
}
if (kind == RefactoringKind.INLINE_METHOD) {
return new InlineMethodOptions.fromJson(jsonDecoder, jsonPath, json);
}
if (kind == RefactoringKind.MOVE_FILE) {
return new MoveFileOptions.fromJson(jsonDecoder, jsonPath, json);
}
if (kind == RefactoringKind.RENAME) {
return new RenameOptions.fromJson(jsonDecoder, jsonPath, json);
}
return null;
}
/**
* Type of callbacks used to decode parts of JSON objects. [jsonPath] is a
* string describing the part of the JSON object being decoded, and [value] is
* the part to decode.
*/
typedef E JsonDecoderCallback<E>(String jsonPath, Object value);
/**
* Instances of the class [HasToJson] implement [toJson] method that returns
* a JSON presentation.
*/
abstract class HasToJson {
/**
* Returns a JSON presentation of the object.
*/
Map<String, Object> toJson();
}
/**
* Base class for decoding JSON objects. The derived class must implement
* error reporting logic.
*/
abstract class JsonDecoder {
/**
* Retrieve the RefactoringKind that should be assumed when decoding
* refactoring feedback objects, or null if no refactoring feedback object is
* expected to be encountered.
*/
RefactoringKind get refactoringKind;
/**
* Decode a JSON object that is expected to be a boolean. The strings "true"
* and "false" are also accepted.
*/
bool decodeBool(String jsonPath, Object json) {
if (json is bool) {
return json;
} else if (json == 'true') {
return true;
} else if (json == 'false') {
return false;
}
throw mismatch(jsonPath, 'bool', json);
}
/**
* Decode a JSON object that is expected to be an integer. A string
* representation of an integer is also accepted.
*/
int decodeInt(String jsonPath, Object json) {
if (json is int) {
return json;
} else if (json is String) {
int value = int.tryParse(json);
if (value == null) {
throw mismatch(jsonPath, 'int', json);
}
return value;
}
throw mismatch(jsonPath, 'int', json);
}
/**
* Decode a JSON object that is expected to be a List. The [decoder] is used
* to decode the items in the list.
*
* The type parameter [E] is the expected type of the elements in the list.
*/
List<E> decodeList<E>(String jsonPath, Object json,
[JsonDecoderCallback<E> decoder]) {
if (json == null) {
return <E>[];
} else if (json is List) {
List<E> result = <E>[];
for (int i = 0; i < json.length; i++) {
result.add(decoder('$jsonPath[$i]', json[i]));
}
return result;
} else {
throw mismatch(jsonPath, 'List', json);
}
}
/**
* Decode a JSON object that is expected to be a Map. [keyDecoder] is used
* to decode the keys, and [valueDecoder] is used to decode the values.
*/
Map<K, V> decodeMap<K, V>(String jsonPath, Object jsonData,
{JsonDecoderCallback<K> keyDecoder,
JsonDecoderCallback<V> valueDecoder}) {
if (jsonData == null) {
return {};
} else if (jsonData is Map) {
Map<K, V> result = <K, V>{};
jsonData.forEach((key, value) {
K decodedKey;
if (keyDecoder != null) {
decodedKey = keyDecoder('$jsonPath.key', key);
} else {
decodedKey = key as K;
}
if (valueDecoder != null) {
value = valueDecoder('$jsonPath[${json.encode(key)}]', value);
}
result[decodedKey] = value as V;
});
return result;
} else {
throw mismatch(jsonPath, 'Map', jsonData);
}
}
/**
* Decode a JSON object that is expected to be a string.
*/
String decodeString(String jsonPath, Object json) {
if (json is String) {
return json;
} else {
throw mismatch(jsonPath, 'String', json);
}
}
/**
* Decode a JSON object that is expected to be one of several choices,
* where the choices are disambiguated by the contents of the field [field].
* [decoders] is a map from each possible string in the field to the decoder
* that should be used to decode the JSON object.
*/
Object decodeUnion(String jsonPath, Map jsonData, String field,
Map<String, JsonDecoderCallback> decoders) {
if (jsonData is Map) {
if (!jsonData.containsKey(field)) {
throw missingKey(jsonPath, field);
}
var disambiguatorPath = '$jsonPath[${json.encode(field)}]';
String disambiguator = decodeString(disambiguatorPath, jsonData[field]);
if (!decoders.containsKey(disambiguator)) {
throw mismatch(
disambiguatorPath, 'One of: ${decoders.keys.toList()}', jsonData);
}
return decoders[disambiguator](jsonPath, jsonData);
} else {
throw mismatch(jsonPath, 'Map', jsonData);
}
}
/**
* Create an exception to throw if the JSON object at [jsonPath] fails to
* match the API definition of [expected].
*/
dynamic mismatch(String jsonPath, String expected, [Object actual]);
/**
* Create an exception to throw if the JSON object at [jsonPath] is missing
* the key [key].
*/
dynamic missingKey(String jsonPath, String key);
}
/**
* JsonDecoder for decoding requests. Errors are reporting by throwing a
* [RequestFailure].
*/
class RequestDecoder extends JsonDecoder {
/**
* The request being deserialized.
*/
final Request _request;
RequestDecoder(this._request);
RefactoringKind get refactoringKind {
// Refactoring feedback objects should never appear in requests.
return null;
}
@override
dynamic mismatch(String jsonPath, String expected, [Object actual]) {
StringBuffer buffer = new StringBuffer();
buffer.write('Expected to be ');
buffer.write(expected);
if (actual != null) {
buffer.write('; found "');
buffer.write(json.encode(actual));
buffer.write('"');
}
return new RequestFailure(
new Response.invalidParameter(_request, jsonPath, buffer.toString()));
}
@override
dynamic missingKey(String jsonPath, String key) {
return new RequestFailure(new Response.invalidParameter(
_request, jsonPath, 'Expected to contain key ${json.encode(key)}'));
}
}
abstract class RequestParams implements HasToJson {
/**
* Return a request whose parameters are taken from this object and that has
* the given [id].
*/
Request toRequest(String id);
}
/**
* JsonDecoder for decoding responses from the server. This is intended to be
* used only for testing. Errors are reported using bare [Exception] objects.
*/
class ResponseDecoder extends JsonDecoder {
final RefactoringKind refactoringKind;
ResponseDecoder(this.refactoringKind);
@override
dynamic mismatch(String jsonPath, String expected, [Object actual]) {
StringBuffer buffer = new StringBuffer();
buffer.write('Expected ');
buffer.write(expected);
if (actual != null) {
buffer.write(' found "');
buffer.write(json.encode(actual));
buffer.write('"');
}
buffer.write(' at ');
buffer.write(jsonPath);
return new Exception(buffer.toString());
}
@override
dynamic missingKey(String jsonPath, String key) {
return new Exception('Missing key $key at $jsonPath');
}
}
/**
* The result data associated with a response.
*/
abstract class ResponseResult implements HasToJson {
/**
* Return a response whose result data is this object for the request with the
* given [id].
*/
Response toResponse(String id);
}

View file

@ -0,0 +1,53 @@
// 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.
/// Jenkins hash function, optimized for small integers.
///
/// Static methods borrowed from sdk/lib/math/jenkins_smi_hash.dart. Non-static
/// methods are an enhancement for the "front_end" package.
///
/// Where performance is critical, use [hash2], [hash3], or [hash4], or the
/// pattern `finish(combine(combine(...combine(0, a), b)..., z))`, where a..z
/// are hash codes to be combined.
///
/// For ease of use, you may also use this pattern:
/// `(new JenkinsSmiHash()..add(a)..add(b)....add(z)).hashCode`, where a..z are
/// the sub-objects whose hashes should be combined. This pattern performs the
/// same operations as the performance critical variant, but allocates an extra
/// object.
class JenkinsSmiHash {
/// Accumulates the hash code [value] into the running hash [hash].
static int combine(int hash, int value) {
hash = 0x1fffffff & (hash + value);
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
return hash ^ (hash >> 6);
}
/// Finalizes a running hash produced by [combine].
static int finish(int hash) {
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
hash = hash ^ (hash >> 11);
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
}
/// Combines together two hash codes.
static int hash2(a, b) => finish(combine(combine(0, a), b));
/// Combines together three hash codes.
static int hash3(a, b, c) => finish(combine(combine(combine(0, a), b), c));
/// Combines together four hash codes.
static int hash4(a, b, c, d) =>
finish(combine(combine(combine(combine(0, a), b), c), d));
int _hash = 0;
/// Accumulates the object [o] into the hash.
void add(Object o) {
_hash = combine(_hash, o.hashCode);
}
/// Finalizes the hash and return the resulting hashcode.
int get hashCode => finish(_hash);
}