Skip to content

Commit a89f2cf

Browse files
committed
improvement: use pc for finding references of local symbols and when semanticdb is missing
1 parent dea3d10 commit a89f2cf

12 files changed

+584
-434
lines changed

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

Lines changed: 60 additions & 371 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import org.eclipse.lsp4j.DocumentHighlightKind
1414
final class PcDocumentHighlightProvider(
1515
driver: InteractiveDriver,
1616
params: OffsetParams
17-
) extends PcCollector[DocumentHighlight](driver, params):
17+
) extends WithSymbolSearchCollector[DocumentHighlight](driver, params):
1818

1919
def collect(
2020
parent: Option[Tree]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import dotty.tools.pc.utils.InteractiveEnrichments.*
2222
import org.eclipse.lsp4j as l
2323

2424
final class PcInlineValueProviderImpl(
25-
val driver: InteractiveDriver,
25+
driver: InteractiveDriver,
2626
val params: OffsetParams
27-
) extends PcCollector[Option[Occurence]](driver, params)
27+
) extends WithSymbolSearchCollector[Option[Occurence]](driver, params)
2828
with InlineValueProvider:
2929

3030
val position: l.Position = pos.toLsp.getStart().nn
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package dotty.tools.pc
2+
3+
import scala.language.unsafeNulls
4+
5+
import scala.jdk.CollectionConverters.*
6+
7+
import scala.meta.internal.metals.CompilerOffsetParams
8+
import scala.meta.pc.ReferencesRequest
9+
import scala.meta.pc.ReferencesResult
10+
11+
import dotty.tools.dotc.ast.tpd
12+
import dotty.tools.dotc.ast.tpd.*
13+
import dotty.tools.dotc.core.Symbols.*
14+
import dotty.tools.dotc.interactive.InteractiveDriver
15+
import dotty.tools.dotc.util.SourcePosition
16+
import org.eclipse.lsp4j
17+
import org.eclipse.lsp4j.Location
18+
import dotty.tools.pc.utils.InteractiveEnrichments.*
19+
import scala.meta.internal.pc.PcReferencesResult
20+
21+
class PcReferencesProvider(
22+
driver: InteractiveDriver,
23+
request: ReferencesRequest,
24+
) extends WithCompilationUnit(driver, request.file()) with PcCollector[Option[(String, Option[lsp4j.Range])]]:
25+
26+
private def soughtSymbols =
27+
if(request.offsetOrSymbol().isLeft()) {
28+
val offsetParams = CompilerOffsetParams(
29+
request.file().uri(),
30+
request.file().text(),
31+
request.offsetOrSymbol().getLeft()
32+
)
33+
val symbolSearch = new WithCompilationUnit(driver, offsetParams) with PcSymbolSearch
34+
symbolSearch.soughtSymbols.map(_._1)
35+
} else {
36+
SymbolProvider.compilerSymbol(request.offsetOrSymbol().getRight()).map(symbolAlternatives(_))
37+
}
38+
39+
def collect(parent: Option[Tree])(
40+
tree: Tree | EndMarker,
41+
toAdjust: SourcePosition,
42+
symbol: Option[Symbol],
43+
): Option[(String, Option[lsp4j.Range])] =
44+
val (pos, _) = toAdjust.adjust(text)
45+
tree match
46+
case t: DefTree if !request.includeDefinition() =>
47+
val sym = symbol.getOrElse(t.symbol)
48+
Some(SemanticdbSymbols.symbolName(sym), None)
49+
case t: Tree =>
50+
val sym = symbol.getOrElse(t.symbol)
51+
Some(SemanticdbSymbols.symbolName(sym), Some(pos.toLsp))
52+
case _ => None
53+
54+
def references(): List[ReferencesResult] =
55+
soughtSymbols match
56+
case Some(sought) if sought.nonEmpty =>
57+
resultWithSought(sought)
58+
.flatten
59+
.groupMap(_._1) { case (_, optRange) =>
60+
optRange.map(new Location(request.file().uri().toString(), _))
61+
}
62+
.map { case (symbol, locs) =>
63+
PcReferencesResult(symbol, locs.flatten.asJava)
64+
}
65+
.toList
66+
case _ => Nil
67+
end PcReferencesProvider

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class PcRenameProvider(
1616
driver: InteractiveDriver,
1717
params: OffsetParams,
1818
name: Option[String]
19-
) extends PcCollector[l.TextEdit](driver, params):
19+
) extends WithSymbolSearchCollector[l.TextEdit](driver, params):
2020
private val forbiddenMethods =
2121
Set("equals", "hashCode", "unapply", "unary_!", "!")
2222
def canRenameSymbol(sym: Symbol)(using Context): Boolean =
@@ -25,7 +25,7 @@ final class PcRenameProvider(
2525
|| sym.source.path.isWorksheet)
2626

2727
def prepareRename(): Option[l.Range] =
28-
soughtSymbols(path).flatMap((symbols, pos) =>
28+
soughtSymbols.flatMap((symbols, pos) =>
2929
if symbols.forall(canRenameSymbol) then Some(pos.toLsp)
3030
else None
3131
)
@@ -42,13 +42,10 @@ final class PcRenameProvider(
4242
)
4343
end collect
4444

45-
def rename(
46-
): List[l.TextEdit] =
47-
val (symbols, _) = soughtSymbols(path).getOrElse(Set.empty, pos)
45+
def rename(): List[l.TextEdit] =
46+
val (symbols, _) = soughtSymbols.getOrElse(Set.empty, pos)
4847
if symbols.nonEmpty && symbols.forall(canRenameSymbol(_))
49-
then
50-
val res = result()
51-
res
48+
then result()
5249
else Nil
5350
end rename
5451
end PcRenameProvider

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ final class PcSemanticTokensProvider(
6060
case _ => !df.rhs.isEmpty
6161
case _ => false
6262

63-
object Collector extends PcCollector[Option[Node]](driver, params):
63+
object Collector extends SimpleCollector[Option[Node]](driver, params):
6464
override def collect(
6565
parent: Option[Tree]
6666
)(tree: Tree | EndMarker, pos: SourcePosition, symbol: Option[Symbol]): Option[Node] =

0 commit comments

Comments
 (0)