Skip to content

Commit 49b81cb

Browse files
DanTupCommit Queue
authored andcommitted
[analyzer] Handle Function.call methods in ElementLocator
Fixes the hover part of #61319 Change-Id: Iabff1bf61c14ade37d9392f65709a5d6e0750643 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/448980 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
1 parent b6d110e commit 49b81cb

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

pkg/analysis_server/test/lsp/hover_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,26 @@ print();
628628
String [!a^bc!] = '';
629629
''', contains('This is a string.'));
630630

631+
Future<void> test_method_callMethod() async {
632+
var content = '''
633+
/// f doc.
634+
void f(int i) {
635+
f.[!call^!](1);
636+
}
637+
''';
638+
var expected = '''
639+
```dart
640+
void f(int i)
641+
```
642+
Type: `void Function(int)`
643+
644+
Declared in _package:test/main.dart_.
645+
646+
---
647+
f doc.''';
648+
await assertStringContents(content, equals(expected));
649+
}
650+
631651
Future<void> test_method_mixin_onImplementation() async {
632652
var content = '''
633653
abstract class A {

pkg/analyzer/lib/src/dart/ast/element_locator.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/visitor.dart';
77
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/dart/element/type.dart';
89
import 'package:analyzer/src/dart/ast/extensions.dart';
910

1011
/// An object used to locate the [Element] associated with a given [AstNode].
@@ -192,6 +193,14 @@ class _ElementMapper2 extends GeneralizingAstVisitor<Element> {
192193
return grandParent.element;
193194
}
194195
return null;
196+
} else if (parent is MethodInvocation &&
197+
parent.methodName == node &&
198+
parent.methodName.name == MethodElement.CALL_METHOD_NAME) {
199+
// Handle .call() invocations on functions.
200+
var method = parent.realTarget;
201+
if (method is Identifier && method.staticType is FunctionType) {
202+
return method.element;
203+
}
195204
}
196205
return node.writeOrReadElement;
197206
}

pkg/analyzer/test/src/dart/ast/element_locator_test.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,51 @@ class A {
453453
''');
454454
}
455455

456+
test_locate_MethodInvocation_class_callMethod_argument() async {
457+
await resolveTestCode(r'''
458+
class A {
459+
void call(int i) {}
460+
}
461+
void f(A a) {
462+
a.call(1);
463+
}
464+
''');
465+
var node = findNode.methodInvocation('call(1)').methodName;
466+
var element = ElementLocator.locate(node);
467+
_assertElement(element, r'''
468+
<testLibrary>::@class::A::@method::call
469+
''');
470+
}
471+
472+
test_locate_MethodInvocation_class_callMethod_constructor() async {
473+
await resolveTestCode(r'''
474+
class A {
475+
void call(int i) {}
476+
}
477+
void f() {
478+
A().call(1);
479+
}
480+
''');
481+
var node = findNode.methodInvocation('call(1)').methodName;
482+
var element = ElementLocator.locate(node);
483+
_assertElement(element, r'''
484+
<testLibrary>::@class::A::@method::call
485+
''');
486+
}
487+
488+
test_locate_MethodInvocation_function_callMethod() async {
489+
await resolveTestCode(r'''
490+
void f(int i) {
491+
f.call(1);
492+
}
493+
''');
494+
var node = findNode.methodInvocation('call').methodName;
495+
var element = ElementLocator.locate(node);
496+
_assertElement(element, r'''
497+
<testLibrary>::@function::f
498+
''');
499+
}
500+
456501
test_locate_MethodInvocation_method() async {
457502
await resolveTestCode(r'''
458503
class A {

0 commit comments

Comments
 (0)