Skip to content

Commit 135b0d4

Browse files
committed
refactor(langserver): move local import to global imports in document_symbols
1 parent 755daf7 commit 135b0d4

File tree

153 files changed

+862
-671
lines changed

Some content is hidden

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

153 files changed

+862
-671
lines changed
Lines changed: 39 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import ast
22
import itertools
3-
from typing import TYPE_CHECKING, Any, List, Optional, Union, cast
3+
from typing import TYPE_CHECKING, Any, List, Optional, Union
44

5+
from robot.errors import VariableError
56
from robot.parsing.lexer.tokens import Token
7+
from robot.parsing.model.blocks import Keyword, Section, TestCase
8+
from robot.parsing.model.statements import Statement
9+
from robot.variables import search_variable
610
from robotcode.core.lsp.types import DocumentSymbol, SymbolInformation, SymbolKind
711
from robotcode.core.utils.logging import LoggingDescriptor
812
from robotcode.robot.utils.ast import range_from_node, range_from_token, tokenize_variables
@@ -29,7 +33,7 @@ def __init__(self, parent: "RobotLanguageServerProtocol") -> None:
2933
def collect(
3034
self, sender: Any, document: TextDocument
3135
) -> Optional[Union[List[DocumentSymbol], List[SymbolInformation], None]]:
32-
return _Visitor.find_from(self.parent.documents_cache.get_model(document), self)
36+
return _Visitor.find_from(self.parent.documents_cache.get_model(document, False), self)
3337

3438

3539
class _Visitor(Visitor):
@@ -56,22 +60,14 @@ def find_from(cls, model: ast.AST, parent: RobotDocumentSymbolsProtocolPart) ->
5660

5761
return finder.result if finder.result else None
5862

59-
def visit_Section(self, node: ast.AST) -> None: # noqa: N802
60-
from robot.parsing.model.blocks import Section
61-
from robot.parsing.model.statements import SectionHeader
62-
63-
section = cast(Section, node)
64-
if section.header is None:
65-
return
66-
67-
header = cast(SectionHeader, section.header)
68-
if not header.name:
63+
def visit_Section(self, node: Section) -> None: # noqa: N802
64+
if not node.header or not node.header.name:
6965
return
7066

71-
r = range_from_node(section)
67+
r = range_from_node(node)
7268
symbol = DocumentSymbol(
73-
name=header.name.replace("*", "").strip(),
74-
kind=SymbolKind.NAMESPACE,
69+
name=node.header.name,
70+
kind=SymbolKind.MODULE,
7571
range=r,
7672
selection_range=r,
7773
children=[],
@@ -81,45 +77,33 @@ def visit_Section(self, node: ast.AST) -> None: # noqa: N802
8177

8278
self.generic_visit_current_symbol(node, symbol)
8379

84-
def visit_TestCase(self, node: ast.AST) -> None: # noqa: N802
85-
from robot.parsing.model.blocks import TestCase
86-
87-
testcase = cast(TestCase, node)
88-
if testcase.name is None:
80+
def visit_TestCase(self, node: TestCase) -> None: # noqa: N802
81+
if node.name is None:
8982
return
9083

9184
if self.current_symbol is not None and self.current_symbol.children is not None:
92-
r = range_from_node(testcase)
93-
symbol = DocumentSymbol(name=testcase.name, kind=SymbolKind.METHOD, range=r, selection_range=r, children=[])
85+
r = range_from_node(node)
86+
symbol = DocumentSymbol(name=node.name, kind=SymbolKind.METHOD, range=r, selection_range=r, children=[])
9487
self.current_symbol.children.append(symbol)
9588

9689
self.generic_visit_current_symbol(node, symbol)
9790

98-
def visit_Keyword(self, node: ast.AST) -> None: # noqa: N802
99-
from robot.parsing.model.blocks import Keyword
100-
101-
keyword = cast(Keyword, node)
102-
if keyword.name is None:
91+
def visit_Keyword(self, node: Keyword) -> None: # noqa: N802
92+
if node.name is None:
10393
return
10494

10595
if self.current_symbol is not None and self.current_symbol.children is not None:
106-
r = range_from_node(keyword)
107-
symbol = DocumentSymbol(
108-
name=keyword.name, kind=SymbolKind.FUNCTION, range=r, selection_range=r, children=[]
109-
)
96+
r = range_from_node(node)
97+
symbol = DocumentSymbol(name=node.name, kind=SymbolKind.FUNCTION, range=r, selection_range=r, children=[])
11098
self.current_symbol.children.append(symbol)
11199

112100
self.generic_visit_current_symbol(node, symbol)
113101

114-
def visit_Arguments(self, node: ast.AST) -> None: # noqa: N802
115-
from robot.parsing.lexer.tokens import Token as RobotToken
116-
from robot.parsing.model.statements import Arguments
117-
118-
n = cast(Arguments, node)
119-
arguments = n.get_tokens(RobotToken.ARGUMENT)
102+
def visit_Arguments(self, node: Statement) -> None: # noqa: N802
103+
arguments = node.get_tokens(Token.ARGUMENT)
120104

121105
if self.current_symbol is not None and self.current_symbol.children is not None:
122-
for argument_token in (cast(RobotToken, e) for e in arguments):
106+
for argument_token in arguments:
123107
if argument_token.value == "@{}":
124108
continue
125109

@@ -133,29 +117,22 @@ def visit_Arguments(self, node: ast.AST) -> None: # noqa: N802
133117
self.current_symbol.children.append(symbol)
134118

135119
def get_variable_token(self, token: Token) -> Optional[Token]:
136-
from robot.parsing.lexer.tokens import Token as RobotToken
137-
138120
return next(
139121
(
140122
v
141123
for v in itertools.dropwhile(
142-
lambda t: t.type in RobotToken.NON_DATA_TOKENS,
124+
lambda t: t.type in Token.NON_DATA_TOKENS,
143125
tokenize_variables(token, ignore_errors=True),
144126
)
145-
if v.type == RobotToken.VARIABLE
127+
if v.type == Token.VARIABLE
146128
),
147129
None,
148130
)
149131

150-
def visit_KeywordCall(self, node: ast.AST) -> None: # noqa: N802
151-
from robot.errors import VariableError
152-
from robot.parsing.lexer.tokens import Token as RobotToken
153-
from robot.parsing.model.statements import KeywordCall
154-
132+
def visit_KeywordCall(self, node: Statement) -> None: # noqa: N802
155133
# TODO analyse "Set Local/Global/Suite Variable"
156134

157-
keyword_call = cast(KeywordCall, node)
158-
for assign_token in keyword_call.get_tokens(RobotToken.ASSIGN):
135+
for assign_token in node.get_tokens(Token.ASSIGN):
159136
if assign_token is None:
160137
continue
161138

@@ -175,12 +152,8 @@ def visit_KeywordCall(self, node: ast.AST) -> None: # noqa: N802
175152
except VariableError:
176153
pass
177154

178-
def visit_ForHeader(self, node: ast.AST) -> None: # noqa: N802
179-
from robot.parsing.lexer.tokens import Token as RobotToken
180-
from robot.parsing.model.statements import ForHeader
181-
182-
n = cast(ForHeader, node)
183-
variables = n.get_tokens(RobotToken.VARIABLE)
155+
def visit_ForHeader(self, node: Statement) -> None: # noqa: N802
156+
variables = node.get_tokens(Token.VARIABLE)
184157

185158
if self.current_symbol is not None and self.current_symbol.children is not None:
186159
for variable in variables:
@@ -193,12 +166,8 @@ def visit_ForHeader(self, node: ast.AST) -> None: # noqa: N802
193166
if symbol.name not in map(lambda v: v.name, self.current_symbol.children):
194167
self.current_symbol.children.append(symbol)
195168

196-
def visit_ExceptHeader(self, node: ast.AST) -> None: # noqa: N802
197-
from robot.parsing.lexer.tokens import Token as RobotToken
198-
from robot.parsing.model.statements import ExceptHeader
199-
200-
n = cast(ExceptHeader, node)
201-
variables = n.get_tokens(RobotToken.VARIABLE)
169+
def visit_ExceptHeader(self, node: Statement) -> None: # noqa: N802
170+
variables = node.get_tokens(Token.VARIABLE)
202171

203172
if self.current_symbol is not None and self.current_symbol.children is not None:
204173
for variable in variables:
@@ -211,12 +180,8 @@ def visit_ExceptHeader(self, node: ast.AST) -> None: # noqa: N802
211180
if symbol.name not in map(lambda v: v.name, self.current_symbol.children):
212181
self.current_symbol.children.append(symbol)
213182

214-
def visit_Var(self, node: ast.AST) -> None: # noqa: N802
215-
from robot.parsing.lexer.tokens import Token as RobotToken
216-
from robot.parsing.model.statements import Var
217-
218-
n = cast(Var, node)
219-
variables = n.get_tokens(RobotToken.VARIABLE)
183+
def visit_Var(self, node: Statement) -> None: # noqa: N802
184+
variables = node.get_tokens(Token.VARIABLE)
220185

221186
if self.current_symbol is not None and self.current_symbol.children is not None:
222187
for variable in variables:
@@ -229,20 +194,14 @@ def visit_Var(self, node: ast.AST) -> None: # noqa: N802
229194
if symbol.name not in map(lambda v: v.name, self.current_symbol.children):
230195
self.current_symbol.children.append(symbol)
231196

232-
def visit_KeywordName(self, node: ast.AST) -> None: # noqa: N802
233-
from robot.parsing.lexer.tokens import Token as RobotToken
234-
from robot.parsing.model.statements import KeywordName
235-
236-
n = cast(KeywordName, node)
237-
nt = n.get_token(RobotToken.KEYWORD_NAME)
238-
if nt is None:
197+
def visit_KeywordName(self, node: Statement) -> None: # noqa: N802
198+
name_token = node.get_token(Token.KEYWORD_NAME)
199+
if name_token is None:
239200
return
240201

241-
name_token = cast(Token, nt)
242-
243202
if self.current_symbol is not None and self.current_symbol.children is not None:
244203
for variable in filter(
245-
lambda e: e.type == RobotToken.VARIABLE,
204+
lambda e: e.type == Token.VARIABLE,
246205
tokenize_variables(name_token, identifiers="$", ignore_errors=True),
247206
):
248207
variable_token = self.get_variable_token(variable)
@@ -254,14 +213,8 @@ def visit_KeywordName(self, node: ast.AST) -> None: # noqa: N802
254213
if symbol.name not in map(lambda v: v.name, self.current_symbol.children):
255214
self.current_symbol.children.append(symbol)
256215

257-
def visit_Variable(self, node: ast.AST) -> None: # noqa: N802
258-
from robot.api.parsing import Token as RobotToken
259-
from robot.parsing.model.statements import Variable
260-
from robot.variables import search_variable
261-
262-
variable = cast(Variable, node)
263-
264-
name_token = variable.get_token(RobotToken.VARIABLE)
216+
def visit_Variable(self, node: Statement) -> None: # noqa: N802
217+
name_token = node.get_token(Token.VARIABLE)
265218
name = name_token.value
266219

267220
if name is not None:
@@ -273,6 +226,6 @@ def visit_Variable(self, node: ast.AST) -> None: # noqa: N802
273226
name = name[:-1].rstrip()
274227

275228
if self.current_symbol is not None and self.current_symbol.children is not None:
276-
r = range_from_node(variable)
229+
r = range_from_node(node)
277230
symbol = DocumentSymbol(name=name, kind=SymbolKind.VARIABLE, range=r, selection_range=r)
278231
self.current_symbol.children.append(symbol)

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/.gitignore

Whitespace-only changes.

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_document_symbols.test[symbols.robot-000-001-settings_section].out

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ result: !DocumentSymbol
66
children: []
77
deprecated: null
88
detail: null
9-
kind: 3
9+
kind: 2
1010
name: Settings
1111
range:
1212
end:
13-
character: 22
14-
line: 2
13+
character: 1
14+
line: 3
1515
start:
1616
character: 0
1717
line: 0
1818
selection_range:
1919
end:
20-
character: 22
21-
line: 2
20+
character: 1
21+
line: 3
2222
start:
2323
character: 0
2424
line: 0

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_document_symbols.test[symbols.robot-000-008-settings_section].out

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ result: !DocumentSymbol
66
children: []
77
deprecated: null
88
detail: null
9-
kind: 3
9+
kind: 2
1010
name: Settings
1111
range:
1212
end:
13-
character: 22
14-
line: 2
13+
character: 1
14+
line: 3
1515
start:
1616
character: 0
1717
line: 0
1818
selection_range:
1919
end:
20-
character: 22
21-
line: 2
20+
character: 1
21+
line: 3
2222
start:
2323
character: 0
2424
line: 0

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_document_symbols.test[symbols.robot-000-015-settings_section].out

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ result: !DocumentSymbol
66
children: []
77
deprecated: null
88
detail: null
9-
kind: 3
9+
kind: 2
1010
name: Settings
1111
range:
1212
end:
13-
character: 22
14-
line: 2
13+
character: 1
14+
line: 3
1515
start:
1616
character: 0
1717
line: 0
1818
selection_range:
1919
end:
20-
character: 22
21-
line: 2
20+
character: 1
21+
line: 3
2222
start:
2323
character: 0
2424
line: 0

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_document_symbols.test[symbols.robot-004-001-variables_section].out

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ result: !DocumentSymbol
66
children: []
77
deprecated: null
88
detail: null
9-
kind: 3
9+
kind: 2
1010
name: Variables
1111
range:
1212
end:
13-
character: 13
14-
line: 6
13+
character: 1
14+
line: 8
1515
start:
1616
character: 0
1717
line: 4
1818
selection_range:
1919
end:
20-
character: 13
21-
line: 6
20+
character: 1
21+
line: 8
2222
start:
2323
character: 0
2424
line: 4

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_document_symbols.test[symbols.robot-004-008-variables_section].out

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ result: !DocumentSymbol
66
children: []
77
deprecated: null
88
detail: null
9-
kind: 3
9+
kind: 2
1010
name: Variables
1111
range:
1212
end:
13-
character: 13
14-
line: 6
13+
character: 1
14+
line: 8
1515
start:
1616
character: 0
1717
line: 4
1818
selection_range:
1919
end:
20-
character: 13
21-
line: 6
20+
character: 1
21+
line: 8
2222
start:
2323
character: 0
2424
line: 4

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf41/test_document_symbols.test[symbols.robot-004-015-variables_section].out

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ result: !DocumentSymbol
66
children: []
77
deprecated: null
88
detail: null
9-
kind: 3
9+
kind: 2
1010
name: Variables
1111
range:
1212
end:
13-
character: 13
14-
line: 6
13+
character: 1
14+
line: 8
1515
start:
1616
character: 0
1717
line: 4
1818
selection_range:
1919
end:
20-
character: 13
21-
line: 6
20+
character: 1
21+
line: 8
2222
start:
2323
character: 0
2424
line: 4

0 commit comments

Comments
 (0)