28
28
from ..diagnostics .imports_manager import ImportsManager
29
29
from ..diagnostics .namespace import DocumentType , Namespace
30
30
from ..utils .ast_utils import Token
31
+ from ..utils .version import get_robot_version
31
32
32
33
if TYPE_CHECKING :
33
34
from ..protocol import RobotLanguageServerProtocol
@@ -47,6 +48,23 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
47
48
self ._imports_managers : weakref .WeakKeyDictionary [WorkspaceFolder , ImportsManager ] = weakref .WeakKeyDictionary ()
48
49
self ._default_imports_manager : Optional [ImportsManager ] = None
49
50
51
+ async def get_languages (self , document : TextDocument ) -> Optional [List [str ]]:
52
+ if get_robot_version () < (5 , 1 ):
53
+ return None
54
+
55
+ folder = self .parent .workspace .get_workspace_folder (document .uri )
56
+ if folder is None :
57
+ return None
58
+
59
+ config = await self .parent .workspace .get_configuration (RobotConfig , folder .uri )
60
+
61
+ lang = config .languages
62
+
63
+ if isinstance (lang , List ) and len (lang ) == 0 :
64
+ lang = None
65
+
66
+ return lang
67
+
50
68
async def get_document_type (self , document : TextDocument ) -> DocumentType :
51
69
return await document .get_cache (self .__get_document_type )
52
70
@@ -95,21 +113,59 @@ async def get_general_tokens(self, document: TextDocument, data_only: bool = Fal
95
113
return await document .get_cache (self .__get_general_tokens_data_only )
96
114
return await document .get_cache (self .__get_general_tokens )
97
115
98
- async def __get_general_tokens_data_only (self , document : TextDocument ) -> List [Token ]:
116
+ def __internal_get_tokens (
117
+ self , source : Any , data_only : bool = False , tokenize_variables : bool = False , lang : Any = None
118
+ ) -> Any :
99
119
import robot .api
100
120
121
+ if get_robot_version () >= (5 , 1 ):
122
+ return robot .api .get_tokens (source , data_only = data_only , tokenize_variables = tokenize_variables , lang = lang )
123
+ else :
124
+ return robot .api .get_tokens (source , data_only = data_only , tokenize_variables = tokenize_variables )
125
+
126
+ def __internal_get_resource_tokens (
127
+ self ,
128
+ source : Any ,
129
+ data_only : bool = False ,
130
+ tokenize_variables : bool = False ,
131
+ lang : Any = None ,
132
+ ) -> Any :
133
+ import robot .api
134
+
135
+ if get_robot_version () >= (5 , 1 ):
136
+ return robot .api .get_resource_tokens (
137
+ source , data_only = data_only , tokenize_variables = tokenize_variables , lang = lang
138
+ )
139
+ else :
140
+ return robot .api .get_resource_tokens (source , data_only = data_only , tokenize_variables = tokenize_variables )
141
+
142
+ def __internal_get_init_tokens (
143
+ self , source : Any , data_only : bool = False , tokenize_variables : bool = False , lang : Any = None
144
+ ) -> Any :
145
+ import robot .api
146
+
147
+ if get_robot_version () >= (5 , 1 ):
148
+ return robot .api .get_init_tokens (
149
+ source , data_only = data_only , tokenize_variables = tokenize_variables , lang = lang
150
+ )
151
+ else :
152
+ return robot .api .get_init_tokens (source , data_only = data_only , tokenize_variables = tokenize_variables )
153
+
154
+ async def __get_general_tokens_data_only (self , document : TextDocument ) -> List [Token ]:
155
+ lang = await self .get_languages (document )
156
+
101
157
def get (text : str ) -> List [Token ]:
102
158
with io .StringIO (text ) as content :
103
- return [e for e in robot . api . get_tokens (content , True ) if check_canceled_sync ()]
159
+ return [e for e in self . __internal_get_tokens (content , True , lang = lang ) if check_canceled_sync ()]
104
160
105
161
return await self .__get_tokens_internal (document , get )
106
162
107
163
async def __get_general_tokens (self , document : TextDocument ) -> List [Token ]:
108
- import robot . api
164
+ lang = await self . get_languages ( document )
109
165
110
166
def get (text : str ) -> List [Token ]:
111
167
with io .StringIO (text ) as content :
112
- return [e for e in robot . api . get_tokens (content ) if check_canceled_sync ()]
168
+ return [e for e in self . __internal_get_tokens (content , lang = lang ) if check_canceled_sync ()]
113
169
114
170
return await self .__get_tokens_internal (document , get )
115
171
@@ -128,20 +184,22 @@ async def get_resource_tokens(self, document: TextDocument, data_only: bool = Fa
128
184
return await document .get_cache (self .__get_resource_tokens )
129
185
130
186
async def __get_resource_tokens_data_only (self , document : TextDocument ) -> List [Token ]:
131
- import robot . api
187
+ lang = await self . get_languages ( document )
132
188
133
189
def get (text : str ) -> List [Token ]:
134
190
with io .StringIO (text ) as content :
135
- return [e for e in robot .api .get_resource_tokens (content , True ) if check_canceled_sync ()]
191
+ return [
192
+ e for e in self .__internal_get_resource_tokens (content , True , lang = lang ) if check_canceled_sync ()
193
+ ]
136
194
137
195
return await self .__get_tokens_internal (document , get )
138
196
139
197
async def __get_resource_tokens (self , document : TextDocument ) -> List [Token ]:
140
- import robot . api
198
+ lang = await self . get_languages ( document )
141
199
142
200
def get (text : str ) -> List [Token ]:
143
201
with io .StringIO (text ) as content :
144
- return [e for e in robot . api . get_resource_tokens (content ) if check_canceled_sync ()]
202
+ return [e for e in self . __internal_get_resource_tokens (content , lang = lang ) if check_canceled_sync ()]
145
203
146
204
return await self .__get_tokens_internal (document , get )
147
205
@@ -151,20 +209,20 @@ async def get_init_tokens(self, document: TextDocument, data_only: bool = False)
151
209
return await document .get_cache (self .__get_init_tokens )
152
210
153
211
async def __get_init_tokens_data_only (self , document : TextDocument ) -> List [Token ]:
154
- import robot . api
212
+ lang = await self . get_languages ( document )
155
213
156
214
def get (text : str ) -> List [Token ]:
157
215
with io .StringIO (text ) as content :
158
- return [e for e in robot . api . get_init_tokens (content , True ) if check_canceled_sync ()]
216
+ return [e for e in self . __internal_get_init_tokens (content , True , lang = lang ) if check_canceled_sync ()]
159
217
160
218
return await self .__get_tokens_internal (document , get )
161
219
162
220
async def __get_init_tokens (self , document : TextDocument ) -> List [Token ]:
163
- import robot . api
221
+ lang = await self . get_languages ( document )
164
222
165
223
def get (text : str ) -> List [Token ]:
166
224
with io .StringIO (text ) as content :
167
- return [e for e in robot . api . get_init_tokens (content ) if check_canceled_sync ()]
225
+ return [e for e in self . __internal_get_init_tokens (content , lang = lang ) if check_canceled_sync ()]
168
226
169
227
return await self .__get_tokens_internal (document , get )
170
228
@@ -184,7 +242,7 @@ def __get_model(self, document: TextDocument, tokens: Iterable[Any], document_ty
184
242
from robot .parsing .lexer import Token
185
243
from robot .parsing .parser .parser import _get_model
186
244
187
- def get_tokens (_source : str , _data_only : bool = False ) -> Generator [Token , None , None ]:
245
+ def get_tokens (source : str , data_only : bool = False , lang : Any = None ) -> Generator [Token , None , None ]:
188
246
for t in tokens :
189
247
check_canceled_sync ()
190
248
0 commit comments