Skip to content

Commit 75169e9

Browse files
committed
correct find references at token ends
1 parent 0542abc commit 75169e9

File tree

277 files changed

+1685
-1413
lines changed

Some content is hidden

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

277 files changed

+1685
-1413
lines changed

CHANGELOG.md

Lines changed: 450 additions & 447 deletions
Large diffs are not rendered by default.

robotcode/language_server/common/lsp_types.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,10 @@ def __ne__(self, other: Any) -> bool:
10521052
def __iter__(self) -> Iterator[int]:
10531053
return iter((self.line, self.character))
10541054

1055-
def is_in_range(self, range: "Range") -> bool:
1055+
def is_in_range(self, range: "Range", include_end: bool = True) -> bool:
1056+
if include_end:
1057+
return range.start <= self <= range.end
1058+
10561059
return range.start <= self < range.end
10571060

10581061

robotcode/language_server/robotframework/parts/completion.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ async def complete_default(
683683
only_stars = value is not None and "*" in value and all(v == "*" for v in value)
684684
if (
685685
r.start.character == 0
686-
and (position.is_in_range(r) or position == r.end)
686+
and (position.is_in_range(r))
687687
and (only_stars or value.startswith("*") or position.character == 0)
688688
):
689689
return await self.create_section_completion_items(r)
@@ -692,7 +692,7 @@ async def complete_default(
692692
ws = whitespace_at_begin_of_token(statement_node.tokens[1])
693693
if ws > 0:
694694
r1.end.character = r1.start.character + ws
695-
if position.is_in_range(r1) or position == r1.end:
695+
if position.is_in_range(r1):
696696
r.end = r1.end
697697
return await self.create_section_completion_items(r)
698698

@@ -782,7 +782,7 @@ async def complete_SettingSection( # noqa: N802
782782
if len(statement_node.tokens) > 0:
783783
token = cast(Token, statement_node.tokens[0])
784784
r = range_from_token(token)
785-
if position.is_in_range(r) or r.end == position:
785+
if position.is_in_range(r):
786786
return await self.create_settings_completion_items(r)
787787

788788
return None
@@ -826,7 +826,7 @@ async def _complete_TestCase_or_Keyword( # noqa: N802
826826
index += 1
827827
in_assign = True
828828
r = range_from_token(token)
829-
if position.is_in_range(r) or r.end == position:
829+
if position.is_in_range(r):
830830
break
831831

832832
if len(statement_node.tokens) > index:
@@ -839,7 +839,7 @@ async def _complete_TestCase_or_Keyword( # noqa: N802
839839
ws_b = whitespace_from_begin_of_token(token)
840840
r.start.character += 2 if ws_b and ws_b[0] != "\t" else 1
841841

842-
if position.is_in_range(r) or r.end == position:
842+
if position.is_in_range(r):
843843
return await create_items(
844844
in_assign,
845845
in_template,
@@ -865,7 +865,7 @@ async def _complete_TestCase_or_Keyword( # noqa: N802
865865
token = self.strip_bdd_prefix(token)
866866

867867
r = range_from_token(token)
868-
if position.is_in_range(r) or r.end == position:
868+
if position.is_in_range(r):
869869
return await create_items(in_assign, in_template, r, token, position)
870870

871871
if len(statement_node.tokens) > index + 1:
@@ -875,7 +875,7 @@ async def _complete_TestCase_or_Keyword( # noqa: N802
875875
return None
876876

877877
r.end.character += 1
878-
if position.is_in_range(r) or r.end == position:
878+
if position.is_in_range(r):
879879
return await create_items(
880880
in_assign, in_template, r, None if self.is_bdd_token(token) else token, position
881881
)
@@ -987,7 +987,7 @@ async def _complete_SuiteSetup_or_SuiteTeardown_or_TestTemplate( # noqa: N802
987987
ws_b = whitespace_from_begin_of_token(token)
988988
r.start.character += 2 if ws_b and ws_b[0] != "\t" else 1
989989

990-
if position.is_in_range(r) or r.end == position:
990+
if position.is_in_range(r):
991991
return await self.create_keyword_completion_items(
992992
statement_node.tokens[2] if r.end == position and len(statement_node.tokens) > 2 else None,
993993
position,
@@ -1001,7 +1001,7 @@ async def _complete_SuiteSetup_or_SuiteTeardown_or_TestTemplate( # noqa: N802
10011001
token = self.strip_bdd_prefix(token)
10021002

10031003
r = range_from_token(token)
1004-
if position.is_in_range(r) or r.end == position:
1004+
if position.is_in_range(r):
10051005
return await self.create_keyword_completion_items(
10061006
None if self.is_bdd_token(token) else token,
10071007
position,
@@ -1016,7 +1016,7 @@ async def _complete_SuiteSetup_or_SuiteTeardown_or_TestTemplate( # noqa: N802
10161016
return None
10171017

10181018
r.end.character += 1
1019-
if position.is_in_range(r) or r.end == position:
1019+
if position.is_in_range(r):
10201020
return await self.create_keyword_completion_items(
10211021
None if self.is_bdd_token(token) else token,
10221022
position,
@@ -1115,7 +1115,7 @@ async def complete_Setup_or_Teardown_or_Template( # noqa: N802
11151115
ws_b = whitespace_from_begin_of_token(token)
11161116
r.start.character += 2 if ws_b and ws_b[0] != "\t" else 1
11171117

1118-
if position.is_in_range(r) or r.end == position:
1118+
if position.is_in_range(r):
11191119
return await self.create_keyword_completion_items(
11201120
statement_node.tokens[3] if r.end == position and len(statement_node.tokens) > 3 else None,
11211121
position,
@@ -1129,7 +1129,7 @@ async def complete_Setup_or_Teardown_or_Template( # noqa: N802
11291129
token = self.strip_bdd_prefix(token)
11301130

11311131
r = range_from_token(token)
1132-
if position.is_in_range(r) or r.end == position:
1132+
if position.is_in_range(r):
11331133
return await self.create_keyword_completion_items(
11341134
token,
11351135
position,
@@ -1144,7 +1144,7 @@ async def complete_Setup_or_Teardown_or_Template( # noqa: N802
11441144
return None
11451145

11461146
r.end.character += 1
1147-
if position.is_in_range(r) or r.end == position:
1147+
if position.is_in_range(r):
11481148
return await self.create_keyword_completion_items(
11491149
None if self.is_bdd_token(token) else token,
11501150
position,
@@ -1210,18 +1210,18 @@ async def complete_import() -> Optional[List[CompletionItem]]:
12101210

12111211
if len(import_node.tokens) > import_token_index + 2:
12121212
name_token = import_node.tokens[import_token_index + 2]
1213-
if not position.is_in_range(r := range_from_token(name_token)) and r.end != position:
1213+
if not position.is_in_range(r := range_from_token(name_token)):
12141214
return None
12151215

12161216
elif len(import_node.tokens) > import_token_index + 1:
12171217
name_token = import_node.tokens[import_token_index + 1]
1218-
if position.is_in_range(r := range_from_token(name_token)) or r.end == position:
1218+
if position.is_in_range(r := range_from_token(name_token)):
12191219
if whitespace_at_begin_of_token(name_token) > 1:
12201220

12211221
ws_b = whitespace_from_begin_of_token(name_token)
12221222
r.start.character += 2 if ws_b and ws_b[0] != "\t" else 1
12231223

1224-
if not position.is_in_range(r) and r.end != position:
1224+
if not position.is_in_range(r):
12251225
return None
12261226
else:
12271227
return None
@@ -1448,18 +1448,18 @@ async def complete_ResourceImport( # noqa: N802
14481448

14491449
if len(import_node.tokens) > import_token_index + 2:
14501450
name_token = import_node.tokens[import_token_index + 2]
1451-
if not position.is_in_range(r := range_from_token(name_token)) and r.end != position:
1451+
if not position.is_in_range(r := range_from_token(name_token)):
14521452
return None
14531453

14541454
elif len(import_node.tokens) > import_token_index + 1:
14551455
name_token = import_node.tokens[import_token_index + 1]
1456-
if position.is_in_range(r := range_from_token(name_token)) or r.end == position:
1456+
if position.is_in_range(r := range_from_token(name_token)):
14571457
if whitespace_at_begin_of_token(name_token) > 1:
14581458

14591459
ws_b = whitespace_from_begin_of_token(name_token)
14601460
r.start.character += 2 if ws_b and ws_b[0] != "\t" else 1
14611461

1462-
if not position.is_in_range(r) and r.end != position:
1462+
if not position.is_in_range(r):
14631463
return None
14641464
else:
14651465
return None
@@ -1549,18 +1549,18 @@ async def complete_VariablesImport( # noqa: N802
15491549

15501550
if len(import_node.tokens) > import_token_index + 2:
15511551
name_token = import_node.tokens[import_token_index + 2]
1552-
if not position.is_in_range(r := range_from_token(name_token)) and r.end != position:
1552+
if not position.is_in_range(r := range_from_token(name_token)):
15531553
return None
15541554

15551555
elif len(import_node.tokens) > import_token_index + 1:
15561556
name_token = import_node.tokens[import_token_index + 1]
1557-
if position.is_in_range(r := range_from_token(name_token)) or r.end == position:
1557+
if position.is_in_range(r := range_from_token(name_token)):
15581558
if whitespace_at_begin_of_token(name_token) > 1:
15591559

15601560
ws_b = whitespace_from_begin_of_token(name_token)
15611561
r.start.character += 2 if ws_b and ws_b[0] != "\t" else 1
15621562

1563-
if not position.is_in_range(r) and r.end != position:
1563+
if not position.is_in_range(r):
15641564
return None
15651565
else:
15661566
return None

robotcode/language_server/robotframework/parts/document_highlight.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ async def _highlight_Template_or_TestTemplate( # noqa: N802
317317

318318
keyword_token = self.strip_bdd_prefix(keyword_token)
319319

320-
if position.is_in_range(range_from_token(keyword_token)):
320+
if position.is_in_range(range_from_token(keyword_token), False):
321321
namespace = await self.parent.documents_cache.get_namespace(document)
322322
if namespace is None:
323323
return None

robotcode/language_server/robotframework/utils/ast.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ def range_from_node(node: ast.AST, skip_non_data: bool = False, only_start: bool
135135
)
136136

137137

138-
def token_in_range(token: Token, range: Range) -> bool:
138+
def token_in_range(token: Token, range: Range, include_end: bool = False) -> bool:
139139
token_range = range_from_token(token)
140-
return token_range.start.is_in_range(range) or token_range.end.is_in_range(range)
140+
return token_range.start.is_in_range(range, include_end) or token_range.end.is_in_range(range, include_end)
141141

142142

143-
def node_in_range(node: ast.AST, range: Range) -> bool:
143+
def node_in_range(node: ast.AST, range: Range, include_end: bool = False) -> bool:
144144
node_range = range_from_node(node)
145-
return node_range.start.is_in_range(range) or node_range.end.is_in_range(range)
145+
return node_range.start.is_in_range(range, include_end) or node_range.end.is_in_range(range, include_end)
146146

147147

148148
def range_from_node_or_token(node: ast.AST, token: Optional[Token]) -> Range:
@@ -196,20 +196,20 @@ def get_tokens_at_position(node: HasTokens, position: Position) -> List[Token]:
196196
return [t for t in node.tokens if position.is_in_range(range := range_from_token(t)) or range.end == position]
197197

198198

199-
def iter_nodes_at_position(node: ast.AST, position: Position) -> AsyncIterator[ast.AST]:
199+
def iter_nodes_at_position(node: ast.AST, position: Position, include_end: bool = False) -> AsyncIterator[ast.AST]:
200200
return (
201201
n
202202
async for n in async_ast.iter_nodes(node)
203-
if position.is_in_range(range := range_from_node(n)) or range.end == position
203+
if position.is_in_range(range := range_from_node(n), include_end) or range.end == position
204204
)
205205

206206

207-
async def get_nodes_at_position(node: ast.AST, position: Position) -> List[ast.AST]:
208-
return [n async for n in iter_nodes_at_position(node, position)]
207+
async def get_nodes_at_position(node: ast.AST, position: Position, include_end: bool = False) -> List[ast.AST]:
208+
return [n async for n in iter_nodes_at_position(node, position, include_end)]
209209

210210

211-
async def get_node_at_position(node: ast.AST, position: Position) -> Optional[ast.AST]:
212-
result_nodes = await get_nodes_at_position(node, position)
211+
async def get_node_at_position(node: ast.AST, position: Position, include_end: bool = False) -> Optional[ast.AST]:
212+
result_nodes = await get_nodes_at_position(node, position, include_end)
213213
if not result_nodes:
214214
return None
215215

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ first
3636
# ^^^^^^^^^^^^^^ multiple references with namespace
3737
# ^^^^^ multiple variables
3838
Log ${A_VAR_FROM_RESOURE}
39-
# ^^^^^^^^^^^^^^^^^^^^^ a var from resource
39+
# ^^^^^^^^^^^^^^^^^^ a var from resource
4040

4141
second
4242
[Template] Log To Console
@@ -52,10 +52,10 @@ third
5252

5353
forth
5454
${result} lib_hello.A Library Keyword
55-
# ^^^^^^^^^ Keyword assignement
55+
# ^^^^^^ Keyword assignement
5656
Should Be Equal ${result} from hello
5757
${result}= lib_var.A Library Keyword
58-
# ^^^^^^^^^ Keyword assignment with equals sign
58+
# ^^^^^^ Keyword assignment with equals sign
5959
Should Be Equal ${result} ${LIB_ARG}
6060

6161

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Variables ${CURDIR}/../lib/myvariables.py
1212
# ^^^^^^^^^^^^^^ variable import by path name
1313
Resource ${CURDIR}/../resources/firstresource.resource
1414
# ^^^^^^^^^^^^^^ resource import by path name
15-
# ^^^^^^^ variable in resource import
15+
# ^^^^^^ variable in resource import
1616
Resource ../resources/folder_a/duplicated.resource
1717
Resource ../resources/folder_b/duplicated.resource
1818

@@ -40,7 +40,8 @@ ${K} ${A+'${B+"${F}"}'+'${D}'} ${C}
4040
# ^ inner var
4141
# ^ inner var
4242
# ^ inner inner var
43-
# ^ outer var
43+
# ^ outer var
44+
# ^ extra var
4445

4546
${D} 3
4647
${E} SEPARATOR=\n asd def hij
@@ -68,7 +69,7 @@ first
6869
# ^^^^^^^^^^^^^^ Keyword with namespace
6970
# ^^^^^^^^^^^ namespace before keyword
7071
FOR ${key} ${value} IN &{A DICT}
71-
# ^^^^^^ FOR loop variable declaration
72+
# ^^^^ FOR loop variable declaration
7273
Log ${key}=${value}
7374
# ^^^ Keyword in FOR loop
7475
END
@@ -79,7 +80,7 @@ first
7980
# ^^^^^^ BuiltIn variable
8081
#^^^ Spaces
8182
Log ${A_VAR_FROM_LIB}
82-
# ^^^^^^^^^^^^^^^^^ variable from lib
83+
# ^^^^^^^^^^^^^^ variable from lib
8384

8485
do something in a resource
8586
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ Keyword from resource
@@ -101,7 +102,7 @@ third
101102
# ^^^^^^^^^ Keyword assignement
102103
Should Be Equal ${result} from hello
103104
${result}= lib_var.A Library Keyword
104-
# ^^^^^^^^^ Keyword assignment with equals sign
105+
# ^^^^^^ Keyword assignment with equals sign
105106
Should Be Equal first=${result} second=${LIB_ARG}
106107

107108
forth

tests/robotcode/language_server/robotframework/parts/test_document_highlight/test_document_highlight_robot_009_045_Variable_in_library_params_.yml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,31 @@ data: !GeneratedTestData
22
character: 45
33
line: 9
44
name: Variable in library params
5-
result: null
5+
result:
6+
- !DocumentHighlight
7+
kind: !DocumentHighlightKind 'TEXT'
8+
range:
9+
end:
10+
character: 45
11+
line: 9
12+
start:
13+
character: 38
14+
line: 9
15+
- !DocumentHighlight
16+
kind: !DocumentHighlightKind 'TEXT'
17+
range:
18+
end:
19+
character: 9
20+
line: 20
21+
start:
22+
character: 2
23+
line: 20
24+
- !DocumentHighlight
25+
kind: !DocumentHighlightKind 'TEXT'
26+
range:
27+
end:
28+
character: 44
29+
line: 58
30+
start:
31+
character: 37
32+
line: 58

tests/robotcode/language_server/robotframework/parts/test_document_highlight/test_document_highlight_robot_037_011_a_var_from_resource_.yml

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
data: !GeneratedTestData
2-
character: 21
2+
character: 13
33
line: 37
44
name: a var from resource
55
result:

0 commit comments

Comments
 (0)