Skip to content

Commit ffe97e5

Browse files
bwilkersonCommit Queue
authored andcommitted
[migrate] Partial kythe_visitors.dart
Change-Id: I571ba21c663a577a34daa02a50d232f05ba1857c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/409565 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Auto-Submit: Brian Wilkerson <[email protected]>
1 parent bb15e60 commit ffe97e5

File tree

1 file changed

+65
-62
lines changed

1 file changed

+65
-62
lines changed

pkg/analysis_server/lib/src/services/kythe/kythe_visitors.dart

Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
// ignore_for_file: analyzer_use_new_elements
66

77
import 'package:analyzer/dart/element/element.dart';
8-
import 'package:analyzer/dart/element/visitor.dart';
8+
import 'package:analyzer/dart/element/element2.dart';
99
import 'package:analyzer/file_system/file_system.dart';
10+
import 'package:analyzer/src/utilities/extensions/element.dart';
1011
import 'package:analyzer/src/workspace/blaze.dart';
1112
import 'package:path/path.dart' show relative;
1213

@@ -15,25 +16,25 @@ import 'schema.dart' as schema;
1516
/// Given some [ConstructorElement], this method returns '<class-name>' as the
1617
/// name of the constructor, unless the constructor is a named constructor in
1718
/// which '<class-name>.<constructor-name>' is returned.
18-
String _computeConstructorElementName(ConstructorElement element) {
19-
var name = element.enclosingElement3.name;
20-
var constructorName = element.name;
21-
if (constructorName.isNotEmpty) {
19+
String _computeConstructorElementName(ConstructorElement2 element) {
20+
var name = element.enclosingElement2.name3!;
21+
var constructorName = element.name3;
22+
if (constructorName != null && constructorName != 'new') {
2223
name = '$name.$constructorName';
2324
}
2425
return name;
2526
}
2627

27-
String? _getNodeKind(Element e) {
28-
if (e is FieldElement && e.isEnumConstant) {
28+
String? _getNodeKind(Element2 e) {
29+
if (e is FieldElement2 && e.isEnumConstant) {
2930
// FieldElement is a kind of VariableElement, so this test case must be
3031
// before the e is VariableElement check.
3132
return schema.CONSTANT_KIND;
32-
} else if (e is VariableElement || e is PrefixElement) {
33+
} else if (e is VariableElement2 || e is PrefixElement2) {
3334
return schema.VARIABLE_KIND;
34-
} else if (e is ExecutableElement) {
35+
} else if (e is ExecutableElement2) {
3536
return schema.FUNCTION_KIND;
36-
} else if (e is InterfaceElement || e is TypeParameterElement) {
37+
} else if (e is InterfaceElement2 || e is TypeParameterElement2) {
3738
// TODO(jwren): this should be using absvar instead, see
3839
// https://kythe.io/docs/schema/#absvar
3940
return schema.RECORD_KIND;
@@ -43,15 +44,15 @@ String? _getNodeKind(Element e) {
4344

4445
String _getPath(
4546
ResourceProvider provider,
46-
Element? e, {
47+
Element2? e, {
4748
String? sdkRootPath,
4849
String? corpus,
4950
}) {
5051
// TODO(jwren): This method simply serves to provide the WORKSPACE relative
5152
// path for sources in Elements, it needs to be written in a more robust way.
5253
// TODO(jwren): figure out what source generates a e != null, but
5354
// e.source == null to ensure that it is not a bug somewhere in the stack.
54-
var source = e?.source;
55+
var source = e?.firstFragment.libraryFragment!.source;
5556
if (source == null) {
5657
// null sometimes when the element is used to generate the node type
5758
// "dynamic"
@@ -88,7 +89,7 @@ String _getPath(
8889
/// returned.
8990
String _getSignature(
9091
ResourceProvider provider,
91-
Element? element,
92+
Element2? element,
9293
String nodeKind,
9394
String corpus, {
9495
String? sdkRootPath,
@@ -97,15 +98,16 @@ String _getSignature(
9798
if (element == null) {
9899
return schema.DYNAMIC_KIND;
99100
}
100-
if (element is CompilationUnitElement) {
101+
if (element is LibraryElement2) {
101102
return _getPath(
102103
provider,
103104
element,
104105
sdkRootPath: sdkRootPath,
105106
corpus: corpus,
106107
);
107108
}
108-
return '$nodeKind:${element.accept(_SignatureElementVisitor.instance)}';
109+
var builder = _SignatureBuilder.instance;
110+
return '$nodeKind:${builder.signatureFor(element)}';
109111
}
110112

111113
/// A helper class for getting the Kythe uri's for elements for querying
@@ -119,14 +121,19 @@ class CiderKytheHelper {
119121

120122
/// Returns a URI that can be used to query Kythe.
121123
String toKytheUri(Element e) {
124+
return toKytheUri2(e.asElement2!);
125+
}
126+
127+
/// Returns a URI that can be used to query Kythe.
128+
String toKytheUri2(Element2 e) {
122129
var nodeKind = _getNodeKind(e) ?? schema.RECORD_KIND;
123130
var vname = _vNameFromElement(e, nodeKind);
124131
return 'kythe://$corpus?lang=dart?path=${vname.path}#${vname.signature}';
125132
}
126133

127134
/// Given some [Element] and Kythe node kind, this method generates and
128135
/// returns the [_KytheVName].
129-
_KytheVName _vNameFromElement(Element? e, String nodeKind) {
136+
_KytheVName _vNameFromElement(Element2? e, String nodeKind) {
130137
assert(nodeKind != schema.FILE_KIND);
131138
// general case
132139
return _KytheVName(
@@ -154,56 +161,52 @@ class _KytheVName {
154161
_KytheVName({required this.path, required this.signature});
155162
}
156163

157-
/// This visitor class should be used by [_getSignature].
164+
/// An objects that builds up a string signature for an element.
158165
///
159-
/// This visitor is an [GeneralizingElementVisitor] which builds up a [String]
160-
/// signature for a given [Element], uniqueness is guaranteed within the
161-
/// enclosing file.
162-
class _SignatureElementVisitor
163-
extends GeneralizingElementVisitor<StringBuffer> {
164-
static _SignatureElementVisitor instance = _SignatureElementVisitor();
165-
166-
@override
167-
StringBuffer visitCompilationUnitElement(CompilationUnitElement element) {
168-
return StringBuffer();
169-
}
166+
/// Uniqueness is guaranteed within the enclosing file.
167+
class _SignatureBuilder {
168+
static _SignatureBuilder instance = _SignatureBuilder();
170169

171-
@override
172-
StringBuffer visitElement(Element element) {
173-
var enclosingElt = element.enclosingElement3!;
174-
var buffer = enclosingElt.accept(this)!;
175-
if (buffer.isNotEmpty) {
176-
buffer.write('#');
177-
}
178-
if (element is MethodElement &&
179-
element.name == '-' &&
180-
element.parameters.length == 1) {
181-
buffer.write('unary-');
182-
} else if (element is ConstructorElement) {
183-
buffer.write(_computeConstructorElementName(element));
184-
} else {
185-
buffer.write(element.name);
186-
}
187-
if (enclosingElt is ExecutableElement) {
188-
buffer
189-
..write('@')
190-
..write(element.nameOffset - enclosingElt.nameOffset);
191-
}
170+
StringBuffer signatureFor(Element2 element) {
171+
var buffer = StringBuffer();
172+
_appendSignatureTo(buffer, element);
192173
return buffer;
193174
}
194175

195-
@override
196-
StringBuffer visitLibraryElement(LibraryElement element) {
197-
return StringBuffer('library:${element.displayName}');
198-
}
199-
200-
@override
201-
StringBuffer visitTypeParameterElement(TypeParameterElement element) {
202-
// It is legal to have a named constructor with the same name as a type
203-
// parameter. So we distinguish them by using '.' between the class (or
204-
// typedef) name and the type parameter name.
205-
return element.enclosingElement3!.accept(this)!
206-
..write('.')
207-
..write(element.name);
176+
void _appendSignatureTo(StringBuffer buffer, Element2 element) {
177+
if (element is LibraryElement2) {
178+
buffer.write('library:${element.displayName}');
179+
} else if (element is TypeParameterElement2) {
180+
// It is legal to have a named constructor with the same name as a type
181+
// parameter. So we distinguish them by using '.' between the class (or
182+
// typedef) name and the type parameter name.
183+
_appendSignatureTo(buffer, element.enclosingElement2!);
184+
buffer
185+
..write('.')
186+
..write(element.name3!);
187+
} else {
188+
var enclosingElt = element.enclosingElement2!;
189+
_appendSignatureTo(buffer, enclosingElt);
190+
if (buffer.isNotEmpty) {
191+
buffer.write('#');
192+
}
193+
if (element is MethodElement2 &&
194+
element.name3 == '-' &&
195+
element.formalParameters.length == 1) {
196+
buffer.write('unary-');
197+
} else if (element is ConstructorElement2) {
198+
buffer.write(_computeConstructorElementName(element));
199+
} else {
200+
buffer.write(element.name3);
201+
}
202+
if (enclosingElt is ExecutableElement) {
203+
buffer
204+
..write('@')
205+
..write(
206+
element.firstFragment.nameOffset2! -
207+
enclosingElt.firstFragment.nameOffset2!,
208+
);
209+
}
210+
}
208211
}
209212
}

0 commit comments

Comments
 (0)