Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow range selection on function parameter to select a parameter list #19777

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import java.util as ju
import scala.jdk.CollectionConverters._
import scala.meta.pc.OffsetParams

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.interactive.Interactive
import dotty.tools.dotc.interactive.InteractiveDriver
import dotty.tools.dotc.util.SourceFile
import dotty.tools.dotc.util.SourcePosition
import dotty.tools.pc.utils.MtagsEnrichments.*

import org.eclipse.lsp4j
Expand Down Expand Up @@ -46,11 +48,7 @@ class SelectionRangeProvider(
Interactive.pathTo(driver.openedTrees(uri), pos)(using ctx)

val bareRanges = path
.map { tree =>
val selectionRange = new SelectionRange()
selectionRange.setRange(tree.sourcePos.toLsp)
selectionRange
}
.flatMap(selectionRangesFromTree(pos))

val comments =
driver.compilationUnits.get(uri).map(_.comments).toList.flatten
Expand Down Expand Up @@ -79,6 +77,33 @@ class SelectionRangeProvider(
}
end selectionRange

/** Given a tree, create a seq of [[SelectionRange]]s corresponding to that tree. */
private def selectionRangesFromTree(pos: SourcePosition)(tree: tpd.Tree)(using Context) =
def toSelectionRange(srcPos: SourcePosition) =
val selectionRange = new SelectionRange()
selectionRange.setRange(srcPos.toLsp)
selectionRange

val treeSelectionRange = toSelectionRange(tree.sourcePos)

tree match
case tpd.DefDef(name, paramss, tpt, rhs) =>
// If source position is within a parameter list, add a selection range covering that whole list.
val selectedParams =
paramss
.iterator
.flatMap: // parameter list to a sourcePosition covering the whole list
case Seq(param) => Some(param.sourcePos)
case params @ Seq(head, tail*) =>
val srcPos = head.sourcePos
val lastSpan = tail.last.span
Some(SourcePosition(srcPos.source, srcPos.span union lastSpan, srcPos.outer))
case Seq() => None
.find(_.contains(pos))
.map(toSelectionRange)
selectedParams ++ Seq(treeSelectionRange)
case _ => Seq(treeSelectionRange)

private def setParent(
child: SelectionRange,
parent: SelectionRange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,49 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
|}<<region<<""".stripMargin
)
)

@Test def `function params` =
check(
"""|object Main extends App {
| def func(a@@: Int, b: Int) =
| a + b
|}""".stripMargin,
List[String](
"""|object Main extends App {
| def func(>>region>>a: Int<<region<<, b: Int) =
| a + b
|}""".stripMargin,
"""|object Main extends App {
| def func(>>region>>a: Int, b: Int<<region<<) =
| a + b
|}""".stripMargin,
"""|object Main extends App {
| >>region>>def func(a: Int, b: Int) =
| a + b<<region<<
|}""".stripMargin
)
)
check(
"""|object Main extends App {
| val func = (a@@: Int, b: Int) =>
| a + b
|}""".stripMargin,
List[String](
"""|object Main extends App {
| val func = (>>region>>a: Int<<region<<, b: Int) =>
| a + b
|}""".stripMargin,
"""|object Main extends App {
| val func = (>>region>>a: Int, b: Int<<region<<) =>
| a + b
|}""".stripMargin,
"""|object Main extends App {
| val func = >>region>>(a: Int, b: Int) =>
| a + b<<region<<
|}""".stripMargin,
"""|object Main extends App {
| >>region>>val func = (a: Int, b: Int) =>
| a + b<<region<<
|}""".stripMargin
)
)
Loading