6
6
List ,
7
7
Optional ,
8
8
Tuple ,
9
- Type ,
10
9
TypeVar ,
11
10
Union ,
12
11
cast ,
36
35
)
37
36
from robotcode .robot .diagnostics .library_doc import KeywordDoc
38
37
from robotcode .robot .diagnostics .model_helper import ModelHelper
39
- from robotcode .robot .utils .ast import (
40
- get_nodes_at_position ,
41
- )
42
38
43
39
from ...common .parts .rename import CantRenameError
44
40
from .protocol_part import RobotLanguageServerProtocolPart
@@ -62,90 +58,36 @@ def __init__(self, parent: "RobotLanguageServerProtocol") -> None:
62
58
parent .rename .collect .add (self .collect )
63
59
parent .rename .collect_prepare .add (self .collect_prepare )
64
60
65
- def _find_method (self , cls : Type [Any ], prefix : str ) -> Optional [_T ]:
66
- if cls is ast .AST :
67
- return None
68
- method_name = prefix + "_" + cls .__name__
69
- if hasattr (self , method_name ):
70
- method = getattr (self , method_name )
71
- if callable (method ):
72
- return cast (_T , method )
73
- for base in cls .__bases__ :
74
- method = self ._find_method (base , prefix )
75
- if method :
76
- return cast (_T , method )
77
- return None
78
-
79
61
@language_id ("robotframework" )
80
62
@_logger .call
81
63
def collect (
82
- self ,
83
- sender : Any ,
84
- document : TextDocument ,
85
- position : Position ,
86
- new_name : str ,
64
+ self , sender : Any , document : TextDocument , position : Position , new_name : str
87
65
) -> Optional [WorkspaceEdit ]:
88
- result_nodes = get_nodes_at_position (
89
- self .parent .documents_cache .get_model (document ),
90
- position ,
91
- include_end = True ,
92
- )
93
-
94
- if not result_nodes :
95
- return None
96
-
97
- result_node = result_nodes [- 1 ]
98
-
99
- result = self ._rename_variable (result_nodes , document , position , new_name )
66
+ result = self ._rename_variable (document , position , new_name )
100
67
if result :
101
68
return result
102
69
103
- result = self ._rename_keyword (result_nodes , document , position , new_name )
70
+ result = self ._rename_keyword (document , position , new_name )
104
71
if result :
105
72
return result
106
73
107
- method : Optional [_RenameMethod ] = self ._find_method (type (result_node ), "rename" )
108
- if method is not None :
109
- result = method (result_node , document , position , new_name )
110
- if result is not None :
111
- return result
112
-
113
74
return None
114
75
115
76
@language_id ("robotframework" )
116
77
@_logger .call
117
78
def collect_prepare (self , sender : Any , document : TextDocument , position : Position ) -> Optional [PrepareRenameResult ]:
118
- result_nodes = get_nodes_at_position (
119
- self .parent .documents_cache .get_model (document ),
120
- position ,
121
- include_end = True ,
122
- )
123
-
124
- if not result_nodes :
125
- return None
126
-
127
- result_node = result_nodes [- 1 ]
128
-
129
- result = self ._prepare_rename_variable (result_nodes , document , position )
79
+ result = self ._prepare_rename_variable (document , position )
130
80
if result :
131
81
return result
132
82
133
- result = self ._prepare_rename_keyword (result_nodes , document , position )
83
+ result = self ._prepare_rename_keyword (document , position )
134
84
if result :
135
85
return result
136
86
137
- method : Optional [_PrepareRenameMethod ] = self ._find_method (type (result_node ), "prepare_rename" )
138
- if method is not None :
139
- result = method (result_node , document , position )
140
- if result is not None :
141
- return result
142
-
143
87
return None
144
88
145
- def _prepare_rename_variable (
146
- self , nodes : List [ast .AST ], document : TextDocument , position : Position
147
- ) -> Optional [PrepareRenameResult ]:
148
- result = self ._find_variable_definition_on_pos (nodes , document , position )
89
+ def _prepare_rename_variable (self , document : TextDocument , position : Position ) -> Optional [PrepareRenameResult ]:
90
+ result = self ._find_variable_definition_on_pos (document , position )
149
91
if result is not None :
150
92
var , found_range = result
151
93
@@ -172,20 +114,14 @@ def _prepare_rename_variable(
172
114
173
115
return None
174
116
175
- def _rename_variable (
176
- self ,
177
- nodes : List [ast .AST ],
178
- document : TextDocument ,
179
- position : Position ,
180
- new_name : str ,
181
- ) -> Optional [WorkspaceEdit ]:
117
+ def _rename_variable (self , document : TextDocument , position : Position , new_name : str ) -> Optional [WorkspaceEdit ]:
182
118
if " " in new_name or "\t " in new_name :
183
119
raise CantRenameError (
184
120
"Variable names cannot contain more then one spaces or tabs. "
185
121
"Please use only one space or underscores instead." ,
186
122
)
187
123
188
- result = self ._find_variable_definition_on_pos (nodes , document , position )
124
+ result = self ._find_variable_definition_on_pos (document , position )
189
125
190
126
if result is not None :
191
127
var , _ = result
@@ -218,7 +154,7 @@ def _rename_variable(
218
154
return None
219
155
220
156
def _find_variable_definition_on_pos (
221
- self , nodes : List [ ast . AST ], document : TextDocument , position : Position
157
+ self , document : TextDocument , position : Position
222
158
) -> Optional [Tuple [VariableDefinition , Range ]]:
223
159
namespace = self .parent .documents_cache .get_namespace (document )
224
160
@@ -243,10 +179,8 @@ def _find_variable_definition_on_pos(
243
179
return variable , found_range
244
180
return None
245
181
246
- def _prepare_rename_keyword (
247
- self , nodes : List [ast .AST ], document : TextDocument , position : Position
248
- ) -> Optional [PrepareRenameResult ]:
249
- result = self ._find_keyword_definition_on_pos (nodes , document , position )
182
+ def _prepare_rename_keyword (self , document : TextDocument , position : Position ) -> Optional [PrepareRenameResult ]:
183
+ result = self ._find_keyword_definition_on_pos (document , position )
250
184
if result is not None :
251
185
kw_doc , found_range = result
252
186
@@ -263,20 +197,14 @@ def _prepare_rename_keyword(
263
197
264
198
return None
265
199
266
- def _rename_keyword (
267
- self ,
268
- nodes : List [ast .AST ],
269
- document : TextDocument ,
270
- position : Position ,
271
- new_name : str ,
272
- ) -> Optional [WorkspaceEdit ]:
200
+ def _rename_keyword (self , document : TextDocument , position : Position , new_name : str ) -> Optional [WorkspaceEdit ]:
273
201
if " " in new_name or "\t " in new_name :
274
202
raise CantRenameError (
275
203
"Keyword names cannot contain more then one spaces or tabs. "
276
204
"Please use only one space or underscores instead." ,
277
205
)
278
206
279
- result = self ._find_keyword_definition_on_pos (nodes , document , position )
207
+ result = self ._find_keyword_definition_on_pos (document , position )
280
208
if result is not None :
281
209
kw_doc , _ = result
282
210
@@ -303,7 +231,7 @@ def _rename_keyword(
303
231
return None
304
232
305
233
def _find_keyword_definition_on_pos (
306
- self , nodes : List [ ast . AST ], document : TextDocument , position : Position
234
+ self , document : TextDocument , position : Position
307
235
) -> Optional [Tuple [KeywordDoc , Range ]]:
308
236
namespace = self .parent .documents_cache .get_namespace (document )
309
237
0 commit comments