@@ -53,10 +53,11 @@ class SourcePath(
53
53
val isTemporary : Boolean = false , // A temporary source file will not be returned by .all()
54
54
var lastSavedFile : KtFile ? = null ,
55
55
) {
56
- val extension: String? = uri.fileExtension ? : " kt" // TODO: Use language?.associatedFileType?.defaultExtension again
56
+ val extension: String =
57
+ uri.fileExtension ? : " kt" // TODO: Use language?.associatedFileType?.defaultExtension again
57
58
val isScript: Boolean = extension == " kts"
58
59
val kind: CompilationKind =
59
- if (path?.fileName?.toString()?.endsWith(" .gradle.kts" ) ? : false ) CompilationKind .BUILD_SCRIPT
60
+ if (path?.fileName?.toString()?.endsWith(" .gradle.kts" ) == true ) CompilationKind .BUILD_SCRIPT
60
61
else CompilationKind .DEFAULT
61
62
62
63
fun put (newContent : String ) {
@@ -115,25 +116,29 @@ class SourcePath(
115
116
}
116
117
117
118
fun prepareCompiledFile (): CompiledFile =
118
- parseIfChanged().apply { compileIfNull() }.let { doPrepareCompiledFile() }
119
+ parseIfChanged().apply { compileIfNull() }.let { doPrepareCompiledFile() }
119
120
120
121
private fun doPrepareCompiledFile (): CompiledFile =
121
- CompiledFile (content, compiledFile!! , compiledContext!! , module!! , allIncludingThis(), cp, isScript, kind)
122
+ CompiledFile (content, compiledFile!! , compiledContext!! , module!! , allIncludingThis(), cp, isScript, kind)
122
123
123
124
private fun allIncludingThis (): Collection <KtFile > = parseIfChanged().let {
124
125
if (isTemporary) (all().asSequence() + sequenceOf(parsed!! )).toList()
125
126
else all()
126
127
}
127
128
128
129
// Creates a shallow copy
129
- fun clone (): SourceFile = SourceFile (uri, content, path, parsed, compiledFile, compiledContext, module, language, isTemporary)
130
+ fun clone (): SourceFile =
131
+ SourceFile (uri, content, path, parsed, compiledFile, compiledContext, module, language, isTemporary)
130
132
}
131
133
132
134
private fun sourceFile (uri : URI ): SourceFile {
133
135
if (uri !in files) {
134
136
// Fallback solution, usually *all* source files
135
137
// should be added/opened through SourceFiles
136
- LOG .warn(" Requested source file {} is not on source path, this is most likely a bug. Adding it now temporarily..." , describeURI(uri))
138
+ LOG .warn(
139
+ " Requested source file {} is not on source path, this is most likely a bug. Adding it now temporarily..." ,
140
+ describeURI(uri)
141
+ )
137
142
put(uri, contentProvider.contentOf(uri), null , temporary = true )
138
143
}
139
144
return files[uri]!!
@@ -182,13 +187,54 @@ class SourcePath(
182
187
* Compile the latest version of a file
183
188
*/
184
189
fun currentVersion (uri : URI ): CompiledFile =
185
- sourceFile(uri).apply { compileIfChanged() }.prepareCompiledFile()
190
+ sourceFile(uri).apply { compileIfChanged() }.prepareCompiledFile()
186
191
187
192
/* *
188
193
* Return whatever is the most-recent already-compiled version of `file`
189
194
*/
190
195
fun latestCompiledVersion (uri : URI ): CompiledFile =
191
- sourceFile(uri).prepareCompiledFile()
196
+ sourceFile(uri).prepareCompiledFile()
197
+
198
+ // Compile changed files
199
+ private fun compileAndUpdate (changed : List <SourceFile >, kind : CompilationKind ): BindingContext ? {
200
+ if (changed.isEmpty()) return null
201
+
202
+ // Get clones of the old files, so we can remove the old declarations from the index
203
+ val oldFiles = changed.mapNotNull {
204
+ if (it.compiledFile?.text != it.content || it.parsed?.text != it.content) {
205
+ it.clone()
206
+ } else {
207
+ null
208
+ }
209
+ }
210
+
211
+ // Parse the files that have changed
212
+ val parse = changed.associateWith { it.apply { parseIfChanged() }.parsed!! }
213
+
214
+ // Get all the files. This will parse them if they changed
215
+ val allFiles = all()
216
+ beforeCompileCallback.invoke()
217
+ val (context, module) = cp.compiler.compileKtFiles(parse.values, allFiles, kind)
218
+
219
+ // Update cache
220
+ for ((f, parsed) in parse) {
221
+ parseDataWriteLock.withLock {
222
+ if (f.parsed == parsed) {
223
+ // only updated if the parsed file didn't change:
224
+ f.compiledFile = parsed
225
+ f.compiledContext = context
226
+ f.module = module
227
+ }
228
+ }
229
+ }
230
+
231
+ // Only index normal files, not build files
232
+ if (kind == CompilationKind .DEFAULT ) {
233
+ refreshWorkspaceIndexes(oldFiles, parse.keys.toList())
234
+ }
235
+
236
+ return context
237
+ }
192
238
193
239
/* *
194
240
* Compile changed files
@@ -199,53 +245,13 @@ class SourcePath(
199
245
val allChanged = sources.filter { it.content != it.compiledFile?.text }
200
246
val (changedBuildScripts, changedSources) = allChanged.partition { it.kind == CompilationKind .BUILD_SCRIPT }
201
247
202
- // Compile changed files
203
- fun compileAndUpdate (changed : List <SourceFile >, kind : CompilationKind ): BindingContext ? {
204
- if (changed.isEmpty()) return null
205
-
206
- // Get clones of the old files, so we can remove the old declarations from the index
207
- val oldFiles = changed.mapNotNull {
208
- if (it.compiledFile?.text != it.content || it.parsed?.text != it.content) {
209
- it.clone()
210
- } else {
211
- null
212
- }
213
- }
214
-
215
- // Parse the files that have changed
216
- val parse = changed.associateWith { it.apply { parseIfChanged() }.parsed!! }
217
-
218
- // Get all the files. This will parse them if they changed
219
- val allFiles = all()
220
- beforeCompileCallback.invoke()
221
- val (context, module) = cp.compiler.compileKtFiles(parse.values, allFiles, kind)
222
-
223
- // Update cache
224
- for ((f, parsed) in parse) {
225
- parseDataWriteLock.withLock {
226
- if (f.parsed == parsed) {
227
- // only updated if the parsed file didn't change:
228
- f.compiledFile = parsed
229
- f.compiledContext = context
230
- f.module = module
231
- }
232
- }
233
- }
234
-
235
- // Only index normal files, not build files
236
- if (kind == CompilationKind .DEFAULT ) {
237
- refreshWorkspaceIndexes(oldFiles, parse.keys.toList())
238
- }
239
-
240
- return context
241
- }
242
248
243
249
val buildScriptsContext = compileAndUpdate(changedBuildScripts, CompilationKind .BUILD_SCRIPT )
244
250
val sourcesContext = compileAndUpdate(changedSources, CompilationKind .DEFAULT )
245
251
246
252
// Combine with past compilations
247
- val same = sources - allChanged
248
- val combined = listOf (buildScriptsContext, sourcesContext).filterNotNull( ) + same.map { it.compiledContext!! }
253
+ val same = sources - allChanged.toSet()
254
+ val combined = listOfNotNull (buildScriptsContext, sourcesContext) + same.map { it.compiledContext!! }
249
255
250
256
return CompositeBindingContext .create(combined)
251
257
}
@@ -351,7 +357,7 @@ class SourcePath(
351
357
* Get parsed trees for all .kt files on source path
352
358
*/
353
359
fun all (includeHidden : Boolean = false): Collection <KtFile > =
354
- files.values
355
- .filter { includeHidden || ! it.isTemporary }
356
- .map { it.apply { parseIfChanged() }.parsed!! }
360
+ files.values
361
+ .filter { includeHidden || ! it.isTemporary }
362
+ .map { it.apply { parseIfChanged() }.parsed!! }
357
363
}
0 commit comments