-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: use pc for finding references of local symbols and when …
…semanticdb is missing
- Loading branch information
1 parent
dea3d10
commit a89f2cf
Showing
12 changed files
with
584 additions
and
434 deletions.
There are no files selected for viewing
431 changes: 60 additions & 371 deletions
431
presentation-compiler/src/main/dotty/tools/pc/PcCollector.scala
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
presentation-compiler/src/main/dotty/tools/pc/PcReferencesProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.