2525from robotcode .language_server .common .text_document import TextDocument
2626from robotcode .language_server .robotframework .diagnostics .errors import DIAGNOSTICS_SOURCE_NAME , Error
2727from robotcode .language_server .robotframework .utils .ast_utils import (
28+ FirstAndLastRealStatementFinder ,
2829 Token ,
2930 get_node_at_position ,
3031 get_nodes_at_position ,
3940if TYPE_CHECKING :
4041 from robotcode .language_server .robotframework .protocol import RobotLanguageServerProtocol # pragma: no cover
4142
43+ QUICK_FIX_OTHER = "other"
4244
4345KEYWORD_WITH_ARGS_TEMPLATE = Template (
4446 """\
6062)
6163
6264
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-
8065class FindSectionsVisitor (Visitor ):
8166 def __init__ (self ) -> None :
8267 self .keyword_sections : List [ast .AST ] = []
@@ -111,7 +96,7 @@ def find_keyword_sections(node: ast.AST) -> Optional[List[ast.AST]]:
11196 return visitor .keyword_sections if visitor .keyword_sections else None
11297
11398
114- class RobotCodeActionFixesProtocolPart (RobotLanguageServerProtocolPart , ModelHelperMixin ):
99+ class RobotCodeActionQuickFixesProtocolPart (RobotLanguageServerProtocolPart , ModelHelperMixin ):
115100 _logger = LoggingDescriptor ()
116101
117102 def __init__ (self , parent : RobotLanguageServerProtocol ) -> None :
@@ -122,7 +107,7 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
122107 self .parent .commands .register_all (self )
123108
124109 @language_id ("robotframework" )
125- @code_action_kinds ([CodeActionKind .QUICK_FIX , "other" ])
110+ @code_action_kinds ([CodeActionKind .QUICK_FIX , QUICK_FIX_OTHER ])
126111 async def collect (
127112 self , sender : Any , document : TextDocument , range : Range , context : CodeActionContext
128113 ) -> Optional [List [Union [Command , CodeAction ]]]:
@@ -283,7 +268,7 @@ async def code_action_assign_result_to_variable(
283268 range .start .line == range .end .line
284269 and range .start .character <= range .end .character
285270 and (
286- (context .only and "other" in context .only )
271+ (context .only and QUICK_FIX_OTHER in context .only )
287272 or context .trigger_kind in [CodeActionTriggerKind .INVOKED , CodeActionTriggerKind .AUTOMATIC ]
288273 )
289274 ):
@@ -305,7 +290,7 @@ async def code_action_assign_result_to_variable(
305290 return [
306291 CodeAction (
307292 "Assign result to variable" ,
308- kind = "other" ,
293+ kind = QUICK_FIX_OTHER ,
309294 command = Command (
310295 self .parent .commands .get_command_name (self .assign_result_to_variable_command ),
311296 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(
475460 or context .trigger_kind in [CodeActionTriggerKind .INVOKED , CodeActionTriggerKind .AUTOMATIC ]
476461 )
477462 ):
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 :
481465 return [
482466 CodeAction (
483467 f"Disable '{ diagnostics .code } ' for this line" ,
@@ -489,6 +473,7 @@ async def code_action_disable_robotcode_diagnostics_for_line(
489473 ),
490474 diagnostics = [diagnostics ],
491475 )
476+ for diagnostics in all_diagnostics
492477 ]
493478
494479 return None
@@ -594,7 +579,7 @@ async def create_suite_variable_command(self, document_uri: DocumentUri, range:
594579 if finder .variable_sections :
595580 section = finder .variable_sections [- 1 ]
596581
597- last_stmt = LastRealStatementFinder .find_from (section )
582+ _ , last_stmt = FirstAndLastRealStatementFinder .find_from (section )
598583 end_lineno = last_stmt .end_lineno if last_stmt else section .end_lineno
599584 if end_lineno is None :
600585 return
@@ -607,7 +592,7 @@ async def create_suite_variable_command(self, document_uri: DocumentUri, range:
607592 insert_range_suffix = "\n \n "
608593 section = finder .setting_sections [- 1 ]
609594
610- last_stmt = LastRealStatementFinder .find_from (section )
595+ _ , last_stmt = FirstAndLastRealStatementFinder .find_from (section )
611596 end_lineno = last_stmt .end_lineno if last_stmt else section .end_lineno
612597 if end_lineno is None :
613598 return
0 commit comments