Example #1
0
def CreateKeyBindingRegistry():
    """Configures the keybindings for the shell."""
    def _HandleCtrlQ(event):
        event.cli.set_return_value(None)

    def _HandleCtrlT(_):
        global SHOW_HELP_WINDOW
        SHOW_HELP_WINDOW = not SHOW_HELP_WINDOW

    def _HandleCtrlW(event):
        doc = event.cli.current_buffer.document
        browser.OpenReferencePage(event.cli, doc.text, doc.cursor_position)

    m = manager.KeyBindingManager(
        enable_abort_and_exit_bindings=True,
        enable_system_bindings=True,
        enable_search=True,
        enable_auto_suggest_bindings=True,
    )

    m.registry.add_binding(keys.Keys.ControlQ, eager=True)(_HandleCtrlQ)
    m.registry.add_binding(keys.Keys.ControlT, eager=True)(_HandleCtrlT)
    m.registry.add_binding(keys.Keys.ControlW, eager=True)(_HandleCtrlW)

    return m.registry
Example #2
0
  def MakeRegistry(self):
    """Makes and returns a key binding registry populated with the bindings."""
    m = manager.KeyBindingManager(
        enable_abort_and_exit_bindings=True,
        enable_system_bindings=True,
        enable_search=True,
        enable_auto_suggest_bindings=True,)

    for binding in self.bindings:
      m.registry.add_binding(binding.key, eager=True)(binding.Handle)

    return m.registry
Example #3
0
    def build_cli(self):
        history = pt_history.FileHistory(
            os.path.expanduser('~/.edgedbhistory'))

        key_binding_manager = pt_keymanager.KeyBindingManager(
            enable_system_bindings=True,
            enable_search=True,
            enable_abort_and_exit_bindings=True)

        @key_binding_manager.registry.add_binding(pt_keys.Keys.F3)
        def _graphql_toggle(event):
            self.graphql = not self.graphql

        @key_binding_manager.registry.add_binding(pt_keys.Keys.Tab)
        def _tab(event):
            b = cli.current_buffer
            before_cursor = b.document.current_line_before_cursor
            if b.text and (not before_cursor or before_cursor.isspace()):
                b.insert_text('    ')

        layout = pt_shortcuts.create_prompt_layout(
            lexer=pt_lexers.PygmentsLexer(eql_pygments.EdgeQLLexer),
            reserve_space_for_menu=4,
            get_prompt_tokens=self.get_prompt_tokens,
            get_continuation_tokens=self.get_continuation_tokens,
            get_bottom_toolbar_tokens=self.get_toolbar_tokens,
            multiline=True)

        buf = InputBuffer(
            history=history,

            # to make reserve_space_for_menu work:
            complete_while_typing=pt_filters.Always(),

            accept_action=pt_app.AcceptAction.RETURN_DOCUMENT)

        app = pt_app.Application(
            style=self.style,
            layout=layout,
            buffer=buf,
            ignore_case=True,
            key_bindings_registry=key_binding_manager.registry,
            on_exit=pt_app.AbortAction.RAISE_EXCEPTION,
            on_abort=pt_app.AbortAction.RETRY)

        cli = pt_interface.CommandLineInterface(
            application=app,
            eventloop=self.eventloop)

        return cli
Example #4
0
def _KeyBindings():
  """Returns KeyBindingManager that allows key binding in the Application."""

  # Need to set to True to allow any exit bindings like ctrl-d.
  return manager.KeyBindingManager(enable_abort_and_exit_bindings=True,
                                   enable_auto_suggest_bindings=True)
Example #5
0
"""snipping.prompt_toolkit.key_binding

wrappers for key bindings
"""

from prompt_toolkit import keys
from prompt_toolkit import filters
from prompt_toolkit.key_binding import manager
from prompt_toolkit.key_binding import vi_state

from snipping.prompt_toolkit import buffers

_KEY_BINDING_MANAGERS = {
    'default':
    manager.KeyBindingManager(enable_vi_mode=True,
                              enable_extra_page_navigation=True)
}


def key_binding_manager(name='default'):
    global _KEY_BINDING_MANAGERS
    return _KEY_BINDING_MANAGERS[name]


class ViNormalMode(filters.Filter):
    def __init__(self):
        self.state = vi_state.InputMode.NAVIGATION

    def __call__(self, cli):
        kbm = key_binding_manager()
        return kbm.get_vi_state(cli).input_mode == self.state