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

fix: workarounds for incorrect positions in multiple variable declarations #7019

Closed
wants to merge 4 commits into from
Closed
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
34 changes: 24 additions & 10 deletions mtags/src/main/scala-2/scala/meta/internal/pc/HoverProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ class HoverProvider(
)
}

def correctPosDef(df: MemberDef): Boolean =
(df.namePosition.includes(pos) || pos.includes(
df.namePosition
)) && df.symbol != null

tree match {
case i @ Import(_, _) =>
for {
Expand Down Expand Up @@ -136,11 +141,7 @@ class HoverProvider(
)
// Def, val or val definition, example `val x: Int = 1`
// Matches only if the cursor is over the definition name.
case v: ValOrDefDef
if (v.namePosition
.includes(pos) || pos.includes(
v.namePosition
)) && v.symbol != null =>
case v: ValOrDefDef if correctPosDef(v) =>
val symbol = (v.symbol.getter: Symbol) match {
case NoSymbol => v.symbol
case getter => getter
Expand Down Expand Up @@ -178,11 +179,7 @@ class HoverProvider(
Some(report)
)
// Class, object, type definitions, matches only if the cursor is over the definition name.
case t: MemberDef
if (t.namePosition
.includes(pos) || pos.includes(
t.namePosition
)) && t.symbol != null =>
case t: MemberDef if correctPosDef(t) =>
val symbol = tree.symbol
val tpe = seenFromType(tree, symbol)
toHover(
Expand All @@ -194,6 +191,23 @@ class HoverProvider(
range = t.namePosition,
Some(report)
)
case t @ Template(parents, self, body) =>
t.body.collectFirst {
case v: ValDef if correctPosDef(v) =>
val symbol = (v.symbol.getter: Symbol) match {
case NoSymbol => v.symbol
case getter => getter
}
toHover(
symbol,
v.symbol.keyString,
symbol.info,
symbol.info,
pos,
v.pos,
Some(report)
)
}.flatten
case _ =>
// Don't show hover for non-identifiers.
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ trait PcCollector[T] { self: WithCompilationUnit =>
def isCorrectPos(t: Tree): Boolean =
t.pos.isRange || (t.pos.isDefined && allowZeroExtentImplicits && t.symbol != null && t.symbol.isImplicit)

def correctNamePos(t: MemberDef): Boolean =
t.pos.isDefined && t.namePosition.isRange

def traverseSought(
filter: Tree => Boolean,
soughtFilter: (Symbol => Boolean) => Boolean
Expand Down Expand Up @@ -172,7 +175,8 @@ trait PcCollector[T] { self: WithCompilationUnit =>
* class <<Foo>> = ???
* etc.
*/
case df: MemberDef if isCorrectPos(df) && filter(df) =>
case df: MemberDef
if (isCorrectPos(df) || correctNamePos(df)) && filter(df) =>
(annotationChildren(df) ++ df.children).foldLeft({
val t = collect(
df,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,13 @@ object MetalsInteractive:
*/
else if head.isInstanceOf[TypeTree] then
enclosingSymbolsWithExpressionType(tail, pos, indexed)
else Nil
else
path match
case (_: ValDef) :: (t : Template) :: _ =>
t.body.collectFirst {
case d: ValDef if d.nameSpan.contains(pos.span) => List((d.symbol, d.symbol.info))
}.getOrElse(Nil)
case _ => Nil
else
val recovered = recoverError(head, indexed)
if recovered.isEmpty then
Expand Down
11 changes: 11 additions & 0 deletions tests/cross/src/test/scala/tests/hover/HoverTermSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -773,4 +773,15 @@ class HoverTermSuite extends BaseHoverSuite {
)
)

check(
"i7012",
"""|object O {
| val x@@x, yy, zz = 1
|}
|""".stripMargin,
"""|
|val xx: Int
|""".stripMargin.hover
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,12 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite {
)
)

check(
"i7012",
"""|object <<O>>/*class*/ {
| val <<xx>>/*variable,definition,readonly*/, <<yy>>/*variable,definition,readonly*/, <<zz>>/*variable,definition,readonly*/ = 1
|}
|""".stripMargin
)

}
Loading