@@ -53,10 +53,11 @@ class SourcePath(
5353 val isTemporary : Boolean = false , // A temporary source file will not be returned by .all()
5454 var lastSavedFile : KtFile ? = null ,
5555 ) {
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
5758 val isScript: Boolean = extension == " kts"
5859 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
6061 else CompilationKind .DEFAULT
6162
6263 fun put (newContent : String ) {
@@ -115,25 +116,29 @@ class SourcePath(
115116 }
116117
117118 fun prepareCompiledFile (): CompiledFile =
118- parseIfChanged().apply { compileIfNull() }.let { doPrepareCompiledFile() }
119+ parseIfChanged().apply { compileIfNull() }.let { doPrepareCompiledFile() }
119120
120121 private fun doPrepareCompiledFile (): CompiledFile =
121- CompiledFile (content, compiledFile!! , compiledContext!! , module!! , allIncludingThis(), cp, isScript, kind)
122+ CompiledFile (content, compiledFile!! , compiledContext!! , module!! , allIncludingThis(), cp, isScript, kind)
122123
123124 private fun allIncludingThis (): Collection <KtFile > = parseIfChanged().let {
124125 if (isTemporary) (all().asSequence() + sequenceOf(parsed!! )).toList()
125126 else all()
126127 }
127128
128129 // 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)
130132 }
131133
132134 private fun sourceFile (uri : URI ): SourceFile {
133135 if (uri !in files) {
134136 // Fallback solution, usually *all* source files
135137 // 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+ )
137142 put(uri, contentProvider.contentOf(uri), null , temporary = true )
138143 }
139144 return files[uri]!!
@@ -182,13 +187,54 @@ class SourcePath(
182187 * Compile the latest version of a file
183188 */
184189 fun currentVersion (uri : URI ): CompiledFile =
185- sourceFile(uri).apply { compileIfChanged() }.prepareCompiledFile()
190+ sourceFile(uri).apply { compileIfChanged() }.prepareCompiledFile()
186191
187192 /* *
188193 * Return whatever is the most-recent already-compiled version of `file`
189194 */
190195 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+ }
192238
193239 /* *
194240 * Compile changed files
@@ -199,53 +245,13 @@ class SourcePath(
199245 val allChanged = sources.filter { it.content != it.compiledFile?.text }
200246 val (changedBuildScripts, changedSources) = allChanged.partition { it.kind == CompilationKind .BUILD_SCRIPT }
201247
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- }
242248
243249 val buildScriptsContext = compileAndUpdate(changedBuildScripts, CompilationKind .BUILD_SCRIPT )
244250 val sourcesContext = compileAndUpdate(changedSources, CompilationKind .DEFAULT )
245251
246252 // 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!! }
249255
250256 return CompositeBindingContext .create(combined)
251257 }
@@ -351,7 +357,7 @@ class SourcePath(
351357 * Get parsed trees for all .kt files on source path
352358 */
353359 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!! }
357363}
0 commit comments