Skip to content

Commit

Permalink
refactor: Extract Scanner foreach
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik committed Jul 30, 2024
1 parent 4702283 commit c744a47
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
14 changes: 2 additions & 12 deletions metals-bench/src/main/scala/bench/MetalsBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import scala.meta.internal.metals.EmptyReportContext
import scala.meta.internal.metals.IdentifierIndex
import scala.meta.internal.metals.JdkSources
import scala.meta.internal.metals.LoggerReportContext
import scala.meta.internal.metals.MetalsEnrichments.XtensionScanner
import scala.meta.internal.metals.ReportContext
import scala.meta.internal.metals.logging.MetalsLogger
import scala.meta.internal.mtags.JavaMtags
Expand All @@ -23,7 +24,6 @@ import scala.meta.internal.parsing.Trees
import scala.meta.internal.semanticdb.TextDocument
import scala.meta.internal.tokenizers.LegacyScanner
import scala.meta.internal.tokenizers.LegacyToken
import scala.meta.internal.tokenizers.LegacyTokenData
import scala.meta.io.AbsolutePath
import scala.meta.io.Classpath

Expand Down Expand Up @@ -125,17 +125,7 @@ class MetalsBench {
scalaDependencySources.foreach { input =>
val scanner = new LegacyScanner(input, Trees.defaultTokenizerDialect)
var i = 0

def foreach(scanner: LegacyScanner)(f: LegacyTokenData => Unit): Unit = {
scanner.initialize()
var curr = scanner.nextToken()
while (curr.token != LegacyToken.EOF) {
f(curr)
curr = scanner.nextToken()
}
}

foreach(scanner)(_ => i += 1)
scanner.foreach(_ => i += 1)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import scala.util.control.NonFatal

import scala.meta.Dialect
import scala.meta.inputs.Input
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.tokenizers.LegacyScanner
import scala.meta.internal.tokenizers.LegacyToken._
import scala.meta.internal.tokenizers.LegacyTokenData
import scala.meta.io.AbsolutePath

import ch.epfl.scala.bsp4j.BuildTargetIdentifier
Expand Down Expand Up @@ -41,19 +41,9 @@ class IdentifierIndex {
dialect: Dialect,
): Iterable[String] = {
val identifiers = Set.newBuilder[String]

def foreach(scanner: LegacyScanner)(f: LegacyTokenData => Unit): Unit = {
scanner.initialize()
var curr = scanner.nextToken()
while (curr.token != EOF) {
f(curr)
curr = scanner.nextToken()
}
}

try {
val scanner = new LegacyScanner(Input.String(text), dialect)
foreach(scanner) {
scanner.foreach {
case ident if ident.token == IDENTIFIER => identifiers += ident.strVal
case _ =>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import scala.meta.internal.mtags.MtagsEnrichments
import scala.meta.internal.parsing.EmptyResult
import scala.meta.internal.semanticdb.Scala.Descriptor
import scala.meta.internal.semanticdb.Scala.Symbols
import scala.meta.internal.tokenizers.LegacyScanner
import scala.meta.internal.tokenizers.LegacyTokenData
import scala.meta.internal.{semanticdb => s}
import scala.meta.io.AbsolutePath
import scala.meta.io.RelativePath
Expand Down Expand Up @@ -86,6 +88,21 @@ object MetalsEnrichments
with AsScalaExtensions
with MtagsEnrichments {

implicit class XtensionScanner(scanner: LegacyScanner) {

import scala.meta.internal.tokenizers.LegacyToken._

def foreach(f: LegacyTokenData => Unit): Unit = {
scanner.initialize()
var curr = scanner.nextToken()
while (curr.token != EOF) {
f(curr)
curr = scanner.nextToken()
}
}

}

implicit class XtensionFutureOpt[T](future: Future[Option[Future[T]]]) {
def getOrElse(default: => T)(implicit ec: ExecutionContext): Future[T] =
future.flatMap {
Expand Down

0 comments on commit c744a47

Please sign in to comment.