18
18
Union ,
19
19
)
20
20
21
+ from robot .parsing .lexer .tokens import Token
22
+ from robot .utils .escaping import split_from_equals , unescape
23
+ from robot .variables .finders import NOT_FOUND , NumberFinder
24
+ from robot .variables .search import contains_variable , search_variable
21
25
from robotcode .core .lsp .types import Position
22
26
from robotcode .robot .utils import get_robot_version
23
-
24
- from ..utils .ast_utils import (
25
- Token ,
27
+ from robotcode .robot .utils .ast import (
26
28
iter_over_keyword_names_and_owners ,
27
29
range_from_token ,
28
30
strip_variable_token ,
29
31
tokenize_variables ,
30
32
whitespace_at_begin_of_token ,
31
33
whitespace_from_begin_of_token ,
32
34
)
35
+
33
36
from .entities import (
34
37
LibraryEntry ,
35
38
VariableDefinition ,
@@ -56,8 +59,6 @@ async def get_run_keyword_keyworddoc_and_token_from_position(
56
59
namespace : Namespace ,
57
60
position : Position ,
58
61
) -> Tuple [Optional [Tuple [Optional [KeywordDoc ], Token ]], List [Token ]]:
59
- from robot .utils .escaping import unescape
60
-
61
62
if keyword_doc is None or not keyword_doc .is_any_run_keyword ():
62
63
return None , argument_tokens
63
64
@@ -250,8 +251,6 @@ async def iter_expression_variables_from_token(
250
251
skip_commandline_variables : bool = False ,
251
252
return_not_found : bool = False ,
252
253
) -> AsyncIterator [Tuple [Token , VariableDefinition ]]:
253
- from robot .api .parsing import Token as RobotToken
254
-
255
254
variable_started = False
256
255
try :
257
256
for toknum , tokval , (_ , tokcol ), _ , _ in generate_tokens (StringIO (expression .value ).readline ):
@@ -264,7 +263,7 @@ async def iter_expression_variables_from_token(
264
263
skip_commandline_variables = skip_commandline_variables ,
265
264
ignore_error = True ,
266
265
)
267
- sub_token = RobotToken (
266
+ sub_token = Token (
268
267
expression .type ,
269
268
tokval ,
270
269
expression .lineno ,
@@ -291,12 +290,10 @@ async def iter_expression_variables_from_token(
291
290
292
291
@staticmethod
293
292
def remove_index_from_variable_token (token : Token ) -> Tuple [Token , Optional [Token ]]:
294
- from robot .parsing .lexer import Token as RobotToken
295
-
296
293
def escaped (i : int ) -> bool :
297
- return token .value [- i - 3 : - i - 2 ] == "\\ "
294
+ return bool ( token .value [- i - 3 : - i - 2 ] == "\\ " )
298
295
299
- if token .type != RobotToken .VARIABLE or not token .value .endswith ("]" ):
296
+ if token .type != Token .VARIABLE or not token .value .endswith ("]" ):
300
297
return (token , None )
301
298
302
299
braces = 1
@@ -322,9 +319,9 @@ def escaped(i: int) -> bool:
322
319
return (token , None )
323
320
324
321
value = token .value [: - index - 2 ]
325
- var = RobotToken (token .type , value , token .lineno , token .col_offset , token .error ) if len (value ) > 0 else None
326
- rest = RobotToken (
327
- RobotToken .ARGUMENT ,
322
+ var = Token (token .type , value , token .lineno , token .col_offset , token .error ) if len (value ) > 0 else None
323
+ rest = Token (
324
+ Token .ARGUMENT ,
328
325
token .value [- index - 2 :],
329
326
token .lineno ,
330
327
token .col_offset + len (value ),
@@ -342,10 +339,8 @@ def _tokenize_variables(
342
339
* ,
343
340
extra_types : Optional [Set [str ]] = None ,
344
341
) -> Iterator [Token ]:
345
- from robot .api .parsing import Token as RobotToken
346
-
347
342
for t in tokenize_variables (token , identifiers , ignore_errors , extra_types = extra_types ):
348
- if t .type == RobotToken .VARIABLE :
343
+ if t .type == Token .VARIABLE :
349
344
var , rest = cls .remove_index_from_variable_token (t )
350
345
if var is not None :
351
346
yield var
@@ -364,12 +359,7 @@ async def iter_variables_from_token(
364
359
skip_commandline_variables : bool = False ,
365
360
return_not_found : bool = False ,
366
361
) -> AsyncIterator [Tuple [Token , VariableDefinition ]]:
367
- from robot .api .parsing import Token as RobotToken
368
- from robot .variables .search import contains_variable , search_variable
369
-
370
362
def is_number (name : str ) -> bool :
371
- from robot .variables .finders import NOT_FOUND , NumberFinder
372
-
373
363
if name .startswith ("$" ):
374
364
finder = NumberFinder ()
375
365
return bool (finder .find (name ) != NOT_FOUND )
@@ -379,13 +369,13 @@ async def iter_token(
379
369
to : Token , ignore_errors : bool = False
380
370
) -> AsyncIterator [Union [Token , Tuple [Token , VariableDefinition ]]]:
381
371
for sub_token in cls ._tokenize_variables (to , ignore_errors = ignore_errors ):
382
- if sub_token .type == RobotToken .VARIABLE :
372
+ if sub_token .type == Token .VARIABLE :
383
373
base = sub_token .value [2 :- 1 ]
384
374
if base and not (base [0 ] == "{" and base [- 1 ] == "}" ):
385
375
yield sub_token
386
376
elif base :
387
377
async for v in cls .iter_expression_variables_from_token (
388
- RobotToken (
378
+ Token (
389
379
sub_token .type ,
390
380
base [1 :- 1 ],
391
381
sub_token .lineno ,
@@ -413,7 +403,7 @@ async def iter_token(
413
403
414
404
if contains_variable (base , "$@&%" ):
415
405
async for sub_token_or_var in iter_token (
416
- RobotToken (
406
+ Token (
417
407
to .type ,
418
408
base ,
419
409
sub_token .lineno ,
@@ -422,17 +412,17 @@ async def iter_token(
422
412
ignore_errors = ignore_errors ,
423
413
):
424
414
if isinstance (sub_token_or_var , Token ):
425
- if sub_token_or_var .type == RobotToken .VARIABLE :
415
+ if sub_token_or_var .type == Token .VARIABLE :
426
416
yield sub_token_or_var
427
417
else :
428
418
yield sub_token_or_var
429
419
430
- if token .type == RobotToken .VARIABLE and token .value .endswith ("=" ):
420
+ if token .type == Token .VARIABLE and token .value .endswith ("=" ):
431
421
match = search_variable (token .value , ignore_errors = True )
432
422
if not match .is_assign (allow_assign_mark = True ):
433
423
return
434
424
435
- token = RobotToken (
425
+ token = Token (
436
426
token .type ,
437
427
token .value [:- 1 ].strip (),
438
428
token .lineno ,
@@ -459,7 +449,7 @@ async def iter_token(
459
449
continue
460
450
461
451
if (
462
- sub_token .type == RobotToken .VARIABLE
452
+ sub_token .type == Token .VARIABLE
463
453
and sub_token .value [:1 ] in "$@&%"
464
454
and sub_token .value [1 :2 ] == "{"
465
455
and sub_token .value [- 1 :] == "}"
@@ -475,7 +465,7 @@ async def iter_token(
475
465
skip_commandline_variables = skip_commandline_variables ,
476
466
ignore_error = True ,
477
467
)
478
- sub_sub_token = RobotToken (sub_token .type , name , sub_token .lineno , sub_token .col_offset )
468
+ sub_sub_token = Token (sub_token .type , name , sub_token .lineno , sub_token .col_offset )
479
469
if var is not None :
480
470
yield strip_variable_token (sub_sub_token ), var
481
471
continue
@@ -529,8 +519,6 @@ def get_expression_statement_types(cls) -> Tuple[Type[Any]]:
529
519
530
520
@classmethod
531
521
def split_bdd_prefix (cls , namespace : Namespace , token : Token ) -> Tuple [Optional [Token ], Optional [Token ]]:
532
- from robot .parsing .lexer import Token as RobotToken
533
-
534
522
bdd_token = None
535
523
536
524
parts = token .value .split ()
@@ -543,15 +531,15 @@ def split_bdd_prefix(cls, namespace: Namespace, token: Token) -> Tuple[Optional[
543
531
namespace .languages .bdd_prefixes if namespace .languages is not None else DEFAULT_BDD_PREFIXES
544
532
):
545
533
bdd_len = len (prefix )
546
- bdd_token = RobotToken (
534
+ bdd_token = Token (
547
535
token .type ,
548
536
token .value [:bdd_len ],
549
537
token .lineno ,
550
538
token .col_offset ,
551
539
token .error ,
552
540
)
553
541
554
- token = RobotToken (
542
+ token = Token (
555
543
token .type ,
556
544
token .value [bdd_len + 1 :],
557
545
token .lineno ,
@@ -564,14 +552,12 @@ def split_bdd_prefix(cls, namespace: Namespace, token: Token) -> Tuple[Optional[
564
552
565
553
@classmethod
566
554
def strip_bdd_prefix (cls , namespace : Namespace , token : Token ) -> Token :
567
- from robot .parsing .lexer import Token as RobotToken
568
-
569
555
if get_robot_version () < (6 , 0 ):
570
556
bdd_match = cls .BDD_TOKEN_REGEX .match (token .value )
571
557
if bdd_match :
572
558
bdd_len = len (bdd_match .group (1 ))
573
559
574
- token = RobotToken (
560
+ token = Token (
575
561
token .type ,
576
562
token .value [bdd_len + 1 :],
577
563
token .lineno ,
@@ -590,7 +576,7 @@ def strip_bdd_prefix(cls, namespace: Namespace, token: Token) -> Token:
590
576
namespace .languages .bdd_prefixes if namespace .languages is not None else DEFAULT_BDD_PREFIXES
591
577
):
592
578
bdd_len = len (prefix )
593
- token = RobotToken (
579
+ token = Token (
594
580
token .type ,
595
581
token .value [bdd_len + 1 :],
596
582
token .lineno ,
@@ -637,9 +623,6 @@ def get_argument_info_at_position(
637
623
token_at_position : Token ,
638
624
position : Position ,
639
625
) -> Tuple [int , Optional [List [ArgumentInfo ]], Optional [Token ]]:
640
- from robot .parsing .lexer .tokens import Token as RobotToken
641
- from robot .utils .escaping import split_from_equals
642
-
643
626
argument_index = - 1
644
627
named_arg = False
645
628
@@ -656,35 +639,35 @@ def get_argument_info_at_position(
656
639
token_at_position_index = tokens .index (token_at_position )
657
640
658
641
if (
659
- token_at_position .type in [RobotToken .EOL , RobotToken .SEPARATOR ]
642
+ token_at_position .type in [Token .EOL , Token .SEPARATOR ]
660
643
and token_at_position_index > 2
661
- and tokens [token_at_position_index - 1 ].type == RobotToken .CONTINUATION
644
+ and tokens [token_at_position_index - 1 ].type == Token .CONTINUATION
662
645
and position .character < range_from_token (tokens [token_at_position_index - 1 ]).end .character + 2
663
646
):
664
647
return - 1 , None , None
665
648
666
649
token_at_position_index = tokens .index (token_at_position )
667
650
668
651
argument_token_index = token_at_position_index
669
- while argument_token_index >= 0 and tokens [argument_token_index ].type != RobotToken .ARGUMENT :
652
+ while argument_token_index >= 0 and tokens [argument_token_index ].type != Token .ARGUMENT :
670
653
argument_token_index -= 1
671
654
672
655
if (
673
- token_at_position .type == RobotToken .EOL
656
+ token_at_position .type == Token .EOL
674
657
and len (tokens ) > 1
675
- and tokens [argument_token_index - 1 ].type == RobotToken .CONTINUATION
658
+ and tokens [argument_token_index - 1 ].type == Token .CONTINUATION
676
659
):
677
660
argument_token_index -= 2
678
- while argument_token_index >= 0 and tokens [argument_token_index ].type != RobotToken .ARGUMENT :
661
+ while argument_token_index >= 0 and tokens [argument_token_index ].type != Token .ARGUMENT :
679
662
argument_token_index -= 1
680
663
681
- arguments = [a for a in tokens if a .type == RobotToken .ARGUMENT ]
664
+ arguments = [a for a in tokens if a .type == Token .ARGUMENT ]
682
665
683
666
argument_token : Optional [Token ] = None
684
667
685
668
if argument_token_index >= 0 :
686
669
argument_token = tokens [argument_token_index ]
687
- if argument_token is not None and argument_token .type == RobotToken .ARGUMENT :
670
+ if argument_token is not None and argument_token .type == Token .ARGUMENT :
688
671
argument_index = arguments .index (argument_token )
689
672
else :
690
673
argument_index = 0
@@ -705,19 +688,19 @@ def get_argument_info_at_position(
705
688
r .end .character = r .start .character + whitespace_at_begin_of_token (token_at_position ) - 3
706
689
if not position .is_in_range (r , False ):
707
690
argument_token_index += 2
708
- if argument_token_index < len (tokens ) and tokens [argument_token_index ].type == RobotToken .ARGUMENT :
691
+ if argument_token_index < len (tokens ) and tokens [argument_token_index ].type == Token .ARGUMENT :
709
692
argument_token = tokens [argument_token_index ]
710
693
711
694
if (
712
695
argument_index < 0
713
696
or argument_token is not None
714
- and argument_token .type == RobotToken .ARGUMENT
697
+ and argument_token .type == Token .ARGUMENT
715
698
and argument_token .value .startswith (("@{" , "&{" ))
716
699
and argument_token .value .endswith ("}" )
717
700
):
718
701
return - 1 , kw_arguments , argument_token
719
702
720
- if argument_token is not None and argument_token .type == RobotToken .ARGUMENT :
703
+ if argument_token is not None and argument_token .type == Token .ARGUMENT :
721
704
arg_name_or_value , arg_value = split_from_equals (argument_token .value )
722
705
if arg_value is not None :
723
706
old_argument_index = argument_index
0 commit comments