Skip to content

Commit

Permalink
Add additional functionality to the v3 protocol (#2676)
Browse files Browse the repository at this point in the history
* implement v3 versions of document, fix

* add quick fixes, assists to v3 protocol

* remove extra changes
  • Loading branch information
devoncarew committed Oct 16, 2023
1 parent e8c2801 commit 99e80fa
Show file tree
Hide file tree
Showing 15 changed files with 418 additions and 300 deletions.
16 changes: 0 additions & 16 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,3 @@ updates:
labels:
- "autosubmit"
- "type-infra"

- package-ecosystem: "pub"
directory: "/pkgs/dart_pad/"
schedule:
interval: "monthly"
versioning-strategy: increase-if-necessary
labels:
- "type-infra"

- package-ecosystem: "pub"
directory: "/pkgs/dart_services/"
schedule:
interval: "monthly"
versioning-strategy: increase-if-necessary
labels:
- "type-infra"
233 changes: 0 additions & 233 deletions pkgs/dart_pad/lib/parameter_popup.dart

This file was deleted.

24 changes: 22 additions & 2 deletions pkgs/dart_pad/lib/services/dartservices.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ class DartservicesApi {
FormatResponse(),
);

Future<VersionResponse> version() => _request(
Future<VersionResponse> version() => _requestGet(
'version',
VersionRequest(),
VersionResponse(),
);

Expand Down Expand Up @@ -94,6 +93,27 @@ class DartservicesApi {

return result;
}

Future<O> _requestGet<O extends GeneratedMessage>(
String action,
O result,
) async {
final response = await _client.get(Uri.parse('$rootUrl$_apiPath/$action'));
final jsonBody = json.decode(response.body);
result
..mergeFromProto3Json(jsonBody, ignoreUnknownFields: true)
..freeze();

// 99 is the tag number for error message.
if (result.getFieldOrNull(99) != null) {
final br = BadRequest()
..mergeFromProto3Json(jsonBody)
..freeze();
throw ApiRequestError(br.error.message);
}

return result;
}
}

class ApiRequestError implements Exception {
Expand Down
2 changes: 1 addition & 1 deletion pkgs/dart_pad/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies:
html_unescape: ^2.0.0
http: ^0.13.0
js: ^0.6.7
json_annotation: ^4.8.0
json_annotation: ^4.8.1
logging: ^1.2.0
markdown: ^7.0.1
mdc_web: ^0.6.0
Expand Down
75 changes: 75 additions & 0 deletions pkgs/dart_services/lib/src/analysis_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,38 @@ abstract class AnalysisServerWrapper {
return proto.FixesResponse()..fixes.addAll(responseFixes);
}

Future<api.FixesResponse> fixesV3(String src, int offset) async {
final mainFile = _getPathFromName(kMainDart);
final overlay = {mainFile: src};

await _loadSources(overlay);

try {
final fixes = await analysisServer.edit.getFixes(mainFile, offset);
final assists = await analysisServer.edit.getAssists(mainFile, offset, 1);

final fixChanges = fixes.fixes.expand((fixes) => fixes.fixes).toList();
final assistsChanges = assists.assists;

// Filter any source changes that want to act on files other than main.dart.
fixChanges.removeWhere(
(change) => change.edits.any((edit) => edit.file != mainFile));
assistsChanges.removeWhere(
(change) => change.edits.any((edit) => edit.file != mainFile));

return api.FixesResponse(
fixes: fixChanges.map((change) {
return change.toApiSourceChange();
}).toList(),
assists: assistsChanges.map((change) {
return change.toApiSourceChange();
}).toList(),
);
} finally {
await _unloadSources();
}
}

Future<proto.AssistsResponse> getAssists(String src, int offset) async {
return getAssistsMulti({kMainDart: src}, Location(kMainDart, offset));
}
Expand Down Expand Up @@ -285,6 +317,33 @@ abstract class AnalysisServerWrapper {
};
}

Future<api.DocumentResponse> dartdocV3(String src, int offset) async {
final location = Location(kMainDart, offset);
final sources = _getOverlayMapWithPaths({kMainDart: src});
final sourcepath = _getPathFromName(location.sourceName);

await _loadSources(sources);

final result =
await analysisServer.analysis.getHover(sourcepath, location.offset);
await _unloadSources();

if (result.hovers.isEmpty) {
return api.DocumentResponse();
}

final info = result.hovers.first;

return api.DocumentResponse(
dartdoc: info.dartdoc,
containingLibraryName: info.containingLibraryName,
elementDescription: info.elementDescription,
elementKind: info.elementKind,
deprecated: info.isDeprecated,
propagatedType: info.propagatedType,
);
}

Future<proto.AnalysisResults> analyze(String src) {
return analyzeFiles({kMainDart: src});
}
Expand Down Expand Up @@ -619,3 +678,19 @@ class Location {

const Location(this.sourceName, this.offset);
}

extension SourceChangeExtension on SourceChange {
api.SourceChange toApiSourceChange() {
return api.SourceChange(
message: message,
edits: edits
.expand((fileEdit) => fileEdit.edits)
.map((edit) => api.SourceEdit(
offset: edit.offset,
length: edit.length,
replacement: edit.replacement,
))
.toList(),
);
}
}
Loading

0 comments on commit 99e80fa

Please sign in to comment.