Skip to content

Commit

Permalink
Fixed issue where doc highlight could raise error with unclosed varia…
Browse files Browse the repository at this point in the history
…ble.
  • Loading branch information
fabioz committed Jul 25, 2022
1 parent edaa7db commit fcfdd02
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
29 changes: 17 additions & 12 deletions robotframework-ls/src/robotframework_ls/impl/ast_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,16 +669,18 @@ def tokenize_variables_from_name(name):

def tokenize_variables(token: IRobotToken) -> Iterator[IRobotToken]:
# May throw error if it's not OK.
return token.tokenize_variables()
return iter(tuple(token.tokenize_variables()))


def _tokenize_variables_even_when_invalid(token: IRobotToken, col: int):
def _tokenize_variables_even_when_invalid(
token: IRobotToken, col: int
) -> Iterator[IRobotToken]:
"""
If Token.tokenize_variables() fails, this can still provide the variable under
the given column by applying some heuristics to find open variables.
"""
try:
return tokenize_variables(token)
return iter(tuple(tokenize_variables(token)))
except:
pass

Expand Down Expand Up @@ -706,15 +708,18 @@ def _tokenize_variables_even_when_invalid(token: IRobotToken, col: int):
break
varname.append(c)

return [
Token(
type=token.VARIABLE,
value="".join(varname),
lineno=token.lineno,
col_offset=token.col_offset + open_at - 1,
error=token.error,
)
]
return iter(
[
Token(
type=token.VARIABLE,
value="".join(varname),
lineno=token.lineno,
col_offset=token.col_offset + open_at - 1,
error=token.error,
)
]
)
return iter(())


LIBRARY_IMPORT_CLASSES = ("LibraryImport",)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*** Settings ***
Variables ${CURDIR}${/}variables${/}common variables.yaml


*** Test Cases ***
Test
Log ${COMMON_2} console=True
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COMMON_1: 10
COMMON_2: 20
Original file line number Diff line number Diff line change
Expand Up @@ -2217,3 +2217,10 @@ def test_variables_cls(workspace, libspec_manager, data_regression):
doc = workspace.get_doc("case_vars_file_cls.robot", accept_from_file=True)

_collect_errors(workspace, doc, data_regression, basename="no_error")


def test_variables_curdir(workspace, libspec_manager, data_regression):
workspace.set_root("case_vars_file", libspec_manager=libspec_manager)
doc = workspace.get_doc("case_vars_curdir.robot", accept_from_file=True)

_collect_errors(workspace, doc, data_regression, basename="no_error")
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,19 @@ def test_document_highlight_argument_in_caller(
),
)
)


def test_document_highlight_unclosed_var(workspace, libspec_manager, data_regression):
from robotframework_ls.impl.completion_context import CompletionContext
from robotframework_ls.impl.doc_highlight import doc_highlight

workspace.set_root("case4", libspec_manager=libspec_manager)
doc = workspace.put_doc(
"my.robot",
"""
*** Settings ***
Variables ${CURDIR}${/common vari""",
)
completion_context = CompletionContext(doc, workspace=workspace.ws)
result = doc_highlight(completion_context)
assert len(result) == 0

0 comments on commit fcfdd02

Please sign in to comment.