-
Notifications
You must be signed in to change notification settings - Fork 1.1k
add info
implementation to pc
#19812
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
128 changes: 128 additions & 0 deletions
128
presentation-compiler/src/main/dotty/tools/pc/SymbolInformationProvider.scala
This file contains hidden or 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,128 @@ | ||
package scala.meta.internal.pc | ||
|
||
import scala.util.control.NonFatal | ||
|
||
import scala.meta.pc.PcSymbolKind | ||
import scala.meta.pc.PcSymbolProperty | ||
|
||
import dotty.tools.dotc.core.Contexts.Context | ||
import dotty.tools.dotc.core.Denotations.Denotation | ||
import dotty.tools.dotc.core.Denotations.MultiDenotation | ||
import dotty.tools.dotc.core.Flags | ||
import dotty.tools.dotc.core.Names.* | ||
import dotty.tools.dotc.core.StdNames.nme | ||
import dotty.tools.dotc.core.Symbols.* | ||
import dotty.tools.pc.utils.MtagsEnrichments.metalsDealias | ||
import dotty.tools.pc.SemanticdbSymbols | ||
|
||
class SymbolInformationProvider(using Context): | ||
private def toSymbols( | ||
pkg: String, | ||
parts: List[(String, Boolean)], | ||
): List[Symbol] = | ||
def collectSymbols(denotation: Denotation): List[Symbol] = | ||
denotation match | ||
case MultiDenotation(denot1, denot2) => | ||
collectSymbols(denot1) ++ collectSymbols(denot2) | ||
case denot => List(denot.symbol) | ||
|
||
def loop( | ||
owners: List[Symbol], | ||
parts: List[(String, Boolean)], | ||
): List[Symbol] = | ||
parts match | ||
case (head, isClass) :: tl => | ||
val foundSymbols = | ||
owners.flatMap { owner => | ||
val next = | ||
if isClass then owner.info.member(typeName(head)) | ||
else owner.info.member(termName(head)) | ||
collectSymbols(next).filter(_.exists) | ||
} | ||
if foundSymbols.nonEmpty then loop(foundSymbols, tl) | ||
else Nil | ||
case Nil => owners | ||
|
||
val pkgSym = | ||
if pkg == "_empty_" then requiredPackage(nme.EMPTY_PACKAGE) | ||
else requiredPackage(pkg) | ||
loop(List(pkgSym), parts) | ||
end toSymbols | ||
|
||
def info(symbol: String): Option[PcSymbolInformation] = | ||
val index = symbol.lastIndexOf("/") | ||
val pkg = normalizePackage(symbol.take(index + 1)) | ||
|
||
def loop( | ||
symbol: String, | ||
acc: List[(String, Boolean)], | ||
): List[(String, Boolean)] = | ||
if symbol.isEmpty() then acc.reverse | ||
else | ||
val newSymbol = symbol.takeWhile(c => c != '.' && c != '#') | ||
val rest = symbol.drop(newSymbol.size) | ||
loop(rest.drop(1), (newSymbol, rest.headOption.exists(_ == '#')) :: acc) | ||
val names = | ||
loop(symbol.drop(index + 1).takeWhile(_ != '('), List.empty) | ||
|
||
val foundSymbols = | ||
try toSymbols(pkg, names) | ||
catch case NonFatal(e) => Nil | ||
|
||
val (searchedSymbol, alternativeSymbols) = | ||
foundSymbols.partition(compilerSymbol => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can look better with fewer braces :D : foundSymbols.partition: compilerSymbol =>
SemanticdbSymbols.symbolName(compilerSymbol) == symbol |
||
SemanticdbSymbols.symbolName(compilerSymbol) == symbol | ||
) | ||
|
||
searchedSymbol match | ||
case Nil => None | ||
case sym :: _ => | ||
val classSym = if sym.isClass then sym else sym.moduleClass | ||
val parents = | ||
if classSym.isClass | ||
then classSym.asClass.parentSyms.map(SemanticdbSymbols.symbolName) | ||
else Nil | ||
val dealisedSymbol = | ||
if sym.isAliasType then sym.info.metalsDealias.typeSymbol else sym | ||
val classOwner = | ||
sym.ownersIterator.drop(1).find(s => s.isClass || s.is(Flags.Module)) | ||
val overridden = sym.denot.allOverriddenSymbols.toList | ||
|
||
val pcSymbolInformation = | ||
PcSymbolInformation( | ||
symbol = SemanticdbSymbols.symbolName(sym), | ||
kind = getSymbolKind(sym), | ||
parents = parents, | ||
dealiasedSymbol = SemanticdbSymbols.symbolName(dealisedSymbol), | ||
classOwner = classOwner.map(SemanticdbSymbols.symbolName), | ||
overriddenSymbols = overridden.map(SemanticdbSymbols.symbolName), | ||
alternativeSymbols = | ||
alternativeSymbols.map(SemanticdbSymbols.symbolName), | ||
properties = | ||
if sym.is(Flags.Abstract) then List(PcSymbolProperty.ABSTRACT) | ||
else Nil, | ||
) | ||
|
||
Some(pcSymbolInformation) | ||
end match | ||
end info | ||
|
||
private def getSymbolKind(sym: Symbol): PcSymbolKind = | ||
if sym.isAllOf(Flags.JavaInterface) then PcSymbolKind.INTERFACE | ||
else if sym.is(Flags.Trait) then PcSymbolKind.TRAIT | ||
else if sym.isConstructor then PcSymbolKind.CONSTRUCTOR | ||
else if sym.isPackageObject then PcSymbolKind.PACKAGE_OBJECT | ||
else if sym.isClass then PcSymbolKind.CLASS | ||
else if sym.is(Flags.Macro) then PcSymbolKind.MACRO | ||
else if sym.is(Flags.Local) then PcSymbolKind.LOCAL | ||
else if sym.is(Flags.Method) then PcSymbolKind.METHOD | ||
else if sym.is(Flags.Param) then PcSymbolKind.PARAMETER | ||
else if sym.is(Flags.Package) then PcSymbolKind.PACKAGE | ||
else if sym.is(Flags.TypeParam) then PcSymbolKind.TYPE_PARAMETER | ||
else if sym.isType then PcSymbolKind.TYPE | ||
else PcSymbolKind.UNKNOWN_KIND | ||
|
||
private def normalizePackage(pkg: String): String = | ||
pkg.replace("/", ".").nn.stripSuffix(".") | ||
|
||
end SymbolInformationProvider |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't
.allSymbols
handle this ?