Skip to content

Commit

Permalink
Properly color keyword calls with namespace in fixtures. Fixes #533
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Feb 3, 2022
1 parent acded7a commit 850a2cd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
5 changes: 4 additions & 1 deletion robotframework-ls/src/robotframework_ls/impl/ast_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,9 @@ def is_argument_keyword_name(node, token) -> bool:
return False


CLASSES_WITH_ARGUMENTS_AS_KEYWORD_CALLS = ("Fixture", "TestTemplate", "Template")


def get_keyword_name_token(ast, token):
"""
If the given token is a keyword, return the token, otherwise return None.
Expand All @@ -553,7 +556,7 @@ def get_keyword_name_token(ast, token):
"""
if token.type == token.KEYWORD or (
token.type == token.NAME
and isinstance_name(ast, ("Fixture", "TestTemplate", "Template"))
and isinstance_name(ast, CLASSES_WITH_ARGUMENTS_AS_KEYWORD_CALLS)
):
return _strip_token_bdd_prefix(token)

Expand Down
10 changes: 9 additions & 1 deletion robotframework-ls/src/robotframework_ls/impl/semantic_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import itertools
from robocorp_ls_core.protocols import IDocument, IMonitor
from robotframework_ls.impl.protocols import ICompletionContext
from robocorp_ls_core.basic import isinstance_name


TOKEN_TYPES = [
Expand Down Expand Up @@ -112,7 +113,10 @@ def semantic_tokens_range(context, range):


def _tokenize_token(node, initial_token):
from robotframework_ls.impl.ast_utils import is_argument_keyword_name
from robotframework_ls.impl.ast_utils import (
is_argument_keyword_name,
CLASSES_WITH_ARGUMENTS_AS_KEYWORD_CALLS,
)

initial_token_type = initial_token.type

Expand All @@ -122,6 +126,10 @@ def _tokenize_token(node, initial_token):
yield initial_token, token_type_index
return

if initial_token_type == NAME:
if isinstance_name(node, CLASSES_WITH_ARGUMENTS_AS_KEYWORD_CALLS):
initial_token_type = KEYWORD

if initial_token_type == KEYWORD:
dot_pos = initial_token.value.find(".")
if dot_pos > 0:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,51 @@ def test_semantic_highlighting_dotted_access_to_keyword(workspace):
)


def test_semantic_highlighting_dotted_access_to_keyword_suite_setup(workspace):
from robotframework_ls.impl.completion_context import CompletionContext
from robotframework_ls.impl.semantic_tokens import semantic_tokens_full

workspace.set_root("case1")
doc = workspace.put_doc("case1.robot")
doc.source = """*** Settings ***
Library Collections WITH NAME Col
Suite Setup Col.Append to list
*** Test Cases ***
Some test
[Setup] Col.Append to list
Col.Append to list
""".replace(
"\r\n", "\n"
).replace(
"\r", "\n"
)
context = CompletionContext(doc, workspace=workspace.ws)
semantic_tokens = semantic_tokens_full(context)
check(
(semantic_tokens, doc),
[
("*** Settings ***", "header"),
("Library", "setting"),
("Collections", "name"),
("WITH NAME", "control"),
("Col", "name"),
("Suite Setup", "setting"),
("Col", "name"),
("Append to list", "keywordNameCall"),
("*** Test Cases ***", "header"),
("Some test", "testCaseName"),
("[", "variableOperator"),
("Setup", "setting"),
("]", "variableOperator"),
("Col", "name"),
("Append to list", "keywordNameCall"),
("Col", "name"),
("Append to list", "keywordNameCall"),
],
)


@pytest.mark.skipif(get_robot_major_version() < 5, reason="Requires RF 5 onwards")
def test_semantic_highlighting_try_except(workspace):
from robotframework_ls.impl.completion_context import CompletionContext
Expand Down

0 comments on commit 850a2cd

Please sign in to comment.