Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions mtags/src/main/scala/scala/meta/internal/mtags/Mtags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ final class Mtags(implicit rc: ReportContext) {
addLines(language, input.text)
mtags
.index()
.textDocument
.occurrences
.iterator
.filterNot(_.symbol.isPackage)
Expand All @@ -55,8 +56,9 @@ final class Mtags(implicit rc: ReportContext) {
JavaMtags
.index(input, includeMembers = true)
.index()
.textDocument
} else if (language.isScala) {
ScalaMtags.index(input, dialect).index()
ScalaMtags.index(input, dialect).index().textDocument
} else {
TextDocument()
}
Expand Down Expand Up @@ -91,11 +93,11 @@ object Mtags {
.toList
}

def allToplevels(
def allToplevelsEnriched(
input: Input.VirtualFile,
dialect: Dialect,
includeMembers: Boolean = true
)(implicit rc: ReportContext = EmptyReportContext): TextDocument = {
)(implicit rc: ReportContext = EmptyReportContext): EnrichedTextDocument = {
Copy link
Contributor

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

input.toLanguage match {
case Language.JAVA =>
new JavaMtags(input, includeMembers = true).index()
Expand All @@ -104,9 +106,17 @@ object Mtags {
new ScalaToplevelMtags(input, true, includeMembers, dialect)
mtags.index()
case _ =>
TextDocument()
JustDocument(TextDocument())
}
}

def allToplevels(
input: Input.VirtualFile,
dialect: Dialect,
includeMembers: Boolean = true
)(implicit rc: ReportContext = EmptyReportContext): TextDocument =
allToplevelsEnriched(input, dialect, includeMembers).textDocument

def toplevels(
input: Input.VirtualFile,
dialect: Dialect = dialects.Scala213
Expand Down
31 changes: 23 additions & 8 deletions mtags/src/main/scala/scala/meta/internal/mtags/MtagsIndexer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Expand Down Expand Up @@ -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
124 changes: 104 additions & 20 deletions mtags/src/main/scala/scala/meta/internal/mtags/ScalaToplevelMtags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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._

Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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(
Expand All @@ -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)
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -568,7 +632,7 @@ class ScalaToplevelMtags(
private def emitEnumCases(
region: Region,
nextIsNewLine: Boolean
): (Boolean, Boolean) = {
): Boolean = {
def ownerCompanionObject =
if (currentOwner.endsWith("#"))
s"${currentOwner.stripSuffix("#")}."
Expand All @@ -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)
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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 &&
Expand All @@ -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 = {
Expand Down Expand Up @@ -953,3 +1032,8 @@ object ScalaToplevelMtags {
}
}
}

case class TextDocumentWithOverridden(
textDocument: TextDocument,
overridden: List[(String, List[OverriddenSymbol])]
) extends EnrichedTextDocument
Loading