[analysis_server] Fix hover on constructor name declarations

Change-Id: I6254a8a8d82a1f2b12e8fa3589fbe114d9945af6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/263401
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Danny Tuppeny 2022-10-10 16:43:57 +00:00 committed by Commit Queue
parent cf111fdcfb
commit 172bcc33dd
2 changed files with 51 additions and 9 deletions

View file

@ -42,6 +42,8 @@ class DartUnitHoverComputer {
locationEntity = node.name;
} else if (node is MethodDeclaration) {
locationEntity = node.name;
} else if (node is ConstructorDeclaration) {
locationEntity = node.name ?? node.returnType;
} else if (node is VariableDeclaration) {
locationEntity = node.name;
}
@ -58,22 +60,34 @@ class DartUnitHoverComputer {
} else if (parent is ConstructorName &&
grandParent is InstanceCreationExpression) {
node = grandParent;
} else if (node is SimpleIdentifier &&
parent is ConstructorDeclaration &&
parent.name != null) {
node = parent;
}
if (node != null &&
(node is CompilationUnitMember ||
node is Expression ||
node is FormalParameter ||
node is MethodDeclaration ||
node is ConstructorDeclaration ||
node is VariableDeclaration)) {
// For constructor calls the whole expression is selected (above) but this
// results in the range covering the whole call so narrow it to just the
// ConstructorName.
var hover = node is InstanceCreationExpression
? HoverInformation(
node.constructorName.offset,
node.constructorName.length,
)
: HoverInformation(locationEntity.offset, locationEntity.length);
// For constructors, the location should cover the type name and
// constructor name (for both calls and declarations).
HoverInformation hover;
if (node is InstanceCreationExpression) {
hover = HoverInformation(
node.constructorName.offset,
node.constructorName.length,
);
} else if (node is ConstructorDeclaration) {
var offset = node.returnType.offset;
var end = node.name?.end ?? node.returnType.end;
var length = end - node.returnType.offset;
hover = HoverInformation(offset, length);
} else {
hover = HoverInformation(locationEntity.offset, locationEntity.length);
}
// element
var element = ElementLocator.locate(node);
if (element != null) {

View file

@ -97,6 +97,34 @@ void f() {
}
}
Future<void> test_class_constructor_named_declaration() async {
newFile(testFilePath, '''
library my.library;
class A {
/// my doc
A.named() {}
}
''');
void onConstructor(HoverInformation hover) {
// range
expect(hover.offset, findOffset('A.named'));
expect(hover.length, 'A.named'.length);
// element
expect(hover.dartdoc, 'my doc');
expect(hover.elementDescription, 'A A.named()');
expect(hover.elementKind, 'constructor');
}
{
var hover = await prepareHover('A.');
onConstructor(hover);
}
{
var hover = await prepareHover('named()');
onConstructor(hover);
}
}
Future<void> test_class_constructor_noKeyword_const() async {
newFile(testFilePath, '''
library my.library;