Beispiel #1
0
    def setUpClass(cls):
        cls.client_server = ClientServer()
        cls.client, cls.server = cls.client_server

        @cls.server.feature(
            CODE_ACTION,
            CodeActionOptions(code_action_kinds=[CodeActionKind.Refactor])
        )
        def f(params: CodeActionParams) -> Optional[List[Union[Command, CodeAction]]]:
            if params.text_document.uri == 'file://return.list':
                return [
                    CodeAction(title='action1'),
                    CodeAction(title='action2', kind=CodeActionKind.Refactor),
                    Command(title='cmd1', command='cmd1', arguments=[1, 'two']),
                ]
            else:
                return None

        cls.client_server.start()
Beispiel #2
0
    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


@SERVER.feature(
    CODE_ACTION,
    CodeActionOptions(code_action_kinds=[
        CodeActionKind.RefactorInline,
        CodeActionKind.RefactorExtract,
    ], ),
)
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)