Skip to content

Commit

Permalink
improvement: use pc for finding references of local symbols and when …
Browse files Browse the repository at this point in the history
…semanticdb is missing
  • Loading branch information
kasiaMarek committed Jun 10, 2024
1 parent dea3d10 commit a89f2cf
Show file tree
Hide file tree
Showing 12 changed files with 584 additions and 434 deletions.
431 changes: 60 additions & 371 deletions presentation-compiler/src/main/dotty/tools/pc/PcCollector.scala

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.eclipse.lsp4j.DocumentHighlightKind
final class PcDocumentHighlightProvider(
driver: InteractiveDriver,
params: OffsetParams
) extends PcCollector[DocumentHighlight](driver, params):
) extends WithSymbolSearchCollector[DocumentHighlight](driver, params):

def collect(
parent: Option[Tree]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import dotty.tools.pc.utils.InteractiveEnrichments.*
import org.eclipse.lsp4j as l

final class PcInlineValueProviderImpl(
val driver: InteractiveDriver,
driver: InteractiveDriver,
val params: OffsetParams
) extends PcCollector[Option[Occurence]](driver, params)
) extends WithSymbolSearchCollector[Option[Occurence]](driver, params)
with InlineValueProvider:

val position: l.Position = pos.toLsp.getStart().nn
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package dotty.tools.pc

import scala.language.unsafeNulls

import scala.jdk.CollectionConverters.*

import scala.meta.internal.metals.CompilerOffsetParams
import scala.meta.pc.ReferencesRequest
import scala.meta.pc.ReferencesResult

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.ast.tpd.*
import dotty.tools.dotc.core.Symbols.*
import dotty.tools.dotc.interactive.InteractiveDriver
import dotty.tools.dotc.util.SourcePosition
import org.eclipse.lsp4j
import org.eclipse.lsp4j.Location
import dotty.tools.pc.utils.InteractiveEnrichments.*
import scala.meta.internal.pc.PcReferencesResult

class PcReferencesProvider(
driver: InteractiveDriver,
request: ReferencesRequest,
) extends WithCompilationUnit(driver, request.file()) with PcCollector[Option[(String, Option[lsp4j.Range])]]:

private def soughtSymbols =
if(request.offsetOrSymbol().isLeft()) {
val offsetParams = CompilerOffsetParams(
request.file().uri(),
request.file().text(),
request.offsetOrSymbol().getLeft()
)
val symbolSearch = new WithCompilationUnit(driver, offsetParams) with PcSymbolSearch
symbolSearch.soughtSymbols.map(_._1)
} else {
SymbolProvider.compilerSymbol(request.offsetOrSymbol().getRight()).map(symbolAlternatives(_))
}

def collect(parent: Option[Tree])(
tree: Tree | EndMarker,
toAdjust: SourcePosition,
symbol: Option[Symbol],
): Option[(String, Option[lsp4j.Range])] =
val (pos, _) = toAdjust.adjust(text)
tree match
case t: DefTree if !request.includeDefinition() =>
val sym = symbol.getOrElse(t.symbol)
Some(SemanticdbSymbols.symbolName(sym), None)
case t: Tree =>
val sym = symbol.getOrElse(t.symbol)
Some(SemanticdbSymbols.symbolName(sym), Some(pos.toLsp))
case _ => None

def references(): List[ReferencesResult] =
soughtSymbols match
case Some(sought) if sought.nonEmpty =>
resultWithSought(sought)
.flatten
.groupMap(_._1) { case (_, optRange) =>
optRange.map(new Location(request.file().uri().toString(), _))
}
.map { case (symbol, locs) =>
PcReferencesResult(symbol, locs.flatten.asJava)
}
.toList
case _ => Nil
end PcReferencesProvider
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class PcRenameProvider(
driver: InteractiveDriver,
params: OffsetParams,
name: Option[String]
) extends PcCollector[l.TextEdit](driver, params):
) extends WithSymbolSearchCollector[l.TextEdit](driver, params):
private val forbiddenMethods =
Set("equals", "hashCode", "unapply", "unary_!", "!")
def canRenameSymbol(sym: Symbol)(using Context): Boolean =
Expand All @@ -25,7 +25,7 @@ final class PcRenameProvider(
|| sym.source.path.isWorksheet)

def prepareRename(): Option[l.Range] =
soughtSymbols(path).flatMap((symbols, pos) =>
soughtSymbols.flatMap((symbols, pos) =>
if symbols.forall(canRenameSymbol) then Some(pos.toLsp)
else None
)
Expand All @@ -42,13 +42,10 @@ final class PcRenameProvider(
)
end collect

def rename(
): List[l.TextEdit] =
val (symbols, _) = soughtSymbols(path).getOrElse(Set.empty, pos)
def rename(): List[l.TextEdit] =
val (symbols, _) = soughtSymbols.getOrElse(Set.empty, pos)
if symbols.nonEmpty && symbols.forall(canRenameSymbol(_))
then
val res = result()
res
then result()
else Nil
end rename
end PcRenameProvider
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ final class PcSemanticTokensProvider(
case _ => !df.rhs.isEmpty
case _ => false

object Collector extends PcCollector[Option[Node]](driver, params):
object Collector extends SimpleCollector[Option[Node]](driver, params):
override def collect(
parent: Option[Tree]
)(tree: Tree | EndMarker, pos: SourcePosition, symbol: Option[Symbol]): Option[Node] =
Expand Down
Loading

0 comments on commit a89f2cf

Please sign in to comment.