Skip to content

Commit 716c9a7

Browse files
committed
Store source language in SourceFile
1 parent 551ed92 commit 716c9a7

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

server/src/main/kotlin/org/javacs/kt/SourceFiles.kt

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import java.nio.file.Path
2222
import java.nio.file.Paths
2323
import java.util.stream.Collectors
2424

25-
private class SourceVersion(val content: String, val version: Int, val isTemporary: Boolean)
25+
private class SourceVersion(val content: String, val version: Int, val language: Language?, val isTemporary: Boolean)
2626

2727
/**
2828
* Notify SourcePath whenever a file changes
@@ -36,7 +36,7 @@ private class NotifySourcePath(private val sp: SourcePath) {
3636
val content = convertLineSeparators(source.content)
3737

3838
files[uri] = source
39-
sp.put(uri, content, source.isTemporary)
39+
sp.put(uri, content, source.language, source.isTemporary)
4040
}
4141

4242
fun remove(uri: URI) {
@@ -74,7 +74,7 @@ class SourceFiles(
7474
private val open = mutableSetOf<URI>()
7575

7676
fun open(uri: URI, content: String, version: Int) {
77-
files[uri] = SourceVersion(content, version, isTemporary = !exclusions.isURIIncluded(uri))
77+
files[uri] = SourceVersion(content, version, languageOf(uri), isTemporary = !exclusions.isURIIncluded(uri))
7878
open.add(uri)
7979
}
8080

@@ -110,7 +110,7 @@ class SourceFiles(
110110
else newText = patch(newText, change)
111111
}
112112

113-
files[uri] = SourceVersion(newText, newVersion, existing.isTemporary)
113+
files[uri] = SourceVersion(newText, newVersion, existing.language, existing.isTemporary)
114114
}
115115
}
116116

@@ -133,7 +133,7 @@ class SourceFiles(
133133

134134
private fun readFromDisk(uri: URI, temporary: Boolean): SourceVersion? = try {
135135
val content = contentProvider.contentOf(uri)
136-
SourceVersion(content, -1, isTemporary = temporary)
136+
SourceVersion(content, -1, languageOf(uri), isTemporary = temporary)
137137
} catch (e: FileNotFoundException) {
138138
null
139139
} catch (e: IOException) {
@@ -144,19 +144,14 @@ class SourceFiles(
144144
private fun isSource(uri: URI): Boolean = exclusions.isURIIncluded(uri) && languageOf(uri) != null
145145

146146
private fun languageOf(uri: URI): Language? {
147-
val path = uri.path
147+
val fileName = uri.filePath?.fileName?.toString() ?: return null
148148
return when {
149-
path.endsWith(".kt") || path.endsWith(".kts") -> KotlinLanguage.INSTANCE
150-
path.endsWith(".java") -> JavaLanguage.INSTANCE
149+
fileName.endsWith(".kt") || fileName.endsWith(".kts") -> KotlinLanguage.INSTANCE
150+
fileName.endsWith(".java") -> JavaLanguage.INSTANCE
151151
else -> null
152152
}
153153
}
154154

155-
private fun findSourceFiles(root: Path): Set<URI> = Files.walk(root)
156-
.map(Path::toUri)
157-
.filter(this::isSource)
158-
.collect(Collectors.toSet())
159-
160155
fun addWorkspaceRoot(root: Path) {
161156
val addSources = findSourceFiles(root)
162157

@@ -221,6 +216,15 @@ private fun patch(sourceText: String, change: TextDocumentContentChangeEvent): S
221216
}
222217
}
223218

219+
private fun findSourceFiles(root: Path): Set<URI> {
220+
val sourceMatcher = FileSystems.getDefault().getPathMatcher("glob:*.{kt,kts,java}")
221+
val exclusions = SourceExclusions(root)
222+
return Files.walk(root)
223+
.filter { exclusions.isPathIncluded(it) && sourceMatcher.matches(it.fileName) }
224+
.map(Path::toUri)
225+
.collect(Collectors.toSet())
226+
}
227+
224228
private fun logAdded(sources: Collection<URI>, rootPath: Path?) {
225229
LOG.info("Adding {} under {} to source path", describeURIs(sources), rootPath)
226230
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.javacs.kt
22

33
import org.javacs.kt.util.filePath
44
import org.javacs.kt.util.describeURI
5+
import org.jetbrains.kotlin.com.intellij.lang.Language
56
import org.jetbrains.kotlin.container.ComponentProvider
67
import org.jetbrains.kotlin.psi.KtFile
78
import org.jetbrains.kotlin.resolve.BindingContext
@@ -29,6 +30,7 @@ class SourcePath(
2930
var compiledFile: KtFile? = null,
3031
var compiledContext: BindingContext? = null,
3132
var compiledContainer: ComponentProvider? = null,
33+
val language: Language? = null,
3234
val isTemporary: Boolean = false // A temporary source file will not be returned by .all()
3335
) {
3436
val isScript: Boolean = uri.toString().endsWith(".kts")
@@ -42,7 +44,7 @@ class SourcePath(
4244

4345
fun parseIfChanged(): SourceFile {
4446
if (content != parsed?.text) {
45-
parsed = cp.compiler.createFile(content, path ?: Paths.get("sourceFile.virtual.kt"), kind)
47+
parsed = cp.compiler.createFile(content, path ?: Paths.get("sourceFile.virtual.${language?.associatedFileType?.defaultExtension ?: "kt"}"), kind)
4648
}
4749

4850
return this
@@ -92,12 +94,12 @@ class SourcePath(
9294
// Fallback solution, usually *all* source files
9395
// should be added/opened through SourceFiles
9496
LOG.warn("Requested source file {} is not on source path, this is most likely a bug. Adding it now temporarily...", describeURI(uri))
95-
put(uri, contentProvider.contentOf(uri), temporary = true)
97+
put(uri, contentProvider.contentOf(uri), null, temporary = true)
9698
}
9799
return files[uri]!!
98100
}
99101

100-
fun put(uri: URI, content: String, temporary: Boolean = false) {
102+
fun put(uri: URI, content: String, language: Language?, temporary: Boolean = false) {
101103
assert(!content.contains('\r'))
102104

103105
if (temporary) {
@@ -107,7 +109,7 @@ class SourcePath(
107109
if (uri in files) {
108110
sourceFile(uri).put(content)
109111
} else {
110-
files[uri] = SourceFile(uri, content, isTemporary = temporary)
112+
files[uri] = SourceFile(uri, content, language = language, isTemporary = temporary)
111113
}
112114
}
113115

0 commit comments

Comments
 (0)