@@ -6,10 +6,12 @@ import java.util as ju
66import scala .jdk .CollectionConverters ._
77import scala .meta .pc .OffsetParams
88
9+ import dotty .tools .dotc .ast .tpd
910import dotty .tools .dotc .core .Contexts .Context
1011import dotty .tools .dotc .interactive .Interactive
1112import dotty .tools .dotc .interactive .InteractiveDriver
1213import dotty .tools .dotc .util .SourceFile
14+ import dotty .tools .dotc .util .SourcePosition
1315import dotty .tools .pc .utils .MtagsEnrichments .*
1416
1517import org .eclipse .lsp4j
@@ -46,11 +48,7 @@ class SelectionRangeProvider(
4648 Interactive .pathTo(driver.openedTrees(uri), pos)(using ctx)
4749
4850 val bareRanges = path
49- .map { tree =>
50- val selectionRange = new SelectionRange ()
51- selectionRange.setRange(tree.sourcePos.toLsp)
52- selectionRange
53- }
51+ .flatMap(selectionRangesFromTree(pos))
5452
5553 val comments =
5654 driver.compilationUnits.get(uri).map(_.comments).toList.flatten
@@ -79,6 +77,33 @@ class SelectionRangeProvider(
7977 }
8078 end selectionRange
8179
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+
82107 private def setParent (
83108 child : SelectionRange ,
84109 parent : SelectionRange
0 commit comments