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 10, 2024
1 parent f5e8afe commit f53ea56
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 5 deletions.
Expand Up @@ -53,6 +53,13 @@ object MtagsEnrichments extends ScalametaCommonEnrichments:
new SourcePosition(source, span)
end sourcePosition

def latestRun =
if driver.currentCtx.run.units.nonEmpty then
driver.currentCtx.run.units.head
else
throw new RuntimeException(
"No source files were compiled. Might be an error in the compiler itself."
)
def localContext(params: OffsetParams): Context =
if driver.currentCtx.run.units.isEmpty then
throw new RuntimeException(
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 @@ -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 @@ -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 @@ -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 f53ea56

Please sign in to comment.