Skip to content

Commit b8efd1d

Browse files
committed
refactor(langserver): move code action assign result to variable to refactorings
1 parent 9499d43 commit b8efd1d

File tree

2 files changed

+98
-96
lines changed

2 files changed

+98
-96
lines changed

packages/language_server/src/robotcode/language_server/robotframework/parts/code_action_quick_fixes.py

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
if TYPE_CHECKING:
4343
from robotcode.language_server.robotframework.protocol import RobotLanguageServerProtocol # pragma: no cover
4444

45-
QUICK_FIX_OTHER = "other"
4645

4746
KEYWORD_WITH_ARGS_TEMPLATE = Template(
4847
"""\
@@ -109,7 +108,7 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
109108
self.parent.commands.register_all(self)
110109

111110
@language_id("robotframework")
112-
@code_action_kinds([CodeActionKind.QUICK_FIX, QUICK_FIX_OTHER])
111+
@code_action_kinds([CodeActionKind.QUICK_FIX])
113112
async def collect(
114113
self, sender: Any, document: TextDocument, range: Range, context: CodeActionContext
115114
) -> Optional[List[Union[Command, CodeAction]]]:
@@ -308,100 +307,6 @@ async def _apply_create_keyword(self, document: TextDocument, insert_text: str)
308307
insert_range.end.character += len(lines[-1])
309308
await self.parent.window.show_document(str(document.uri), take_focus=True, selection=insert_range)
310309

311-
async def code_action_assign_result_to_variable(
312-
self, document: TextDocument, range: Range, context: CodeActionContext
313-
) -> Optional[List[Union[Command, CodeAction]]]:
314-
from robot.parsing.lexer import Token as RobotToken
315-
from robot.parsing.model.statements import (
316-
Fixture,
317-
KeywordCall,
318-
Template,
319-
TestTemplate,
320-
)
321-
322-
if range.start.line == range.end.line and (
323-
(context.only and QUICK_FIX_OTHER in context.only)
324-
or context.trigger_kind
325-
in [
326-
CodeActionTriggerKind.INVOKED,
327-
CodeActionTriggerKind.AUTOMATIC,
328-
]
329-
):
330-
model = await self.parent.documents_cache.get_model(document, False)
331-
node = await get_node_at_position(model, range.start)
332-
333-
if not isinstance(node, KeywordCall) or node.assign:
334-
return None
335-
336-
keyword_token = (
337-
node.get_token(RobotToken.NAME)
338-
if isinstance(node, (TestTemplate, Template, Fixture))
339-
else node.get_token(RobotToken.KEYWORD)
340-
)
341-
342-
if keyword_token is None or range.start not in range_from_token(keyword_token):
343-
return None
344-
345-
return [
346-
CodeAction(
347-
"Assign result to variable",
348-
kind=QUICK_FIX_OTHER,
349-
command=Command(
350-
self.parent.commands.get_command_name(self.assign_result_to_variable_command),
351-
self.parent.commands.get_command_name(self.assign_result_to_variable_command),
352-
[document.document_uri, range],
353-
),
354-
)
355-
]
356-
357-
return None
358-
359-
@command("robotcode.assignResultToVariable")
360-
async def assign_result_to_variable_command(self, document_uri: DocumentUri, range: Range) -> None:
361-
from robot.parsing.lexer import Token as RobotToken
362-
from robot.parsing.model.statements import (
363-
Fixture,
364-
KeywordCall,
365-
Template,
366-
TestTemplate,
367-
)
368-
369-
if range.start.line == range.end.line and range.start.character <= range.end.character:
370-
document = await self.parent.documents.get(document_uri)
371-
if document is None:
372-
return
373-
374-
model = await self.parent.documents_cache.get_model(document, False)
375-
node = await get_node_at_position(model, range.start)
376-
377-
if not isinstance(node, KeywordCall) or node.assign:
378-
return
379-
380-
keyword_token = (
381-
node.get_token(RobotToken.NAME)
382-
if isinstance(node, (TestTemplate, Template, Fixture))
383-
else node.get_token(RobotToken.KEYWORD)
384-
)
385-
386-
if keyword_token is None or range.start not in range_from_token(keyword_token):
387-
return
388-
389-
start = range_from_token(keyword_token).start
390-
we = WorkspaceEdit(
391-
document_changes=[
392-
TextDocumentEdit(
393-
OptionalVersionedTextDocumentIdentifier(str(document.uri), document.version),
394-
[AnnotatedTextEdit("assign_result_to_variable", Range(start, start), "${result} ")],
395-
)
396-
],
397-
change_annotations={"assign_result_to_variable": ChangeAnnotation("Assign result to variable", False)},
398-
)
399-
400-
if (await self.parent.workspace.apply_edit(we)).applied:
401-
insert_range = Range(start, start).extend(start_character=2, end_character=8)
402-
403-
await self.parent.window.show_document(str(document.uri), take_focus=True, selection=insert_range)
404-
405310
async def code_action_disable_robotcode_diagnostics_for_line(
406311
self, document: TextDocument, range: Range, context: CodeActionContext
407312
) -> Optional[List[Union[Command, CodeAction]]]:

packages/language_server/src/robotcode/language_server/robotframework/parts/code_action_refactor.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
CodeActionContext,
1414
CodeActionDisabledType,
1515
CodeActionKind,
16+
CodeActionTriggerKind,
1617
Command,
1718
DocumentUri,
1819
OptionalVersionedTextDocumentIdentifier,
@@ -28,8 +29,10 @@
2829
from robotcode.language_server.robotframework.utils import ast_utils
2930
from robotcode.language_server.robotframework.utils.ast_utils import (
3031
BodyBlock,
32+
get_node_at_position,
3133
get_nodes_at_position,
3234
range_from_node,
35+
range_from_token,
3336
)
3437
from robotcode.robot.utils import get_robot_version
3538

@@ -328,3 +331,97 @@ async def surround_with_command(
328331

329332
if (await self.parent.workspace.apply_edit(we)).applied and selection_range is not None:
330333
await self.parent.window.show_document(str(document.uri), take_focus=True, selection=selection_range)
334+
335+
async def code_action_assign_result_to_variable(
336+
self, document: TextDocument, range: Range, context: CodeActionContext
337+
) -> Optional[List[Union[Command, CodeAction]]]:
338+
from robot.parsing.lexer import Token as RobotToken
339+
from robot.parsing.model.statements import (
340+
Fixture,
341+
KeywordCall,
342+
Template,
343+
TestTemplate,
344+
)
345+
346+
if range.start.line == range.end.line and (
347+
(context.only and CodeActionKind.REFACTOR_EXTRACT in context.only)
348+
or context.trigger_kind
349+
in [
350+
CodeActionTriggerKind.INVOKED,
351+
CodeActionTriggerKind.AUTOMATIC,
352+
]
353+
):
354+
model = await self.parent.documents_cache.get_model(document, False)
355+
node = await get_node_at_position(model, range.start)
356+
357+
if not isinstance(node, KeywordCall) or node.assign:
358+
return None
359+
360+
keyword_token = (
361+
node.get_token(RobotToken.NAME)
362+
if isinstance(node, (TestTemplate, Template, Fixture))
363+
else node.get_token(RobotToken.KEYWORD)
364+
)
365+
366+
if keyword_token is None or range.start not in range_from_token(keyword_token):
367+
return None
368+
369+
return [
370+
CodeAction(
371+
"Assign keyword result to variable",
372+
kind=CodeActionKind.REFACTOR_EXTRACT,
373+
command=Command(
374+
self.parent.commands.get_command_name(self.assign_result_to_variable_command),
375+
self.parent.commands.get_command_name(self.assign_result_to_variable_command),
376+
[document.document_uri, range],
377+
),
378+
)
379+
]
380+
381+
return None
382+
383+
@command("robotcode.assignResultToVariable")
384+
async def assign_result_to_variable_command(self, document_uri: DocumentUri, range: Range) -> None:
385+
from robot.parsing.lexer import Token as RobotToken
386+
from robot.parsing.model.statements import (
387+
Fixture,
388+
KeywordCall,
389+
Template,
390+
TestTemplate,
391+
)
392+
393+
if range.start.line == range.end.line and range.start.character <= range.end.character:
394+
document = await self.parent.documents.get(document_uri)
395+
if document is None:
396+
return
397+
398+
model = await self.parent.documents_cache.get_model(document, False)
399+
node = await get_node_at_position(model, range.start)
400+
401+
if not isinstance(node, KeywordCall) or node.assign:
402+
return
403+
404+
keyword_token = (
405+
node.get_token(RobotToken.NAME)
406+
if isinstance(node, (TestTemplate, Template, Fixture))
407+
else node.get_token(RobotToken.KEYWORD)
408+
)
409+
410+
if keyword_token is None or range.start not in range_from_token(keyword_token):
411+
return
412+
413+
start = range_from_token(keyword_token).start
414+
we = WorkspaceEdit(
415+
document_changes=[
416+
TextDocumentEdit(
417+
OptionalVersionedTextDocumentIdentifier(str(document.uri), document.version),
418+
[AnnotatedTextEdit("assign_result_to_variable", Range(start, start), "${result} ")],
419+
)
420+
],
421+
change_annotations={"assign_result_to_variable": ChangeAnnotation("Assign result to variable", False)},
422+
)
423+
424+
if (await self.parent.workspace.apply_edit(we)).applied:
425+
insert_range = Range(start, start).extend(start_character=2, end_character=8)
426+
427+
await self.parent.window.show_document(str(document.uri), take_focus=True, selection=insert_range)

0 commit comments

Comments
 (0)