Merge pull request #614 from adqm/less_aggressive_newline

be less aggressive about adding newlines in multiline mode
This commit is contained in:
Anthony Scopatz 2016-01-10 17:43:50 -05:00
commit 90f496a429
2 changed files with 39 additions and 10 deletions

View file

@ -21,8 +21,7 @@ class TabShouldInsertIndentFilter(Filter):
def can_compile(src):
"""Returns whether the code can be compiled, i.e. it is valid xonsh."""
if not src.endswith('\n') and not src.endswith('\''):
src = src + '\n'
src = src if src.endswith('\n') else '{}\n'.format(src)
try:
builtins.__xonsh_execer__.compile(src, mode='single', glbs=None,
locs=builtins.__xonsh_ctx__)
@ -75,25 +74,33 @@ def load_xonsh_bindings(key_bindings_manager):
b = event.cli.current_buffer
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.char_before_cursor == ':':
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
elif (not (len(b.document.current_line) == 0 or
b.document.current_line.isspace()) and
elif (not current_line_blank and
b.document.current_line.split(maxsplit=1)[0] in DEDENT_TOKENS):
b.newline(copy_margin=True)
_ = b.delete_before_cursor(count=len(indent_))
elif (not b.document.on_first_line and
not (len(b.document.current_line) == 0 or
b.document.current_line.isspace())):
not current_line_blank):
b.newline(copy_margin=True)
elif b.document.char_before_cursor == '\\':
b.newline()
elif b.document.find_next_word_beginning() is not None:
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:]))):
b.newline(copy_margin=True)
elif not can_compile(b.document.text):
elif not current_line_blank and not can_compile(b.document.text):
b.newline()
else:
b.accept_action.validate_and_handle(event.cli, b)
def _is_blank(l):
return len(l.strip()) == 0

View file

@ -13,7 +13,8 @@ from pygments.token import (Keyword, Name, Comment, String, Error, Number,
Operator, Generic, Whitespace, Token)
from xonsh.base_shell import BaseShell
from xonsh.tools import format_prompt_for_prompt_toolkit, _make_style
from xonsh.tools import (format_prompt_for_prompt_toolkit, _make_style,
print_exception)
from xonsh.ptk.completer import PromptToolkitCompleter
from xonsh.ptk.history import PromptToolkitHistory
from xonsh.ptk.key_bindings import load_xonsh_bindings
@ -37,6 +38,27 @@ class PromptToolkitShell(BaseShell):
enable_open_in_editor=True)
load_xonsh_bindings(self.key_bindings_manager)
def push(self, line):
"""Pushes a line onto the buffer and compiles the code in a way that
enables multiline input.
"""
code = None
self.buffer.append(line)
if self.need_more_lines:
return None, code
src = ''.join(self.buffer)
try:
code = self.execer.compile(src,
mode='single',
glbs=None,
locs=self.ctx)
self.reset_buffer()
except Exception: # pylint: disable=broad-except
self.reset_buffer()
print_exception()
return src, None
return src, code
def cmdloop(self, intro=None):
"""Enters a loop that reads and execute input from user."""
if intro: