Skip to content

Commit faf492e

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 24c2e41 commit faf492e

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

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

Lines changed: 10 additions & 0 deletions
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
@@ -108,4 +117,5 @@ public data class Configuration(
108117
val externalSources: ExternalSourcesConfiguration = ExternalSourcesConfiguration(),
109118
val inlayHints: InlayHintsConfiguration = InlayHintsConfiguration(),
110119
val formatting: FormattingConfiguration = FormattingConfiguration(),
120+
val workspace: WorkspaceConfiguration = WorkspaceConfiguration()
111121
)

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
@@ -189,7 +189,7 @@ class KotlinWorkspaceService(
189189

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

194194
return CompletableFuture.completedFuture(Either.forRight(result))
195195
}

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)