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

bugfix: Fix select.nameSpan for expressions in parentheses #4907

Merged
merged 1 commit into from
Jan 28, 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
18 changes: 16 additions & 2 deletions mtags/src/main/scala-3/scala/meta/internal/pc/PcCollector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ abstract class PcCollector[T](driver: InteractiveDriver, params: OffsetParams):
/* simple selector:
* object.val@@ue
*/
case (sel: Select) :: _ if sel.nameSpan.contains(pos.span) =>
case (sel: Select) :: _ if selectNameSpan(sel).contains(pos.span) =>
Some(symbolAlternatives(sel.symbol), pos.withSpan(sel.nameSpan))
/* named argument:
* foo(nam@@e = "123")
Expand Down Expand Up @@ -271,7 +271,7 @@ abstract class PcCollector[T](driver: InteractiveDriver, params: OffsetParams):
if soughtOrOverride(sel.symbol) && !sel.span.isZeroExtent =>
occurences + collect(
sel,
pos.withSpan(sel.nameSpan),
pos.withSpan(selectNameSpan(sel)),
)
/* all definitions:
* def <<foo>> = ???
Expand Down Expand Up @@ -383,6 +383,20 @@ abstract class PcCollector[T](driver: InteractiveDriver, params: OffsetParams):
trees.collect { case t: Tree =>
t
}

// NOTE: Connected to https://github.com/lampepfl/dotty/issues/16771
// `sel.nameSpan` is calculated incorrectly in (1 + 2).toString
// See test DocumentHighlightSuite.select-parentheses
private def selectNameSpan(sel: Select)(using Context): Span =
val span = sel.span
if span.exists then
val point = span.point
if sel.name.toTermName == nme.ERROR then Span(point)
else if sel.qualifier.span.start > span.point then // right associative
val realName = sel.name.stripModuleClassSuffix.lastPart
Span(span.start, span.start + realName.length, point)
else Span(point, span.end, point)
else span
end PcCollector

object PcCollector:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,4 +699,17 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite {
|}""".stripMargin,
)

check(
"select-parentheses",
"""|object Main {
| val a = (1 + 2 + 3).<<toStr@@ing>>
|}""".stripMargin,
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add:

  check(
    "select-parentheses2",
    """|object Main {
       |  val a = (1 + 2 + 3) <<:@@:>> Nil
       |}""".stripMargin,
  )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was worth adding this test, now I know it doesn't work for scala 2 😅 . I will take a look on that in a follow up, the issue might be a little bit more complicated, because right associative operator is translated to block with synthetic symbol $rightassoc1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uch, yeah let's do a follow up and ignore for now

check(
"select-parentheses2".tag(IgnoreScala2),
"""|object Main {
| val a = (1 + 2 + 3) <<:@@:>> Nil
|}""".stripMargin,
)
}