Skip to content

Commit 2022884

Browse files
committed
Refactor source archive providers
1 parent c31709c commit 2022884

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import org.eclipse.lsp4j.services.LanguageClient
77
import org.eclipse.lsp4j.services.LanguageClientAware
88
import org.eclipse.lsp4j.services.LanguageServer
99
import org.javacs.kt.command.ALL_COMMANDS
10-
import org.javacs.kt.externalsources.ClassContentProvider
11-
import org.javacs.kt.externalsources.ClassPathSourceArchiveProvider
10+
import org.javacs.kt.externalsources.*
1211
import org.javacs.kt.util.AsyncExecutor
1312
import org.javacs.kt.util.TemporaryDirectory
1413
import org.javacs.kt.util.parseURI
@@ -25,7 +24,7 @@ class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
2524
val classPath = CompilerClassPath(config.compiler)
2625

2726
private val tempDirectory = TemporaryDirectory()
28-
private val uriContentProvider = URIContentProvider(ClassContentProvider(config.externalSources, classPath, tempDirectory, ClassPathSourceArchiveProvider(classPath)))
27+
private val uriContentProvider = URIContentProvider(ClassContentProvider(config.externalSources, classPath, tempDirectory, CompositeSourceArchiveProvider(JdkSourceArchiveProvider(classPath), ClassPathSourceArchiveProvider(classPath))))
2928
val sourcePath = SourcePath(classPath, uriContentProvider, config.indexing)
3029
val sourceFiles = SourceFiles(sourcePath, uriContentProvider)
3130

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,11 @@
11
package org.javacs.kt.externalsources
22

33
import org.javacs.kt.CompilerClassPath
4-
import java.io.File
54
import java.nio.file.Path
6-
import java.nio.file.Paths
75

86
class ClassPathSourceArchiveProvider(
97
private val cp: CompilerClassPath
108
) : SourceArchiveProvider {
119
override fun fetchSourceArchive(compiledArchive: Path): Path? =
12-
getJdkSource(compiledArchive) ?: cp.classPath.firstOrNull { it.compiledJar == compiledArchive }?.sourceJar
13-
14-
/**
15-
* Checks if the given path is inside the JDK. If it is, we return the corresponding source zip.
16-
* Note that this method currently doesn't take into the account the JDK version, which means JDK source code
17-
* is only available for JDK 9+ builds.
18-
* TODO: improve this resolution logic to work for older JDK versions as well.
19-
*/
20-
private fun getJdkSource(path: Path): Path? {
21-
cp.javaHome?.let {
22-
val javaHomePath = File(it).toPath()
23-
if (path == javaHomePath) {
24-
return Paths.get(path.toString(), "lib", "src.zip")
25-
}
26-
}
27-
return null
28-
}
10+
cp.classPath.firstOrNull { it.compiledJar == compiledArchive }?.sourceJar
2911
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.javacs.kt.externalsources
2+
3+
import org.javacs.kt.CompilerClassPath
4+
import java.io.File
5+
import java.nio.file.Path
6+
import java.nio.file.Paths
7+
8+
class JdkSourceArchiveProvider(
9+
private val cp: CompilerClassPath
10+
) : SourceArchiveProvider {
11+
12+
/**
13+
* Checks if the given path is inside the JDK. If it is, we return the corresponding source zip.
14+
* Note that this method currently doesn't take into the account the JDK version, which means JDK source code
15+
* is only available for JDK 9+ builds.
16+
* TODO: improve this resolution logic to work for older JDK versions as well.
17+
*/
18+
override fun fetchSourceArchive(compiledArchive: Path): Path? {
19+
cp.javaHome?.let {
20+
val javaHomePath = File(it).toPath()
21+
if (compiledArchive == javaHomePath) {
22+
return Paths.get(compiledArchive.toString(), "lib", "src.zip")
23+
}
24+
}
25+
return null
26+
}
27+
}

server/src/main/kotlin/org/javacs/kt/externalsources/SourceArchiveProvider.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ import java.nio.file.Path
55
interface SourceArchiveProvider {
66
fun fetchSourceArchive(compiledArchive: Path): Path?
77
}
8+
9+
class CompositeSourceArchiveProvider(val lhs: SourceArchiveProvider, val rhs: SourceArchiveProvider) : SourceArchiveProvider {
10+
override fun fetchSourceArchive(compiledArchive: Path): Path? =
11+
lhs.fetchSourceArchive(compiledArchive) ?: rhs.fetchSourceArchive(compiledArchive)
12+
}

0 commit comments

Comments
 (0)