mirror of
https://github.com/latex-lsp/texlab.git
synced 2025-12-23 09:19:21 +00:00
Implemenet document highlight request for labels
This commit is contained in:
parent
80d14d7d16
commit
896806348e
6 changed files with 82 additions and 0 deletions
|
|
@ -50,6 +50,7 @@ class LanguageServerImpl : LanguageServer {
|
|||
hoverProvider = true
|
||||
documentFormattingProvider = true
|
||||
referencesProvider = true
|
||||
documentHighlightProvider = true
|
||||
}
|
||||
|
||||
return CompletableFuture.completedFuture(InitializeResult(capabilities))
|
||||
|
|
|
|||
|
|
@ -17,6 +17,10 @@ import texlab.diagnostics.*
|
|||
import texlab.folding.*
|
||||
import texlab.formatting.BibtexFormatter
|
||||
import texlab.formatting.BibtexFormatterConfig
|
||||
import texlab.highlight.AggregateHighlightProvider
|
||||
import texlab.highlight.HighlightProvider
|
||||
import texlab.highlight.HighlightRequest
|
||||
import texlab.highlight.LatexLabelHighlightProvider
|
||||
import texlab.link.AggregateLinkProvider
|
||||
import texlab.link.LatexIncludeLinkProvider
|
||||
import texlab.link.LinkProvider
|
||||
|
|
@ -104,6 +108,9 @@ class TextDocumentServiceImpl(private val workspace: Workspace) : TextDocumentSe
|
|||
LatexLabelDefinitionProvider,
|
||||
BibtexEntryDefinitionProvider)
|
||||
|
||||
private val highlightProvider: HighlightProvider =
|
||||
AggregateHighlightProvider(LatexLabelHighlightProvider)
|
||||
|
||||
private val metadataProvider: PackageMetadataProvider = CtanPackageMetadataProvider()
|
||||
|
||||
private val referenceProvider: ReferenceProvider =
|
||||
|
|
@ -296,6 +303,19 @@ class TextDocumentServiceImpl(private val workspace: Workspace) : TextDocumentSe
|
|||
}
|
||||
}
|
||||
|
||||
override fun documentHighlight(params: TextDocumentPositionParams):
|
||||
CompletableFuture<MutableList<out DocumentHighlight>> {
|
||||
synchronized(workspace) {
|
||||
val uri = URI.create(params.textDocument.uri)
|
||||
val document = workspace.documents.firstOrNull { it.uri == uri }
|
||||
?: return CompletableFuture.completedFuture(null)
|
||||
|
||||
val request = HighlightRequest(document, params.position)
|
||||
val highlights = highlightProvider.getHighlights(request)
|
||||
return CompletableFuture.completedFuture(highlights?.toMutableList())
|
||||
}
|
||||
}
|
||||
|
||||
fun build(params: BuildParams): CompletableFuture<BuildStatus> {
|
||||
return CompletableFuture.supplyAsync {
|
||||
val childUri = URI.create(params.textDocument.uri)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
package texlab.highlight
|
||||
|
||||
import org.eclipse.lsp4j.DocumentHighlight
|
||||
|
||||
class AggregateHighlightProvider(private vararg val providers: HighlightProvider) : HighlightProvider {
|
||||
override fun getHighlights(request: HighlightRequest): List<DocumentHighlight>? {
|
||||
for (provider in providers) {
|
||||
val highlights = provider.getHighlights(request)
|
||||
if (highlights != null) {
|
||||
return highlights
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
7
src/main/kotlin/texlab/highlight/HighlightProvider.kt
Normal file
7
src/main/kotlin/texlab/highlight/HighlightProvider.kt
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package texlab.highlight
|
||||
|
||||
import org.eclipse.lsp4j.DocumentHighlight
|
||||
|
||||
interface HighlightProvider {
|
||||
fun getHighlights(request: HighlightRequest): List<DocumentHighlight>?
|
||||
}
|
||||
6
src/main/kotlin/texlab/highlight/HighlightRequest.kt
Normal file
6
src/main/kotlin/texlab/highlight/HighlightRequest.kt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
package texlab.highlight
|
||||
|
||||
import org.eclipse.lsp4j.Position
|
||||
import texlab.Document
|
||||
|
||||
data class HighlightRequest(val document: Document, val position: Position)
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package texlab.highlight
|
||||
|
||||
import org.eclipse.lsp4j.DocumentHighlight
|
||||
import org.eclipse.lsp4j.DocumentHighlightKind
|
||||
import texlab.LatexDocument
|
||||
import texlab.contains
|
||||
import texlab.syntax.latex.LatexLabel
|
||||
|
||||
object LatexLabelHighlightProvider : HighlightProvider {
|
||||
override fun getHighlights(request: HighlightRequest): List<DocumentHighlight>? {
|
||||
if (request.document !is LatexDocument) {
|
||||
return null
|
||||
}
|
||||
|
||||
val label = request.document.tree.labelDefinitions
|
||||
.plus(request.document.tree.labelReferences)
|
||||
.firstOrNull { it.name.range.contains(request.position) }
|
||||
?: return null
|
||||
|
||||
return request.document.tree.labelDefinitions
|
||||
.plus(request.document.tree.labelReferences)
|
||||
.filter { it.name.text == label.name.text }
|
||||
.map { DocumentHighlight(it.name.range, getHighlightKind(it)) }
|
||||
}
|
||||
|
||||
private fun getHighlightKind(label: LatexLabel): DocumentHighlightKind {
|
||||
return if (label.command.name.text == "\\label") {
|
||||
DocumentHighlightKind.Write
|
||||
} else {
|
||||
DocumentHighlightKind.Read
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue