Skip to content

Commit 3cb1cfa

Browse files
committed
Automatically add missing files to SourcePath
1 parent 4e46844 commit 3cb1cfa

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/main/kotlin/org/javacs/kt/SourcePath.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,20 @@ class SourcePath(private val cp: CompilerClassPath) {
6060
private fun doPrepareCompiledFile(): CompiledFile =
6161
CompiledFile(content, compiledFile!!, compiledContext!!, compiledContainer!!, all(), cp)
6262
}
63+
64+
private fun sourceFile(file: Path): SourceFile {
65+
if (file !in files) {
66+
LOG.warn("File {} is not on SourcePath, adding it now...", file)
67+
put(file, file.toFile().readText())
68+
}
69+
return files[file]!!
70+
}
6371

6472
fun put(file: Path, content: String) {
6573
assert(!content.contains('\r'))
6674

67-
if (files.contains(file))
68-
files[file]!!.put(content)
75+
if (file in files)
76+
sourceFile(file).put(content)
6977
else
7078
files[file] = SourceFile(file, content)
7179
}
@@ -77,21 +85,21 @@ class SourcePath(private val cp: CompilerClassPath) {
7785
/**
7886
* Get the latest content of a file
7987
*/
80-
fun content(file: Path): String = files[file]!!.content
88+
fun content(file: Path): String = sourceFile(file).content
8189

82-
fun parsedFile(file: Path): KtFile = files[file]!!.parseIfChanged().parsed!!
90+
fun parsedFile(file: Path): KtFile = sourceFile(file).parseIfChanged().parsed!!
8391

8492
/**
8593
* Compile the latest version of a file
8694
*/
8795
fun currentVersion(file: Path): CompiledFile =
88-
files[file]!!.compileIfChanged().prepareCompiledFile()
96+
sourceFile(file).compileIfChanged().prepareCompiledFile()
8997

9098
/**
9199
* Return whatever is the most-recent already-compiled version of `file`
92100
*/
93101
fun latestCompiledVersion(file: Path): CompiledFile =
94-
files[file]!!.prepareCompiledFile()
102+
sourceFile(file).prepareCompiledFile()
95103

96104
/**
97105
* Compile changed files

0 commit comments

Comments
 (0)