Skip to content

Commit 32fb05c

Browse files
committed
Avoid exception in sourcePosition
1 parent 883726a commit 32fb05c

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

compiler/src/dotty/tools/dotc/interactive/InteractiveDriver.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ class InteractiveDriver(settings: List[String]) extends Driver {
4444

4545
def currentCtx: Context = myCtx
4646

47-
private val myOpenedFiles = new mutable.LinkedHashMap[URI, SourceFile]
47+
private val myOpenedFiles = new mutable.LinkedHashMap[URI, SourceFile] {
48+
override def default(key: URI) = NoSource
49+
}
4850

4951
private val myOpenedTrees = new mutable.LinkedHashMap[URI, List[SourceTree]] {
5052
override def default(key: URI) = Nil

language-server/src/dotty/tools/languageserver/DottyLanguageServer.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,12 @@ object DottyLanguageServer {
351351

352352
/** Convert an lsp4j.Position to a SourcePosition */
353353
def sourcePosition(driver: InteractiveDriver, uri: URI, pos: lsp4j.Position): SourcePosition = {
354-
val source = driver.openedFiles(uri) // might throw exception
355-
val p = Positions.Position(source.lineToOffset(pos.getLine) + pos.getCharacter)
356-
new SourcePosition(source, p)
354+
val source = driver.openedFiles(uri)
355+
if (source.exists) {
356+
val p = Positions.Position(source.lineToOffset(pos.getLine) + pos.getCharacter)
357+
new SourcePosition(source, p)
358+
}
359+
else NoSourcePosition
357360
}
358361

359362
/** Convert a SourcePosition to an lsp4j.Range */

0 commit comments

Comments
 (0)