mirror of
https://github.com/latex-lsp/texlab.git
synced 2025-12-23 09:19:21 +00:00
Implement hover request for packages
This commit is contained in:
parent
92fbb19c0b
commit
f77a59a593
6 changed files with 55 additions and 11 deletions
|
|
@ -44,6 +44,7 @@ class LanguageServerImpl : LanguageServer {
|
|||
completionProvider = CompletionOptions(true, listOf("\\", "{", "}", "@"))
|
||||
foldingRangeProvider = Either.forLeft(true)
|
||||
definitionProvider = true
|
||||
hoverProvider = true
|
||||
}
|
||||
|
||||
return CompletableFuture.completedFuture(InitializeResult(capabilities))
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package texlab
|
||||
|
||||
import com.overzealous.remark.Remark
|
||||
import org.eclipse.lsp4j.*
|
||||
import org.eclipse.lsp4j.jsonrpc.messages.Either
|
||||
import org.eclipse.lsp4j.services.TextDocumentService
|
||||
|
|
@ -189,14 +188,8 @@ class TextDocumentServiceImpl(private val workspace: Workspace) : TextDocumentSe
|
|||
if (unresolved.kind == CompletionItemKind.Class) {
|
||||
val metadata = metadataProvider.getMetadata(unresolved.label)
|
||||
if (metadata != null) {
|
||||
val description = Remark().convert(metadata.descriptions.firstOrNull()?.text)
|
||||
val documentation = MarkupContent().apply {
|
||||
kind = MarkupKind.MARKDOWN
|
||||
value = description
|
||||
}
|
||||
|
||||
unresolved.detail = metadata.caption
|
||||
unresolved.setDocumentation(documentation)
|
||||
unresolved.setDocumentation(metadata.description)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -226,4 +219,29 @@ class TextDocumentServiceImpl(private val workspace: Workspace) : TextDocumentSe
|
|||
return CompletableFuture.completedFuture(location?.let { mutableListOf(it) })
|
||||
}
|
||||
}
|
||||
|
||||
override fun hover(params: TextDocumentPositionParams): CompletableFuture<Hover> {
|
||||
val include = synchronized(workspace) {
|
||||
val uri = URI.create(params.textDocument.uri)
|
||||
val document = workspace.documents
|
||||
.filterIsInstance<LatexDocument>()
|
||||
.firstOrNull { it.uri == uri }
|
||||
?: return CompletableFuture.completedFuture(null)
|
||||
|
||||
document.tree.includes
|
||||
.filter { it.command.name.text == "\\usepackage" || it.command.name.text == "\\documentclass" }
|
||||
.firstOrNull { it.command.range.contains(params.position) }
|
||||
?: return CompletableFuture.completedFuture(null)
|
||||
}
|
||||
|
||||
return CompletableFuture.supplyAsync {
|
||||
val metadata = metadataProvider.getMetadata(include.path)
|
||||
val description = metadata?.description
|
||||
if (description != null) {
|
||||
Hover(description)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ class CtanPackageMetadataProvider : PackageMetadataProvider {
|
|||
val json = URL("https://ctan.org/json/2.0/pkg/$name").readText()
|
||||
val mapper = jacksonObjectMapper()
|
||||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
||||
return mapper.readValue<PackageMetadata>(json)
|
||||
mapper.readValue<PackageMetadata>(json)
|
||||
} catch (e: IOException) {
|
||||
null
|
||||
}
|
||||
|
|
|
|||
15
src/main/kotlin/texlab/metadata/HtmlToMarkdownConverter.kt
Normal file
15
src/main/kotlin/texlab/metadata/HtmlToMarkdownConverter.kt
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package texlab.metadata
|
||||
|
||||
import com.fasterxml.jackson.databind.util.StdConverter
|
||||
import com.overzealous.remark.Remark
|
||||
import org.eclipse.lsp4j.MarkupContent
|
||||
import org.eclipse.lsp4j.MarkupKind
|
||||
|
||||
class HtmlToMarkdownConverter : StdConverter<String, MarkupContent>() {
|
||||
override fun convert(value: String): MarkupContent {
|
||||
return MarkupContent().apply {
|
||||
kind = MarkupKind.MARKDOWN
|
||||
this.value = Remark().convert(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,9 @@
|
|||
package texlab.metadata
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
|
||||
import org.eclipse.lsp4j.MarkupContent
|
||||
|
||||
data class PackageDescription(val language: String?,
|
||||
val text: String)
|
||||
@JsonDeserialize(converter = HtmlToMarkdownConverter::class)
|
||||
@JsonProperty("text") val markup: MarkupContent)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,10 @@
|
|||
package texlab.metadata
|
||||
|
||||
import org.eclipse.lsp4j.MarkupContent
|
||||
|
||||
data class PackageMetadata(val name: String,
|
||||
val caption: String,
|
||||
val descriptions: List<PackageDescription>)
|
||||
private val descriptions: List<PackageDescription>) {
|
||||
val description: MarkupContent?
|
||||
get() = descriptions.firstOrNull() { it.language == null }?.markup
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue