@@ -6,10 +6,12 @@ import java.util as ju
6
6
import scala .jdk .CollectionConverters ._
7
7
import scala .meta .pc .OffsetParams
8
8
9
+ import dotty .tools .dotc .ast .tpd
9
10
import dotty .tools .dotc .core .Contexts .Context
10
11
import dotty .tools .dotc .interactive .Interactive
11
12
import dotty .tools .dotc .interactive .InteractiveDriver
12
13
import dotty .tools .dotc .util .SourceFile
14
+ import dotty .tools .dotc .util .SourcePosition
13
15
import dotty .tools .pc .utils .MtagsEnrichments .*
14
16
15
17
import org .eclipse .lsp4j
@@ -46,11 +48,7 @@ class SelectionRangeProvider(
46
48
Interactive .pathTo(driver.openedTrees(uri), pos)(using ctx)
47
49
48
50
val bareRanges = path
49
- .map { tree =>
50
- val selectionRange = new SelectionRange ()
51
- selectionRange.setRange(tree.sourcePos.toLsp)
52
- selectionRange
53
- }
51
+ .flatMap(selectionRangesFromTree(pos))
54
52
55
53
val comments =
56
54
driver.compilationUnits.get(uri).map(_.comments).toList.flatten
@@ -79,6 +77,33 @@ class SelectionRangeProvider(
79
77
}
80
78
end selectionRange
81
79
80
+ /** Given a tree, create a seq of [[SelectionRange ]]s corresponding to that tree. */
81
+ private def selectionRangesFromTree (pos : SourcePosition )(tree : tpd.Tree )(using Context ) =
82
+ def toSelectionRange (srcPos : SourcePosition ) =
83
+ val selectionRange = new SelectionRange ()
84
+ selectionRange.setRange(srcPos.toLsp)
85
+ selectionRange
86
+
87
+ val treeSelectionRange = toSelectionRange(tree.sourcePos)
88
+
89
+ tree match
90
+ case tpd.DefDef (name, paramss, tpt, rhs) =>
91
+ // 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)
104
+ selectedParams ++ Seq (treeSelectionRange)
105
+ case _ => Seq (treeSelectionRange)
106
+
82
107
private def setParent (
83
108
child : SelectionRange ,
84
109
parent : SelectionRange
0 commit comments