From 6e8cd43a0b86e2d9ad3607094e9f01b2a773416d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20F=C3=B6rster?= Date: Mon, 24 Dec 2018 20:27:32 +0100 Subject: [PATCH] Refactor document symbols --- src/main/kotlin/texlab/BibtexDocument.kt | 16 ---------- src/main/kotlin/texlab/Document.kt | 22 +++++++++++--- src/main/kotlin/texlab/LatexDocument.kt | 19 ------------ .../kotlin/texlab/TextDocumentServiceImpl.kt | 19 ++++++++---- .../texlab/symbol/AggregateSymbolProvider.kt | 9 ++++++ .../symbol/LatexEnvironmentSymbolProvider.kt | 25 ++++++++++++++++ .../kotlin/texlab/symbol/SymbolProvider.kt | 8 +++++ .../kotlin/texlab/symbol/SymbolRequest.kt | 5 ++++ .../latex/LatexEnvironmentSymbolFinder.kt | 29 ------------------- .../LatexEnvironmentSymbolFinderTests.kt | 24 --------------- 10 files changed, 79 insertions(+), 97 deletions(-) delete mode 100644 src/main/kotlin/texlab/BibtexDocument.kt delete mode 100644 src/main/kotlin/texlab/LatexDocument.kt create mode 100644 src/main/kotlin/texlab/symbol/AggregateSymbolProvider.kt create mode 100644 src/main/kotlin/texlab/symbol/LatexEnvironmentSymbolProvider.kt create mode 100644 src/main/kotlin/texlab/symbol/SymbolProvider.kt create mode 100644 src/main/kotlin/texlab/symbol/SymbolRequest.kt delete mode 100644 src/main/kotlin/texlab/syntax/latex/LatexEnvironmentSymbolFinder.kt delete mode 100644 src/test/kotlin/texlab/syntax/latex/LatexEnvironmentSymbolFinderTests.kt diff --git a/src/main/kotlin/texlab/BibtexDocument.kt b/src/main/kotlin/texlab/BibtexDocument.kt deleted file mode 100644 index adb779cb..00000000 --- a/src/main/kotlin/texlab/BibtexDocument.kt +++ /dev/null @@ -1,16 +0,0 @@ -package texlab - -import org.eclipse.lsp4j.DocumentSymbol -import java.net.URI - -class BibtexDocument(uri: URI) : Document(uri) { - - override fun analyze() { - // TODO - } - - override fun documentSymbol(workspace: Workspace): List { - // TODO - return emptyList() - } -} diff --git a/src/main/kotlin/texlab/Document.kt b/src/main/kotlin/texlab/Document.kt index 03ac3bea..4918f500 100644 --- a/src/main/kotlin/texlab/Document.kt +++ b/src/main/kotlin/texlab/Document.kt @@ -1,11 +1,11 @@ package texlab -import org.eclipse.lsp4j.DocumentSymbol import org.eclipse.lsp4j.TextDocumentContentChangeEvent import texlab.syntax.CharStream +import texlab.syntax.latex.LatexSyntaxTree import java.net.URI -abstract class Document(val uri: URI) { +sealed class Document(val uri: URI) { private var version: Int = -1 @@ -36,6 +36,20 @@ abstract class Document(val uri: URI) { } protected abstract fun analyze() - - abstract fun documentSymbol(workspace: Workspace): List +} + +class LatexDocument(uri: URI) : Document(uri) { + + var tree: LatexSyntaxTree = LatexSyntaxTree(text) + + override fun analyze() { + tree = LatexSyntaxTree(text) + } +} + +class BibtexDocument(uri: URI) : Document(uri) { + + override fun analyze() { + // TODO + } } diff --git a/src/main/kotlin/texlab/LatexDocument.kt b/src/main/kotlin/texlab/LatexDocument.kt deleted file mode 100644 index fe6d84c3..00000000 --- a/src/main/kotlin/texlab/LatexDocument.kt +++ /dev/null @@ -1,19 +0,0 @@ -package texlab - -import org.eclipse.lsp4j.DocumentSymbol -import texlab.syntax.latex.LatexEnvironmentSymbolFinder -import texlab.syntax.latex.LatexSyntaxTree -import java.net.URI - -class LatexDocument(uri: URI) : Document(uri) { - - var tree: LatexSyntaxTree = LatexSyntaxTree(text) - - override fun analyze() { - tree = LatexSyntaxTree(text) - } - - override fun documentSymbol(workspace: Workspace): List { - return LatexEnvironmentSymbolFinder.find(tree) - } -} diff --git a/src/main/kotlin/texlab/TextDocumentServiceImpl.kt b/src/main/kotlin/texlab/TextDocumentServiceImpl.kt index 5f21848d..7c6755fc 100644 --- a/src/main/kotlin/texlab/TextDocumentServiceImpl.kt +++ b/src/main/kotlin/texlab/TextDocumentServiceImpl.kt @@ -17,6 +17,10 @@ import texlab.rename.AggregateRenamer import texlab.rename.LatexEnvironmentRenamer import texlab.rename.RenameRequest import texlab.rename.Renamer +import texlab.symbol.AggregateSymbolProvider +import texlab.symbol.LatexEnvironmentSymbolProvider +import texlab.symbol.SymbolProvider +import texlab.symbol.SymbolRequest import java.net.URI import java.util.concurrent.CompletableFuture @@ -39,6 +43,8 @@ class TextDocumentServiceImpl(private val workspace: Workspace) : TextDocumentSe LatexKernelCommandProvider(), LatexUserCommandProvider())) + private val symbolProvider: SymbolProvider = AggregateSymbolProvider(LatexEnvironmentSymbolProvider) + private val renamer: Renamer = AggregateRenamer(LatexEnvironmentRenamer) private val foldingProvider: FoldingProvider = @@ -78,12 +84,15 @@ class TextDocumentServiceImpl(private val workspace: Workspace) : TextDocumentSe CompletableFuture>> { synchronized(workspace) { val uri = URI.create(params.textDocument.uri) - val symbols = workspace.documents + val document = workspace.documents .firstOrNull { it.uri == uri } - ?.documentSymbol(workspace) - ?.map { Either.forRight(it) } - ?.toMutableList() - ?: mutableListOf() + ?: return CompletableFuture.completedFuture(null) + + val request = SymbolRequest(document) + val symbols = symbolProvider + .getSymbols(request) + .map { Either.forRight(it) } + .toMutableList() return CompletableFuture.completedFuture(symbols) } } diff --git a/src/main/kotlin/texlab/symbol/AggregateSymbolProvider.kt b/src/main/kotlin/texlab/symbol/AggregateSymbolProvider.kt new file mode 100644 index 00000000..8bb62661 --- /dev/null +++ b/src/main/kotlin/texlab/symbol/AggregateSymbolProvider.kt @@ -0,0 +1,9 @@ +package texlab.symbol + +import org.eclipse.lsp4j.DocumentSymbol + +class AggregateSymbolProvider(private vararg val providers: SymbolProvider) : SymbolProvider { + override fun getSymbols(request: SymbolRequest): List { + return providers.flatMap { it.getSymbols(request) } + } +} diff --git a/src/main/kotlin/texlab/symbol/LatexEnvironmentSymbolProvider.kt b/src/main/kotlin/texlab/symbol/LatexEnvironmentSymbolProvider.kt new file mode 100644 index 00000000..c375401a --- /dev/null +++ b/src/main/kotlin/texlab/symbol/LatexEnvironmentSymbolProvider.kt @@ -0,0 +1,25 @@ +package texlab.symbol + +import org.eclipse.lsp4j.DocumentSymbol +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.SymbolKind +import texlab.LatexDocument + +object LatexEnvironmentSymbolProvider : SymbolProvider { + override fun getSymbols(request: SymbolRequest): List { + if (request.document !is LatexDocument) { + return emptyList() + } + + val symbols = mutableListOf() + for (environment in request.document.tree.environments) { + symbols.add(createSymbol(environment.beginName, environment.beginNameRange)) + symbols.add(createSymbol(environment.endName, environment.endNameRange)) + } + return symbols + } + + private fun createSymbol(name: String, range: Range): DocumentSymbol { + return DocumentSymbol(name, SymbolKind.EnumMember, range, range) + } +} diff --git a/src/main/kotlin/texlab/symbol/SymbolProvider.kt b/src/main/kotlin/texlab/symbol/SymbolProvider.kt new file mode 100644 index 00000000..af42b717 --- /dev/null +++ b/src/main/kotlin/texlab/symbol/SymbolProvider.kt @@ -0,0 +1,8 @@ +package texlab.symbol + +import org.eclipse.lsp4j.DocumentSymbol + +interface SymbolProvider { + fun getSymbols(request: SymbolRequest): List +} + diff --git a/src/main/kotlin/texlab/symbol/SymbolRequest.kt b/src/main/kotlin/texlab/symbol/SymbolRequest.kt new file mode 100644 index 00000000..1c9ca686 --- /dev/null +++ b/src/main/kotlin/texlab/symbol/SymbolRequest.kt @@ -0,0 +1,5 @@ +package texlab.symbol + +import texlab.Document + +data class SymbolRequest(val document: Document) diff --git a/src/main/kotlin/texlab/syntax/latex/LatexEnvironmentSymbolFinder.kt b/src/main/kotlin/texlab/syntax/latex/LatexEnvironmentSymbolFinder.kt deleted file mode 100644 index 147c9038..00000000 --- a/src/main/kotlin/texlab/syntax/latex/LatexEnvironmentSymbolFinder.kt +++ /dev/null @@ -1,29 +0,0 @@ -package texlab.syntax.latex - -import org.eclipse.lsp4j.DocumentSymbol -import org.eclipse.lsp4j.SymbolKind - -abstract class LatexEnvironmentSymbolFinder { - companion object { - val kind: SymbolKind = SymbolKind.EnumMember - - fun find(tree: LatexSyntaxTree): List { - val symbols = mutableListOf() - for (environment in tree.environments) { - symbols.add( - DocumentSymbol( - environment.beginName, - kind, - environment.beginNameRange, - environment.beginNameRange)) - symbols.add( - DocumentSymbol( - environment.endName, - kind, - environment.endNameRange, - environment.endNameRange)) - } - return symbols - } - } -} diff --git a/src/test/kotlin/texlab/syntax/latex/LatexEnvironmentSymbolFinderTests.kt b/src/test/kotlin/texlab/syntax/latex/LatexEnvironmentSymbolFinderTests.kt deleted file mode 100644 index 0ae258a2..00000000 --- a/src/test/kotlin/texlab/syntax/latex/LatexEnvironmentSymbolFinderTests.kt +++ /dev/null @@ -1,24 +0,0 @@ -package texlab.syntax.latex - -import org.eclipse.lsp4j.DocumentSymbol -import org.eclipse.lsp4j.Position -import org.eclipse.lsp4j.Range -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test - -class LatexEnvironmentSymbolFinderTests { - - @Test - fun `it should find empty and nonempty environments`() { - val text = "\\begin{foo}\n\\end{}" - val tree = LatexSyntaxTree(text) - val range1 = Range(Position(0, 7), Position(0, 10)) - val range2 = Range(Position(1, 5), Position(1, 5)) - val symbol1 = DocumentSymbol("foo", LatexEnvironmentSymbolFinder.kind, range1, range1) - val symbol2 = DocumentSymbol("", LatexEnvironmentSymbolFinder.kind, range2, range2) - val symbols = LatexEnvironmentSymbolFinder.find(tree) - assertEquals(2, symbols.size) - assertEquals(symbol1, symbols[0]) - assertEquals(symbol2, symbols[1]) - } -}