Exemple #1
0
 def __init__(self, *args, **kwargs):
     """ Create a Completer that reuses the advanced completion support of PyDev
         in addition to the completion support provided by IPython """
     IPCompleter.__init__(self, *args, **kwargs)
     # Use PyDev for python matches, see getCompletions below
     if self.python_matches in self.matchers:
         # `self.python_matches` matches attributes or global python names
         self.matchers.remove(self.python_matches)
 def __init__(self, *args, **kwargs):
     """ Create a Completer that reuses the advanced completion support of PyDev
         in addition to the completion support provided by IPython """
     IPCompleter.__init__(self, *args, **kwargs)
     # Use PyDev for python matches, see getCompletions below
     if self.python_matches in self.matchers:
         # `self.python_matches` matches attributes or global python names
         self.matchers.remove(self.python_matches)
Exemple #3
0
    def pt_init(self):
        def get_prompt_tokens():
            return [(Token.Prompt, self.prompt)]

        if self._ptcomp is None:
            compl = IPCompleter(shell=self.shell,
                                        namespace={},
                                        global_namespace={},
                                        parent=self.shell,
                                       )
            self._ptcomp = IPythonPTCompleter(compl)

        kb = KeyBindings()
        supports_suspend = Condition(lambda: hasattr(signal, 'SIGTSTP'))
        kb.add('c-z', filter=supports_suspend)(suspend_to_bg)

        if self.shell.display_completions == 'readlinelike':
            kb.add('tab', filter=(has_focus(DEFAULT_BUFFER)
                                  & ~has_selection
                                  & vi_insert_mode | emacs_insert_mode
                                  & ~cursor_in_leading_ws
                              ))(display_completions_like_readline)

        self.pt_app = PromptSession(
                            message=(lambda: PygmentsTokens(get_prompt_tokens())),
                            editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()),
                            key_bindings=kb,
                            history=self.shell.debugger_history,
                            completer=self._ptcomp,
                            enable_history_search=True,
                            mouse_support=self.shell.mouse_support,
                            complete_style=self.shell.pt_complete_style,
                            style=self.shell.style,
                            inputhook=self.shell.inputhook,
        )
Exemple #4
0
    def pt_init(self):
        def get_prompt_tokens(cli):
            return [(Token.Prompt, self.prompt)]

        def patch_stdout(**kwargs):
            return self.pt_cli.patch_stdout_context(**kwargs)

        if self._ptcomp is None:
            compl = IPCompleter(
                shell=self.shell,
                namespace={},
                global_namespace={},
                parent=self.shell,
            )
            self._ptcomp = IPythonPTCompleter(compl, patch_stdout=patch_stdout)

        self._pt_app = create_prompt_application(
            editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()),
            history=self.shell.debugger_history,
            completer=self._ptcomp,
            enable_history_search=True,
            mouse_support=self.shell.mouse_support,
            get_prompt_tokens=get_prompt_tokens)
        self.pt_cli = CommandLineInterface(self._pt_app,
                                           eventloop=self.shell._eventloop)
    def pt_init(self):
        def get_prompt_tokens():
            return [(Token.Prompt, self.prompt)]

        if self._ptcomp is None:
            compl = IPCompleter(
                shell=self.shell,
                namespace={},
                global_namespace={},
                parent=self.shell,
            )
            self._ptcomp = IPythonPTCompleter(compl)

        options = dict(
            message=(lambda: PygmentsTokens(get_prompt_tokens())),
            editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()),
            key_bindings=create_ipython_shortcuts(self.shell),
            history=self.shell.debugger_history,
            completer=self._ptcomp,
            enable_history_search=True,
            mouse_support=self.shell.mouse_support,
            complete_style=self.shell.pt_complete_style,
            style=self.shell.style,
            color_depth=self.shell.color_depth,
        )

        if not PTK3:
            options['inputhook'] = self.shell.inputhook
        self.pt_loop = asyncio.new_event_loop()
        self.pt_app = PromptSession(**options)
Exemple #6
0
    def pt_init(self, pt_session_options=None):
        """Initialize the prompt session and the prompt loop
        and store them in self.pt_app and self.pt_loop.

        Additional keyword arguments for the PromptSession class
        can be specified in pt_session_options.
        """
        if pt_session_options is None:
            pt_session_options = {}

        def get_prompt_tokens():
            return [(Token.Prompt, self.prompt)]

        if self._ptcomp is None:
            compl = IPCompleter(shell=self.shell,
                                namespace={},
                                global_namespace={},
                                parent=self.shell)
            # add a completer for all the do_ methods
            methods_names = [m[3:] for m in dir(self) if m.startswith("do_")]

            def gen_comp(self, text):
                return [m for m in methods_names if m.startswith(text)]

            import types
            newcomp = types.MethodType(gen_comp, compl)
            compl.custom_matchers.insert(0, newcomp)
            # end add completer.

            self._ptcomp = IPythonPTCompleter(compl)

        # setup history only when we start pdb
        if self.shell.debugger_history is None:
            if self.shell.debugger_history_file is not None:

                p = Path(self.shell.debugger_history_file).expanduser()
                if not p.exists():
                    p.touch()
                self.debugger_history = FileHistory(os.path.expanduser(str(p)))
            else:
                self.debugger_history = InMemoryHistory()

        options = dict(
            message=(lambda: PygmentsTokens(get_prompt_tokens())),
            editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()),
            key_bindings=create_ipython_shortcuts(self.shell),
            history=self.debugger_history,
            completer=self._ptcomp,
            enable_history_search=True,
            mouse_support=self.shell.mouse_support,
            complete_style=self.shell.pt_complete_style,
            style=getattr(self.shell, "style", None),
            color_depth=self.shell.color_depth,
        )

        if not PTK3:
            options['inputhook'] = self.shell.inputhook
        options.update(pt_session_options)
        self.pt_loop = asyncio.new_event_loop()
        self.pt_app = PromptSession(**options)
Exemple #7
0
    def pt_init(self):
        def get_prompt_tokens(cli):
            return [(Token.Prompt, self.prompt)]

        def patch_stdout(**kwargs):
            return self.pt_cli.patch_stdout_context(**kwargs)

        if self._ptcomp is None:
            compl = IPCompleter(
                shell=self.shell,
                namespace={},
                global_namespace={},
                parent=self.shell,
            )
            self._ptcomp = IPythonPTCompleter(compl, patch_stdout=patch_stdout)

        kbmanager = KeyBindingManager.for_prompt()
        supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP'))
        kbmanager.registry.add_binding(Keys.ControlZ,
                                       filter=supports_suspend)(suspend_to_bg)

        self._pt_app = create_prompt_application(
            editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()),
            key_bindings_registry=kbmanager.registry,
            history=self.shell.debugger_history,
            completer=self._ptcomp,
            enable_history_search=True,
            mouse_support=self.shell.mouse_support,
            get_prompt_tokens=get_prompt_tokens)
        self.pt_cli = CommandLineInterface(self._pt_app,
                                           eventloop=self.shell._eventloop)
Exemple #8
0
def initialize_completer():
    global ip_completer, p_completer
    try:
        __IPYTHON__
        from IPython.core.completer import IPCompleter
        ip = get_ipython()
        ip_completer = IPCompleter(ip, global_namespace=global_dict)
    except NameError:
        try:
            from rlcompleter import Completer
            p_completer = Completer(namespace=global_dict)
        except:
            pass
Exemple #9
0
    def mb_completer(event):
        """ Custom completer for minibuffer """

        c = g_ipm.c
        ip = g_ipm.ip

        cmd_param = event.line.split()
        if event.line.endswith(' '):
            cmd_param.append('')

        if len(cmd_param) > 2:
            if g_legacy:
                return ip.IP.Completer.file_matches(event.symbol)
            else:
                completer = IPCompleter(shell=ip,
                                        namespace=ip.user_ns,
                                        global_namespace=None,
                                        alias_table=None,
                                        use_readline=True,
                                        config=None)
                return completer.file_matches(event.symbol)

        return sorted(list(c.commandsDict.keys()))
    def mb_completer(event):

        """ Custom completer for minibuffer """

        c = g_ipm.c
        ip = g_ipm.ip

        cmd_param = event.line.split()
        if event.line.endswith(' '):
            cmd_param.append('')

        if len(cmd_param) > 2:
            if g_legacy:
                return ip.IP.Completer.file_matches(event.symbol)
            else:
                completer = IPCompleter(shell=ip,
                    namespace=ip.user_ns,
                    global_namespace=None,
                    alias_table=None,
                    use_readline=True,
                    config=None)
                return completer.file_matches(event.symbol)

        return sorted(list(c.commandsDict.keys()))
Exemple #11
0
    def pt_init(self):
        def get_prompt_tokens(cli):
            return [(Token.Prompt, self.prompt)]

        def patch_stdout(**kwargs):
            return self.pt_cli.patch_stdout_context(**kwargs)

        if self._ptcomp is None:
            compl = IPCompleter(
                shell=self.shell,
                namespace={},
                global_namespace={},
                use_readline=False,
                parent=self.shell,
            )
            self._ptcomp = IPythonPTCompleter(compl, patch_stdout=patch_stdout)

        kbmanager = KeyBindingManager.for_prompt()
        supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP'))
        kbmanager.registry.add_binding(Keys.ControlZ,
                                       filter=supports_suspend)(suspend_to_bg)

        if self.shell.display_completions == 'readlinelike':
            kbmanager.registry.add_binding(
                Keys.ControlI,
                filter=(HasFocus(DEFAULT_BUFFER)
                        & ~HasSelection()
                        & ViInsertMode() | EmacsInsertMode()
                        & ~cursor_in_leading_ws
                        ))(display_completions_like_readline)
        multicolumn = (self.shell.display_completions == 'multicolumn')

        self._pt_app = create_prompt_application(
            editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()),
            key_bindings_registry=kbmanager.registry,
            history=self.shell.debugger_history,
            completer=self._ptcomp,
            enable_history_search=True,
            mouse_support=self.shell.mouse_support,
            get_prompt_tokens=get_prompt_tokens,
            display_completions_in_columns=multicolumn,
        )
        self.pt_cli = CommandLineInterface(self._pt_app,
                                           eventloop=self.shell._eventloop)
Exemple #12
0
    def pt_init(self):
        def get_prompt_tokens():
            return [(Token.Prompt, self.prompt)]

        if self._ptcomp is None:
            compl = IPCompleter(
                shell=self.shell,
                namespace={},
                global_namespace={},
                parent=self.shell,
            )
            # add a completer for all the do_ methods
            methods_names = [m[3:] for m in dir(self) if m.startswith("do_")]

            def gen_comp(self, text):
                return [m for m in methods_names if m.startswith(text)]

            import types
            newcomp = types.MethodType(gen_comp, compl)
            compl.custom_matchers.insert(0, newcomp)
            # end add completer.

            self._ptcomp = IPythonPTCompleter(compl)

        options = dict(
            message=(lambda: PygmentsTokens(get_prompt_tokens())),
            editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()),
            key_bindings=create_ipython_shortcuts(self.shell),
            history=self.shell.debugger_history,
            completer=self._ptcomp,
            enable_history_search=True,
            mouse_support=self.shell.mouse_support,
            complete_style=self.shell.pt_complete_style,
            style=self.shell.style,
            color_depth=self.shell.color_depth,
        )

        if not PTK3:
            options['inputhook'] = self.shell.inputhook
        self.pt_loop = asyncio.new_event_loop()
        self.pt_app = PromptSession(**options)
Exemple #13
0
 def __init__(self, *args, **kwargs):
     """ Create a Completer that reuses the advanced completion support of PyDev
         in addition to the completion support provided by IPython """
     IPCompleter.__init__(self, *args, **kwargs)
 def __init__(self, *args, **kwargs):
     """ Create a Completer that reuses the advanced completion support of PyDev
         in addition to the completion support provided by IPython """
     IPCompleter.__init__(self, *args, **kwargs)
Exemple #15
0
def add_custom_keybinds(p):
    def next_command(event):
        p.preloop()
        line = p.precmd("next")
        stop = p.onecmd(line)
        stop = p.postcmd(stop, line)
        p.postloop()
        buff = event.current_buffer
        buff.validate_and_handle()

    def step_command(event):
        p.preloop()
        line = p.precmd("step")
        stop = p.onecmd(line)
        stop = p.postcmd(stop, line)
        p.postloop()
        buff = event.current_buffer
        buff.validate_and_handle()

    def up_command(event):
        p.preloop()
        line = p.precmd("up")
        stop = p.onecmd(line)
        stop = p.postcmd(stop, line)
        p.postloop()
        buff = event.current_buffer
        buff.validate_and_handle()

    def down_command(event):
        p.preloop()
        line = p.precmd("down")
        stop = p.onecmd(line)
        stop = p.postcmd(stop, line)
        p.postloop()
        buff = event.current_buffer
        buff.validate_and_handle()

    def where_command(event):
        p.preloop()
        line = p.precmd("where")
        stop = p.onecmd(line)
        stop = p.postcmd(stop, line)
        p.postloop()
        buff = event.current_buffer
        buff.validate_and_handle()

    def args_command(event):
        p.preloop()
        line = p.precmd("args")
        stop = p.onecmd(line)
        stop = p.postcmd(stop, line)
        p.postloop()
        buff = event.current_buffer
        buff.validate_and_handle()

    def continue_command(event):
        p.preloop()
        line = p.precmd("continue")
        stop = p.onecmd(line)
        stop = p.postcmd(stop, line)
        p.postloop()
        buff = event.current_buffer
        buff.validate_and_handle()

    def longlist_command(event):
        p.preloop()
        line = p.precmd("longlist")
        stop = p.onecmd(line)
        stop = p.postcmd(stop, line)
        p.postloop()
        buff = event.current_buffer
        buff.validate_and_handle()

    def list_locals_command(event):
        p.preloop()
        line = p.precmd(
            "from tabulate import tabulate;;pp tabulate([[k, type(v).__name__, '|'] for k, v in locals().items() if k not in ['tabulate', 'ipdb']])"
        )
        stop = p.onecmd(line)
        stop = p.postcmd(stop, line)
        p.postloop()
        buff = event.current_buffer
        buff.validate_and_handle()

    from prompt_toolkit.key_binding import KeyBindings
    from prompt_toolkit.shortcuts.prompt import PromptSession
    from prompt_toolkit.enums import EditingMode
    from prompt_toolkit.formatted_text import PygmentsTokens
    from prompt_toolkit.enums import DEFAULT_BUFFER
    from prompt_toolkit.filters import (Condition, has_focus, has_selection,
                                        vi_insert_mode, emacs_insert_mode)
    from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline
    from pygments.token import Token
    from IPython.terminal.shortcuts import suspend_to_bg, cursor_in_leading_ws
    from IPython.core.completer import IPCompleter
    from IPython.terminal.ptutils import IPythonPTCompleter
    import signal

    def get_prompt_tokens():
        return [(Token.Prompt, p.prompt)]

    compl = IPCompleter(
        shell=p.shell,
        namespace={},
        global_namespace={},
        parent=p.shell,
    )
    p._ptcomp = IPythonPTCompleter(compl)

    kb = KeyBindings()
    supports_suspend = Condition(lambda: hasattr(signal, 'SIGTSTP'))
    kb.add('c-z', filter=supports_suspend)(suspend_to_bg)

    if p.shell.display_completions == 'readlinelike':
        kb.add('tab',
               filter=(
                   has_focus(DEFAULT_BUFFER)
                   & ~has_selection
                   & vi_insert_mode | emacs_insert_mode
                   & ~cursor_in_leading_ws))(display_completions_like_readline)

    kb.add('c-n')(next_command)
    kb.add('c-s')(step_command)
    kb.add('c-o')(up_command)
    kb.add('c-p')(down_command)
    kb.add('c-w')(where_command)
    kb.add('c-a')(args_command)
    kb.add('c-t')(continue_command)
    kb.add('c-l')(longlist_command)
    kb.add('c-v')(list_locals_command)

    p.pt_app = PromptSession(
        message=(lambda: PygmentsTokens(get_prompt_tokens())),
        editing_mode=getattr(EditingMode, p.shell.editing_mode.upper()),
        key_bindings=kb,
        history=p.shell.debugger_history,
        completer=p._ptcomp,
        enable_history_search=True,
        mouse_support=p.shell.mouse_support,
        complete_style=p.shell.pt_complete_style,
        style=p.shell.style,
    )
    return p