Skip to content

Commit 4056a8b

Browse files
author
Lorenz Bateman
committed
Fix compatibility issues + bugs
1) From LSP spec 3.16 to 3.17, dropping the location attribute from the response to `workspace/symbol` is only permitted if the client capability `workspace.symbol.resolveSupport` is advertized. Without this setting, the `location` attribute should be present. 2) `findDeclarationCursorSite` constructs a location using a non-uri path in the uri property. This leads to document symbol rename for example to not work. 3) THe stdio backend for logging accidentally overrides the stdout backend with the one meant for stderr.
1 parent eee8afa commit 4056a8b

File tree

6 files changed

+44
-13
lines changed

6 files changed

+44
-13
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.javacs.kt.position.changedRegion
77
import org.javacs.kt.position.location
88
import org.javacs.kt.position.position
99
import org.javacs.kt.position.range
10+
import org.javacs.kt.position.toURIString
1011
import org.javacs.kt.util.findParent
1112
import org.javacs.kt.util.nullResult
1213
import org.javacs.kt.util.toPath
@@ -214,7 +215,7 @@ class CompiledFile(
214215

215216
return declaration?.let {
216217
Pair(it,
217-
Location(it.containingFile.name,
218+
Location(it.containingFile.toURIString(),
218219
range(content, it.nameIdentifier?.textRange ?: return null)))
219220
}
220221
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ public data class CompilerConfiguration(
3939
val jvm: JVMConfiguration = JVMConfiguration()
4040
)
4141

42+
public data class SymbolResolveSupport(
43+
val enabled: Boolean = false,
44+
val properties: List<String> = emptyList()
45+
)
46+
47+
public data class WorkspaceConfiguration(
48+
var symbolResolveSupport: SymbolResolveSupport = SymbolResolveSupport()
49+
)
50+
4251
public data class IndexingConfiguration(
4352
/** Whether an index of global symbols should be built in the background. */
4453
var enabled: Boolean = true
@@ -94,5 +103,6 @@ public data class Configuration(
94103
val scripts: ScriptsConfiguration = ScriptsConfiguration(),
95104
val indexing: IndexingConfiguration = IndexingConfiguration(),
96105
val externalSources: ExternalSourcesConfiguration = ExternalSourcesConfiguration(),
97-
val inlayHints: InlayHintsConfiguration = InlayHintsConfiguration()
106+
val inlayHints: InlayHintsConfiguration = InlayHintsConfiguration(),
107+
val workspace: WorkspaceConfiguration = WorkspaceConfiguration()
98108
)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ class KotlinLanguageServer(
108108
serverCapabilities.renameProvider = Either.forRight(RenameOptions(false))
109109
}
110110

111+
config.workspace.symbolResolveSupport = clientCapabilities?.workspace?.symbol?.resolveSupport?.properties?.let { properties ->
112+
if (properties.size > 0) SymbolResolveSupport(true, properties) else null
113+
} ?: SymbolResolveSupport(false, emptyList())
114+
111115
@Suppress("DEPRECATION")
112116
val folders = params.workspaceFolders?.takeIf { it.isNotEmpty() }
113117
?: params.rootUri?.let(::WorkspaceFolder)?.let(::listOf)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class KotlinWorkspaceService(
173173

174174
@Suppress("DEPRECATION")
175175
override fun symbol(params: WorkspaceSymbolParams): CompletableFuture<Either<List<SymbolInformation>, List<WorkspaceSymbol>>> {
176-
val result = workspaceSymbols(params.query, sp)
176+
val result = workspaceSymbols(!config.workspace.symbolResolveSupport.enabled, params.query, sp)
177177

178178
return CompletableFuture.completedFuture(Either.forRight(result))
179179
}

server/src/main/kotlin/org/javacs/kt/symbols/Symbols.kt

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package org.javacs.kt.symbols
44

55
import com.intellij.psi.PsiElement
6+
import org.eclipse.lsp4j.Location
67
import org.eclipse.lsp4j.SymbolInformation
78
import org.eclipse.lsp4j.SymbolKind
89
import org.eclipse.lsp4j.DocumentSymbol
@@ -33,10 +34,10 @@ private fun doDocumentSymbols(element: PsiElement): List<DocumentSymbol> {
3334
} ?: children
3435
}
3536

36-
fun workspaceSymbols(query: String, sp: SourcePath): List<WorkspaceSymbol> =
37+
fun workspaceSymbols(locationRequired: Boolean, query: String, sp: SourcePath): List<WorkspaceSymbol> =
3738
doWorkspaceSymbols(sp)
3839
.filter { containsCharactersInOrder(it.name!!, query, false) }
39-
.mapNotNull(::workspaceSymbol)
40+
.mapNotNull(workspaceSymbol(locationRequired))
4041
.toList()
4142

4243
private fun doWorkspaceSymbols(sp: SourcePath): Sequence<KtNamedDeclaration> =
@@ -56,10 +57,18 @@ private fun pickImportantElements(node: PsiElement, includeLocals: Boolean): KtN
5657
else -> null
5758
}
5859

59-
private fun workspaceSymbol(d: KtNamedDeclaration): WorkspaceSymbol? {
60-
val name = d.name ?: return null
60+
private fun workspaceSymbol(locationRequired: Boolean): (KtNamedDeclaration) -> WorkspaceSymbol? {
61+
return { d ->
62+
d.name?.let { name ->
63+
val location: Either<Location, WorkspaceSymbolLocation>? = if (locationRequired) {
64+
location(d)?.let { l -> Either.forLeft(l) }
65+
} else {
66+
Either.forRight(workspaceSymbolLocation(d))
67+
}
6168

62-
return WorkspaceSymbol(name, symbolKind(d), Either.forRight(workspaceLocation(d)), symbolContainer(d))
69+
location?.let { WorkspaceSymbol(name, symbolKind(d), it, symbolContainer(d)) }
70+
}
71+
}
6372
}
6473

6574
private fun symbolKind(d: KtNamedDeclaration): SymbolKind =
@@ -73,10 +82,18 @@ private fun symbolKind(d: KtNamedDeclaration): SymbolKind =
7382
else -> throw IllegalArgumentException("Unexpected symbol $d")
7483
}
7584

76-
private fun workspaceLocation(d: KtNamedDeclaration): WorkspaceSymbolLocation {
77-
val file = d.containingFile
78-
val uri = file.toPath().toUri().toString()
85+
private fun location(d: KtNamedDeclaration): Location? {
86+
val uri = d.containingFile.toPath().toUri().toString()
87+
val (content, textRange) = try { d.containingFile?.text to d.nameIdentifier?.textRange } catch (e: Exception) { null to null }
88+
return if (content != null && textRange != null) {
89+
Location(uri, range(content, textRange))
90+
} else {
91+
null
92+
}
93+
}
7994

95+
private fun workspaceSymbolLocation(d: KtNamedDeclaration): WorkspaceSymbolLocation {
96+
val uri = d.containingFile.toPath().toUri().toString()
8097
return WorkspaceSymbolLocation(uri)
8198
}
8299

shared/src/main/kotlin/org/javacs/kt/Logger.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package org.javacs.kt
33
import java.io.PrintWriter
44
import java.io.StringWriter
55
import java.util.*
6-
import java.util.logging.Formatter
76
import java.util.logging.LogRecord
87
import java.util.logging.Handler
98
import java.util.logging.Level
@@ -136,7 +135,7 @@ class Logger {
136135

137136
fun connectStdioBackend() {
138137
connectOutputBackend { println(it.formatted) }
139-
connectOutputBackend { System.err.println(it.formatted) }
138+
connectErrorBackend { System.err.println(it.formatted) }
140139
}
141140

142141
private fun insertPlaceholders(msg: String, placeholders: Array<out Any?>): String {

0 commit comments

Comments
 (0)