Skip to content

Commit c9aaa60

Browse files
committed
refactor(langserver): remove AsyncVisitor code
1 parent 1ff1e44 commit c9aaa60

File tree

27 files changed

+585
-79
lines changed

27 files changed

+585
-79
lines changed

packages/core/src/robotcode/core/utils/caching.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections import defaultdict
2-
from threading import Lock
2+
from threading import RLock
33
from typing import Any, Callable, Dict, List, Optional, Tuple, TypeVar, cast
44

55
_T = TypeVar("_T")
@@ -15,7 +15,7 @@ class CacheEntry:
1515
def __init__(self) -> None:
1616
self.data: Any = None
1717
self.has_data: bool = False
18-
self.lock = Lock()
18+
self.lock = RLock()
1919

2020

2121
class SimpleLRUCache:
@@ -26,7 +26,7 @@ def __init__(self, max_items: Optional[int] = 128) -> None:
2626
self._order: Optional[List[Tuple[Any, ...]]] = None
2727
if self.max_items:
2828
self._order = []
29-
self._lock = Lock()
29+
self._lock = RLock()
3030

3131
def has(self, *args: Any, **kwargs: Any) -> bool:
3232
key = self._make_key(*args, **kwargs)

packages/jsonrpc2/src/robotcode/jsonrpc2/protocol.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -555,17 +555,18 @@ def send_request(
555555
params: Optional[Any] = None,
556556
return_type_or_converter: Optional[Type[_TResult]] = None,
557557
) -> concurrent.futures.Future[_TResult]:
558+
result: concurrent.futures.Future[_TResult] = concurrent.futures.Future()
559+
558560
with self._sended_request_lock:
559-
result: concurrent.futures.Future[_TResult] = concurrent.futures.Future()
560561
self._sended_request_count += 1
561562
id = self._sended_request_count
562563

563564
self._sended_request[id] = SendedRequestEntry(result, return_type_or_converter)
564565

565-
request = JsonRPCRequest(id=id, method=method, params=params)
566-
self.send_message(request)
566+
request = JsonRPCRequest(id=id, method=method, params=params)
567+
self.send_message(request)
567568

568-
return result
569+
return result
569570

570571
def send_request_async(
571572
self,

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from ..diagnostics.library_doc import KeywordDoc
1414
from ..diagnostics.model_helper import ModelHelperMixin
1515
from ..utils.ast_utils import range_from_token
16-
from ..utils.async_ast import AsyncVisitor
16+
from ..utils.async_ast import Visitor
1717
from .protocol_part import RobotLanguageServerProtocolPart
1818

1919
if TYPE_CHECKING:
@@ -22,34 +22,34 @@
2222
)
2323

2424

25-
class _Visitor(AsyncVisitor):
25+
class _Visitor(Visitor):
2626
def __init__(self, parent: RobotCodeLensProtocolPart, document: TextDocument) -> None:
2727
super().__init__()
2828
self.parent = parent
2929
self.document = document
3030

3131
self.result: List[CodeLens] = []
3232

33-
async def visit(self, node: ast.AST) -> None:
34-
await super().visit(node)
33+
def visit(self, node: ast.AST) -> None:
34+
super().visit(node)
3535

3636
@classmethod
37-
async def find_from(
37+
def find_from(
3838
cls, model: ast.AST, parent: RobotCodeLensProtocolPart, document: TextDocument
3939
) -> Optional[List[CodeLens]]:
4040
finder = cls(parent, document)
4141

42-
await finder.visit(model)
42+
finder.visit(model)
4343

4444
return finder.result if finder.result else None
4545

46-
async def visit_Section(self, node: ast.AST) -> None: # noqa: N802
46+
def visit_Section(self, node: ast.AST) -> None: # noqa: N802
4747
from robot.parsing.model.blocks import KeywordSection
4848

4949
if isinstance(node, KeywordSection):
50-
await self.generic_visit(node)
50+
self.generic_visit(node)
5151

52-
async def visit_KeywordName(self, node: ast.AST) -> None: # noqa: N802
52+
def visit_KeywordName(self, node: ast.AST) -> None: # noqa: N802
5353
from robot.parsing.lexer.tokens import Token as RobotToken
5454
from robot.parsing.model.statements import KeywordName
5555

@@ -94,7 +94,7 @@ async def collect(self, sender: Any, document: TextDocument) -> Optional[List[Co
9494
if not (await self.parent.workspace.get_configuration_async(AnalysisConfig, document.uri)).references_code_lens:
9595
return None
9696

97-
return await _Visitor.find_from(self.parent.documents_cache.get_model(document), self, document)
97+
return _Visitor.find_from(self.parent.documents_cache.get_model(document), self, document)
9898

9999
@language_id("robotframework")
100100
async def resolve(self, sender: Any, code_lens: CodeLens) -> Optional[CodeLens]:

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

+18-20
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,38 @@
1010
from ...common.decorators import language_id
1111
from ...common.text_document import TextDocument
1212
from ..utils.ast_utils import Token, range_from_node, range_from_token, tokenize_variables
13-
from ..utils.async_ast import AsyncVisitor
13+
from ..utils.async_ast import Visitor
1414
from .protocol_part import RobotLanguageServerProtocolPart
1515

1616
if TYPE_CHECKING:
1717
from ..protocol import RobotLanguageServerProtocol
1818

1919

20-
class _Visitor(AsyncVisitor):
20+
class _Visitor(Visitor):
2121
def __init__(self, parent: RobotDocumentSymbolsProtocolPart) -> None:
2222
super().__init__()
2323
self.parent = parent
2424

2525
self.result: List[DocumentSymbol] = []
2626
self.current_symbol: Optional[DocumentSymbol] = None
2727

28-
async def generic_visit_current_symbol(self, node: ast.AST, symbol: DocumentSymbol) -> None:
28+
def generic_visit_current_symbol(self, node: ast.AST, symbol: DocumentSymbol) -> None:
2929
old = self.current_symbol
3030
self.current_symbol = symbol
3131
try:
32-
await self.generic_visit(node)
32+
self.generic_visit(node)
3333
finally:
3434
self.current_symbol = old
3535

3636
@classmethod
37-
async def find_from(
38-
cls, model: ast.AST, parent: RobotDocumentSymbolsProtocolPart
39-
) -> Optional[List[DocumentSymbol]]:
37+
def find_from(cls, model: ast.AST, parent: RobotDocumentSymbolsProtocolPart) -> Optional[List[DocumentSymbol]]:
4038
finder = cls(parent)
4139

42-
await finder.visit(model)
40+
finder.visit(model)
4341

4442
return finder.result if finder.result else None
4543

46-
async def visit_Section(self, node: ast.AST) -> None: # noqa: N802
44+
def visit_Section(self, node: ast.AST) -> None: # noqa: N802
4745
from robot.parsing.model.blocks import Section
4846
from robot.parsing.model.statements import SectionHeader
4947

@@ -66,9 +64,9 @@ async def visit_Section(self, node: ast.AST) -> None: # noqa: N802
6664

6765
self.result.append(symbol)
6866

69-
await self.generic_visit_current_symbol(node, symbol)
67+
self.generic_visit_current_symbol(node, symbol)
7068

71-
async def visit_TestCase(self, node: ast.AST) -> None: # noqa: N802
69+
def visit_TestCase(self, node: ast.AST) -> None: # noqa: N802
7270
from robot.parsing.model.blocks import TestCase
7371

7472
testcase = cast(TestCase, node)
@@ -80,9 +78,9 @@ async def visit_TestCase(self, node: ast.AST) -> None: # noqa: N802
8078
symbol = DocumentSymbol(name=testcase.name, kind=SymbolKind.METHOD, range=r, selection_range=r, children=[])
8179
self.current_symbol.children.append(symbol)
8280

83-
await self.generic_visit_current_symbol(node, symbol)
81+
self.generic_visit_current_symbol(node, symbol)
8482

85-
async def visit_Keyword(self, node: ast.AST) -> None: # noqa: N802
83+
def visit_Keyword(self, node: ast.AST) -> None: # noqa: N802
8684
from robot.parsing.model.blocks import Keyword
8785

8886
keyword = cast(Keyword, node)
@@ -96,9 +94,9 @@ async def visit_Keyword(self, node: ast.AST) -> None: # noqa: N802
9694
)
9795
self.current_symbol.children.append(symbol)
9896

99-
await self.generic_visit_current_symbol(node, symbol)
97+
self.generic_visit_current_symbol(node, symbol)
10098

101-
async def visit_Arguments(self, node: ast.AST) -> None: # noqa: N802
99+
def visit_Arguments(self, node: ast.AST) -> None: # noqa: N802
102100
from robot.parsing.lexer.tokens import Token as RobotToken
103101
from robot.parsing.model.statements import Arguments
104102

@@ -134,7 +132,7 @@ def get_variable_token(self, token: Token) -> Optional[Token]:
134132
None,
135133
)
136134

137-
async def visit_KeywordCall(self, node: ast.AST) -> None: # noqa: N802
135+
def visit_KeywordCall(self, node: ast.AST) -> None: # noqa: N802
138136
from robot.errors import VariableError
139137
from robot.parsing.lexer.tokens import Token as RobotToken
140138
from robot.parsing.model.statements import KeywordCall
@@ -162,7 +160,7 @@ async def visit_KeywordCall(self, node: ast.AST) -> None: # noqa: N802
162160
except VariableError:
163161
pass
164162

165-
async def visit_ForHeader(self, node: ast.AST) -> None: # noqa: N802
163+
def visit_ForHeader(self, node: ast.AST) -> None: # noqa: N802
166164
from robot.parsing.lexer.tokens import Token as RobotToken
167165
from robot.parsing.model.statements import ForHeader
168166

@@ -180,7 +178,7 @@ async def visit_ForHeader(self, node: ast.AST) -> None: # noqa: N802
180178
if symbol.name not in map(lambda v: v.name, self.current_symbol.children):
181179
self.current_symbol.children.append(symbol)
182180

183-
async def visit_KeywordName(self, node: ast.AST) -> None: # noqa: N802
181+
def visit_KeywordName(self, node: ast.AST) -> None: # noqa: N802
184182
from robot.parsing.lexer.tokens import Token as RobotToken
185183
from robot.parsing.model.statements import KeywordName
186184

@@ -205,7 +203,7 @@ async def visit_KeywordName(self, node: ast.AST) -> None: # noqa: N802
205203
if symbol.name not in map(lambda v: v.name, self.current_symbol.children):
206204
self.current_symbol.children.append(symbol)
207205

208-
async def visit_Variable(self, node: ast.AST) -> None: # noqa: N802
206+
def visit_Variable(self, node: ast.AST) -> None: # noqa: N802
209207
from robot.api.parsing import Token as RobotToken
210208
from robot.parsing.model.statements import Variable
211209
from robot.variables import search_variable
@@ -242,4 +240,4 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
242240
async def collect(
243241
self, sender: Any, document: TextDocument
244242
) -> Optional[Union[List[DocumentSymbol], List[SymbolInformation], None]]:
245-
return await _Visitor.find_from(self.parent.documents_cache.get_model(document), self)
243+
return _Visitor.find_from(self.parent.documents_cache.get_model(document), self)

packages/language_server/src/robotcode/language_server/robotframework/utils/async_ast.py

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import ast
22
from abc import ABC
33
from collections import defaultdict
4+
from typing import Any, AsyncIterator, Callable, Dict, Iterator, Optional, Type, Union
45

56
from robot.parsing.model.statements import Statement
6-
from typing_extensions import Any, AsyncIterator, Callable, Dict, Iterator, Optional, Type, Union
77

8-
__all__ = ["iter_fields", "iter_child_nodes", "AsyncVisitor"]
8+
__all__ = ["iter_fields", "iter_child_nodes"]
99

1010

1111
def _patch_robot() -> None:
@@ -92,23 +92,6 @@ def _find_visitor(cls, node_cls: Type[Any]) -> Optional[Callable[..., Any]]:
9292
return result # type: ignore[return-value]
9393

9494

95-
class AsyncVisitor(VisitorFinder):
96-
async def visit(self, node: ast.AST) -> None:
97-
visitor = self._find_visitor(type(node)) or self.__class__.generic_visit
98-
await visitor(self, node)
99-
100-
async def generic_visit(self, node: ast.AST) -> None:
101-
for value in iter_field_values(node):
102-
if value is None:
103-
continue
104-
if isinstance(value, ast.AST):
105-
await self.visit(value)
106-
elif isinstance(value, list):
107-
for item in value:
108-
if isinstance(item, ast.AST):
109-
await self.visit(item)
110-
111-
11295
class Visitor(VisitorFinder):
11396
def visit(self, node: ast.AST) -> None:
11497
visitor = self._find_visitor(type(node)) or self.__class__.generic_visit

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import ast
22
from abc import ABC
33
from collections import defaultdict
4-
5-
from typing_extensions import Any, AsyncIterator, Callable, Dict, Iterator, Optional, Type, Union
4+
from typing import Any, AsyncIterator, Callable, Dict, Iterator, Optional, Type, Union
65

76
from robot.parsing.model.statements import Statement
87

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf70/test_goto_definition.test_definition[goto.robot-007-018-Variables_Import].out

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,27 @@ data: !GeneratedTestData
22
character: 18
33
line: 7
44
name: Variables Import
5-
result: null
5+
result:
6+
- !LocationLink
7+
origin_selection_range:
8+
end:
9+
character: 49
10+
line: 7
11+
start:
12+
character: 18
13+
line: 7
14+
target_range:
15+
end:
16+
character: 0
17+
line: 0
18+
start:
19+
character: 0
20+
line: 0
21+
target_selection_range:
22+
end:
23+
character: 0
24+
line: 0
25+
start:
26+
character: 0
27+
line: 0
28+
target_uri: myvariables.py

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf70/test_goto_definition.test_definition[goto.robot-007-033-Variables_Import].out

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,27 @@ data: !GeneratedTestData
22
character: 33
33
line: 7
44
name: Variables Import
5-
result: null
5+
result:
6+
- !LocationLink
7+
origin_selection_range:
8+
end:
9+
character: 49
10+
line: 7
11+
start:
12+
character: 18
13+
line: 7
14+
target_range:
15+
end:
16+
character: 0
17+
line: 0
18+
start:
19+
character: 0
20+
line: 0
21+
target_selection_range:
22+
end:
23+
character: 0
24+
line: 0
25+
start:
26+
character: 0
27+
line: 0
28+
target_uri: myvariables.py

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf70/test_goto_definition.test_definition[goto.robot-007-048-Variables_Import].out

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,27 @@ data: !GeneratedTestData
22
character: 48
33
line: 7
44
name: Variables Import
5-
result: null
5+
result:
6+
- !LocationLink
7+
origin_selection_range:
8+
end:
9+
character: 49
10+
line: 7
11+
start:
12+
character: 18
13+
line: 7
14+
target_range:
15+
end:
16+
character: 0
17+
line: 0
18+
start:
19+
character: 0
20+
line: 0
21+
target_selection_range:
22+
end:
23+
character: 0
24+
line: 0
25+
start:
26+
character: 0
27+
line: 0
28+
target_uri: myvariables.py

tests/robotcode/language_server/robotframework/parts/_regtest_outputs/rf70/test_goto_definition.test_definition[goto.robot-075-013-Imported_Variable].out

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,27 @@ data: !GeneratedTestData
22
character: 13
33
line: 75
44
name: Imported Variable
5-
result: null
5+
result:
6+
- !LocationLink
7+
origin_selection_range:
8+
end:
9+
character: 27
10+
line: 75
11+
start:
12+
character: 13
13+
line: 75
14+
target_range:
15+
end:
16+
character: 0
17+
line: 0
18+
start:
19+
character: 0
20+
line: 0
21+
target_selection_range:
22+
end:
23+
character: 0
24+
line: 0
25+
start:
26+
character: 0
27+
line: 0
28+
target_uri: myvariables.py

0 commit comments

Comments
 (0)