Implement search for other local elements.

R=brianwilkerson@google.com
BUG=

Review URL: https://codereview.chromium.org/2529473002 .
This commit is contained in:
Konstantin Shcheglov 2016-11-22 12:48:15 -08:00
parent 679e8fd9ff
commit 366e727048
3 changed files with 102 additions and 7 deletions

View file

@ -29,8 +29,17 @@ class Search {
}
ElementKind kind = element.kind;
if (kind == ElementKind.LABEL || kind == ElementKind.LOCAL_VARIABLE) {
if (kind == ElementKind.FUNCTION || kind == ElementKind.METHOD) {
if (element.enclosingElement is ExecutableElement) {
return _searchReferences_Local(element, (n) => n is Block);
}
// return _searchReferences_Function(element);
} else if (kind == ElementKind.LABEL ||
kind == ElementKind.LOCAL_VARIABLE) {
return _searchReferences_Local(element, (n) => n is Block);
} else if (kind == ElementKind.TYPE_PARAMETER) {
return _searchReferences_Local(
element, (n) => n.parent is CompilationUnit);
}
// TODO(scheglov) support other kinds
return const <SearchResult>[];
@ -58,6 +67,9 @@ class Search {
// Prepare the enclosing node.
AstNode enclosingNode = node.getAncestor(isRootNode);
if (enclosingNode == null) {
return const <SearchResult>[];
}
// Find the matches.
_LocalReferencesVisitor visitor =

View file

@ -1051,7 +1051,7 @@ class ClassElementImpl extends AbstractClassElementImpl
super.visitChildren(visitor);
safelyVisitChildren(_constructors, visitor);
safelyVisitChildren(methods, visitor);
safelyVisitChildren(_typeParameters, visitor);
safelyVisitChildren(typeParameters, visitor);
}
void _collectAllSupertypes(List<InterfaceType> supertypes) {

View file

@ -81,7 +81,24 @@ class ExpectedResult {
class SearchTest extends BaseAnalysisDriverTest {
static const testUri = 'package:test/test.dart';
test_searchReferences_Label() async {
test_searchReferences_FunctionElement_local() async {
addTestFile('''
main() {
test() {}
test();
print(test);
}
''');
FunctionElement element = await _findElement('test');
List<String> main = [testUri, 'main'];
var expected = [
_expectId(main, SearchResultKind.INVOCATION, 'test();'),
_expectId(main, SearchResultKind.REFERENCE, 'test);')
];
await _verifyReferences(element, expected);
}
test_searchReferences_LabelElement() async {
addTestFile('''
main() {
label:
@ -93,7 +110,7 @@ label:
}
}
''');
Element element = await _findElementAtString('label:');
Element element = await _findElement('label');
List<String> main = [testUri, 'main'];
var expected = [
_expectId(main, SearchResultKind.REFERENCE, 'label; // 1'),
@ -102,7 +119,7 @@ label:
await _verifyReferences(element, expected);
}
test_searchReferences_localVariable() async {
test_searchReferences_LocalVariableElement() async {
addTestFile(r'''
main() {
var v;
@ -112,7 +129,7 @@ main() {
v();
}
''');
Element element = await _findElementAtString('v;');
Element element = await _findElement('v');
List<String> main = [testUri, 'main'];
var expected = [
_expectId(main, SearchResultKind.WRITE, 'v = 1;'),
@ -123,7 +140,7 @@ main() {
await _verifyReferences(element, expected);
}
test_searchReferences_localVariable_inForEachLoop() async {
test_searchReferences_localVariableElement_inForEachLoop() async {
addTestFile('''
main() {
for (var v in []) {
@ -145,6 +162,67 @@ main() {
await _verifyReferences(element, expected);
}
test_searchReferences_TypeParameterElement_ofClass() async {
addTestFile('''
class A<T> {
foo(T a) {}
bar(T b) {}
}
''');
TypeParameterElement element = await _findElement('T');
var expected = [
_expectId([testUri, 'A', 'foo', 'a'], SearchResultKind.REFERENCE, 'T a'),
_expectId([testUri, 'A', 'bar', 'b'], SearchResultKind.REFERENCE, 'T b'),
];
await _verifyReferences(element, expected);
}
test_searchReferences_TypeParameterElement_ofLocalFunction() async {
addTestFile('''
main() {
void foo<T>(T a) {
void bar(T b) {}
}
}
''');
TypeParameterElement element = await _findElement('T');
var expected = [
_expectId(
[testUri, 'main', 'foo', 'a'], SearchResultKind.REFERENCE, 'T a'),
_expectId([testUri, 'main', 'foo', 'bar', 'b'],
SearchResultKind.REFERENCE, 'T b'),
];
await _verifyReferences(element, expected);
}
test_searchReferences_TypeParameterElement_ofMethod() async {
addTestFile('''
class A {
foo<T>(T p) {}
}
''');
TypeParameterElement element = await _findElement('T');
var expected = [
_expectId([testUri, 'A', 'foo', 'p'], SearchResultKind.REFERENCE, 'T p'),
];
await _verifyReferences(element, expected);
}
test_searchReferences_TypeParameterElement_ofTopLevelFunction() async {
addTestFile('''
foo<T>(T a) {
bar(T b) {}
}
''');
TypeParameterElement element = await _findElement('T');
var expected = [
_expectId([testUri, 'foo', 'a'], SearchResultKind.REFERENCE, 'T a'),
_expectId(
[testUri, 'foo', 'bar', 'b'], SearchResultKind.REFERENCE, 'T b'),
];
await _verifyReferences(element, expected);
}
ExpectedResult _expectId(
List<String> enclosingComponents, SearchResultKind kind, String search,
{int length, bool isResolved: true, bool isQualified: false}) {
@ -156,6 +234,11 @@ main() {
isResolved: isResolved, isQualified: isQualified);
}
Future<Element> _findElement(String name) async {
AnalysisResult result = await driver.getResult(testFile);
return findChildElement(result.unit.element, name);
}
Future<Element> _findElementAtString(String search) async {
AnalysisResult result = await driver.getResult(testFile);
int offset = findOffset(search);