From e20b3779966d5b34f418ef7b9869737bbbc7e15a Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Mon, 16 Oct 2023 14:13:22 -0700 Subject: [PATCH] fix build issue due to PR conflicts --- pkgs/dart_services/lib/src/shared/model.dart | 82 +++++++++++++++++++ .../dart_services/lib/src/shared/model.g.dart | 62 ++++++++++++++ .../lib/src/shared/services.dart | 39 +++++---- 3 files changed, 163 insertions(+), 20 deletions(-) diff --git a/pkgs/dart_services/lib/src/shared/model.dart b/pkgs/dart_services/lib/src/shared/model.dart index aa624606e..fa9544165 100644 --- a/pkgs/dart_services/lib/src/shared/model.dart +++ b/pkgs/dart_services/lib/src/shared/model.dart @@ -118,6 +118,88 @@ class FlutterBuildResponse { Map toJson() => _$FlutterBuildResponseToJson(this); } +@JsonSerializable() +class FixesResponse { + final List fixes; + final List assists; + + FixesResponse({ + required this.fixes, + required this.assists, + }); + + factory FixesResponse.fromJson(Map json) => + _$FixesResponseFromJson(json); + + Map toJson() => _$FixesResponseToJson(this); +} + +@JsonSerializable() +class SourceChange { + final String message; + final List edits; + // TODO: Add linked edit groups once we start using them. + // final List linkedEditGroups; + + SourceChange({ + required this.message, + required this.edits, + }); + + factory SourceChange.fromJson(Map json) => + _$SourceChangeFromJson(json); + + Map toJson() => _$SourceChangeToJson(this); + + @override + String toString() => 'SourceChange [$message]'; +} + +@JsonSerializable() +class SourceEdit { + final int offset; + final int length; + final String replacement; + + SourceEdit({ + required this.offset, + required this.length, + required this.replacement, + }); + + factory SourceEdit.fromJson(Map json) => + _$SourceEditFromJson(json); + + Map toJson() => _$SourceEditToJson(this); + + @override + String toString() => 'SourceEdit [$offset,$length,$replacement]'; +} + +@JsonSerializable() +class DocumentResponse { + final String? dartdoc; + final String? elementKind; + final String? elementDescription; + final String? containingLibraryName; + final bool? deprecated; + final String? propagatedType; + + DocumentResponse({ + this.dartdoc, + this.elementKind, + this.elementDescription, + this.containingLibraryName, + this.deprecated, + this.propagatedType, + }); + + factory DocumentResponse.fromJson(Map json) => + _$DocumentResponseFromJson(json); + + Map toJson() => _$DocumentResponseToJson(this); +} + @JsonSerializable() class CompleteResponse { final int replacementOffset; diff --git a/pkgs/dart_services/lib/src/shared/model.g.dart b/pkgs/dart_services/lib/src/shared/model.g.dart index 801f6ca5d..b65de8fb0 100644 --- a/pkgs/dart_services/lib/src/shared/model.g.dart +++ b/pkgs/dart_services/lib/src/shared/model.g.dart @@ -97,6 +97,68 @@ Map _$FlutterBuildResponseToJson( 'artifacts': instance.artifacts, }; +FixesResponse _$FixesResponseFromJson(Map json) => + FixesResponse( + fixes: (json['fixes'] as List) + .map((e) => SourceChange.fromJson(e as Map)) + .toList(), + assists: (json['assists'] as List) + .map((e) => SourceChange.fromJson(e as Map)) + .toList(), + ); + +Map _$FixesResponseToJson(FixesResponse instance) => + { + 'fixes': instance.fixes, + 'assists': instance.assists, + }; + +SourceChange _$SourceChangeFromJson(Map json) => SourceChange( + message: json['message'] as String, + edits: (json['edits'] as List) + .map((e) => SourceEdit.fromJson(e as Map)) + .toList(), + ); + +Map _$SourceChangeToJson(SourceChange instance) => + { + 'message': instance.message, + 'edits': instance.edits, + }; + +SourceEdit _$SourceEditFromJson(Map json) => SourceEdit( + offset: json['offset'] as int, + length: json['length'] as int, + replacement: json['replacement'] as String, + ); + +Map _$SourceEditToJson(SourceEdit instance) => + { + 'offset': instance.offset, + 'length': instance.length, + 'replacement': instance.replacement, + }; + +DocumentResponse _$DocumentResponseFromJson(Map json) => + DocumentResponse( + dartdoc: json['dartdoc'] as String?, + elementKind: json['elementKind'] as String?, + elementDescription: json['elementDescription'] as String?, + containingLibraryName: json['containingLibraryName'] as String?, + deprecated: json['deprecated'] as bool?, + propagatedType: json['propagatedType'] as String?, + ); + +Map _$DocumentResponseToJson(DocumentResponse instance) => + { + 'dartdoc': instance.dartdoc, + 'elementKind': instance.elementKind, + 'elementDescription': instance.elementDescription, + 'containingLibraryName': instance.containingLibraryName, + 'deprecated': instance.deprecated, + 'propagatedType': instance.propagatedType, + }; + CompleteResponse _$CompleteResponseFromJson(Map json) => CompleteResponse( replacementOffset: json['replacementOffset'] as int, diff --git a/pkgs/dart_services/lib/src/shared/services.dart b/pkgs/dart_services/lib/src/shared/services.dart index 7ac82928c..5cefd016d 100644 --- a/pkgs/dart_services/lib/src/shared/services.dart +++ b/pkgs/dart_services/lib/src/shared/services.dart @@ -25,13 +25,11 @@ class ServicesClient { Future complete(SourceRequest request) => _requestPost('complete', request.toJson(), CompleteResponse.fromJson); - // TODO: Implement document(). - // Future document(SourceRequest request) => - // _request('document', request.toJson(), DocumentResponse.fromJson); + Future document(SourceRequest request) => + _requestPost('document', request.toJson(), DocumentResponse.fromJson); - // TODO: Implement fixes(). - // Future fixes(SourceRequest request) => - // _request('fixes', request.toJson(), FixesResponse.fromJson); + Future fixes(SourceRequest request) => + _requestPost('fixes', request.toJson(), FixesResponse.fromJson); Future format(SourceRequest request) => _requestPost('format', request.toJson(), FormatResponse.fromJson); @@ -52,18 +50,19 @@ class ServicesClient { ) async { final response = await client.get(Uri.parse('${rootUrl}api/dartservices/v3/$action')); + if (response.statusCode != 200) { throw ApiRequestError( '$action: ${response.statusCode}: ${response.reasonPhrase}', response.body, ); - } - - try { - return responseFactory( - json.decode(response.body) as Map); - } on FormatException catch (e) { - throw ApiRequestError('$action: $e', response.body); + } else { + try { + return responseFactory( + json.decode(response.body) as Map); + } on FormatException catch (e) { + throw ApiRequestError('$action: $e', response.body); + } } } @@ -81,13 +80,13 @@ class ServicesClient { '$action: ${response.statusCode}: ${response.reasonPhrase}', response.body, ); - } - - try { - return responseFactory( - json.decode(response.body) as Map); - } on FormatException catch (e) { - throw ApiRequestError('$action: $e', response.body); + } else { + try { + return responseFactory( + json.decode(response.body) as Map); + } on FormatException catch (e) { + throw ApiRequestError('$action: $e', response.body); + } } } }