Skip to content

Commit 1956e05

Browse files
authored
Merge pull request #437 from scala/backport-lts-3.3-23257
Backport "add selection ranges for more names" to 3.3 LTS
2 parents 01fa3f0 + 3361014 commit 1956e05

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class SelectionRangeProvider(driver: InteractiveDriver, params: ju.List[OffsetPa
8585
selectionRange.setRange(srcPos.toLsp)
8686
selectionRange
8787

88+
def maybeToSelectionRange(srcPos: SourcePosition): Option[SelectionRange] =
89+
if srcPos.contains(pos) then Some(toSelectionRange(srcPos)) else None
90+
8891
val treeSelectionRange = Seq(toSelectionRange(tree.sourcePos))
8992

9093
def allArgsSelectionRange(args: List[Tree]): Option[SelectionRange] =
@@ -94,16 +97,25 @@ class SelectionRangeProvider(driver: InteractiveDriver, params: ju.List[OffsetPa
9497
val srcPos = list.head.sourcePos
9598
val lastSpan = list.last.span
9699
val allArgsSrcPos = SourcePosition(srcPos.source, srcPos.span union lastSpan, srcPos.outer)
97-
if allArgsSrcPos.contains(pos) then Some(toSelectionRange(allArgsSrcPos))
98-
else None
99-
100-
tree match
101-
case DefDef(_, paramss, _, _) => paramss.flatMap(allArgsSelectionRange) ++ treeSelectionRange
102-
case Apply(_, args) => allArgsSelectionRange(args) ++ treeSelectionRange
103-
case TypeApply(_, args) => allArgsSelectionRange(args) ++ treeSelectionRange
104-
case UnApply(_, _, pattern) => allArgsSelectionRange(pattern) ++ treeSelectionRange
105-
case Function(args, body) => allArgsSelectionRange(args) ++ treeSelectionRange
106-
case _ => treeSelectionRange
100+
maybeToSelectionRange(allArgsSrcPos)
101+
102+
val allSelectionRanges: Iterable[SelectionRange] = tree match
103+
case vdef @ ValDef(_, _, _) =>
104+
maybeToSelectionRange(vdef.namePos)
105+
case tdef @ TypeDef(_, _) =>
106+
maybeToSelectionRange(tdef.namePos)
107+
case mdef @ ModuleDef(_, _) =>
108+
maybeToSelectionRange(mdef.namePos)
109+
case DefDef(_, paramss, _, _) =>
110+
paramss.flatMap(allArgsSelectionRange)
111+
case Apply(_, args) =>
112+
allArgsSelectionRange(args)
113+
case TypeApply(_, args) =>
114+
allArgsSelectionRange(args)
115+
case Function(args, _) =>
116+
allArgsSelectionRange(args)
117+
case _ => Seq.empty
118+
allSelectionRanges ++ treeSelectionRange
107119

108120
private def setParent(
109121
child: SelectionRange,

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
115115
| a + b
116116
|}""".stripMargin,
117117
List[String](
118+
"""|object Main extends App {
119+
| def func(>>region>>a<<region<<: Int, b: Int) =
120+
| a + b
121+
|}""".stripMargin,
118122
"""|object Main extends App {
119123
| def func(>>region>>a: Int<<region<<, b: Int) =
120124
| a + b
@@ -137,6 +141,10 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
137141
| a + b
138142
|}""".stripMargin,
139143
List[String](
144+
"""|object Main extends App {
145+
| val func = (>>region>>a<<region<<: Int, b: Int) =>
146+
| a + b
147+
|}""".stripMargin,
140148
"""|object Main extends App {
141149
| val func = (>>region>>a: Int<<region<<, b: Int) =>
142150
| a + b
@@ -160,6 +168,7 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
160168
check(
161169
"object Main extends App { def foo[Type@@ <: T1, B](hi: Int, b: Int, c:Int) = ??? }",
162170
List(
171+
"object Main extends App { def foo[>>region>>Type<<region<< <: T1, B](hi: Int, b: Int, c:Int) = ??? }",
163172
"object Main extends App { def foo[>>region>>Type <: T1<<region<<, B](hi: Int, b: Int, c:Int) = ??? }",
164173
"object Main extends App { def foo[>>region>>Type <: T1, B<<region<<](hi: Int, b: Int, c:Int) = ??? }",
165174
"object Main extends App { >>region>>def foo[Type <: T1, B](hi: Int, b: Int, c:Int) = ???<<region<< }"
@@ -192,6 +201,7 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
192201
check(
193202
"val hello = (aaa: Int, bb@@b: Int, ccc: Int) => ???",
194203
List(
204+
"val hello = (aaa: Int, >>region>>bbb<<region<<: Int, ccc: Int) => ???",
195205
"val hello = (aaa: Int, >>region>>bbb: Int<<region<<, ccc: Int) => ???",
196206
"val hello = (>>region>>aaa: Int, bbb: Int, ccc: Int<<region<<) => ???",
197207
"val hello = >>region>>(aaa: Int, bbb: Int, ccc: Int) => ???<<region<<",
@@ -203,6 +213,7 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
203213
check(
204214
"def hello(aaa: Int, bb@@b: Int, ccc: Int) = ???",
205215
List(
216+
"def hello(aaa: Int, >>region>>bbb<<region<<: Int, ccc: Int) = ???",
206217
"def hello(aaa: Int, >>region>>bbb: Int<<region<<, ccc: Int) = ???",
207218
"def hello(>>region>>aaa: Int, bbb: Int, ccc: Int<<region<<) = ???",
208219
">>region>>def hello(aaa: Int, bbb: Int, ccc: Int) = ???<<region<<",
@@ -252,3 +263,21 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
252263
">>region>>def hello = List(222)<<region<<",
253264
)
254265
)
266+
267+
@Test def `constructor-argument` =
268+
check(
269+
"class Foo(val ar@@g: Int)",
270+
List(
271+
"""class Foo(val >>region>>arg<<region<<: Int)""",
272+
"""class Foo(>>region>>val arg: Int<<region<<)""",
273+
)
274+
)
275+
276+
@Test def `object-backticked` =
277+
check(
278+
"object `Foo B@@ar Baz` extends SomeTrait",
279+
List(
280+
"""object `>>region>>Foo Bar Baz<<region<<` extends SomeTrait""",
281+
""">>region>>object `Foo Bar Baz` extends SomeTrait<<region<<""",
282+
)
283+
)

0 commit comments

Comments
 (0)