diff --git a/xonsh/ptk/key_bindings.py b/xonsh/ptk/key_bindings.py index 58cd7f8ca..14567f4b2 100644 --- a/xonsh/ptk/key_bindings.py +++ b/xonsh/ptk/key_bindings.py @@ -9,14 +9,28 @@ env = builtins.__xonsh_env__ indent_ = env.get('INDENT') DEDENT_TOKENS = frozenset(['raise', 'return', 'pass', 'break', 'continue']) + def carriage_return(b, cli): + """ + Preliminary parser to determine if 'Enter' key should send command to + the xonsh parser for execution or should insert a newline for continued + input. + + Current 'triggers' for inserting a newline are: + - Not on first line of buffer and line is non-empty + - Previous character is a colon (covers if, for, etc...) + - User is in an open paren-block + - Line ends with backslash + - Any text exists below cursor position (relevant when editing previous + multiline blocks) + """ at_end_of_line = _is_blank(b.document.current_line_after_cursor) current_line_blank = _is_blank(b.document.current_line) # indent after a colon - if (b.document.current_line_before_cursor.strip().endswith(':') - and at_end_of_line): + if (b.document.current_line_before_cursor.strip().endswith(':') and + at_end_of_line): b.newline() b.insert_text(indent_, fire_event=False) # if current line isn't blank, check dedent tokens @@ -31,14 +45,15 @@ def carriage_return(b, cli): b.newline() elif (b.document.find_next_word_beginning() is not None and (any(not _is_blank(i) - for i - in b.document.lines_from_current[1:]))): + for i + in b.document.lines_from_current[1:]))): b.newline(copy_margin=True) elif not current_line_blank and not can_compile(b.document.text): b.newline() else: b.accept_action.validate_and_handle(cli, b) + class TabShouldInsertIndentFilter(Filter): """ Filter that is intended to check if should insert indent instead of @@ -70,7 +85,6 @@ def load_xonsh_bindings(key_bindings_manager): """ handle = key_bindings_manager.registry.add_binding - @handle(Keys.Tab, filter=TabShouldInsertIndentFilter()) def _(event): """ @@ -81,27 +95,12 @@ def load_xonsh_bindings(key_bindings_manager): @handle(Keys.BackTab) def insert_literal_tab(event): - """ - Insert literal tab on Shift+Tab instead of autocompleting - """ + """ Insert literal tab on Shift+Tab instead of autocompleting """ event.cli.current_buffer.insert_text(env.get('INDENT')) @handle(Keys.ControlJ, filter=IsMultiline()) def multiline_carriage_return(event): - """ - Preliminary parser to determine if 'Enter' key should send command to - the xonsh parser for execution or should insert a newline for continued - input. - - Current 'triggers' for inserting a newline are: - - Not on first line of buffer and line is non-empty - - Previous character is a colon (covers if, for, etc...) - - User is in an open paren-block - - Line ends with backslash - - Any text exists below cursor position (relevant when editing previous - multiline blocks) - """ - + """ Wrapper around carriage_return multiline parser """ b = event.cli.current_buffer carriage_return(b, event.cli)