Files
sdk/pkg/analyzer_plugin/tool/spec/codegen_protocol_common.dart
T
Danny Tuppeny 8d936d38fc [analysis_server]/[analyzer_plugin] Make all toJson/fromJson methods go through a central converstion for Path<->URI Strings
This change shouldn't change any current behaviour, but means all "FilePath" types in the legacy protocol spec will go through a (currently no-op) conversion. The server will be able to replace this conversion based on client capabilities in a future CL.

Because a lot of the generated classes are in analyzer_plugin, this also moves the ClientUriConverter class there.

`pkg\analysis_server\test\src\utilities\json_test.dart` contains tests that the toJson/fromJson methods go through the converter recursively (inc. map keys/values/etc.).

Change-Id: If5aec884070128eea594540fd25a9017ada86079
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/349060
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2024-01-30 16:58:25 +00:00

79 lines
2.8 KiB
Dart

// Copyright (c) 2017, 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 'package:analyzer_utilities/tools.dart';
import 'package:path/path.dart' as path;
import 'api.dart';
import 'codegen_dart_protocol.dart';
import 'from_html.dart';
import 'implied_types.dart';
GeneratedFile clientTarget(
bool responseRequiresRequestTime, bool requiresProtocolJsonMethods) =>
GeneratedFile(
'../analysis_server_client/lib/src/protocol/protocol_common.dart',
(String pkgPath) async {
var visitor = CodegenCommonVisitor(
path.basename(pkgPath),
responseRequiresRequestTime,
requiresProtocolJsonMethods,
readApi(pkgPath),
forClient: true);
return visitor.collectCode(visitor.visitApi);
});
GeneratedFile pluginTarget(
bool responseRequiresRequestTime, bool requiresProtocolJsonMethods) =>
GeneratedFile('lib/protocol/protocol_common.dart', (String pkgPath) async {
var visitor = CodegenCommonVisitor(
path.basename(pkgPath),
responseRequiresRequestTime,
requiresProtocolJsonMethods,
readApi(pkgPath));
return visitor.collectCode(visitor.visitApi);
});
/// A visitor that produces Dart code defining the common types associated with
/// the API.
class CodegenCommonVisitor extends CodegenProtocolVisitor {
final bool forClient;
/// Initialize a newly created visitor to generate code in the package with
/// the given [packageName] corresponding to the types in the given [api] that
/// are common to multiple protocols.
CodegenCommonVisitor(super.packageName, super.responseRequiresRequestTime,
super.requiresProtocolJsonMethods, super.api,
{this.forClient = false});
@override
void emitImports() {
writeln("import 'dart:convert' hide JsonDecoder;");
writeln();
if (forClient) {
writeln(
"import 'package:analysis_server_client/src/protocol/protocol_base.dart';");
writeln(
"import 'package:analysis_server_client/src/protocol/protocol_internal.dart';");
} else {
writeln("import 'package:$packageName/protocol/protocol.dart';");
writeln(
"import 'package:$packageName/src/protocol/protocol_internal.dart';");
}
writeln();
writeln('// ignore_for_file: flutter_style_todos');
}
@override
List<ImpliedType> getClassesToEmit() {
var types = impliedTypes.values.where((ImpliedType type) {
var node = type.apiNode;
return node is TypeDefinition && node.isExternal;
}).toList();
types.sort((first, second) =>
capitalize(first.camelName).compareTo(capitalize(second.camelName)));
return types;
}
}