Skip to content

Commit

Permalink
Update pygls to 0.10.2
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Mar 27, 2021
1 parent 93071f2 commit eda819e
Show file tree
Hide file tree
Showing 24 changed files with 225 additions and 139 deletions.
19 changes: 10 additions & 9 deletions server/galaxyls/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from typing import List, Optional

from pygls.features import (
from pygls.lsp.methods import (
COMPLETION,
FORMATTING,
HOVER,
Expand All @@ -13,8 +13,9 @@
TEXT_DOCUMENT_DID_SAVE,
WORKSPACE_DID_CHANGE_CONFIGURATION,
)
from pygls.lsp.types.language_features.completion import CompletionOptions
from pygls.server import LanguageServer
from pygls.types import (
from pygls.lsp.types import (
CompletionList,
CompletionParams,
ConfigurationItem,
Expand Down Expand Up @@ -95,12 +96,12 @@ async def did_change_configuration(server: GalaxyToolsLanguageServer, params: Di
server.show_message("Settings updated")


@language_server.feature(COMPLETION, trigger_characters=["<", " "])
@language_server.feature(COMPLETION, CompletionOptions(trigger_characters=["<", " "]))
def completions(server: GalaxyToolsLanguageServer, params: CompletionParams) -> Optional[CompletionList]:
"""Returns completion items depending on the current document context."""
if server.configuration.completion_mode == CompletionMode.DISABLED:
return None
document = _get_valid_document(server, params.textDocument.uri)
document = _get_valid_document(server, params.text_document.uri)
if document:
xml_document = _get_xml_document(document)
return server.service.get_completion(xml_document, params, server.configuration.completion_mode)
Expand All @@ -110,7 +111,7 @@ def completions(server: GalaxyToolsLanguageServer, params: CompletionParams) ->
def auto_close_tag(server: GalaxyToolsLanguageServer, params: TextDocumentPositionParams) -> Optional[AutoCloseTagResult]:
"""Responds to a close tag request to close the currently opened node."""
if server.configuration.auto_close_tags:
document = _get_valid_document(server, params.textDocument.uri)
document = _get_valid_document(server, params.text_document.uri)
if document:
xml_document = _get_xml_document(document)
return server.service.get_auto_close_tag(xml_document, params)
Expand All @@ -119,7 +120,7 @@ def auto_close_tag(server: GalaxyToolsLanguageServer, params: TextDocumentPositi
@language_server.feature(HOVER)
def hover(server: GalaxyToolsLanguageServer, params: TextDocumentPositionParams) -> Optional[Hover]:
"""Displays Markdown documentation for the element under the cursor."""
document = _get_valid_document(server, params.textDocument.uri)
document = _get_valid_document(server, params.text_document.uri)
if document:
xml_document = _get_xml_document(document)
return server.service.get_documentation(xml_document, params.position)
Expand All @@ -128,7 +129,7 @@ def hover(server: GalaxyToolsLanguageServer, params: TextDocumentPositionParams)
@language_server.feature(FORMATTING)
def formatting(server: GalaxyToolsLanguageServer, params: DocumentFormattingParams) -> Optional[List[TextEdit]]:
"""Formats the whole document using the provided parameters"""
document = _get_valid_document(server, params.textDocument.uri)
document = _get_valid_document(server, params.text_document.uri)
if document:
content = document.source
return server.service.format_document(content, params)
Expand Down Expand Up @@ -177,7 +178,7 @@ def sort_single_param_attrs_command(
server: GalaxyToolsLanguageServer, params: TextDocumentPositionParams
) -> Optional[ReplaceTextRangeResult]:
"""Sorts the attributes of the param element under the cursor."""
document = _get_valid_document(server, params.textDocument.uri)
document = _get_valid_document(server, params.text_document.uri)
if document:
xml_document = _get_xml_document(document)
return server.service.sort_single_param_attrs(xml_document, params)
Expand All @@ -202,7 +203,7 @@ def discover_tests_command(server: GalaxyToolsLanguageServer, params: TextDocume

def _validate(server: GalaxyToolsLanguageServer, params) -> None:
"""Validates the Galaxy tool and reports any problem found."""
document = _get_valid_document(server, params.textDocument.uri)
document = _get_valid_document(server, params.text_document.uri)
if document:
xml_document = _get_xml_document(document)
diagnostics = server.service.get_diagnostics(xml_document)
Expand Down
24 changes: 12 additions & 12 deletions server/galaxyls/services/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Optional

from galaxyls.services.xml.nodes import XmlCDATASection
from pygls.types import (
from pygls.lsp.types import (
CompletionContext,
CompletionItem,
CompletionItemKind,
Expand Down Expand Up @@ -33,11 +33,11 @@ def get_completion_at_context(
) -> Optional[CompletionList]:
if isinstance(context.token, XmlCDATASection):
return None
triggerKind = completion_context.triggerKind
triggerKind = completion_context.trigger_kind
if mode == CompletionMode.AUTO and triggerKind == CompletionTriggerKind.TriggerCharacter and not context.is_attribute:
if completion_context.triggerCharacter == "<":
if completion_context.trigger_character == "<":
return self.get_node_completion(context)
if completion_context.triggerCharacter == " ":
if completion_context.trigger_character == " ":
return self.get_attribute_completion(context)
elif triggerKind == CompletionTriggerKind.Invoked:
if context.is_attribute_value:
Expand Down Expand Up @@ -114,9 +114,9 @@ def get_attribute_value_completion(self, context: XmlContext) -> CompletionList:
if context.attribute_name:
attribute: Optional[XsdAttribute] = context.xsd_element.attributes.get(context.attribute_name)
if attribute and attribute.enumeration:
result = [CompletionItem(item, CompletionItemKind.Value) for item in attribute.enumeration]
result = [CompletionItem(label=item, kind=CompletionItemKind.Value) for item in attribute.enumeration]
return CompletionList(items=result, is_incomplete=False)
return CompletionList(False)
return CompletionList(is_incomplete=False)

def get_auto_close_tag(self, context: XmlContext, trigger_character: str) -> Optional[AutoCloseTagResult]:
"""Gets the closing result for the currently opened tag in context."""
Expand All @@ -133,11 +133,11 @@ def get_auto_close_tag(self, context: XmlContext, trigger_character: str) -> Opt
replace_range = None
is_self_closing = trigger_character == "/"
if is_self_closing:
start = Position(context.position.line, context.position.character)
start = Position(line=context.position.line, character=context.position.character)
end_character = context.position.character + 1
if len(context.line_text) > end_character and context.line_text[end_character] == ">":
end_character = end_character + 1
end = Position(context.position.line, end_character)
end = Position(line=context.position.line, character=end_character)
replace_range = Range(start=start, end=end)
if not context.is_content:
snippet = "/>$0"
Expand All @@ -160,8 +160,8 @@ def _build_node_completion_item(self, node: XsdNode, order: int = 0) -> Completi
about the node.
"""
return CompletionItem(
node.name,
CompletionItemKind.Class,
label=node.name,
kind=CompletionItemKind.Class,
documentation=node.get_doc(),
sort_text=str(order).zfill(2),
)
Expand All @@ -180,8 +180,8 @@ def _build_attribute_completion_item(self, attr: XsdAttribute, order: int = 0) -
about the attribute.
"""
return CompletionItem(
attr.name,
CompletionItemKind.Variable,
label=attr.name,
kind=CompletionItemKind.Variable,
documentation=attr.get_doc(),
insert_text=f'{attr.name}="$1"',
insert_text_format=InsertTextFormat.Snippet,
Expand Down
2 changes: 1 addition & 1 deletion server/galaxyls/services/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import List, Optional

from pygls.types import Range
from pygls.lsp.types import Range
from pygls.workspace import Position

from galaxyls.services.tools.constants import MACROS
Expand Down
14 changes: 7 additions & 7 deletions server/galaxyls/services/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"""

from lxml import etree
from typing import List
from typing import List, cast

from pygls.types import DocumentFormattingParams, Position, Range, TextEdit
from pygls.lsp.types import DocumentFormattingParams, Position, Range, TextEdit


class GalaxyToolFormatService:
Expand All @@ -19,12 +19,12 @@ def format(self, content: str, params: DocumentFormattingParams) -> List[TextEdi
"""Given the document contents returns the list of TextEdits
needed to properly layout the document.
"""
formatted_result = self._format_document(content, params.options.tabSize)
formatted_result = self._format_document(content, params.options.tab_size)

lines = content.count("\n")
start = Position(0, 0)
end = Position(lines + 1, 0)
return [TextEdit(Range(start, end), formatted_result)]
start = Position(line=0, character=0)
end = Position(line=lines + 1, character=0)
return [TextEdit(range=Range(start=start, end=end), new_text=formatted_result)]

def _format_document(self, content: str, tabSize: int) -> str:
"""Formats the whole XML document."""
Expand All @@ -34,6 +34,6 @@ def _format_document(self, content: str, tabSize: int) -> str:
spaces = " " * tabSize
etree.indent(xml, space=spaces)
result = etree.tostring(xml, pretty_print=True, encoding=str)
return result
return cast(str, result)
except etree.XMLSyntaxError:
return content # Do not auto-format if there are syntax errors
6 changes: 3 additions & 3 deletions server/galaxyls/services/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from galaxyls.services.tools.generators.command import GalaxyToolCommandSnippetGenerator
from galaxyls.services.tools.generators.tests import GalaxyToolTestSnippetGenerator
from galaxyls.services.tools.iuc import IUCToolParamAttributeSorter
from pygls.types import (
from pygls.lsp.types import (
CompletionList,
CompletionParams,
Diagnostic,
Expand Down Expand Up @@ -56,7 +56,7 @@ def get_documentation(self, xml_document: XmlDocument, position: Position) -> Op
if context.token and (context.is_tag or context.is_attribute_key):
documentation = self.xsd_service.get_documentation_for(context)
context_range = self.xml_context_service.get_range_for_context(xml_document, context)
return Hover(documentation, context_range)
return Hover(contents=documentation, range=context_range)
return None

def format_document(self, content: str, params: DocumentFormattingParams) -> List[TextEdit]:
Expand All @@ -77,7 +77,7 @@ def get_auto_close_tag(
) -> Optional[AutoCloseTagResult]:
"""Gets the closing result for the currently opened tag in context."""
trigger_character = xml_document.document.lines[params.position.line][params.position.character - 1]
position_before_trigger = Position(params.position.line, params.position.character - 1)
position_before_trigger = Position(line=params.position.line, character=params.position.character - 1)
context = self.xml_context_service.get_xml_context(xml_document, position_before_trigger)
return self.completion_service.get_auto_close_tag(context, trigger_character)

Expand Down
2 changes: 1 addition & 1 deletion server/galaxyls/services/tools/document.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List, Optional, cast

from anytree import find
from pygls.types import Position, Range
from pygls.lsp.types import Position, Range
from pygls.workspace import Document
from galaxyls.services.tools.constants import INPUTS, OUTPUTS, TESTS, TOOL
from galaxyls.services.tools.inputs import GalaxyToolInputTree
Expand Down
6 changes: 3 additions & 3 deletions server/galaxyls/services/tools/generators/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
SectionInputNode,
)
from galaxyls.services.xml.nodes import XmlElement
from pygls.types import Position, Range
from pygls.lsp.types import Position, Range

ARG_PLACEHOLDER = "TODO_argument"
REPEAT_VAR = "item"
Expand Down Expand Up @@ -88,7 +88,7 @@ def _find_snippet_insert_position(self) -> Union[Position, Range]:
else: # is self closed <tests/>
start = tool.get_position_before(section)
end = tool.get_position_after(section)
return Range(start, end)
return Range(start=start, end=end)
else:
section = tool.find_element(ENV_VARIABLES)
if section:
Expand All @@ -102,7 +102,7 @@ def _find_snippet_insert_position(self) -> Union[Position, Range]:
section = tool.find_element(TOOL)
if section:
return tool.get_content_range(section).end
return Position()
return Position(line=0, character=0)

def _generate_command_snippet(self, input_tree: GalaxyToolInputTree, outputs: List[XmlElement]) -> str:
snippets = [
Expand Down
2 changes: 1 addition & 1 deletion server/galaxyls/services/tools/generators/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from galaxyls.services.tools.document import GalaxyToolXmlDocument
from galaxyls.types import GeneratedSnippetResult
from lxml import etree
from pygls.types import Position, Range
from pygls.lsp.types import Position, Range
from pygls.workspace import Document


Expand Down
6 changes: 3 additions & 3 deletions server/galaxyls/services/tools/generators/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from galaxyls.services.tools.inputs import ConditionalInputNode, InputNode, RepeatInputNode, SectionInputNode
from galaxyls.services.xml.nodes import XmlElement
from lxml import etree
from pygls.types import Position, Range
from pygls.lsp.types import Position, Range

AUTO_GEN_TEST_COMMENT = "TODO: auto-generated test case. Please fill in the required values"
BOOLEAN_CONDITIONAL_NOT_RECOMMENDED_COMMENT = (
Expand Down Expand Up @@ -99,7 +99,7 @@ def _find_snippet_insert_position(self) -> Union[Position, Range]:
else: # is self closed <tests/>
start = tool.get_position_before(section)
end = tool.get_position_after(section)
return Range(start, end)
return Range(start=start, end=end)
else:
section = tool.find_element(OUTPUTS)
if section:
Expand All @@ -110,7 +110,7 @@ def _find_snippet_insert_position(self) -> Union[Position, Range]:
section = tool.find_element(TOOL)
if section:
return tool.get_content_range(section).end
return Position()
return Position(line=0, character=0)

def _generate_test_case_snippet(self, input_node: InputNode, outputs: List[XmlElement]) -> str:
"""Generates the code snippet for a single <test> element given an InputNode and the list of outputs
Expand Down
2 changes: 1 addition & 1 deletion server/galaxyls/services/xml/document.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict, List, Optional

from anytree.search import findall
from pygls.types import Position, Range
from pygls.lsp.types import Position, Range
from pygls.workspace import Document

from .nodes import XmlContainerNode, XmlElement, XmlSyntaxNode
Expand Down
8 changes: 4 additions & 4 deletions server/galaxyls/services/xml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from typing import Callable, List

from pygls.types import Position, Range
from pygls.lsp.types import Position, Range
from pygls.workspace import Document

from .constants import NEW_LINE, _LAN, WHITESPACE_CHARS
Expand Down Expand Up @@ -41,7 +41,7 @@ def convert_document_offset_to_position(document: Document, offset: int) -> Posi
character = offset - line_offset
if line > 0:
character -= 1
return Position(line, character)
return Position(line=line, character=character)


def convert_document_offsets_to_range(document: Document, start_offset: int, end_offset: int) -> Range:
Expand All @@ -56,8 +56,8 @@ def convert_document_offsets_to_range(document: Document, start_offset: int, end
Range: The resulting Range with the correct line number and character offset
"""
return Range(
convert_document_offset_to_position(document, start_offset),
convert_document_offset_to_position(document, end_offset),
start=convert_document_offset_to_position(document, start_offset),
end=convert_document_offset_to_position(document, end_offset),
)


Expand Down
4 changes: 2 additions & 2 deletions server/galaxyls/services/xsd/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
from typing import List

from lxml import etree
from pygls.types import Diagnostic, MarkupContent, MarkupKind
from pygls.lsp.types import Diagnostic, MarkupContent, MarkupKind

from ..context import XmlContext
from ..xml.document import XmlDocument
from .constants import MSG_NO_DOCUMENTATION_AVAILABLE, TOOL_XSD_FILE
from .parser import GalaxyToolXsdParser
from .validation import GalaxyToolValidationService

NO_DOC_MARKUP = MarkupContent(MarkupKind.Markdown, MSG_NO_DOCUMENTATION_AVAILABLE)
NO_DOC_MARKUP = MarkupContent(kind=MarkupKind.Markdown, value=MSG_NO_DOCUMENTATION_AVAILABLE)


class GalaxyToolXsdService:
Expand Down
6 changes: 3 additions & 3 deletions server/galaxyls/services/xsd/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from anytree import NodeMixin, RenderTree, Resolver, ResolverError, findall
from lxml import etree
from pygls.types import MarkupContent, MarkupKind
from pygls.lsp.types import MarkupContent, MarkupKind

from .constants import MSG_NO_DOCUMENTATION_AVAILABLE

Expand Down Expand Up @@ -43,8 +43,8 @@ def get_doc(self, lang: str = "en") -> MarkupContent:
"""
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)
return MarkupContent(kind=MarkupKind.Markdown, value=doc)
return MarkupContent(kind=MarkupKind.Markdown, value=MSG_NO_DOCUMENTATION_AVAILABLE)

def _get_doc_text_of_element(self, element: Optional[etree._Element], lang: str = "en") -> str:
try:
Expand Down
Loading

0 comments on commit eda819e

Please # to comment.