Skip to content

Commit 7fb5b9b

Browse files
committed
Fixed Go To Definition
1 parent 9cf8d00 commit 7fb5b9b

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class JarClassContentProvider(
3535
* If the file is inside a source JAR, the source code is returned as is.
3636
*/
3737
public fun contentOf(uri: KlsURI): Pair<KlsURI, String> {
38-
val resolvedUri = sourceJarProvider.fetchSourceJar(uri.jarPath)?.let(uri::withJarPath) ?: uri
38+
val resolvedUri = sourceJarProvider.fetchSourceJar(uri.jarPath)?.let(uri.withSource(true)::withJarPath) ?: uri
3939
val key = resolvedUri.toString()
4040
val (contents, extension) = cachedContents[key] ?: run {
4141
LOG.info("Finding contents of {}", describeURI(uri.fileUri))
@@ -56,15 +56,16 @@ class JarClassContentProvider(
5656
}
5757

5858
private fun tryReadContentOf(uri: KlsURI): Pair<String, String>? = try {
59-
when (uri.fileExtension) {
60-
"class" -> Pair(uri.extractToTemporaryFile(tempDir)
59+
val actualUri = KlsURI(uri.fileUri, mapOf())
60+
when (actualUri.fileExtension) {
61+
"class" -> Pair(actualUri.extractToTemporaryFile(tempDir)
6162
.let(decompiler::decompileClass)
6263
.let { Files.newInputStream(it) }
6364
.bufferedReader()
6465
.use(BufferedReader::readText)
6566
.let(this::convertToKotlinIfNeeded), if (config.autoConvertToKotlin) "kt" else "java")
66-
"java" -> if (uri.source) Pair(uri.readContents(), "java") else Pair(convertToKotlinIfNeeded(uri.readContents()), "kt")
67-
else -> Pair(uri.readContents(), "kt") // e.g. for Kotlin source files
67+
"java" -> if (uri.source) Pair(actualUri.readContents(), "java") else Pair(convertToKotlinIfNeeded(actualUri.readContents()), "kt")
68+
else -> Pair(actualUri.readContents(), "kt") // e.g. for Kotlin source files
6869
}
6970
} catch (e: FileNotFoundException) { null }
7071
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.javacs.kt.externalsources
22

33
import org.javacs.kt.util.partitionAroundLast
44
import org.javacs.kt.util.TemporaryDirectory
5+
import org.javacs.kt.util.parseURI
56
import java.net.URI
67
import java.net.URL
78
import java.net.JarURLConnection
@@ -54,19 +55,20 @@ data class KlsURI(val fileUri: URI, val query: Map<QueryParam, Any>) {
5455
?.lastOrNull()
5556

5657
val jarPath: Path
57-
get() = Paths.get(fileUri.schemeSpecificPart.split("!")[0])
58+
get() = Paths.get(parseURI(fileUri.schemeSpecificPart.split("!")[0]))
5859
val innerPath: String?
5960
get() = fileUri.schemeSpecificPart.split("!", limit = 2).get(1)
6061

6162
val source: Boolean
62-
get() = query[QueryParam.SOURCE] as? Boolean ?: false
63+
get() = query[QueryParam.SOURCE].toString().toBoolean()
6364
val isCompiled: Boolean
6465
get() = fileExtension == "class"
6566

6667
constructor(uri: URI) : this(parseKlsURIFileURI(uri), parseKlsURIQuery(uri))
6768

68-
fun withJarPath(newJarPath: Path): KlsURI =
69-
KlsURI(URI(newJarPath.toUri().toString() + (innerPath?.let { "!$it" } ?: "")), query)
69+
// If the newJarPath doesn't have the kls scheme, it is added in the returned KlsURI.
70+
fun withJarPath(newJarPath: Path): KlsURI? =
71+
URI(newJarPath.toUri().toString() + (innerPath?.let { "!$it" } ?: "")).toKlsURI()?.let { KlsURI(it.fileUri, query) }
7072

7173
fun withFileExtension(newExtension: String): KlsURI {
7274
val (parentUri, fileName) = fileUri.toString().partitionAroundLast("/")

0 commit comments

Comments
 (0)