示例#1
0
    def create_extract_to_macros_file_actions(
            self, tool: GalaxyToolXmlDocument,
            macro_definitions: ToolMacroDefinitions, macro: MacroData,
            params: CodeActionParams) -> List[CodeAction]:
        """Returns refactoring actions for extracting a macro into an external macros definition file."""
        if not macro_definitions.imported_macros:
            return [
                CodeAction(
                    title=
                    f"Extract to macro, create and import {DEFAULT_MACROS_FILENAME}",
                    kind=CodeActionKind.RefactorExtract,
                    edit=WorkspaceEdit(
                        document_changes=self.
                        _calculate_external_changes_for_macro_in_new_file(
                            tool, DEFAULT_MACROS_FILENAME, macro, params)),
                )
            ]
        code_actions = []
        for file_name, macro_file_definition in macro_definitions.imported_macros.items(
        ):
            code_actions.append(
                CodeAction(
                    title=f"Extract to macro in {file_name}",
                    kind=CodeActionKind.RefactorExtract,
                    edit=WorkspaceEdit(
                        changes=self._calculate_external_changes_for_macro(
                            tool, macro_file_definition, macro, params)),
                ))

        return code_actions
示例#2
0
 def create_extract_to_local_macro_actions(
         self, tool: GalaxyToolXmlDocument, macro: MacroData,
         params: CodeActionParams) -> List[CodeAction]:
     """Returns refactoring actions to extract a macro into the local <macros> section of a tool wrapper."""
     return [
         CodeAction(
             title="Extract to local macro",
             kind=CodeActionKind.RefactorExtract,
             edit=WorkspaceEdit(
                 changes=self._calculate_local_changes_for_macro(
                     tool, macro, params)),
         )
     ]
示例#3
0
def rename(server: JediLanguageServer,
           params: RenameParams) -> Optional[WorkspaceEdit]:
    """Rename a symbol across a workspace."""
    document = server.workspace.get_document(params.text_document.uri)
    jedi_script = jedi_utils.script(server.project, document)
    jedi_lines = jedi_utils.line_column(jedi_script, params.position)
    try:
        refactoring = jedi_script.rename(new_name=params.new_name,
                                         **jedi_lines)
    except RefactoringError:
        return None
    changes = text_edit_utils.lsp_document_changes(server.workspace,
                                                   refactoring)
    return WorkspaceEdit(document_changes=changes) if changes else None
示例#4
0
def code_action(server: JediLanguageServer,
                params: CodeActionParams) -> Optional[List[CodeAction]]:
    """Get code actions.

    Currently supports:
        1. Inline variable
        2. Extract variable
        3. Extract function
    """
    document = server.workspace.get_document(params.text_document.uri)
    jedi_script = jedi_utils.script(server.project, document)
    code_actions = []
    jedi_lines = jedi_utils.line_column(jedi_script, params.range.start)
    jedi_lines_extract = jedi_utils.line_column_range(params.range)

    try:
        if params.range.start.line != params.range.end.line:
            # refactor this at some point; control flow with exception == bad
            raise RefactoringError("inline only viable for single-line range")
        inline_refactoring = jedi_script.inline(**jedi_lines)
    except (RefactoringError, AttributeError, IndexError):
        inline_changes = []
    else:
        inline_changes = text_edit_utils.lsp_document_changes(
            server.workspace, inline_refactoring)
    if inline_changes:
        code_actions.append(
            CodeAction(
                title="Inline variable",
                kind=CodeActionKind.RefactorInline,
                edit=WorkspaceEdit(document_changes=inline_changes, ),
            ))

    extract_var = (
        server.initialization_options.code_action.name_extract_variable)
    try:
        extract_variable_refactoring = jedi_script.extract_variable(
            new_name=extract_var, **jedi_lines_extract)
    except (RefactoringError, AttributeError, IndexError):
        extract_variable_changes = []
    else:
        extract_variable_changes = text_edit_utils.lsp_document_changes(
            server.workspace, extract_variable_refactoring)
    if extract_variable_changes:
        code_actions.append(
            CodeAction(
                title=f"Extract expression into variable '{extract_var}'",
                kind=CodeActionKind.RefactorExtract,
                edit=WorkspaceEdit(
                    document_changes=extract_variable_changes, ),
            ))

    extract_func = (
        server.initialization_options.code_action.name_extract_function)
    try:
        extract_function_refactoring = jedi_script.extract_function(
            new_name=extract_func, **jedi_lines_extract)
    except (RefactoringError, AttributeError, IndexError):
        extract_function_changes = []
    else:
        extract_function_changes = text_edit_utils.lsp_document_changes(
            server.workspace, extract_function_refactoring)
    if extract_function_changes:
        code_actions.append(
            CodeAction(
                title=f"Extract expression into function '{extract_func}'",
                kind=CodeActionKind.RefactorExtract,
                edit=WorkspaceEdit(
                    document_changes=extract_function_changes, ),
            ))

    return code_actions if code_actions else None
示例#5
0
 def f(params: RenameParams) -> Optional[WorkspaceEdit]:
     if params.text_document.uri == 'file://return.workspace_edit':
         return WorkspaceEdit(
             changes={
                 'uri1': [
                     TextEdit(
                         range=Range(
                             start=Position(line=0, character=0),
                             end=Position(line=1, character=1),
                         ),
                         new_text='text1',
                     ),
                     TextEdit(
                         range=Range(
                             start=Position(line=1, character=1),
                             end=Position(line=2, character=2),
                         ),
                         new_text='text2',
                     ),
                 ],
             },
             document_changes=[
                 TextDocumentEdit(
                     text_document=
                     OptionalVersionedTextDocumentIdentifier(
                         uri='uri',
                         version=3,
                     ),
                     edits=[
                         TextEdit(
                             range=Range(
                                 start=Position(line=2, character=2),
                                 end=Position(line=3, character=3),
                             ),
                             new_text='text3',
                         ),
                     ]),
                 CreateFile(
                     kind=ResourceOperationKind.Create,
                     uri='create file',
                     options=CreateFileOptions(
                         overwrite=True,
                         ignore_if_exists=True,
                     ),
                 ),
                 RenameFile(kind=ResourceOperationKind.Rename,
                            old_uri='rename old uri',
                            new_uri='rename new uri',
                            options=RenameFileOptions(
                                overwrite=True,
                                ignore_if_exists=True,
                            )),
                 DeleteFile(
                     kind=ResourceOperationKind.Delete,
                     uri='delete file',
                     options=DeleteFileOptions(
                         recursive=True,
                         ignore_if_exists=True,
                     ),
                 ),
             ])
     else:
         return None