Skip to content

Commit 6fad9b1

Browse files
committed
feat(langserver): better support for indexed assignments
1 parent f06bcbc commit 6fad9b1

File tree

208 files changed

+3885
-161737
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

208 files changed

+3885
-161737
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,12 @@
548548
"variableEnd": [
549549
"punctuation.definition.variable.end.robotframework"
550550
],
551+
"expressionBegin": [
552+
"punctuation.definition.expression.begin.robotframework"
553+
],
554+
"expressionEnd": [
555+
"punctuation.definition.expression.end.robotframework"
556+
],
551557
"escape": [
552558
"constant.character.escape.robotframework"
553559
],

packages/language_server/src/robotcode/language_server/robotframework/parts/semantic_tokens.py

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
iter_over_keyword_names_and_owners,
7171
token_in_range,
7272
)
73-
from robotcode.robot.utils.variables import is_variable, split_from_equals
73+
from robotcode.robot.utils.variables import split_from_equals
7474

7575
from .protocol_part import RobotLanguageServerProtocolPart
7676

@@ -116,6 +116,8 @@ class RobotSemTokenTypes(Enum):
116116
FOR_SEPARATOR = "forSeparator"
117117
VARIABLE_BEGIN = "variableBegin"
118118
VARIABLE_END = "variableEnd"
119+
EXPRESSION_BEGIN = "expressionBegin"
120+
EXPRESSION_END = "expressionEnd"
119121
VARIABLE_EXPRESSION = "variableExpression"
120122
ESCAPE = "escape"
121123
NAMESPACE = "namespace"
@@ -362,46 +364,8 @@ def generate_sem_sub_tokens(
362364
sem_mod = {SemanticTokenModifiers.DOCUMENTATION}
363365

364366
if token.type in [Token.VARIABLE, Token.ASSIGN]:
365-
if is_variable(token.value, "$@&%"):
366-
if col_offset is None:
367-
col_offset = token.col_offset
368-
if length is None:
369-
length = token.end_col_offset - token.col_offset
370-
371-
last_index = token.value.rfind("}")
372-
373-
is_expr = token.value[1:2] == "{" and token.value[last_index - 1 : last_index] == "}"
374-
375-
if last_index >= 0:
376-
yield SemTokenInfo(
377-
token.lineno,
378-
col_offset,
379-
3 if is_expr else 2,
380-
RobotSemTokenTypes.VARIABLE_BEGIN,
381-
sem_mod,
382-
)
383-
384-
yield SemTokenInfo(
385-
token.lineno,
386-
col_offset + ((last_index - 1) if is_expr else last_index),
387-
2 if is_expr else 1,
388-
RobotSemTokenTypes.VARIABLE_END,
389-
sem_mod,
390-
)
391-
392-
if length - last_index - 1 > 0:
393-
yield SemTokenInfo.from_token(
394-
token,
395-
sem_type,
396-
sem_mod,
397-
col_offset + last_index + 1,
398-
length - last_index - 1,
399-
)
400-
else:
401-
yield SemTokenInfo.from_token(token, sem_type, sem_mod)
402-
403-
else:
404-
yield SemTokenInfo.from_token(token, sem_type, sem_mod)
367+
# TODO: maybe we can distinguish between local and global variables, by default all variables are global
368+
pass
405369

406370
elif token.type in [Token.KEYWORD, ROBOT_KEYWORD_INNER] or (
407371
token.type == Token.NAME and cached_isinstance(node, Fixture, Template, TestTemplate)

packages/robot/src/robotcode/robot/config/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,11 +2371,11 @@ class RobotConfig(RobotExtendBaseProfile):
23712371
23722372
Examples:
23732373
```toml
2374-
default_profiles = "default"
2374+
default-profiles = "default"
23752375
```
23762376
23772377
```toml
2378-
default_profiles = ["default", "Firefox"]
2378+
default-profiles = ["default", "Firefox"]
23792379
```
23802380
"""
23812381
)

packages/robot/src/robotcode/robot/diagnostics/entities.py

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717

1818
from robot.parsing.lexer.tokens import Token
1919
from robotcode.core.lsp.types import Position, Range
20-
from robotcode.robot.utils.match import normalize
2120

2221
from ..utils.ast import range_from_token
23-
from ..utils.variables import search_variable
22+
from ..utils.variables import VariableMatcher
2423

2524
if TYPE_CHECKING:
2625
from robotcode.robot.diagnostics.library_doc import KeywordDoc, LibraryDoc
@@ -164,48 +163,6 @@ def __hash__(self) -> int:
164163
return hash((type(self), self.name, self.args))
165164

166165

167-
class InvalidVariableError(Exception):
168-
pass
169-
170-
171-
class VariableMatcher:
172-
def __init__(self, name: str) -> None:
173-
self.name = name
174-
175-
match = search_variable(name, "$@&%", ignore_errors=True)
176-
177-
if match.base is None:
178-
raise InvalidVariableError(f"Invalid variable '{name}'")
179-
180-
self.base = match.base
181-
182-
self.normalized_name = normalize(self.base)
183-
184-
def __eq__(self, o: object) -> bool:
185-
if type(o) is VariableMatcher:
186-
return o.normalized_name == self.normalized_name
187-
188-
if type(o) is str:
189-
match = search_variable(o, "$@&%", ignore_errors=True)
190-
base = match.base
191-
if base is None:
192-
return False
193-
194-
normalized = normalize(base)
195-
return self.normalized_name == normalized
196-
197-
return False
198-
199-
def __hash__(self) -> int:
200-
return hash(self.normalized_name)
201-
202-
def __str__(self) -> str:
203-
return self.name
204-
205-
def __repr__(self) -> str:
206-
return f"{type(self).__name__}(name={self.name!r})"
207-
208-
209166
class VariableDefinitionType(Enum):
210167
VARIABLE = "suite variable"
211168
LOCAL_VARIABLE = "local variable"

packages/robot/src/robotcode/robot/diagnostics/errors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ class Error:
4343
VARIABLE_OVERRIDDEN = "VariableOverridden"
4444
MODEL_ERROR = "ModelError"
4545
TOKEN_ERROR = "TokenError"
46+
ASSIGN_MARK_ALLOWED_ONLY_ON_LAST_VAR = "AssignmentMarkAllowedOnlyOnLastVariable"

packages/robot/src/robotcode/robot/diagnostics/model_helper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def get_namespace_info_from_keyword_token(
253253

254254
return lib_entry, kw_namespace
255255

256-
match_extended = re.compile(
256+
MATCH_EXTENDED = re.compile(
257257
r"""
258258
(.+?) # base name (group 1)
259259
([^\s\w].+) # extended part (group 2)
@@ -500,9 +500,9 @@ def iter_token(
500500
and sub_token.value[1:2] == "{"
501501
and sub_token.value[-1:] == "}"
502502
):
503-
match = cls.match_extended.match(name[2:-1])
504-
if match is not None:
505-
base_name, _ = match.groups()
503+
extended_match = cls.MATCH_EXTENDED.match(name[2:-1])
504+
if extended_match is not None:
505+
base_name, _ = extended_match.groups()
506506
name = f"{name[0]}{{{base_name.strip()}}}"
507507
var = namespace.find_variable(
508508
name,

packages/robot/src/robotcode/robot/diagnostics/namespace.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
from ..utils.stubs import Languages
5656
from ..utils.variables import (
5757
BUILTIN_VARIABLES,
58+
InvalidVariableError,
59+
VariableMatcher,
5860
is_scalar_assign,
5961
is_variable,
6062
search_variable,
@@ -66,7 +68,6 @@
6668
EnvironmentVariableDefinition,
6769
GlobalVariableDefinition,
6870
Import,
69-
InvalidVariableError,
7071
LibraryEntry,
7172
LibraryImport,
7273
LocalVariableDefinition,
@@ -76,7 +77,6 @@
7677
TestCaseDefinition,
7778
TestVariableDefinition,
7879
VariableDefinition,
79-
VariableMatcher,
8080
VariablesEntry,
8181
VariablesImport,
8282
)

0 commit comments

Comments
 (0)