1
1
package org.javacs.kt
2
2
3
3
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
4
7
import org.eclipse.lsp4j.TextDocumentContentChangeEvent
5
8
import org.javacs.kt.util.KotlinLSException
6
9
import org.javacs.kt.util.filePath
@@ -116,14 +119,16 @@ class SourceFiles(
116
119
}
117
120
118
121
fun deletedOnDisk (uri : URI ) {
119
- if (isSource(uri))
122
+ if (isSource(uri)) {
120
123
files.remove(uri)
124
+ }
121
125
}
122
126
123
127
fun changedOnDisk (uri : URI ) {
124
- if (isSource(uri))
128
+ if (isSource(uri)) {
125
129
files[uri] = readFromDisk(uri, files[uri]?.isTemporary ? : true )
126
130
? : throw KotlinLSException (" Could not read source file '$uri ' after being changed on disk" )
131
+ }
127
132
}
128
133
129
134
private fun readFromDisk (uri : URI , temporary : Boolean ): SourceVersion ? = try {
@@ -136,20 +141,31 @@ class SourceFiles(
136
141
null
137
142
}
138
143
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 ? {
140
147
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
+ }
142
153
}
143
154
155
+ private fun findSourceFiles (root : Path ): Set <URI > = Files .walk(root)
156
+ .map(Path ::toUri)
157
+ .filter(this ::isSource)
158
+ .collect(Collectors .toSet())
159
+
144
160
fun addWorkspaceRoot (root : Path ) {
145
161
val addSources = findSourceFiles(root)
146
162
147
163
logAdded(addSources, root)
148
164
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 )
153
169
}
154
170
155
171
workspaceRoots.add(root)
@@ -159,7 +175,7 @@ class SourceFiles(
159
175
fun removeWorkspaceRoot (root : Path ) {
160
176
val rmSources = files.keys.filter { it.filePath?.startsWith(root) ? : false }
161
177
162
- logRemoved(rmSources.map( Paths ::get) , root)
178
+ logRemoved(rmSources, root)
163
179
164
180
files.removeAll(rmSources)
165
181
workspaceRoots.remove(root)
@@ -205,18 +221,10 @@ private fun patch(sourceText: String, change: TextDocumentContentChangeEvent): S
205
221
}
206
222
}
207
223
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)
218
226
}
219
227
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)
222
230
}
0 commit comments