Skip to content

Commit dffcba7

Browse files
committed
Using DocumentSymbol instead of SymbolInformation for documentSymbols so that a structure can be provided.
1 parent 777c664 commit dffcba7

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,21 @@ import org.jetbrains.kotlin.psi.*
1515
import org.jetbrains.kotlin.psi.psiUtil.parents
1616

1717
fun documentSymbols(file: KtFile): List<Either<SymbolInformation, DocumentSymbol>> =
18-
doDocumentSymbols(file).mapNotNull(::symbolInformation).toList().map { Either.forLeft<SymbolInformation, DocumentSymbol>(it) }
18+
doDocumentSymbols(file).toList().map { Either.forRight<SymbolInformation, DocumentSymbol>(it) }
1919

20-
private fun doDocumentSymbols(file: KtFile): Sequence<KtNamedDeclaration> =
21-
file.preOrderTraversal().mapNotNull { pickImportantElements(it, true) }
20+
private fun doDocumentSymbols(element: PsiElement): List<DocumentSymbol> {
21+
val children = element.children.flatMap(::doDocumentSymbols);
22+
val currentDecl = pickImportantElements(element, true);
23+
if (currentDecl != null) {
24+
val file = element.containingFile
25+
val span = range(file.text, currentDecl.textRange)
26+
val nameIdentifier = currentDecl.nameIdentifier
27+
val nameSpan = if (nameIdentifier != null) range(file.text, nameIdentifier.textRange) else span
28+
return listOf(DocumentSymbol(currentDecl.name?:"<anonymous>", symbolKind(currentDecl), span, nameSpan, null, children));
29+
} else {
30+
return children;
31+
}
32+
}
2233

2334
private const val MAX_SYMBOLS = 50
2435

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package org.javacs.kt
22

3+
import org.eclipse.lsp4j.DocumentSymbol
34
import org.eclipse.lsp4j.DocumentSymbolParams
5+
import org.eclipse.lsp4j.Position
6+
import org.eclipse.lsp4j.Range
47
import org.eclipse.lsp4j.SymbolKind
58
import org.eclipse.lsp4j.TextDocumentIdentifier
69
import org.hamcrest.Matchers.hasItem
710
import org.hamcrest.Matchers.not
8-
import org.junit.Assert.assertThat
11+
import org.junit.Assert.assertEquals
912
import org.junit.Before
1013
import org.junit.Test
1114

@@ -19,14 +22,14 @@ class DocumentSymbolsTest : LanguageServerTestFixture("symbols") {
1922
@Test fun `find document symbols`() {
2023
val fileId = TextDocumentIdentifier(uri(file).toString())
2124
val found = languageServer.textDocumentService.documentSymbol(DocumentSymbolParams(fileId)).get()
22-
val byKind = found.groupBy({ it.left.kind }, { it.left.name })
23-
val all = found.map { it.left.name }.toList()
25+
val expected = listOf(DocumentSymbol("DocumentSymbols", SymbolKind.Class, Range(Position(0, 0), Position(8, 1)), Range(Position(0, 14), Position(0, 29)), null, listOf(
26+
DocumentSymbol("DocumentSymbols", SymbolKind.Constructor, Range(Position(0, 29), Position(0, 31)), Range(Position(0, 29), Position(0, 31)), null, listOf()),
27+
DocumentSymbol("aProperty", SymbolKind.Property, Range(Position(1, 4), Position(1, 21)), Range(Position(1, 8), Position(1, 17)), null, listOf()),
28+
DocumentSymbol("aFunction", SymbolKind.Function, Range(Position(3, 4), Position(4, 5)), Range(Position(3, 8), Position(3, 17)), null, listOf()),
29+
DocumentSymbol("DocumentSymbols", SymbolKind.Constructor, Range(Position(6, 4), Position(7, 5)), Range(Position(6, 4), Position(7, 5)), null, listOf())
30+
)))
31+
val all = found.map { it.right }.toList()
2432

25-
assertThat(byKind[SymbolKind.Class], hasItem("DocumentSymbols"))
26-
assertThat(byKind[SymbolKind.Constructor], hasItem("DocumentSymbols"))
27-
assertThat(byKind[SymbolKind.Property], hasItem("aProperty"))
28-
assertThat(byKind[SymbolKind.Function], hasItem("aFunction"))
29-
assertThat(all, not(hasItem("aFunctionArg")))
30-
assertThat(all, not(hasItem("aConstructorArg")))
33+
assertEquals(all.toString(), expected, all)
3134
}
3235
}

0 commit comments

Comments
 (0)