Skip to content

Commit 0dc2e28

Browse files
rochalaNPCRUSYummy-Yums
committed
Use untpd.Tree instead of tpd.Tree for SelectionRangeProvider
Co-authored-by: NPCRUS <[email protected]> Co-authored-by: Yummy-Yums <[email protected]>
1 parent 91985db commit 0dc2e28

File tree

3 files changed

+89
-19
lines changed

3 files changed

+89
-19
lines changed

presentation-compiler/src/main/dotty/tools/pc/SelectionRangeProvider.scala

+30-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import java.util as ju
66
import scala.jdk.CollectionConverters._
77
import scala.meta.pc.OffsetParams
88

9-
import dotty.tools.dotc.ast.tpd
9+
import dotty.tools.dotc.ast.untpd.*
10+
import dotty.tools.dotc.ast.NavigateAST
1011
import dotty.tools.dotc.core.Contexts.Context
1112
import dotty.tools.dotc.interactive.Interactive
1213
import dotty.tools.dotc.interactive.InteractiveDriver
@@ -44,10 +45,13 @@ class SelectionRangeProvider(
4445
val source = SourceFile.virtual(filePath.toString, text)
4546
driver.run(uri, source)
4647
val pos = driver.sourcePosition(param)
47-
val path =
48-
Interactive.pathTo(driver.openedTrees(uri), pos)(using ctx)
48+
val unit = driver.compilationUnits(uri)
4949

50-
val bareRanges = path
50+
val untpdPath: List[Tree] = NavigateAST
51+
.pathTo(pos.span, List(unit.untpdTree), true).collect:
52+
case untpdTree: Tree => untpdTree
53+
54+
val bareRanges = untpdPath
5155
.flatMap(selectionRangesFromTree(pos))
5256

5357
val comments =
@@ -78,30 +82,38 @@ class SelectionRangeProvider(
7882
end selectionRange
7983

8084
/** Given a tree, create a seq of [[SelectionRange]]s corresponding to that tree. */
81-
private def selectionRangesFromTree(pos: SourcePosition)(tree: tpd.Tree)(using Context) =
85+
private def selectionRangesFromTree(pos: SourcePosition)(tree: Tree)(using Context) =
8286
def toSelectionRange(srcPos: SourcePosition) =
8387
val selectionRange = new SelectionRange()
8488
selectionRange.setRange(srcPos.toLsp)
8589
selectionRange
8690

8791
val treeSelectionRange = toSelectionRange(tree.sourcePos)
8892

93+
def getArgsSpan(args: List[Tree]): Option[SourcePosition] =
94+
args match
95+
case Seq(param) => Some(param.sourcePos)
96+
case params @ Seq(head, tail*) =>
97+
val srcPos = head.sourcePos
98+
val lastSpan = tail.last.span
99+
Some(SourcePosition(srcPos.source, srcPos.span union lastSpan, srcPos.outer))
100+
case Seq() => None
101+
89102
tree match
90-
case tpd.DefDef(name, paramss, tpt, rhs) =>
103+
case DefDef(_, paramss, _, _) =>
91104
// If source position is within a parameter list, add a selection range covering that whole list.
92-
val selectedParams =
93-
paramss
94-
.iterator
95-
.flatMap: // parameter list to a sourcePosition covering the whole list
96-
case Seq(param) => Some(param.sourcePos)
97-
case params @ Seq(head, tail*) =>
98-
val srcPos = head.sourcePos
99-
val lastSpan = tail.last.span
100-
Some(SourcePosition(srcPos.source, srcPos.span union lastSpan, srcPos.outer))
101-
case Seq() => None
102-
.find(_.contains(pos))
103-
.map(toSelectionRange)
105+
val selectedParams = paramss
106+
.flatMap(getArgsSpan) // parameter list to a sourcePosition covering the whole list
107+
.find(_.contains(pos))
108+
.map(toSelectionRange)
104109
selectedParams ++ Seq(treeSelectionRange)
110+
111+
case Function(args, body) =>
112+
val allArgs = getArgsSpan(args)
113+
.find(_.contains(pos))
114+
.map(toSelectionRange)
115+
116+
allArgs ++ Seq(treeSelectionRange)
105117
case _ => Seq(treeSelectionRange)
106118

107119
private def setParent(

presentation-compiler/test/dotty/tools/pc/tests/SelectionRangeSuite.scala

+55-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
7575
| b <- Some(2)
7676
| } yield a + b
7777
|}""".stripMargin,
78+
"""|object Main extends App {
79+
| val total = for {
80+
| >>region>>a <- Some(1)<<region<<
81+
| b <- Some(2)
82+
| } yield a + b
83+
|}""".stripMargin,
7884
"""|object Main extends App {
7985
| val total = >>region>>for {
8086
| a <- Some(1)
@@ -102,7 +108,7 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
102108
)
103109
)
104110

105-
@Test def `function params` =
111+
@Test def `function-params-1` =
106112
check(
107113
"""|object Main extends App {
108114
| def func(a@@: Int, b: Int) =
@@ -124,6 +130,32 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
124130
)
125131
)
126132

133+
@Test def `function-params-2` =
134+
check(
135+
"""|object Main extends App {
136+
| val func = (a@@: Int, b: Int) =>
137+
| a + b
138+
|}""".stripMargin,
139+
List[String](
140+
"""|object Main extends App {
141+
| val func = (>>region>>a: Int<<region<<, b: Int) =>
142+
| a + b
143+
|}""".stripMargin,
144+
"""|object Main extends App {
145+
| val func = (>>region>>a: Int, b: Int<<region<<) =>
146+
| a + b
147+
|}""".stripMargin,
148+
"""|object Main extends App {
149+
| val func = >>region>>(a: Int, b: Int) =>
150+
| a + b<<region<<
151+
|}""".stripMargin,
152+
"""|object Main extends App {
153+
| >>region>>val func = (a: Int, b: Int) =>
154+
| a + b<<region<<
155+
|}""".stripMargin
156+
)
157+
)
158+
127159
@Test def `def - type params` =
128160
check(
129161
"object Main extends App { def foo[Type@@ <: T1, B](hi: Int, b: Int, c:Int) = ??? }",
@@ -133,3 +165,25 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
133165
"object Main extends App { >>region>>def foo[Type <: T1, B](hi: Int, b: Int, c:Int) = ???<<region<< }"
134166
)
135167
)
168+
169+
170+
@Test def `arithmetic` =
171+
check(
172+
"""|object Main extends App {
173+
| def x = 12 * (34 + 5@@6)
174+
|}""".stripMargin,
175+
List[String](
176+
"""|object Main extends App {
177+
| def x = 12 * (34 + >>region>>56<<region<<)
178+
|}""".stripMargin,
179+
"""|object Main extends App {
180+
| def x = 12 * (>>region>>34 + 56<<region<<)
181+
|}""".stripMargin,
182+
"""|object Main extends App {
183+
| def x = 12 * >>region>>(34 + 56)<<region<<
184+
|}""".stripMargin,
185+
"""|object Main extends App {
186+
| def x = >>region>>12 * (34 + 56)<<region<<
187+
|}""".stripMargin
188+
)
189+
)

project/Build.scala

+4
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,10 @@ object Build {
14971497
ivyConfigurations += SourceDeps.hide,
14981498
transitiveClassifiers := Seq("sources"),
14991499
scalacOptions ++= Seq("-source", "3.3"), // To avoid fatal migration warnings
1500+
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
1501+
`scala3-compiler-bootstrapped` / publishLocal,
1502+
`scala3-library-bootstrapped` / publishLocal,
1503+
).value,
15001504
Compile / scalacOptions ++= Seq("-Yexplicit-nulls", "-Wsafe-init"),
15011505
Compile / sourceGenerators += Def.task {
15021506
val s = streams.value

0 commit comments

Comments
 (0)