Skip to content

Commit 5237fae

Browse files
DanTupdart-scoped@luci-project-accounts.iam.gserviceaccount.com
authored andcommitted
[analyzer][analysis_server] Include all parameter names in hovers and completion detail labels
This adds names of positional parameters to the "element description" part of hovers (but not the "type" part) and also the details labels of code completion. + adds some extra tests for signature help to ensure the same. Fixes #63792 Change-Id: I2fdae3fa524895294e7dde6e732fd4e718345901 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/541380 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 99b7cdd commit 5237fae

14 files changed

Lines changed: 315 additions & 26 deletions

File tree

‎pkg/analysis_server/lib/src/computer/computer_hover.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ class DartUnitHoverComputer {
155155
/// information to disambiguate things like constructors from types (and
156156
/// whether they are const).
157157
String? _elementDisplayString(AstNode node, Element? element) {
158-
var displayString = element?.displayString(multiline: true);
158+
var displayString = element?.displayString(
159+
multiline: true,
160+
includePositionalParameterNames: true,
161+
);
159162

160163
if (displayString != null) {
161164
if (element is TypeParameterElement) {

‎pkg/analysis_server/lib/src/lsp/completion_utils.dart‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,16 @@ CompletionDetail _getCompletionDetail(
641641
// the suggestion (handles records).
642642
String? parameterType;
643643
if (element is FormalParameterElement) {
644-
parameterType = element.type.getDisplayString();
644+
parameterType = element.type.getDisplayString(
645+
includePositionalParameterNames: true,
646+
);
645647
}
646-
returnType = server.getReturnTypeString(element) ?? parameterType;
648+
returnType =
649+
server.getReturnTypeString(
650+
element,
651+
includePositionalParameterNames: true,
652+
) ??
653+
parameterType;
647654

648655
// Extract the type from setters to be shown in the place a return type
649656
// would usually be shown.
@@ -657,7 +664,9 @@ CompletionDetail _getCompletionDetail(
657664
// Function calls may not have an element (for example the call method on
658665
// Function), so use the function type to get parameters if available.
659666
parameters = getParametersListString(suggestion.type.formalParameters);
660-
returnType = suggestion.type.returnType.getDisplayString();
667+
returnType = suggestion.type.returnType.getDisplayString(
668+
includePositionalParameterNames: true,
669+
);
661670
}
662671

663672
var truncatedParameters = switch (parameters) {

‎pkg/analysis_server/lib/src/protocol_server.dart‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,25 @@ String? getExtendedTypeString(engine.Element element) {
9898
return null;
9999
}
100100

101-
String? getReturnTypeString(engine.Element element) {
102-
if (element is engine.ExecutableElement) {
103-
if (element.kind == engine.ElementKind.SETTER) {
104-
return null;
105-
} else {
106-
return element.returnType.getDisplayString();
107-
}
101+
String? getReturnTypeString(
102+
engine.Element element, {
103+
bool includePositionalParameterNames = false,
104+
}) {
105+
DartType? returnType;
106+
if (element is engine.ExecutableElement &&
107+
element.kind != engine.ElementKind.SETTER) {
108+
returnType = element.returnType;
108109
} else if (element is engine.VariableElement) {
109-
var type = element.type;
110-
return type.getDisplayString();
110+
returnType = element.type;
111111
} else if (element is engine.TypeAliasElement) {
112112
var aliasedType = element.aliasedType;
113113
if (aliasedType is FunctionType) {
114-
var returnType = aliasedType.returnType;
115-
return returnType.getDisplayString();
114+
returnType = aliasedType.returnType;
116115
}
117116
}
118-
return null;
117+
return returnType?.getDisplayString(
118+
includePositionalParameterNames: includePositionalParameterNames,
119+
);
119120
}
120121

121122
/// Translates engine errors through the ErrorProcessor.

‎pkg/analysis_server/test/analysis/get_hover_test.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2108,7 +2108,7 @@ typedef void A(int a);
21082108
var hover = await prepareHover('A');
21092109
_assertHover(
21102110
hover,
2111-
elementDescription: 'typedef A = void Function(int)',
2111+
elementDescription: 'typedef A = void Function(int a)',
21122112
elementKind: 'type alias',
21132113
);
21142114
}

‎pkg/analysis_server/test/lsp/completion_dart_test.dart‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,40 @@ String m3() {
10791079
);
10801080
}
10811081

1082+
Future<void> test_functionType_parameterName() async {
1083+
content = '''
1084+
void f(void Function(int i) x) {
1085+
x^
1086+
}
1087+
''';
1088+
1089+
await expectLabel(
1090+
content,
1091+
label: 'x',
1092+
labelDetail: ' void Function(int i)',
1093+
labelDescription: null,
1094+
filterText: null,
1095+
detail: 'void Function(int i)',
1096+
);
1097+
}
1098+
1099+
Future<void> test_functionType_parameterName_returnType() async {
1100+
content = '''
1101+
void f(void Function(int i) Function() x) {
1102+
x^
1103+
}
1104+
''';
1105+
1106+
await expectLabel(
1107+
content,
1108+
label: 'x',
1109+
labelDetail: ' void Function(int i) Function()',
1110+
labelDescription: null,
1111+
filterText: null,
1112+
detail: 'void Function(int i) Function()',
1113+
);
1114+
}
1115+
10821116
Future<void> test_imported_function_returnType_args() async {
10831117
newFile(fileAPath, '''
10841118
String a(String a, {String b}) {}

‎pkg/analysis_server/test/lsp/hover_test.dart‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,111 @@ Declared in `f` in *package:test/main.dart*.''';
552552
void [!abc!]^<T>(T a) {}
553553
''', contains('This is a function.'));
554554

555+
Future<void> test_functionType_assignedToVariable() async {
556+
var content = '''
557+
void f(int p1, int p2) {
558+
var [!^x!] = f;
559+
}
560+
''';
561+
562+
var expected =
563+
'''
564+
```dart
565+
void Function(int p1, int p2) x
566+
```
567+
Type: `void Function(int, int)`
568+
569+
Declared in `f` in *package:test/main.dart*.
570+
'''
571+
.trim();
572+
573+
await assertStringContents(content, equals(expected));
574+
}
575+
576+
Future<void> test_functionType_constructorTearOff() async {
577+
var content = '''
578+
class C {
579+
C(int p1, int p2) {}
580+
}
581+
void f() {
582+
C.[!^new!];
583+
}
584+
''';
585+
586+
var expected =
587+
'''
588+
```dart
589+
C(int p1, int p2)
590+
```
591+
Declared in `C` in *package:test/main.dart*.
592+
'''
593+
.trim();
594+
595+
await assertStringContents(content, equals(expected));
596+
}
597+
598+
Future<void> test_functionType_methodTearOff() async {
599+
var content = '''
600+
class C {
601+
f(int p1, int p2) {}
602+
}
603+
void f(C c) {
604+
c.[!^f!];
605+
}
606+
''';
607+
608+
var expected =
609+
'''
610+
```dart
611+
dynamic f(int p1, int p2)
612+
```
613+
Type: `dynamic Function(int, int)`
614+
615+
Declared in `C` in *package:test/main.dart*.
616+
'''
617+
.trim();
618+
619+
await assertStringContents(content, equals(expected));
620+
}
621+
622+
Future<void> test_functionType_parameter() async {
623+
var content = '''
624+
void f(String Function(int p1, int p2) [!^x!]) {}
625+
''';
626+
627+
var expected =
628+
'''
629+
```dart
630+
String Function(int p1, int p2) x
631+
```
632+
Type: `String Function(int, int)`
633+
634+
Declared in `f` in *package:test/main.dart*.
635+
'''
636+
.trim();
637+
638+
await assertStringContents(content, equals(expected));
639+
}
640+
641+
Future<void> test_functionType_typedef() async {
642+
failTestOnAnyErrorNotification = false;
643+
644+
var content = '''
645+
typedef [!^Foo!] = String Function(int p1, int p2);
646+
''';
647+
648+
var expected =
649+
'''
650+
```dart
651+
typedef Foo = String Function(int p1, int p2)
652+
```
653+
Declared in *package:test/main.dart*.
654+
'''
655+
.trim();
656+
657+
await assertStringContents(content, equals(expected));
658+
}
659+
555660
Future<void> test_hover_bad_position() async {
556661
await initialize();
557662
await openFile(mainFileUri, '');

‎pkg/analysis_server/test/lsp/signature_help_test.dart‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,108 @@ void f() {
630630
);
631631
}
632632

633+
Future<void> test_functionType_assignedToVariable() async {
634+
var content = '''
635+
void f(int p1, int p2) {
636+
var x = f;
637+
x(^);
638+
}
639+
''';
640+
var expectedLabel = 'x(int p1, int p2)';
641+
642+
await _expectSignature(
643+
content,
644+
expectedLabel,
645+
expectedParams: [
646+
ParameterInformation(label: 'int p1'),
647+
ParameterInformation(label: 'int p2'),
648+
],
649+
);
650+
}
651+
652+
Future<void> test_functionType_constructorTearOff() async {
653+
var content = '''
654+
class C {
655+
C(int p1, int p2) {}
656+
}
657+
void f() {
658+
var x = C.new;
659+
x(^);
660+
}
661+
''';
662+
var expectedLabel = 'x(int p1, int p2)';
663+
664+
await _expectSignature(
665+
content,
666+
expectedLabel,
667+
expectedParams: [
668+
ParameterInformation(label: 'int p1'),
669+
ParameterInformation(label: 'int p2'),
670+
],
671+
);
672+
}
673+
674+
Future<void> test_functionType_methodTearOff() async {
675+
var content = '''
676+
class C {
677+
f(int p1, int p2) {}
678+
}
679+
void f(C c) {
680+
var x = c.f;
681+
x(^);
682+
}
683+
''';
684+
var expectedLabel = 'x(int p1, int p2)';
685+
686+
await _expectSignature(
687+
content,
688+
expectedLabel,
689+
expectedParams: [
690+
ParameterInformation(label: 'int p1'),
691+
ParameterInformation(label: 'int p2'),
692+
],
693+
);
694+
}
695+
696+
Future<void> test_functionType_parameter() async {
697+
var content = '''
698+
void f(String Function(int p1, int p2) x) {
699+
x(^);
700+
}
701+
''';
702+
var expectedLabel = 'x(int p1, int p2)';
703+
704+
await _expectSignature(
705+
content,
706+
expectedLabel,
707+
expectedParams: [
708+
ParameterInformation(label: 'int p1'),
709+
ParameterInformation(label: 'int p2'),
710+
],
711+
);
712+
}
713+
714+
Future<void> test_functionType_typedef() async {
715+
var content = '''
716+
typedef Foo = String Function(int p1, int p2);
717+
718+
void f() {
719+
Foo x;
720+
x(^);
721+
}
722+
''';
723+
var expectedLabel = 'x(int p1, int p2)';
724+
725+
await _expectSignature(
726+
content,
727+
expectedLabel,
728+
expectedParams: [
729+
ParameterInformation(label: 'int p1'),
730+
ParameterInformation(label: 'int p2'),
731+
],
732+
);
733+
}
734+
633735
Future<void> test_manualTrigger_invalidLocation() async {
634736
// If the user invokes signature help, we should show it even if it's a
635737
// location where we wouldn't automatically trigger (for example in a string).

‎pkg/analyzer/api.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5506,7 +5506,7 @@ package:analyzer/dart/element/element.dart:
55065506
session (getter: AnalysisSession?)
55075507
sinceSdkVersion (getter: Version?)
55085508
accept (method: T? Function<T>(ElementVisitor2<T>))
5509-
displayString (method: String Function({bool multiline, bool preferTypeAlias}))
5509+
displayString (method: String Function({bool includePositionalParameterNames, bool multiline, bool preferTypeAlias}))
55105510
getExtendedDisplayName (method: String Function({String? shortName}))
55115511
isAccessibleIn (method: bool Function(LibraryElement))
55125512
isDeprecatedWithKind (method: bool Function(String))
@@ -6289,7 +6289,7 @@ package:analyzer/dart/element/type.dart:
62896289
accept (method: R Function<R>(TypeVisitor<R>))
62906290
acceptWithArgument (method: R Function<R, A>(TypeVisitorWithArgument<R, A>, A))
62916291
asInstanceOf (method: InterfaceType? Function(InterfaceElement))
6292-
getDisplayString (method: String Function({deprecated bool withNullability}))
6292+
getDisplayString (method: String Function({bool includePositionalParameterNames, deprecated bool withNullability}))
62936293
DynamicType (class extends Object implements DartType, abstract):
62946294
new (constructor: DynamicType Function())
62956295
FunctionType (class extends Object implements DartType, abstract):

‎pkg/analyzer/lib/dart/element/element.dart‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,17 @@ abstract class Element {
578578
/// by a type alias, then the name of the type alias will be used in the
579579
/// returned string rather than the name of the type being aliased.
580580
///
581+
/// If [includePositionalParameterNames] is `true`, names of positional
582+
/// parameters will be included in function types (in addition to named
583+
/// parameters which are always included).
584+
///
581585
/// Clients should not depend on the content of the returned value as it will
582586
/// be changed if doing so would improve the UX.
583-
String displayString({bool multiline = false, bool preferTypeAlias = false});
587+
String displayString({
588+
bool multiline = false,
589+
bool preferTypeAlias = false,
590+
bool includePositionalParameterNames = false,
591+
});
584592

585593
/// Returns a display name for the given element that includes the path to the
586594
/// compilation unit in which the type is defined. If [shortName] is `null`

‎pkg/analyzer/lib/dart/element/type.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ abstract class DartType {
176176
String getDisplayString({
177177
@Deprecated('Only non-nullable by default mode is supported')
178178
bool withNullability = true,
179+
bool includePositionalParameterNames = false,
179180
});
180181
}
181182

0 commit comments

Comments
 (0)