Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check parent docs even when class is indexed #5382

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 40 additions & 34 deletions mtags/src/main/scala/scala/meta/internal/metals/Docstrings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,52 @@ class Docstrings(index: GlobalSymbolIndex) {
symbol: String,
parents: ParentSymbols
): Optional[SymbolDocumentation] = {
cache.get(symbol) match {
val result = cache.get(symbol) match {
case Some(value) =>
if (value == EmptySymbolDocumentation) Optional.empty()
else Optional.of(value)
if (value == EmptySymbolDocumentation) None
else Some(value)
case None =>
indexSymbol(symbol)
val result = cache.get(symbol)
val resultWithDocs = result match {
case None =>
cache(symbol) = EmptySymbolDocumentation
result
/* Fall back to parent scaladocs if nothing is specified for the current symbol
* This way we also cache the result in order not to calculate parents again.
*/
case Some(value: MetalsSymbolDocumentation)
if value.docstring.isEmpty() =>
parents
.parents()
.asScala
.flatMap { s =>
if (cache.contains(s)) cache.get(s)
else {
indexSymbol(s)
cache.get(s)
}
}
.find(_.docstring().nonEmpty)
.fold {
result
} { withDocs =>
val updated = value.copy(docstring = withDocs.docstring())
cache(symbol) = updated
Some(updated)
}
case _ =>
result
if (result.isEmpty)
cache(symbol) = EmptySymbolDocumentation
result
}
/* Fall back to parent javadocs/scaladocs if nothing is specified for the current symbol
* This way we also cache the result in order not to calculate parents again.
*/
val resultWithParentDocs = result match {
case Some(value: MetalsSymbolDocumentation)
if value.docstring.isEmpty() =>
Some(parentDocumentation(symbol, value, parents))
case _ => result
}
Optional.ofNullable(resultWithParentDocs.orNull)
}

def parentDocumentation(
symbol: String,
docs: MetalsSymbolDocumentation,
parents: ParentSymbols
): SymbolDocumentation = {
parents
.parents()
.asScala
.flatMap { s =>
if (cache.contains(s)) cache.get(s)
else {
indexSymbol(s)
cache.get(s)
}
Optional.ofNullable(resultWithDocs.orNull)
}
}
.find(_.docstring().nonEmpty)
.fold {
docs
} { withDocs =>
val updated = docs.copy(docstring = withDocs.docstring())
cache(symbol) = updated
updated
}
}

/**
Expand Down
77 changes: 77 additions & 0 deletions tests/unit/src/test/scala/tests/HoverLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,83 @@ class HoverLspSuite extends BaseLspSuite("hover-") with TestHovers {
} yield ()
}

test("docstrings java parentdoc".tag(FlakyWindows)) {
for {
_ <- initialize(
"""/metals.json
|{"a":{}}
|/a/src/main/java/a/Foo.java
|package a;
|public class Foo {
| public static class Def {
| /**
| * test docs
| */
| public void foo(int x) {}
| }
|
| public static class ChildDef extends Def {
| @Override
| public void foo(int x) {}
| }
| void test() {
| new ChildDef().foo(1);
| }
|}
""".stripMargin
)
_ <- server.assertHover(
"a/src/main/java/a/Foo.java",
"""package a;
|public class Foo {
| public static class Def {
| /**
| * test docs
| */
| public void foo(int x) {}
| }
|
| public static class ChildDef extends Def {
| @Override
| public void foo(int x) {}
| }
| void test() {
| new Chil@@dDef().foo(1);
| }
|}""".stripMargin,
"""```java
|public ChildDef()
|```
|""".stripMargin.hover,
)
_ <- server.assertHover(
"a/src/main/java/a/Foo.java",
"""package a;
|public class Foo {
| public static class Def {
| /**
| * test docs
| */
| public void foo(int x) {}
| }
|
| public static class ChildDef extends Def {
| @Override
| public void foo(int x) {}
| }
| void test() {
| new ChildDef().fo@@o(1);
| }
|}""".stripMargin,
"""```java
|public void foo(int x)
|```
|test docs
|""".stripMargin.hover,
)
} yield ()
}

test("dependencies".tag(FlakyWindows), withoutVirtualDocs = true) {
for {
_ <- initialize(
Expand Down