Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid processing unknown XML documents #75

Merged
merged 8 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions server/galaxyls/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def completions(server: GalaxyToolsLanguageServer, params: CompletionParams) ->
if server.configuration.completion_mode == CompletionMode.DISABLED:
return None
document = server.workspace.get_document(params.textDocument.uri)
xml_document = _get_xml_document(document)
if xml_document.is_unknown:
if not _is_document_supported(document):
return None
xml_document = _get_xml_document(document)
return server.service.get_completion(xml_document, params, server.configuration.completion_mode)


Expand All @@ -103,26 +103,28 @@ def auto_close_tag(server: GalaxyToolsLanguageServer, params: TextDocumentPositi
"""Responds to a close tag request to close the currently opened node."""
if server.configuration.auto_close_tags:
document = server.workspace.get_document(params.textDocument.uri)
xml_document = _get_xml_document(document)
if xml_document.is_unknown:
if not _is_document_supported(document):
return None
xml_document = _get_xml_document(document)
return server.service.get_auto_close_tag(xml_document, params)


@language_server.feature(HOVER)
def hover(server: GalaxyToolsLanguageServer, params: TextDocumentPositionParams) -> Optional[Hover]:
"""Displays Markdown documentation for the element under the cursor."""
document = server.workspace.get_document(params.textDocument.uri)
xml_document = _get_xml_document(document)
if xml_document.is_unknown:
if not _is_document_supported(document):
return None
xml_document = _get_xml_document(document)
return server.service.get_documentation(xml_document, params.position)


@language_server.feature(FORMATTING)
def formatting(server: GalaxyToolsLanguageServer, params: DocumentFormattingParams) -> List[TextEdit]:
def formatting(server: GalaxyToolsLanguageServer, params: DocumentFormattingParams) -> Optional[List[TextEdit]]:
"""Formats the whole document using the provided parameters"""
document = server.workspace.get_document(params.textDocument.uri)
if not _is_document_supported(document):
return None
content = document.source
return server.service.format_document(content, params)

Expand All @@ -149,21 +151,26 @@ def did_close(server: GalaxyToolsLanguageServer, params: DidCloseTextDocumentPar
async def cmd_generate_test(
server: GalaxyToolsLanguageServer, params: TextDocumentIdentifier
) -> Optional[GeneratedTestResult]:
"""Generates some test snippets based on the inputs and outputs of the document."""
document = server.workspace.get_document(params.uri)
return server.service.generate_test(document)


def _validate(server: GalaxyToolsLanguageServer, params) -> None:
"""Validates the Galaxy tool and reports any problem found."""
document = server.workspace.get_document(params.textDocument.uri)
xml_document = _get_xml_document(document)
if xml_document.is_unknown:
return None
diagnostics = server.service.get_diagnostics(xml_document)
server.publish_diagnostics(document.uri, diagnostics)
if _is_document_supported(document):
xml_document = _get_xml_document(document)
diagnostics = server.service.get_diagnostics(xml_document)
server.publish_diagnostics(document.uri, diagnostics)


def _get_xml_document(document: Document) -> XmlDocument:
"""Parses the input Document and returns an XmlDocument."""
xml_document = XmlDocumentParser().parse(document)
return xml_document


def _is_document_supported(document: Document) -> bool:
"""Returns True if the given document is supported by the server."""
return XmlDocument.has_valid_root(document)
17 changes: 16 additions & 1 deletion server/galaxyls/services/xml/document.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Dict, Optional
from anytree.search import findall

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

Expand Down Expand Up @@ -117,3 +118,17 @@ def get_position_after(self, element: XmlElement) -> Position:
if element.is_self_closed:
return convert_document_offset_to_position(self.document, element.end)
return convert_document_offset_to_position(self.document, element.end_tag_close_offset)

@staticmethod
def has_valid_root(document: Document) -> bool:
"""Checks if the document's root element matches one of the supported types."""
try:
xml = etree.parse(str(document.path))
root = xml.getroot()
if root and root.tag:
root_tag = root.tag.upper()
supported = [e.name for e in DocumentType if e != DocumentType.UNKNOWN]
return root_tag in supported
return False
except BaseException:
return False
1 change: 0 additions & 1 deletion server/galaxyls/services/xml/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Dict, List, Optional, Tuple, cast

from anytree import NodeMixin
from anytree.search import findall

from .constants import UNDEFINED_OFFSET
from .types import NodeType
Expand Down
1 change: 1 addition & 0 deletions server/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pytest-mock
flake8
flake8-bugbear
black
lxml-stubs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is this needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not really needed, but it is really useful in development for getting linting and auto-completion in the IDE with lxml. They are just external type annotations since the lxml is mostly compiled code and can not be directly used by the static type linter. I found it here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is nifty thanks for sharing.