Skip to content

Commit 9c054d6

Browse files
committed
Support for simple values (number, bool, str) from variable and yaml files
1 parent fa723ab commit 9c054d6

26 files changed

+1112
-1035
lines changed

CHANGELOG.md

+711-708
Large diffs are not rendered by default.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ put this to the `settings.json`
261261
}
262262
}
263263
]
264-
}
264+
},
265265
266266
"editor.semanticTokenColorCustomizations": {
267267
"rules": {

poetry.lock

+87-86
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

robotcode/language_server/robotframework/diagnostics/imports_manager.py

+46-38
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import asyncio
55
import os
66
import sys
7+
import threading
78
import weakref
89
from abc import ABC, abstractmethod
910
from collections import OrderedDict
@@ -473,67 +474,74 @@ def __init__(self, parent_protocol: RobotLanguageServerProtocol, folder: Uri, co
473474
self.parent_protocol.documents.did_create_uri.add(self._do_imports_changed)
474475
self.parent_protocol.documents.did_change.add(self.resource_document_changed)
475476
self._command_line_variables: Optional[List[VariableDefinition]] = None
477+
self._command_line_variables_lock = Lock()
476478

477479
self._python_path: Optional[List[str]] = None
480+
self._python_path_lock = threading.RLock()
478481
self._environment: Optional[Mapping[str, str]] = None
482+
self._environment_lock = threading.RLock()
479483

480484
self._library_files_cache = AsyncSimpleLRUCache()
481485
self._resource_files_cache = AsyncSimpleLRUCache()
482486
self._variables_files_cache = AsyncSimpleLRUCache()
483487

484488
@property
485489
def environment(self) -> Mapping[str, str]:
486-
if self._environment is None:
487-
self._environment = dict(os.environ)
490+
with self._environment_lock:
491+
if self._environment is None:
492+
self._environment = dict(os.environ)
488493

489-
self._environment.update(self.config.env)
494+
self._environment.update(self.config.env)
490495

491-
return self._environment
496+
return self._environment
492497

493498
@property
494499
def python_path(self) -> List[str]:
495-
if self._python_path is None:
496-
self._python_path = sys.path
500+
with self._python_path_lock:
501+
if self._python_path is None:
502+
self._python_path = sys.path
497503

498-
file = Path(__file__).resolve()
499-
top = file.parents[3]
500-
for p in filter(lambda v: path_is_relative_to(v, top), sys.path.copy()):
501-
self._python_path.remove(p)
504+
file = Path(__file__).resolve()
505+
top = file.parents[3]
506+
for p in filter(lambda v: path_is_relative_to(v, top), sys.path.copy()):
507+
self._python_path.remove(p)
502508

503-
for p in self.config.python_path:
504-
absolute_path = str(Path(p).absolute())
505-
if absolute_path not in self._python_path:
506-
self._python_path.insert(0, absolute_path)
509+
for p in self.config.python_path:
510+
absolute_path = str(Path(p).absolute())
511+
if absolute_path not in self._python_path:
512+
self._python_path.insert(0, absolute_path)
507513

508-
return self._python_path or []
514+
return self._python_path
509515

510516
@_logger.call
511517
async def get_command_line_variables(self) -> List[VariableDefinition]:
512518
from robot.utils.text import split_args_from_name_or_path
513519

514-
if self._command_line_variables is None:
515-
if self.config is None:
516-
self._command_line_variables = []
517-
else:
518-
self._command_line_variables = [
519-
CommandLineVariableDefinition(0, 0, 0, 0, "", f"${{{k}}}", None, has_value=True, value=(v,))
520-
for k, v in self.config.variables.items()
521-
]
522-
for variable_file in self.config.variable_files:
523-
name, args = split_args_from_name_or_path(variable_file)
524-
try:
525-
lib_doc = await self.get_libdoc_for_variables_import(
526-
name, tuple(args), str(self.folder.to_path()), self
527-
)
528-
if lib_doc is not None:
529-
self._command_line_variables += lib_doc.variables
530-
531-
except (SystemExit, KeyboardInterrupt, asyncio.CancelledError):
532-
raise
533-
except BaseException as e:
534-
self._logger.exception(e)
535-
536-
return self._command_line_variables
520+
async with self._command_line_variables_lock:
521+
if self._command_line_variables is None:
522+
523+
if self.config is None:
524+
self._command_line_variables = []
525+
else:
526+
self._command_line_variables = [
527+
CommandLineVariableDefinition(0, 0, 0, 0, "", f"${{{k}}}", None, has_value=True, value=(v,))
528+
for k, v in self.config.variables.items()
529+
]
530+
for variable_file in self.config.variable_files:
531+
name, args = split_args_from_name_or_path(variable_file)
532+
try:
533+
lib_doc = await self.get_libdoc_for_variables_import(
534+
name, tuple(args), str(self.folder.to_path()), self
535+
)
536+
if lib_doc is not None:
537+
self._command_line_variables += lib_doc.variables
538+
539+
except (SystemExit, KeyboardInterrupt, asyncio.CancelledError):
540+
raise
541+
except BaseException as e:
542+
self._logger.exception(e)
543+
544+
return self._command_line_variables
537545

538546
@async_tasking_event
539547
async def libraries_changed(sender, libraries: List[LibraryDoc]) -> None: # NOSONAR

robotcode/language_server/robotframework/diagnostics/library_doc.py

+31-3
Original file line numberDiff line numberDiff line change
@@ -1029,9 +1029,13 @@ def resolve_robot_variables(
10291029

10301030
if variables is not None:
10311031

1032-
vars = [_Variable(k, v) for k, v in variables.items() if v is not None]
1032+
vars = [_Variable(k, v) for k, v in variables.items() if v is not None and not isinstance(v, NativeValue)]
10331033
result.set_from_variable_table(vars)
10341034

1035+
for k2, v2 in variables.items():
1036+
if isinstance(v2, NativeValue):
1037+
result[k2] = v2.value
1038+
10351039
result.resolve_delayed()
10361040

10371041
return result
@@ -1455,6 +1459,20 @@ def find_variables(
14551459
)
14561460

14571461

1462+
# @dataclass
1463+
class NativeValue:
1464+
__slots__ = ["value"]
1465+
1466+
def __init__(self, value: Any) -> None:
1467+
self.value = value
1468+
1469+
def __repr__(self) -> str:
1470+
return repr(self.value)
1471+
1472+
def __str__(self) -> str:
1473+
return str(self.value)
1474+
1475+
14581476
def get_variables_doc(
14591477
name: str,
14601478
args: Optional[Tuple[Any, ...]] = None,
@@ -1513,9 +1531,19 @@ def import_variables(self, path: str, args: Optional[Tuple[Any, ...]] = None) ->
15131531

15141532
vars: List[VariableDefinition] = [
15151533
ImportedVariableDefinition(
1516-
1, 0, 1, 0, source or (module_spec.origin if module_spec is not None else None) or "", var[0], None
1534+
1,
1535+
0,
1536+
1,
1537+
0,
1538+
source or (module_spec.origin if module_spec is not None else None) or "",
1539+
name,
1540+
None,
1541+
value=NativeValue(value)
1542+
if isinstance(value, (int, float, bool, str, set, tuple, dict, list))
1543+
else None,
1544+
has_value=isinstance(value, (int, float, bool, str, set, tuple, dict, list)),
15171545
)
1518-
for var in importer.import_variables(import_name, args)
1546+
for name, value in importer.import_variables(import_name, args)
15191547
]
15201548

15211549
return VariablesDoc(

robotcode/language_server/robotframework/diagnostics/namespace.py

+1
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ async def ensure_initialized(self) -> bool:
810810
self._diagnostics = data_entry.diagnostics.copy()
811811
self._import_entries = data_entry.import_entries.copy()
812812
else:
813+
813814
variables = await self.get_resolvable_variables()
814815

815816
await self._import_default_libraries(variables)

robotcode/language_server/robotframework/parts/hover.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ast
44
import asyncio
5+
import reprlib
56
from typing import (
67
TYPE_CHECKING,
78
Any,
@@ -113,7 +114,7 @@ async def _hover_default(self, nodes: List[ast.AST], document: TextDocument, pos
113114

114115
if variable.has_value or variable.resolvable:
115116
try:
116-
value = repr(
117+
value = reprlib.repr(
117118
await namespace.imports_manager.resolve_variable(
118119
variable.name,
119120
str(document.uri.to_path().parent),

tests/robotcode/language_server/robotframework/parts/data/.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
}
1414
},
1515
"robotcode.languageServer.args": [
16-
"--debugpy",
16+
//"--debugpy",
1717
// "--log",
1818
// "--log-all",
1919
// "--log-level",

tests/robotcode/language_server/robotframework/parts/data/lib/myvariables.py

+5
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ def dummy(): # type: ignore
33

44

55
A_VAR_FROM_LIB = 1
6+
A_DICT_VAR_FROM_LIB = {1, 2, 3}
7+
A_LIST_VAR_FROM_LUB = [1, 2, 3, 4]
8+
A_MIXED_VAR_FROM_LIB = {1: "asd", 2: [1, 2, 3]}
9+
10+
A_VERY_LONG_VAR_FROM_LIB = {str(i) for i in range(1000)}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
TEST_VAR: 1
2-
TEST_VAR2: 2
2+
TEST_VAR2: "hello"
33
TEST_VAR3: [1, 2, 3, 4]

tests/robotcode/language_server/robotframework/parts/data/tests/variables.robot

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
*** Settings ***
22
Suite Teardown Run Keywords Log ${SUITE STATUS} AND Log ${SUITE MESSAGE} ${TEST NAME} AND Log Variables
33
Test Setup Log ${TEST NAME}
4-
4+
Variables myvariables.py
5+
Variables testvars.yml
56

67
*** Variables ***
78
${pre_full_name} ${PREV TEST NAME}_${PREV TEST STATUS}
@@ -181,3 +182,5 @@ a keyword with kwonly separator
181182

182183
dummy
183184
a keyword with kwonly separator a scope=1 keywords=1
185+
186+
*** Test Cases ***

tests/robotcode/language_server/robotframework/parts/test_references/test_references_robot_003_018_Variable_in_library_import_path_.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ result:
124124
range:
125125
end:
126126
character: 19
127-
line: 15
127+
line: 16
128128
start:
129129
character: 13
130-
line: 15
130+
line: 16
131131
uri: tests/variables.robot

tests/robotcode/language_server/robotframework/parts/test_references/test_references_robot_003_021_Variable_in_library_import_path_.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ result:
124124
range:
125125
end:
126126
character: 19
127-
line: 15
127+
line: 16
128128
start:
129129
character: 13
130-
line: 15
130+
line: 16
131131
uri: tests/variables.robot

tests/robotcode/language_server/robotframework/parts/test_references/test_references_robot_003_023_Variable_in_library_import_path_.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ result:
124124
range:
125125
end:
126126
character: 19
127-
line: 15
127+
line: 16
128128
start:
129129
character: 13
130-
line: 15
130+
line: 16
131131
uri: tests/variables.robot

tests/robotcode/language_server/robotframework/parts/test_references/test_references_robot_006_018_Variable_in_variables_import_path_.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ result:
124124
range:
125125
end:
126126
character: 19
127-
line: 15
127+
line: 16
128128
start:
129129
character: 13
130-
line: 15
130+
line: 16
131131
uri: tests/variables.robot

tests/robotcode/language_server/robotframework/parts/test_references/test_references_robot_006_021_Variable_in_variables_import_path_.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ result:
124124
range:
125125
end:
126126
character: 19
127-
line: 15
127+
line: 16
128128
start:
129129
character: 13
130-
line: 15
130+
line: 16
131131
uri: tests/variables.robot

tests/robotcode/language_server/robotframework/parts/test_references/test_references_robot_006_023_Variable_in_variables_import_path_.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ result:
124124
range:
125125
end:
126126
character: 19
127-
line: 15
127+
line: 16
128128
start:
129129
character: 13
130-
line: 15
130+
line: 16
131131
uri: tests/variables.robot

tests/robotcode/language_server/robotframework/parts/test_references/test_references_robot_006_033_a_variable_import_.yml

+9
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,12 @@ result:
3939
character: 16
4040
line: 6
4141
uri: tests/references.robot
42+
- !Location
43+
range:
44+
end:
45+
character: 27
46+
line: 3
47+
start:
48+
character: 13
49+
line: 3
50+
uri: tests/variables.robot

tests/robotcode/language_server/robotframework/parts/test_references/test_references_robot_006_040_a_variable_import_.yml

+9
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,12 @@ result:
3939
character: 16
4040
line: 6
4141
uri: tests/references.robot
42+
- !Location
43+
range:
44+
end:
45+
character: 27
46+
line: 3
47+
start:
48+
character: 13
49+
line: 3
50+
uri: tests/variables.robot

tests/robotcode/language_server/robotframework/parts/test_references/test_references_robot_006_046_a_variable_import_.yml

+9
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,12 @@ result:
3939
character: 16
4040
line: 6
4141
uri: tests/references.robot
42+
- !Location
43+
range:
44+
end:
45+
character: 27
46+
line: 3
47+
start:
48+
character: 13
49+
line: 3
50+
uri: tests/variables.robot

tests/robotcode/language_server/robotframework/parts/test_references/test_references_robot_009_018_Variable_in_resource_import_path_.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ result:
124124
range:
125125
end:
126126
character: 19
127-
line: 15
127+
line: 16
128128
start:
129129
character: 13
130-
line: 15
130+
line: 16
131131
uri: tests/variables.robot

tests/robotcode/language_server/robotframework/parts/test_references/test_references_robot_009_021_Variable_in_resource_import_path_.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ result:
124124
range:
125125
end:
126126
character: 19
127-
line: 15
127+
line: 16
128128
start:
129129
character: 13
130-
line: 15
130+
line: 16
131131
uri: tests/variables.robot

tests/robotcode/language_server/robotframework/parts/test_references/test_references_robot_009_023_Variable_in_resource_import_path_.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ result:
124124
range:
125125
end:
126126
character: 19
127-
line: 15
127+
line: 16
128128
start:
129129
character: 13
130-
line: 15
130+
line: 16
131131
uri: tests/variables.robot

0 commit comments

Comments
 (0)