Skip to content

Commit

Permalink
Fix hover on member defs
Browse files Browse the repository at this point in the history
  • Loading branch information
milesziemer committed Dec 18, 2024
1 parent a95e6c7 commit 722865a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,4 @@ private static String serializeShape(Model model, Shape shape) {
System.lineSeparator() + System.lineSeparator()), System.lineSeparator());
return serializedShape;
}

}
17 changes: 12 additions & 5 deletions src/main/java/software/amazon/smithy/lsp/language/IdlPosition.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
* Represents different kinds of positions within an IDL file.
*/
sealed interface IdlPosition {
default boolean isEasyShapeReference() {
/**
* @return Whether the token at this position is definitely a reference
* to a root/top-level shape.
*/
default boolean isRootShapeReference() {
return switch (this) {
case TraitId ignored -> true;
case MemberTarget ignored -> true;
Expand All @@ -25,6 +29,9 @@ default boolean isEasyShapeReference() {
};
}

/**
* @return The view this position is within.
*/
StatementView view();

record TraitId(StatementView view) implements IdlPosition {}
Expand Down Expand Up @@ -53,7 +60,7 @@ record MetadataValue(StatementView view, Syntax.Statement.Metadata metadata) imp

record StatementKeyword(StatementView view) implements IdlPosition {}

record MemberName(StatementView view) implements IdlPosition {}
record MemberName(StatementView view, String name) implements IdlPosition {}

record ElidedMember(StatementView view) implements IdlPosition {}

Expand Down Expand Up @@ -92,7 +99,7 @@ static IdlPosition of(StatementView view) {
when m.inTarget(documentIndex) -> new IdlPosition.MemberTarget(view);

case Syntax.Statement.MemberDef m
when m.name().isIn(documentIndex) -> new IdlPosition.MemberName(view);
when m.name().isIn(documentIndex) -> new IdlPosition.MemberName(view, m.name().stringValue());

case Syntax.Statement.NodeMemberDef m
when m.inValue(documentIndex) -> new IdlPosition.NodeMemberTarget(view, m);
Expand All @@ -109,9 +116,9 @@ static IdlPosition of(StatementView view) {

case Syntax.Statement.ShapeDef ignored -> new IdlPosition.ShapeDef(view);

case Syntax.Statement.NodeMemberDef ignored -> new IdlPosition.MemberName(view);
case Syntax.Statement.NodeMemberDef m -> new IdlPosition.MemberName(view, m.name().stringValue());

case Syntax.Statement.Block ignored -> new IdlPosition.MemberName(view);
case Syntax.Statement.Block ignored -> new IdlPosition.MemberName(view, "");

case Syntax.Statement.ForResource ignored -> new IdlPosition.ForResource(view);

Expand Down
13 changes: 11 additions & 2 deletions src/main/java/software/amazon/smithy/lsp/language/ShapeSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private static Optional<ShapeId> tryFrom(String id) {

private static Optional<ShapeId> tryFromParts(String namespace, String name) {
try {
return Optional.of(ShapeId.fromParts(namespace, name));
return Optional.of(ShapeId.fromRelative(namespace, name));
} catch (ShapeIdSyntaxException ignored) {
return Optional.empty();
}
Expand Down Expand Up @@ -118,7 +118,16 @@ static Optional<? extends Shape> findShapeDefinition(IdlPosition idlPosition, Do
case IdlPosition.ElidedMember elidedMember ->
findElidedMemberParent(elidedMember, id, model);

case IdlPosition pos when pos.isEasyShapeReference() ->
case IdlPosition.MemberName memberName -> {
var parentDef = memberName.view().nearestShapeDefBefore();
if (parentDef == null) {
yield Optional.empty();
}
var relativeId = parentDef.shapeName().stringValue() + "$" + memberName.name();
yield findShape(memberName.view().parseResult(), relativeId, model);
}

case IdlPosition pos when pos.isRootShapeReference() ->
findShape(pos.view().parseResult(), id.copyIdValue(), model);

default -> Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ public void absoluteShapeId() {
assertThat(hovers, contains(containsString("string String")));
}

@Test
public void selfShapeDefinition() {
TextWithPositions text = TextWithPositions.from("""
$version: "2"
namespace com.foo
structure %Foo {}
""");
List<String> hovers = getHovers(text);

assertThat(hovers, contains(containsString("structure Foo")));
}

@Test
public void selfMemberDefinition() {
TextWithPositions text = TextWithPositions.from("""
$version: "2"
namespace com.foo
structure Foo {
%bar: String
}
""");
List<String> hovers = getHovers(text);

assertThat(hovers, contains(containsString("bar: String")));
}

private static List<String> getHovers(TextWithPositions text) {
return getHovers(text.text(), text.positions());
}
Expand Down

0 comments on commit 722865a

Please sign in to comment.