Skip to content

Commit

Permalink
Add support for tab indentation & backspace de-indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger committed Jul 21, 2017
1 parent 681ed9a commit a05e1cd
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/crate/crash/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
ConditionalProcessor
)
from prompt_toolkit.key_binding.manager import KeyBindingManager
from prompt_toolkit.keys import Keys
from prompt_toolkit.shortcuts import (create_prompt_layout,
create_output,
create_eventloop)
Expand Down Expand Up @@ -194,6 +195,9 @@ def get_completions(self, document, complete_event):
return
line = document.text
word_before_cursor = document.get_word_before_cursor()
# Show completion only if at least 1 character has been typed
if not word_before_cursor.strip():
return
if line.startswith('\\'):
for w in self.get_command_completions(line):
yield Completion(w, -len(word_before_cursor))
Expand Down Expand Up @@ -272,13 +276,39 @@ def create_buffer(cmd, history_file):
return buffer


def _bind_keys(registry):

def _is_start_of_multiline(cli):
doc = cli.current_buffer.document
word = doc.get_word_before_cursor()
return doc.text and not word

@registry.add_binding(Keys.Tab, filter=Condition(_is_start_of_multiline))
def _(event):
event.cli.current_buffer.insert_text(' ')

def _bs_should_deindent(cli):
doc = cli.current_buffer.document
start_of_line_pos = doc.get_start_of_line_position()
return (
not doc.get_word_before_cursor() and
start_of_line_pos != 0 and
start_of_line_pos % 4 == 0
)

@registry.add_binding(Keys.Backspace, filter=Condition(_bs_should_deindent))
def _(event):
event.cli.current_buffer.delete_before_cursor(4)


def loop(cmd, history_file):
key_binding_manager = KeyBindingManager(
enable_search=True,
enable_abort_and_exit_bindings=True,
enable_system_bindings=True,
enable_open_in_editor=True
)
_bind_keys(key_binding_manager.registry)
layout = create_prompt_layout(
message=u'cr> ',
multiline=True,
Expand Down

0 comments on commit a05e1cd

Please sign in to comment.