Reply with MarkupContent in completion documentation (#87)
This commit is contained in:
@@ -92,7 +92,7 @@ class CMakeLanguageServer(LanguageServer):
|
|||||||
CompletionItem(
|
CompletionItem(
|
||||||
label=x,
|
label=x,
|
||||||
kind=CompletionItemKind.Function,
|
kind=CompletionItemKind.Function,
|
||||||
documentation=self._api.get_command_doc(x),
|
documentation=self._get_command_doc(x),
|
||||||
insert_text=x,
|
insert_text=x,
|
||||||
)
|
)
|
||||||
for x in commands
|
for x in commands
|
||||||
@@ -104,7 +104,7 @@ class CMakeLanguageServer(LanguageServer):
|
|||||||
CompletionItem(
|
CompletionItem(
|
||||||
label=x,
|
label=x,
|
||||||
kind=CompletionItemKind.Variable,
|
kind=CompletionItemKind.Variable,
|
||||||
documentation=self._api.get_variable_doc(x),
|
documentation=self._get_variable_doc(x),
|
||||||
insert_text=x,
|
insert_text=x,
|
||||||
)
|
)
|
||||||
for x in variables
|
for x in variables
|
||||||
@@ -129,7 +129,7 @@ class CMakeLanguageServer(LanguageServer):
|
|||||||
CompletionItem(
|
CompletionItem(
|
||||||
label=x,
|
label=x,
|
||||||
kind=CompletionItemKind.Module,
|
kind=CompletionItemKind.Module,
|
||||||
documentation=self._api.get_module_doc(x, False),
|
documentation=self._get_module_doc(x, False),
|
||||||
insert_text=x,
|
insert_text=x,
|
||||||
)
|
)
|
||||||
for x in modules
|
for x in modules
|
||||||
@@ -140,7 +140,7 @@ class CMakeLanguageServer(LanguageServer):
|
|||||||
CompletionItem(
|
CompletionItem(
|
||||||
label=x,
|
label=x,
|
||||||
kind=CompletionItemKind.Module,
|
kind=CompletionItemKind.Module,
|
||||||
documentation=self._api.get_module_doc(x, True),
|
documentation=self._get_module_doc(x, True),
|
||||||
insert_text=x,
|
insert_text=x,
|
||||||
)
|
)
|
||||||
for x in modules
|
for x in modules
|
||||||
@@ -175,27 +175,21 @@ class CMakeLanguageServer(LanguageServer):
|
|||||||
|
|
||||||
@self.feature(TEXT_DOCUMENT_HOVER)
|
@self.feature(TEXT_DOCUMENT_HOVER)
|
||||||
def hover(params: TextDocumentPositionParams) -> Optional[Hover]:
|
def hover(params: TextDocumentPositionParams) -> Optional[Hover]:
|
||||||
assert self._api is not None
|
|
||||||
api = self._api
|
|
||||||
|
|
||||||
word = self._cursor_word(params.text_document.uri, params.position, True)
|
word = self._cursor_word(params.text_document.uri, params.position, True)
|
||||||
if not word:
|
if not word:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
candidates: List[Callable[[str], Optional[str]]] = [
|
candidates: List[Callable[[str], Optional[MarkupContent]]] = [
|
||||||
lambda x: api.get_command_doc(x.lower()),
|
lambda x: self._get_command_doc(x.lower()),
|
||||||
lambda x: api.get_variable_doc(x),
|
lambda x: self._get_variable_doc(x),
|
||||||
lambda x: api.get_module_doc(x, False),
|
lambda x: self._get_module_doc(x, False),
|
||||||
lambda x: api.get_module_doc(x, True),
|
lambda x: self._get_module_doc(x, True),
|
||||||
]
|
]
|
||||||
for c in candidates:
|
for c in candidates:
|
||||||
doc = c(word[0])
|
doc = c(word[0])
|
||||||
if doc is None:
|
if doc is None:
|
||||||
continue
|
continue
|
||||||
return Hover(
|
return Hover(contents=doc, range=word[1])
|
||||||
contents=MarkupContent(kind=MarkupKind.Markdown, value=doc),
|
|
||||||
range=word[1],
|
|
||||||
)
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@self.thread()
|
@self.thread()
|
||||||
@@ -241,6 +235,21 @@ class CMakeLanguageServer(LanguageServer):
|
|||||||
return word
|
return word
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def _get_command_doc(self, command: str) -> Optional[MarkupContent]:
|
||||||
|
assert self._api is not None
|
||||||
|
docs = self._api.get_command_doc(command)
|
||||||
|
return None if docs is None else MarkupContent(MarkupKind.Markdown, docs)
|
||||||
|
|
||||||
|
def _get_variable_doc(self, variable: str) -> Optional[MarkupContent]:
|
||||||
|
assert self._api is not None
|
||||||
|
docs = self._api.get_variable_doc(variable)
|
||||||
|
return None if docs is None else MarkupContent(MarkupKind.Markdown, docs)
|
||||||
|
|
||||||
|
def _get_module_doc(self, module: str, package: bool) -> Optional[MarkupContent]:
|
||||||
|
assert self._api is not None
|
||||||
|
docs = self._api.get_module_doc(module, package)
|
||||||
|
return None if docs is None else MarkupContent(MarkupKind.Markdown, docs)
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ from lsprotocol.types import (
|
|||||||
FormattingOptions,
|
FormattingOptions,
|
||||||
HoverParams,
|
HoverParams,
|
||||||
InitializeParams,
|
InitializeParams,
|
||||||
|
MarkupContent,
|
||||||
Position,
|
Position,
|
||||||
TextDocumentIdentifier,
|
TextDocumentIdentifier,
|
||||||
TextDocumentItem,
|
TextDocumentItem,
|
||||||
@@ -105,8 +106,8 @@ def test_completions(
|
|||||||
response = _test_completion(client_server, datadir, "projec", context)
|
response = _test_completion(client_server, datadir, "projec", context)
|
||||||
item = next(filter(lambda x: x.label == "project", response.items), None)
|
item = next(filter(lambda x: x.label == "project", response.items), None)
|
||||||
assert item is not None
|
assert item is not None
|
||||||
assert isinstance(item.documentation, str)
|
assert isinstance(item.documentation, MarkupContent)
|
||||||
assert "<PROJECT-NAME>" in item.documentation
|
assert "<PROJECT-NAME>" in item.documentation.value
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|||||||
Reference in New Issue
Block a user