Skip to content

Commit

Permalink
Merge pull request #144 from galaxyproject/fix_empty_doc_is_valid
Browse files Browse the repository at this point in the history
Fix empty document validation
  • Loading branch information
davelopez authored Jun 3, 2021
2 parents 552aa77 + bf925c5 commit e063a91
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 3 additions & 1 deletion server/galaxyls/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ def formatting(server: GalaxyToolsLanguageServer, params: DocumentFormattingPara
@language_server.feature(TEXT_DOCUMENT_DID_OPEN)
async def did_open(server: GalaxyToolsLanguageServer, params: DidOpenTextDocumentParams) -> None:
"""Occurs when a new xml document is open."""
_validate(server, params)
document = server.workspace.get_document(params.text_document.uri)
if not DocumentValidator.is_empty_document(document):
_validate(server, params)


@language_server.feature(TEXT_DOCUMENT_DID_SAVE)
Expand Down
12 changes: 10 additions & 2 deletions server/galaxyls/services/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ class DocumentValidator:

@classmethod
def has_valid_root(cls, document: Document) -> bool:
"""Checks if the document's root element matches one of the supported types."""
"""Checks if the document's root element matches one of the supported types
or is an empty document."""
if DocumentValidator.is_empty_document(document):
return True
root = DocumentValidator._get_document_root_tag(document)
if root is not None:
root_tag = root.upper()
supported = [e.name for e in DocumentType if e != DocumentType.UNKNOWN]
return root_tag in supported
return root_tag == "" or root_tag in supported
return False

@classmethod
Expand All @@ -32,6 +35,11 @@ def is_tool_document(cls, document: Document) -> bool:
return root_tag == DocumentType.TOOL.name
return False

@classmethod
def is_empty_document(cls, document: Document) -> bool:
"""Whether the document is empty or just contains spaces or empty lines."""
return not document.source or document.source.isspace()

@classmethod
def _get_document_root_tag(cls, document: Document) -> Optional[str]:
"""Checks the first MAX_PEEK_CONTENT characters of the document for a root tag and
Expand Down
6 changes: 4 additions & 2 deletions server/galaxyls/tests/unit/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class TestDocumentValidatorClass:
@pytest.mark.parametrize(
"source, expected",
[
("<", True),
("", True),
(" ", True),
(" \n ", True),
("<tool>", True),
(" <tool>", True),
("\n<tool>", True),
Expand All @@ -82,8 +86,6 @@ class TestDocumentValidatorClass:
('<?xml version="1.0" encoding="UTF-8"?>\n<tool>', True),
("<macros>", True),
('<?xml version="1.0" encoding="UTF-8"?>\n<macros>', True),
("", False),
(" ", False),
("test", False),
("<test>", False),
],
Expand Down

0 comments on commit e063a91

Please sign in to comment.