-
Notifications
You must be signed in to change notification settings - Fork 338
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
feature: collect overridden symbols in Scala toplevel mtags #5607
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,18 +11,21 @@ import scala.meta.internal.semanticdb.Scala._ | |
import scala.meta.internal.semanticdb.SymbolInformation.Kind | ||
import scala.meta.internal.{semanticdb => s} | ||
|
||
trait MtagsIndexer { | ||
trait GenericMtagsIndexer[T <: EnrichedTextDocument] { | ||
def language: Language | ||
def indexRoot(): Unit | ||
def input: Input.VirtualFile | ||
def index(): s.TextDocument = { | ||
protected def documentToResult(doc: s.TextDocument): T | ||
def index(): T = { | ||
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. Would it make sense to just have overrides accessible as separate method? Do we need to box it together with TextDocument? |
||
indexRoot() | ||
s.TextDocument( | ||
uri = input.path, | ||
text = input.text, | ||
language = language, | ||
occurrences = names.result(), | ||
symbols = symbols.result() | ||
documentToResult( | ||
s.TextDocument( | ||
uri = input.path, | ||
text = input.text, | ||
language = language, | ||
occurrences = names.result(), | ||
symbols = symbols.result() | ||
) | ||
) | ||
} | ||
// This method is intentionally non-final to allow accessing this stream directly without building a s.TextDocument. | ||
|
@@ -170,3 +173,15 @@ trait MtagsIndexer { | |
Symbols.Global(Symbols.RootPackage, signature) | ||
else Symbols.Global(currentOwner, signature) | ||
} | ||
|
||
trait EnrichedTextDocument { | ||
def textDocument: s.TextDocument | ||
} | ||
|
||
case class JustDocument(textDocument: s.TextDocument) | ||
extends EnrichedTextDocument | ||
|
||
trait MtagsIndexer extends GenericMtagsIndexer[JustDocument] { | ||
protected def documentToResult(doc: s.TextDocument): JustDocument = | ||
JustDocument(doc) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package scala.meta.internal.mtags | ||
|
||
sealed trait OverriddenSymbol | ||
case class UnresolvedOverriddenSymbol(name: String, pos: Int) | ||
extends OverriddenSymbol | ||
case class ResolvedOverriddenSymbol(symbol: String) extends OverriddenSymbol |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import scala.meta.internal.semanticdb.Scala | |
import scala.meta.internal.semanticdb.Scala._ | ||
import scala.meta.internal.semanticdb.SymbolInformation | ||
import scala.meta.internal.semanticdb.SymbolInformation.Kind | ||
import scala.meta.internal.semanticdb.TextDocument | ||
import scala.meta.internal.tokenizers.LegacyScanner | ||
import scala.meta.internal.tokenizers.LegacyToken._ | ||
import scala.meta.tokenizers.TokenizeException | ||
|
@@ -46,7 +47,17 @@ class ScalaToplevelMtags( | |
includeMembers: Boolean, | ||
dialect: Dialect | ||
)(implicit rc: ReportContext) | ||
extends MtagsIndexer { | ||
extends GenericMtagsIndexer[TextDocumentWithOverridden] { | ||
|
||
override protected def documentToResult( | ||
doc: TextDocument | ||
): TextDocumentWithOverridden = | ||
new TextDocumentWithOverridden(doc, overridden.result) | ||
|
||
private val overridden = List.newBuilder[(String, List[OverriddenSymbol])] | ||
|
||
private def addOverridden(symbols: List[OverriddenSymbol]) = | ||
overridden += ((currentOwner, symbols)) | ||
|
||
import ScalaToplevelMtags._ | ||
|
||
|
@@ -402,6 +413,23 @@ class ScalaToplevelMtags( | |
currRegion.changeCaseClassState(true), | ||
nextExpectTemplate | ||
) | ||
case EXTENDS => | ||
val (overridden, maybeNewIdent) = findOverridden(List.empty) | ||
expectTemplate.map(tmpl => | ||
withOwner(tmpl.owner) { | ||
Comment on lines
+418
to
+419
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. Do we need owner here? Since we only save those by name? |
||
addOverridden( | ||
overridden.reverse.map(id => | ||
UnresolvedOverriddenSymbol(id.name, id.pos.start) | ||
) | ||
) | ||
} | ||
) | ||
loop( | ||
maybeNewIdent.getOrElse(indent), | ||
isAfterNewline = maybeNewIdent.isDefined, | ||
currRegion, | ||
expectTemplate | ||
) | ||
case IDENTIFIER if currRegion.emitIdentifier && includeMembers => | ||
withOwner(currRegion.owner) { | ||
term( | ||
|
@@ -419,17 +447,14 @@ class ScalaToplevelMtags( | |
) | ||
case CASE => | ||
val nextIsNewLine = nextIsNL() | ||
val (shouldCreateClassTemplate, isAfterNewline) = | ||
val isAfterNewline = | ||
emitEnumCases(region, nextIsNewLine) | ||
val nextExpectTemplate = | ||
if (shouldCreateClassTemplate) newExpectClassTemplate | ||
else expectTemplate.filter(!_.isPackageBody) | ||
loop( | ||
indent, | ||
isAfterNewline, | ||
currRegion, | ||
if (scanner.curr.token == CLASS) newExpectCaseClassTemplate | ||
else nextExpectTemplate | ||
else newExpectClassTemplate | ||
) | ||
case t => | ||
val nextExpectTemplate = expectTemplate.filter(!_.isPackageBody) | ||
|
@@ -485,6 +510,45 @@ class ScalaToplevelMtags( | |
buf.result() | ||
} | ||
|
||
@tailrec | ||
private def acceptAllAfterOverriddenIdentifier(): Option[Int] = { | ||
val maybeNewIdent = acceptTrivia() | ||
scanner.curr.token match { | ||
case LPAREN => | ||
acceptBalancedDelimeters(LPAREN, RPAREN) | ||
acceptAllAfterOverriddenIdentifier() | ||
case LBRACKET => | ||
acceptBalancedDelimeters(LBRACKET, RBRACKET) | ||
acceptAllAfterOverriddenIdentifier() | ||
case _ => maybeNewIdent | ||
} | ||
|
||
} | ||
|
||
@tailrec | ||
private def findOverridden( | ||
acc0: List[Identifier] | ||
): (List[Identifier], Option[Int]) = { | ||
val maybeNewIdent = acceptTrivia() | ||
val (shouldGoOn, acc) = scanner.curr.token match { | ||
case IDENTIFIER => | ||
(true, newIdentifier.toList ++ acc0) | ||
case LBRACE => | ||
acceptBalancedDelimeters(LBRACE, RBRACE) | ||
(true, acc0) | ||
case _ => (false, acc0) | ||
} | ||
|
||
if (shouldGoOn) { | ||
val maybeNewIdent = acceptAllAfterOverriddenIdentifier() | ||
scanner.curr.token match { | ||
case WITH => findOverridden(acc) | ||
case _ => (acc, maybeNewIdent) | ||
} | ||
} else (acc, maybeNewIdent) | ||
|
||
} | ||
|
||
/** | ||
* Enters a toplevel symbol such as class, trait or object | ||
*/ | ||
|
@@ -568,7 +632,7 @@ class ScalaToplevelMtags( | |
private def emitEnumCases( | ||
region: Region, | ||
nextIsNewLine: Boolean | ||
): (Boolean, Boolean) = { | ||
): Boolean = { | ||
def ownerCompanionObject = | ||
if (currentOwner.endsWith("#")) | ||
s"${currentOwner.stripSuffix("#")}." | ||
|
@@ -578,19 +642,22 @@ class ScalaToplevelMtags( | |
val pos = newPosition | ||
val name = scanner.curr.name | ||
def emitEnumCaseObject() = { | ||
withOwner(ownerCompanionObject) { | ||
term( | ||
name, | ||
pos, | ||
Kind.METHOD, | ||
SymbolInformation.Property.VAL.value | ||
) | ||
} | ||
currentOwner = ownerCompanionObject | ||
term( | ||
name, | ||
pos, | ||
Kind.METHOD, | ||
SymbolInformation.Property.VAL.value | ||
) | ||
} | ||
def emitOverridden() = addOverridden( | ||
List(ResolvedOverriddenSymbol(region.owner)) | ||
) | ||
val nextIsNewLine0 = nextIsNL() | ||
scanner.curr.token match { | ||
case COMMA => | ||
emitEnumCaseObject() | ||
emitOverridden() | ||
resetRegion(region) | ||
val nextIsNewLine1 = nextIsNL() | ||
emitEnumCases(region, nextIsNewLine1) | ||
|
@@ -602,12 +669,15 @@ class ScalaToplevelMtags( | |
Kind.CLASS, | ||
SymbolInformation.Property.VAL.value | ||
) | ||
(true, false) | ||
case _ => | ||
false | ||
case tok => | ||
emitEnumCaseObject() | ||
(false, nextIsNewLine0) | ||
if (tok != EXTENDS) { | ||
emitOverridden() | ||
} | ||
nextIsNewLine0 | ||
} | ||
case _ => (false, nextIsNewLine) | ||
case _ => nextIsNewLine | ||
} | ||
} | ||
|
||
|
@@ -668,7 +738,9 @@ class ScalaToplevelMtags( | |
} | ||
} | ||
|
||
private def acceptTrivia(): Unit = { | ||
private def acceptTrivia(): Option[Int] = { | ||
var includedNewline = false | ||
var ident = 0 | ||
scanner.nextToken() | ||
while ( | ||
!isDone && | ||
|
@@ -677,8 +749,15 @@ class ScalaToplevelMtags( | |
case _ => false | ||
}) | ||
) { | ||
if (isNewline) { | ||
includedNewline = true | ||
ident = 0 | ||
} else if (scanner.curr.token == WHITESPACE) { | ||
ident += 1 | ||
} | ||
scanner.nextToken() | ||
} | ||
if (includedNewline) Some(ident) else None | ||
} | ||
|
||
private def nextIsNL(): Boolean = { | ||
|
@@ -953,3 +1032,8 @@ object ScalaToplevelMtags { | |
} | ||
} | ||
} | ||
|
||
case class TextDocumentWithOverridden( | ||
textDocument: TextDocument, | ||
overridden: List[(String, List[OverriddenSymbol])] | ||
) extends EnrichedTextDocument |
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.
Perfect place for union types :( need to migrate faster to Scala 3 :D