Skip to content

Commit 1db6978

Browse files
committed
fix(language_server): improve range calculation for document symbols and add handling for negative ranges
fixes #377
1 parent f2e2064 commit 1db6978

File tree

24 files changed

+542
-2
lines changed

24 files changed

+542
-2
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ def visit_Variable(self, node: Statement) -> None: # noqa: N802
267267
name = name[:-1].rstrip()
268268

269269
if self.current_symbol is not None and self.current_symbol.children is not None:
270-
r = range_from_node(node)
270+
if node.tokens[:1]:
271+
r = range_from_token(node.tokens[0])
272+
else:
273+
r = range_from_node(node)
271274
symbol = DocumentSymbol(name=name, kind=SymbolKind.VARIABLE, range=r, selection_range=r)
272275
self.current_symbol.children.append(symbol)

packages/robot/src/robotcode/robot/utils/ast.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ def range_from_node(
122122
skip_non_data: bool = False,
123123
only_start: bool = False,
124124
allow_comments: bool = False,
125+
) -> Range:
126+
r = _range_from_node(node, skip_non_data, only_start, allow_comments)
127+
if r.end.line < 0 or r.end.character < 0:
128+
return Range(r.start, r.start)
129+
return r
130+
131+
132+
def _range_from_node(
133+
node: ast.AST,
134+
skip_non_data: bool = False,
135+
only_start: bool = False,
136+
allow_comments: bool = False,
125137
) -> Range:
126138
if skip_non_data:
127139
if cached_isinstance(node, Statement) and node.tokens:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
data: !GeneratedTestData
2+
character: 1
3+
line: 6
4+
name: variable in variables section
5+
result: !DocumentSymbol
6+
children: []
7+
deprecated: null
8+
detail: null
9+
kind: 13
10+
name: ${A_VAR}
11+
range:
12+
end:
13+
character: 8
14+
line: 6
15+
start:
16+
character: 0
17+
line: 6
18+
selection_range:
19+
end:
20+
character: 8
21+
line: 6
22+
start:
23+
character: 0
24+
line: 6
25+
tags: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
data: !GeneratedTestData
2+
character: 4
3+
line: 6
4+
name: variable in variables section
5+
result: !DocumentSymbol
6+
children: []
7+
deprecated: null
8+
detail: null
9+
kind: 13
10+
name: ${A_VAR}
11+
range:
12+
end:
13+
character: 8
14+
line: 6
15+
start:
16+
character: 0
17+
line: 6
18+
selection_range:
19+
end:
20+
character: 8
21+
line: 6
22+
start:
23+
character: 0
24+
line: 6
25+
tags: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
data: !GeneratedTestData
2+
character: 7
3+
line: 6
4+
name: variable in variables section
5+
result: !DocumentSymbol
6+
children: []
7+
deprecated: null
8+
detail: null
9+
kind: 13
10+
name: ${A_VAR}
11+
range:
12+
end:
13+
character: 8
14+
line: 6
15+
start:
16+
character: 0
17+
line: 6
18+
selection_range:
19+
end:
20+
character: 8
21+
line: 6
22+
start:
23+
character: 0
24+
line: 6
25+
tags: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
data: !GeneratedTestData
2+
character: 1
3+
line: 6
4+
name: variable in variables section
5+
result: !DocumentSymbol
6+
children: []
7+
deprecated: null
8+
detail: null
9+
kind: 13
10+
name: ${A_VAR}
11+
range:
12+
end:
13+
character: 8
14+
line: 6
15+
start:
16+
character: 0
17+
line: 6
18+
selection_range:
19+
end:
20+
character: 8
21+
line: 6
22+
start:
23+
character: 0
24+
line: 6
25+
tags: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
data: !GeneratedTestData
2+
character: 4
3+
line: 6
4+
name: variable in variables section
5+
result: !DocumentSymbol
6+
children: []
7+
deprecated: null
8+
detail: null
9+
kind: 13
10+
name: ${A_VAR}
11+
range:
12+
end:
13+
character: 8
14+
line: 6
15+
start:
16+
character: 0
17+
line: 6
18+
selection_range:
19+
end:
20+
character: 8
21+
line: 6
22+
start:
23+
character: 0
24+
line: 6
25+
tags: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
data: !GeneratedTestData
2+
character: 7
3+
line: 6
4+
name: variable in variables section
5+
result: !DocumentSymbol
6+
children: []
7+
deprecated: null
8+
detail: null
9+
kind: 13
10+
name: ${A_VAR}
11+
range:
12+
end:
13+
character: 8
14+
line: 6
15+
start:
16+
character: 0
17+
line: 6
18+
selection_range:
19+
end:
20+
character: 8
21+
line: 6
22+
start:
23+
character: 0
24+
line: 6
25+
tags: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
data: !GeneratedTestData
2+
character: 1
3+
line: 6
4+
name: variable in variables section
5+
result: !DocumentSymbol
6+
children: []
7+
deprecated: null
8+
detail: null
9+
kind: 13
10+
name: ${A_VAR}
11+
range:
12+
end:
13+
character: 8
14+
line: 6
15+
start:
16+
character: 0
17+
line: 6
18+
selection_range:
19+
end:
20+
character: 8
21+
line: 6
22+
start:
23+
character: 0
24+
line: 6
25+
tags: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
data: !GeneratedTestData
2+
character: 4
3+
line: 6
4+
name: variable in variables section
5+
result: !DocumentSymbol
6+
children: []
7+
deprecated: null
8+
detail: null
9+
kind: 13
10+
name: ${A_VAR}
11+
range:
12+
end:
13+
character: 8
14+
line: 6
15+
start:
16+
character: 0
17+
line: 6
18+
selection_range:
19+
end:
20+
character: 8
21+
line: 6
22+
start:
23+
character: 0
24+
line: 6
25+
tags: null

0 commit comments

Comments
 (0)