Skip to content

Use untpd.Tree instead of tpd.Tree for SelectionRangeProvider #22702

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Use untpd.Tree instead of tpd.Tree for SelectionRangeProvider
Co-authored-by: NPCRUS <npcrus@gmail.com>
Co-authored-by: Yummy-Yums <sailewalderbs@gmail.com>
  • Loading branch information
3 people committed May 5, 2025
commit 90ebd6f7f0b456859d5c40cf8bd6aaccb98a70d0
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import java.util as ju
import scala.jdk.CollectionConverters._
import scala.meta.pc.OffsetParams

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.ast.untpd.*
import dotty.tools.dotc.ast.NavigateAST
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.interactive.Interactive
import dotty.tools.dotc.interactive.InteractiveDriver
Expand Down Expand Up @@ -44,10 +45,13 @@ class SelectionRangeProvider(
val source = SourceFile.virtual(filePath.toString, text)
driver.run(uri, source)
val pos = driver.sourcePosition(param)
val path =
Interactive.pathTo(driver.openedTrees(uri), pos)(using ctx)
val unit = driver.compilationUnits(uri)

val bareRanges = path
val untpdPath: List[Tree] = NavigateAST
.pathTo(pos.span, List(unit.untpdTree), true).collect:
case untpdTree: Tree => untpdTree

val bareRanges = untpdPath
.flatMap(selectionRangesFromTree(pos))

val comments =
Expand Down Expand Up @@ -78,30 +82,38 @@ class SelectionRangeProvider(
end selectionRange

/** Given a tree, create a seq of [[SelectionRange]]s corresponding to that tree. */
private def selectionRangesFromTree(pos: SourcePosition)(tree: tpd.Tree)(using Context) =
private def selectionRangesFromTree(pos: SourcePosition)(tree: Tree)(using Context) =
def toSelectionRange(srcPos: SourcePosition) =
val selectionRange = new SelectionRange()
selectionRange.setRange(srcPos.toLsp)
selectionRange

val treeSelectionRange = toSelectionRange(tree.sourcePos)

def getArgsSpan(args: List[Tree]): Option[SourcePosition] =
args match
case Seq(param) => Some(param.sourcePos)
Copy link
Member

Choose a reason for hiding this comment

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

Is this needed? Won't it repeat the same selection? Can you add a test with a single param?

Copy link
Contributor

Choose a reason for hiding this comment

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

@rochala do you need help finishing this one?

case params @ Seq(head, tail*) =>
val srcPos = head.sourcePos
val lastSpan = tail.last.span
Some(SourcePosition(srcPos.source, srcPos.span union lastSpan, srcPos.outer))
case Seq() => None

tree match
case tpd.DefDef(name, paramss, tpt, rhs) =>
case DefDef(_, paramss, _, _) =>
// If source position is within a parameter list, add a selection range covering that whole list.
val selectedParams =
paramss
.iterator
.flatMap: // parameter list to a sourcePosition covering the whole list
case Seq(param) => Some(param.sourcePos)
case params @ Seq(head, tail*) =>
val srcPos = head.sourcePos
val lastSpan = tail.last.span
Some(SourcePosition(srcPos.source, srcPos.span union lastSpan, srcPos.outer))
case Seq() => None
.find(_.contains(pos))
.map(toSelectionRange)
val selectedParams = paramss
.flatMap(getArgsSpan) // parameter list to a sourcePosition covering the whole list
.find(_.contains(pos))
.map(toSelectionRange)
selectedParams ++ Seq(treeSelectionRange)

case Function(args, body) =>
val allArgs = getArgsSpan(args)
.find(_.contains(pos))
.map(toSelectionRange)

allArgs ++ Seq(treeSelectionRange)
case _ => Seq(treeSelectionRange)

private def setParent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
| b <- Some(2)
| } yield a + b
|}""".stripMargin,
"""|object Main extends App {
| val total = for {
| >>region>>a <- Some(1)<<region<<
| b <- Some(2)
| } yield a + b
|}""".stripMargin,
"""|object Main extends App {
| val total = >>region>>for {
| a <- Some(1)
Expand Down Expand Up @@ -102,7 +108,7 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
)
)

@Test def `function params` =
@Test def `function-params-1` =
check(
"""|object Main extends App {
| def func(a@@: Int, b: Int) =
Expand All @@ -124,6 +130,32 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
)
)

@Test def `function-params-2` =
check(
"""|object Main extends App {
| val func = (a@@: Int, b: Int) =>
| a + b
|}""".stripMargin,
List[String](
"""|object Main extends App {
| val func = (>>region>>a: Int<<region<<, b: Int) =>
| a + b
|}""".stripMargin,
"""|object Main extends App {
| val func = (>>region>>a: Int, b: Int<<region<<) =>
| a + b
|}""".stripMargin,
"""|object Main extends App {
| val func = >>region>>(a: Int, b: Int) =>
| a + b<<region<<
|}""".stripMargin,
"""|object Main extends App {
| >>region>>val func = (a: Int, b: Int) =>
| a + b<<region<<
|}""".stripMargin
)
)

@Test def `def - type params` =
check(
"object Main extends App { def foo[Type@@ <: T1, B](hi: Int, b: Int, c:Int) = ??? }",
Expand All @@ -133,3 +165,25 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
"object Main extends App { >>region>>def foo[Type <: T1, B](hi: Int, b: Int, c:Int) = ???<<region<< }"
)
)


@Test def `arithmetic` =
check(
"""|object Main extends App {
| def x = 12 * (34 + 5@@6)
|}""".stripMargin,
List[String](
"""|object Main extends App {
| def x = 12 * (34 + >>region>>56<<region<<)
|}""".stripMargin,
"""|object Main extends App {
| def x = 12 * (>>region>>34 + 56<<region<<)
|}""".stripMargin,
"""|object Main extends App {
| def x = 12 * >>region>>(34 + 56)<<region<<
|}""".stripMargin,
"""|object Main extends App {
| def x = >>region>>12 * (34 + 56)<<region<<
|}""".stripMargin
)
)
4 changes: 4 additions & 0 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,10 @@ object Build {
ivyConfigurations += SourceDeps.hide,
transitiveClassifiers := Seq("sources"),
scalacOptions ++= Seq("-source", "3.3"), // To avoid fatal migration warnings
publishLocal := publishLocal.dependsOn( // It is best to publish all together. It is not rare to make changes in both compiler / presentation compiler and it can get misaligned
`scala3-compiler-bootstrapped` / publishLocal,
`scala3-library-bootstrapped` / publishLocal,
).value,
Compile / scalacOptions ++= Seq("-Yexplicit-nulls", "-Wsafe-init"),
Compile / sourceGenerators += Def.task {
val s = streams.value
Expand Down