Skip to content

Commit af12fac

Browse files
committed
Refactor
1 parent 90ebd6f commit af12fac

File tree

2 files changed

+82
-28
lines changed

2 files changed

+82
-28
lines changed

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

+16-27
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ import org.eclipse.lsp4j.SelectionRange
2424
* @param compiler Metals Global presentation compiler wrapper.
2525
* @param params offset params converted from the selectionRange params.
2626
*/
27-
class SelectionRangeProvider(
28-
driver: InteractiveDriver,
29-
params: ju.List[OffsetParams]
30-
):
27+
class SelectionRangeProvider(driver: InteractiveDriver, params: ju.List[OffsetParams]):
3128

3229
/**
3330
* Get the seletion ranges for the provider params
@@ -88,33 +85,25 @@ class SelectionRangeProvider(
8885
selectionRange.setRange(srcPos.toLsp)
8986
selectionRange
9087

91-
val treeSelectionRange = toSelectionRange(tree.sourcePos)
88+
val treeSelectionRange = Seq(toSelectionRange(tree.sourcePos))
9289

93-
def getArgsSpan(args: List[Tree]): Option[SourcePosition] =
90+
def allArgsSelectionRange(args: List[Tree]): Option[SelectionRange] =
9491
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
92+
case Nil => None
93+
case list =>
94+
val srcPos = list.head.sourcePos
95+
val lastSpan = list.last.span
96+
val allArgsSrcPos = SourcePosition(srcPos.source, srcPos.span union lastSpan, srcPos.outer)
97+
if allArgsSrcPos.contains(pos) then Some(toSelectionRange(allArgsSrcPos))
98+
else None
10199

102100
tree match
103-
case DefDef(_, paramss, _, _) =>
104-
// If source position is within a parameter list, add a selection range covering that whole list.
105-
val selectedParams = paramss
106-
.flatMap(getArgsSpan) // parameter list to a sourcePosition covering the whole list
107-
.find(_.contains(pos))
108-
.map(toSelectionRange)
109-
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)
117-
case _ => Seq(treeSelectionRange)
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
118107

119108
private def setParent(
120109
child: SelectionRange,

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

+66-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
172172
"""|object Main extends App {
173173
| def x = 12 * (34 + 5@@6)
174174
|}""".stripMargin,
175-
List[String](
175+
List(
176176
"""|object Main extends App {
177177
| def x = 12 * (34 + >>region>>56<<region<<)
178178
|}""".stripMargin,
@@ -187,3 +187,68 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
187187
|}""".stripMargin
188188
)
189189
)
190+
191+
@Test def `function` =
192+
check(
193+
"val hello = (aaa: Int, bb@@b: Int, ccc: Int) => ???",
194+
List(
195+
"val hello = (aaa: Int, >>region>>bbb: Int<<region<<, ccc: Int) => ???",
196+
"val hello = (>>region>>aaa: Int, bbb: Int, ccc: Int<<region<<) => ???",
197+
"val hello = >>region>>(aaa: Int, bbb: Int, ccc: Int) => ???<<region<<",
198+
">>region>>val hello = (aaa: Int, bbb: Int, ccc: Int) => ???<<region<<",
199+
)
200+
)
201+
202+
@Test def `defdef` =
203+
check(
204+
"def hello(aaa: Int, bb@@b: Int, ccc: Int) = ???",
205+
List(
206+
"def hello(aaa: Int, >>region>>bbb: Int<<region<<, ccc: Int) = ???",
207+
"def hello(>>region>>aaa: Int, bbb: Int, ccc: Int<<region<<) = ???",
208+
">>region>>def hello(aaa: Int, bbb: Int, ccc: Int) = ???<<region<<",
209+
)
210+
)
211+
212+
@Test def `apply` =
213+
check(
214+
"def hello = List(111, 2@@22, 333)",
215+
List(
216+
"def hello = List(111, >>region>>222<<region<<, 333)",
217+
"def hello = List(>>region>>111, 222, 333<<region<<)",
218+
"def hello = >>region>>List(111, 222, 333)<<region<<",
219+
">>region>>def hello = List(111, 222, 333)<<region<<",
220+
)
221+
)
222+
223+
@Test def `type-apply` =
224+
check(
225+
"def hello = Map[String, I@@nt]()",
226+
List(
227+
"def hello = Map[String, >>region>>Int<<region<<]()",
228+
"def hello = Map[>>region>>String, Int<<region<<]()",
229+
"def hello = >>region>>Map[String, Int]<<region<<()",
230+
"def hello = >>region>>Map[String, Int]()<<region<<",
231+
">>region>>def hello = Map[String, Int]()<<region<<",
232+
)
233+
)
234+
235+
@Test def `unapply` =
236+
check(
237+
"val List(aaa, b@@bb, ccc) = List(111, 222, 333)",
238+
List(
239+
"val List(aaa, >>region>>bbb<<region<<, ccc) = List(111, 222, 333)",
240+
"val List(>>region>>aaa, bbb, ccc<<region<<) = List(111, 222, 333)",
241+
"val >>region>>List(aaa, bbb, ccc)<<region<< = List(111, 222, 333)",
242+
">>region>>val List(aaa, bbb, ccc) = List(111, 222, 333)<<region<<",
243+
)
244+
)
245+
246+
@Test def `single` =
247+
check(
248+
"def hello = List(2@@22)",
249+
List(
250+
"def hello = List(>>region>>222<<region<<)",
251+
"def hello = >>region>>List(222)<<region<<",
252+
">>region>>def hello = List(222)<<region<<",
253+
)
254+
)

0 commit comments

Comments
 (0)