25
25
from robotcode .language_server .common .text_document import TextDocument
26
26
from robotcode .language_server .robotframework .diagnostics .errors import DIAGNOSTICS_SOURCE_NAME , Error
27
27
from robotcode .language_server .robotframework .utils .ast_utils import (
28
+ FirstAndLastRealStatementFinder ,
28
29
Token ,
29
30
get_node_at_position ,
30
31
get_nodes_at_position ,
39
40
if TYPE_CHECKING :
40
41
from robotcode .language_server .robotframework .protocol import RobotLanguageServerProtocol # pragma: no cover
41
42
43
+ QUICK_FIX_OTHER = "other"
42
44
43
45
KEYWORD_WITH_ARGS_TEMPLATE = Template (
44
46
"""\
60
62
)
61
63
62
64
63
- class LastRealStatementFinder (Visitor ):
64
- def __init__ (self ) -> None :
65
- self .statement : Optional [ast .AST ] = None
66
-
67
- @classmethod
68
- def find_from (cls , model : ast .AST ) -> Optional [ast .AST ]:
69
- finder = cls ()
70
- finder .visit (model )
71
- return finder .statement
72
-
73
- def visit_Statement (self , statement : ast .AST ) -> None : # noqa: N802
74
- from robot .parsing .model .statements import EmptyLine
75
-
76
- if not isinstance (statement , EmptyLine ):
77
- self .statement = statement
78
-
79
-
80
65
class FindSectionsVisitor (Visitor ):
81
66
def __init__ (self ) -> None :
82
67
self .keyword_sections : List [ast .AST ] = []
@@ -111,7 +96,7 @@ def find_keyword_sections(node: ast.AST) -> Optional[List[ast.AST]]:
111
96
return visitor .keyword_sections if visitor .keyword_sections else None
112
97
113
98
114
- class RobotCodeActionFixesProtocolPart (RobotLanguageServerProtocolPart , ModelHelperMixin ):
99
+ class RobotCodeActionQuickFixesProtocolPart (RobotLanguageServerProtocolPart , ModelHelperMixin ):
115
100
_logger = LoggingDescriptor ()
116
101
117
102
def __init__ (self , parent : RobotLanguageServerProtocol ) -> None :
@@ -122,7 +107,7 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
122
107
self .parent .commands .register_all (self )
123
108
124
109
@language_id ("robotframework" )
125
- @code_action_kinds ([CodeActionKind .QUICK_FIX , "other" ])
110
+ @code_action_kinds ([CodeActionKind .QUICK_FIX , QUICK_FIX_OTHER ])
126
111
async def collect (
127
112
self , sender : Any , document : TextDocument , range : Range , context : CodeActionContext
128
113
) -> Optional [List [Union [Command , CodeAction ]]]:
@@ -283,7 +268,7 @@ async def code_action_assign_result_to_variable(
283
268
range .start .line == range .end .line
284
269
and range .start .character <= range .end .character
285
270
and (
286
- (context .only and "other" in context .only )
271
+ (context .only and QUICK_FIX_OTHER in context .only )
287
272
or context .trigger_kind in [CodeActionTriggerKind .INVOKED , CodeActionTriggerKind .AUTOMATIC ]
288
273
)
289
274
):
@@ -305,7 +290,7 @@ async def code_action_assign_result_to_variable(
305
290
return [
306
291
CodeAction (
307
292
"Assign result to variable" ,
308
- kind = "other" ,
293
+ kind = QUICK_FIX_OTHER ,
309
294
command = Command (
310
295
self .parent .commands .get_command_name (self .assign_result_to_variable_command ),
311
296
self .parent .commands .get_command_name (self .assign_result_to_variable_command ),
@@ -475,9 +460,8 @@ async def code_action_disable_robotcode_diagnostics_for_line(
475
460
or context .trigger_kind in [CodeActionTriggerKind .INVOKED , CodeActionTriggerKind .AUTOMATIC ]
476
461
)
477
462
):
478
- diagnostics = next ((d for d in context .diagnostics if d .source and d .source .startswith ("robotcode." )), None )
479
-
480
- if diagnostics is not None :
463
+ all_diagnostics = [d for d in context .diagnostics if d .source and d .source .startswith ("robotcode." )]
464
+ if all_diagnostics :
481
465
return [
482
466
CodeAction (
483
467
f"Disable '{ diagnostics .code } ' for this line" ,
@@ -489,6 +473,7 @@ async def code_action_disable_robotcode_diagnostics_for_line(
489
473
),
490
474
diagnostics = [diagnostics ],
491
475
)
476
+ for diagnostics in all_diagnostics
492
477
]
493
478
494
479
return None
@@ -594,7 +579,7 @@ async def create_suite_variable_command(self, document_uri: DocumentUri, range:
594
579
if finder .variable_sections :
595
580
section = finder .variable_sections [- 1 ]
596
581
597
- last_stmt = LastRealStatementFinder .find_from (section )
582
+ _ , last_stmt = FirstAndLastRealStatementFinder .find_from (section )
598
583
end_lineno = last_stmt .end_lineno if last_stmt else section .end_lineno
599
584
if end_lineno is None :
600
585
return
@@ -607,7 +592,7 @@ async def create_suite_variable_command(self, document_uri: DocumentUri, range:
607
592
insert_range_suffix = "\n \n "
608
593
section = finder .setting_sections [- 1 ]
609
594
610
- last_stmt = LastRealStatementFinder .find_from (section )
595
+ _ , last_stmt = FirstAndLastRealStatementFinder .find_from (section )
611
596
end_lineno = last_stmt .end_lineno if last_stmt else section .end_lineno
612
597
if end_lineno is None :
613
598
return
0 commit comments