Skip to content

Commit 551ed92

Browse files
committed
Improve source file finder and include Java source files
1 parent 17f032e commit 551ed92

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

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

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.javacs.kt
22

33
import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtil.convertLineSeparators
4+
import org.jetbrains.kotlin.com.intellij.lang.java.JavaLanguage
5+
import org.jetbrains.kotlin.com.intellij.lang.Language
6+
import org.jetbrains.kotlin.idea.KotlinLanguage
47
import org.eclipse.lsp4j.TextDocumentContentChangeEvent
58
import org.javacs.kt.util.KotlinLSException
69
import org.javacs.kt.util.filePath
@@ -116,14 +119,16 @@ class SourceFiles(
116119
}
117120

118121
fun deletedOnDisk(uri: URI) {
119-
if (isSource(uri))
122+
if (isSource(uri)) {
120123
files.remove(uri)
124+
}
121125
}
122126

123127
fun changedOnDisk(uri: URI) {
124-
if (isSource(uri))
128+
if (isSource(uri)) {
125129
files[uri] = readFromDisk(uri, files[uri]?.isTemporary ?: true)
126130
?: throw KotlinLSException("Could not read source file '$uri' after being changed on disk")
131+
}
127132
}
128133

129134
private fun readFromDisk(uri: URI, temporary: Boolean): SourceVersion? = try {
@@ -136,20 +141,31 @@ class SourceFiles(
136141
null
137142
}
138143

139-
private fun isSource(uri: URI): Boolean {
144+
private fun isSource(uri: URI): Boolean = exclusions.isURIIncluded(uri) && languageOf(uri) != null
145+
146+
private fun languageOf(uri: URI): Language? {
140147
val path = uri.path
141-
return (path.endsWith(".kt") || path.endsWith(".kts")) && exclusions.isURIIncluded(uri)
148+
return when {
149+
path.endsWith(".kt") || path.endsWith(".kts") -> KotlinLanguage.INSTANCE
150+
path.endsWith(".java") -> JavaLanguage.INSTANCE
151+
else -> null
152+
}
142153
}
143154

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

147163
logAdded(addSources, root)
148164

149-
for (file in addSources) {
150-
readFromDisk(file.toUri(), temporary = false)?.let {
151-
files[file.toUri()] = it
152-
} ?: LOG.warn("Could not read source file '{}'", file)
165+
for (uri in addSources) {
166+
readFromDisk(uri, temporary = false)?.let {
167+
files[uri] = it
168+
} ?: LOG.warn("Could not read source file '{}'", uri.path)
153169
}
154170

155171
workspaceRoots.add(root)
@@ -159,7 +175,7 @@ class SourceFiles(
159175
fun removeWorkspaceRoot(root: Path) {
160176
val rmSources = files.keys.filter { it.filePath?.startsWith(root) ?: false }
161177

162-
logRemoved(rmSources.map(Paths::get), root)
178+
logRemoved(rmSources, root)
163179

164180
files.removeAll(rmSources)
165181
workspaceRoots.remove(root)
@@ -205,18 +221,10 @@ private fun patch(sourceText: String, change: TextDocumentContentChangeEvent): S
205221
}
206222
}
207223

208-
private fun findSourceFiles(root: Path): Set<Path> {
209-
val pattern = FileSystems.getDefault().getPathMatcher("glob:*.{kt,kts}")
210-
val exclusions = SourceExclusions(root)
211-
return Files.walk(root)
212-
.filter { pattern.matches(it.fileName) && exclusions.isPathIncluded(it) }
213-
.collect(Collectors.toSet())
214-
}
215-
216-
private fun logAdded(sources: Collection<Path>, rootPath: Path?) {
217-
LOG.info("Adding {} under {} to source path", describeURIs(sources.map(Path::toUri)), rootPath)
224+
private fun logAdded(sources: Collection<URI>, rootPath: Path?) {
225+
LOG.info("Adding {} under {} to source path", describeURIs(sources), rootPath)
218226
}
219227

220-
private fun logRemoved(sources: Collection<Path>, rootPath: Path?) {
221-
LOG.info("Removing {} under {} to source path", describeURIs(sources.map(Path::toUri)), rootPath)
228+
private fun logRemoved(sources: Collection<URI>, rootPath: Path?) {
229+
LOG.info("Removing {} under {} to source path", describeURIs(sources), rootPath)
222230
}

0 commit comments

Comments
 (0)