def completion(server: JediLanguageServer, params: CompletionParams) -> CompletionList: """Returns completion items""" jedi_script = jedi_utils.script(server.workspace, params.textDocument.uri) jedi_lines = jedi_utils.line_column(params.position) completions = jedi_script.complete(**jedi_lines) char = pygls_utils.char_before_cursor( document=server.workspace.get_document(params.textDocument.uri), position=params.position, ) markup_preferred = ( server.initialize_params.initializationOptions_markupKindPreferred) markup_supported = ( server.initialize_params. capabilities_textDocument_completion_completionItem_documentationFormat ) markup_kind = (markup_preferred if markup_preferred in markup_supported else markup_supported[0]) return CompletionList( is_incomplete=False, items=[ CompletionItem( label=completion.name, kind=get_lsp_completion_type(completion.type), detail=completion.description, documentation=MarkupContent(kind=markup_kind, value=completion.docstring()), sort_text=jedi_utils.complete_sort_name(completion), insert_text=pygls_utils.clean_completion_name( completion.name, char), ) for completion in completions ], )
def document_hover(ls, c: TextDocumentPositionParams) -> MarkupContent: doc = ls.workspace.get_document(c.textDocument.uri) items = find_definition(doc.uri, doc.source, c.position.line + 1, c.position.character) word = doc.word_at_position(c.position) definition = first_true(items, pred=lambda x: x.name == word) return MarkupContent('plaintext', definition.docstring())
def get_doc(self, lang: str = "en") -> MarkupContent: """Gets the Markdown documentation associated with this element from the XSD schema. If there is no documentation in the schema for the element, a message indicating this will be returned instead. Args: lang (str, optional): The language code of the documentation to retrieve. Defaults to "en" (English). Returns: [str]: The documentation text or a message indicating there is no documentation. """ doc = self._get_doc_text_of_element(self.xsd_element, lang) or self._get_doc_text_of_element(self.xsd_type, lang) if doc: return MarkupContent(MarkupKind.Markdown, doc) return MarkupContent(MarkupKind.Markdown, MSG_NO_DOCUMENTATION_AVAILABLE)
def lsp_completion_item_resolve( item: CompletionItem, markup_kind: MarkupKind, ) -> CompletionItem: """Resolve completion item using cached jedi completion data.""" completion = _MOST_RECENT_COMPLETIONS[item.label] item.detail = completion.description item.documentation = MarkupContent(kind=markup_kind, value=completion.docstring()) return item
def docstring_to_markup_content(docstring): # By default, return the response as plaintext kind = MarkupKind.PlainText # If pandoc is on the path, use it to convert the response from reStructuredText to Markdown if which("pandoc"): kind = MarkupKind.Markdown p = run(["pandoc", "--from", "rst", "--to", "markdown"], check=True, input=docstring.encode(), capture_output=True) docstring = p.stdout.decode() return MarkupContent(kind=kind, value=docstring)
def hover(server: JediLanguageServer, params: TextDocumentPositionParams) -> Optional[Hover]: """Support Hover.""" document = server.workspace.get_document(params.textDocument.uri) jedi_script = jedi_utils.script(server.project, document) jedi_lines = jedi_utils.line_column(jedi_script, params.position) for name in jedi_script.help(**jedi_lines): docstring = name.docstring() if not docstring: continue markup_kind = choose_markup(server) contents = MarkupContent(kind=markup_kind, value=docstring) document = server.workspace.get_document(params.textDocument.uri) _range = pygls_utils.current_word_range(document, params.position) return Hover(contents=contents, range=_range) return None
def hover(params: TextDocumentPositionParams): word = self._cursor_word(params.textDocument.uri, params.position, True) if not word: return None candidates = [ lambda x: self._api.get_command_doc(x.lower()), lambda x: self._api.get_variable_doc(x), lambda x: self._api.get_module_doc(x, False), lambda x: self._api.get_module_doc(x, True), ] for c in candidates: doc = c(word[0]) if doc is None: continue return Hover(MarkupContent(MarkupKind.Markdown, doc), word[1]) return None
def hover(params: TextDocumentPositionParams): text_doc = server.workspace.get_document(params.textDocument.uri) antfile = get_antfile(text_doc) symbols, range_ = antfile.symbols_at(sb_position(params.position)) if not symbols: return None assert range_ is not None # TODO fix the interface sym = symbols[0] text = sym.help_str() contents = MarkupContent(MarkupKind.Markdown, text) return Hover( contents=contents, range=pygls_range(range_), )
def hover(server: JediLanguageServer, params: TextDocumentPositionParams) -> Optional[Hover]: """Support Hover.""" jedi_script = jedi_utils.script(server.workspace, params.textDocument.uri) jedi_lines = jedi_utils.line_column(jedi_script, params.position) for name in jedi_script.help(**jedi_lines): docstring = name.docstring() if not docstring: continue markup_preferred = ( server.initialize_params.initializationOptions_markupKindPreferred) markup_supported = (server.initialize_params. capabilities_textDocument_hover_contentFormat) markup_kind = (markup_preferred if markup_preferred in markup_supported else markup_supported[0]) contents = MarkupContent(kind=markup_kind, value=docstring) document = server.workspace.get_document(params.textDocument.uri) _range = pygls_utils.current_word_range(document, params.position) return Hover(contents=contents, range=_range) return None
def lsp_completion_item( name: Completion, char_before_cursor: str, enable_snippets: bool, markup_kind: MarkupKind, ) -> CompletionItem: """Using a Jedi completion, obtain a jedi completion item.""" name_name = name.name name_clean = clean_completion_name(name_name, char_before_cursor) lsp_type = get_lsp_completion_type(name.type) completion_item = CompletionItem( label=name_name, filter_text=name_name, kind=lsp_type, detail=name.description, documentation=MarkupContent(kind=markup_kind, value=name.docstring()), sort_text=complete_sort_name(name), insert_text=name_clean, insert_text_format=InsertTextFormat.PlainText, ) if not enable_snippets: return completion_item if lsp_type not in _LSP_TYPE_FOR_SNIPPET: return completion_item signatures = name.get_signatures() if not signatures: return completion_item try: snippet_signature = get_snippet_signature(signatures[0]) except Exception: # pylint: disable=broad-except return completion_item new_text = name_name + snippet_signature completion_item.insertText = new_text completion_item.insertTextFormat = InsertTextFormat.Snippet return completion_item
"""Utilities to validate Galaxy xml tool wrappers and extract information from the XSD schema. """ from typing import List from lxml import etree from pygls.workspace import Document from pygls.types import Diagnostic, MarkupContent, MarkupKind from .constants import TOOL_XSD_FILE, MSG_NO_DOCUMENTATION_AVAILABLE from .parser import GalaxyToolXsdParser from .validation import GalaxyToolValidationService from ..context import XmlContext NO_DOC_MARKUP = MarkupContent(MarkupKind.Markdown, MSG_NO_DOCUMENTATION_AVAILABLE) class GalaxyToolXsdService: """Galaxy tool Xml Schema Definition service. This service provides functionality to extract information from the XSD schema and validate XML files against it. """ def __init__(self, server_name: str) -> None: """Initializes the validator by loading the XSD.""" self.server_name = server_name self.xsd_doc: etree._ElementTree = etree.parse(str(TOOL_XSD_FILE)) self.xsd_schema = etree.XMLSchema(self.xsd_doc) self.xsd_parser = GalaxyToolXsdParser(self.xsd_doc.getroot()) self.validator = GalaxyToolValidationService(server_name,