Skip to content

Commit

Permalink
improvement: Check if compiled unit is available before using
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik committed Mar 12, 2024
1 parent d8475d9 commit 9590166
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 12 deletions.
Expand Up @@ -53,12 +53,15 @@ object MtagsEnrichments extends ScalametaCommonEnrichments:
new SourcePosition(source, span)
end sourcePosition

def localContext(params: OffsetParams): Context =
if driver.currentCtx.run.units.isEmpty then
def latestRun =
if driver.currentCtx.run.units.nonEmpty then
driver.currentCtx.run.units.head
else
throw new RuntimeException(
"No source files were passed to the Scala 3 presentation compiler"
"No source files were compiled. Might be an error in the compiler itself."
)
val unit = driver.currentCtx.run.units.head
def localContext(params: OffsetParams): Context =
val unit = driver.latestRun
val pos = driver.sourcePosition(params)
val newctx = driver.currentCtx.fresh.setCompilationUnit(unit)
val tpdPath =
Expand Down
Expand Up @@ -34,7 +34,7 @@ final class AutoImportsProvider(
uri,
SourceFile.virtual(filePath.toString, params.text),
)
val unit = driver.currentCtx.run.units.head
val unit = driver.latestRun
val tree = unit.tpdTree

val pos = driver.sourcePosition(params)
Expand Down
Expand Up @@ -27,7 +27,7 @@ final class ConvertToNamedArgumentsProvider(
uri,
SourceFile.virtual(filePath.toString, params.text),
)
val unit = driver.currentCtx.run.units.head
val unit = driver.latestRun
val newctx = driver.currentCtx.fresh.setCompilationUnit(unit)
val pos = driver.sourcePosition(params)
val trees = driver.openedTrees(uri)
Expand Down
Expand Up @@ -42,7 +42,7 @@ final class ExtractMethodProvider(
val filePath = Paths.get(uri)
val source = SourceFile.virtual(filePath.toString, text)
driver.run(uri, source)
val unit = driver.currentCtx.run.units.head
val unit = driver.latestRun
val pos = driver.sourcePosition(range).startPos
val path =
Interactive.pathTo(driver.openedTrees(uri), pos)(using driver.currentCtx)
Expand Down
Expand Up @@ -69,7 +69,7 @@ final class InferredTypeProvider(
val source =
SourceFile.virtual(filePath.toString, sourceText)
driver.run(uri, source)
val unit = driver.currentCtx.run.units.head
val unit = driver.latestRun
val pos = driver.sourcePosition(params)
val path =
Interactive.pathTo(driver.openedTrees(uri), pos)(using driver.currentCtx)
Expand Down
Expand Up @@ -44,7 +44,7 @@ abstract class PcCollector[T](
driver.run(uri, source)
given ctx: Context = driver.currentCtx

val unit = driver.currentCtx.run.units.head
val unit = driver.latestRun
val compilatonUnitContext = ctx.fresh.setCompilationUnit(unit)
val offset = params match
case op: OffsetParams => op.offset()
Expand Down
Expand Up @@ -39,7 +39,7 @@ class PcInlayHintsProvider(
SourceFile.virtual(filePath.toString, sourceText)
driver.run(uri, source)
given ctx: Context = driver.currentCtx
val unit = driver.currentCtx.run.units.head
val unit = driver.latestRun
given InferredType.Text = InferredType.Text(text)
val pos = driver.sourcePosition(params)

Expand Down
Expand Up @@ -8,6 +8,7 @@ import java.nio.file.Paths
import scala.util.Properties

import scala.meta.internal.mtags.MD5
import scala.meta.internal.mtags.MtagsEnrichments.*

import dotty.tools.dotc.interactive.InteractiveDriver
import dotty.tools.dotc.semanticdb.ExtractSemanticDB
Expand All @@ -32,7 +33,7 @@ class SemanticdbTextDocumentProvider(
uri,
SourceFile.virtual(filePath.toString, validCode),
)
val tree = driver.currentCtx.run.units.head.tpdTree
val tree = driver.latestRun.tpdTree
val extract = ExtractSemanticDB()
val extractor = extract.Extractor()
extractor.traverse(tree)(using driver.currentCtx)
Expand Down
Expand Up @@ -177,7 +177,7 @@ object OverrideCompletions:
uri,
SourceFile.virtual(uri.toASCIIString, params.text),
)
val unit = driver.currentCtx.run.units.head
val unit = driver.latestRun
val pos = driver.sourcePosition(params)

val newctx = driver.currentCtx.fresh.setCompilationUnit(unit)
Expand Down

0 comments on commit 9590166

Please sign in to comment.