mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-05 00:41:00 +01:00
Merge pull request #614 from adqm/less_aggressive_newline
be less aggressive about adding newlines in multiline mode
This commit is contained in:
commit
90f496a429
2 changed files with 39 additions and 10 deletions
|
@ -21,8 +21,7 @@ class TabShouldInsertIndentFilter(Filter):
|
||||||
|
|
||||||
def can_compile(src):
|
def can_compile(src):
|
||||||
"""Returns whether the code can be compiled, i.e. it is valid xonsh."""
|
"""Returns whether the code can be compiled, i.e. it is valid xonsh."""
|
||||||
if not src.endswith('\n') and not src.endswith('\''):
|
src = src if src.endswith('\n') else '{}\n'.format(src)
|
||||||
src = src + '\n'
|
|
||||||
try:
|
try:
|
||||||
builtins.__xonsh_execer__.compile(src, mode='single', glbs=None,
|
builtins.__xonsh_execer__.compile(src, mode='single', glbs=None,
|
||||||
locs=builtins.__xonsh_ctx__)
|
locs=builtins.__xonsh_ctx__)
|
||||||
|
@ -75,25 +74,33 @@ def load_xonsh_bindings(key_bindings_manager):
|
||||||
|
|
||||||
b = event.cli.current_buffer
|
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
|
# 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.newline()
|
||||||
b.insert_text(indent_, fire_event=False)
|
b.insert_text(indent_, fire_event=False)
|
||||||
# if current line isn't blank, check dedent tokens
|
# if current line isn't blank, check dedent tokens
|
||||||
elif (not (len(b.document.current_line) == 0 or
|
elif (not current_line_blank and
|
||||||
b.document.current_line.isspace()) and
|
|
||||||
b.document.current_line.split(maxsplit=1)[0] in DEDENT_TOKENS):
|
b.document.current_line.split(maxsplit=1)[0] in DEDENT_TOKENS):
|
||||||
b.newline(copy_margin=True)
|
b.newline(copy_margin=True)
|
||||||
_ = b.delete_before_cursor(count=len(indent_))
|
_ = b.delete_before_cursor(count=len(indent_))
|
||||||
elif (not b.document.on_first_line and
|
elif (not b.document.on_first_line and
|
||||||
not (len(b.document.current_line) == 0 or
|
not current_line_blank):
|
||||||
b.document.current_line.isspace())):
|
|
||||||
b.newline(copy_margin=True)
|
b.newline(copy_margin=True)
|
||||||
elif b.document.char_before_cursor == '\\':
|
elif b.document.char_before_cursor == '\\':
|
||||||
b.newline()
|
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)
|
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()
|
b.newline()
|
||||||
else:
|
else:
|
||||||
b.accept_action.validate_and_handle(event.cli, b)
|
b.accept_action.validate_and_handle(event.cli, b)
|
||||||
|
|
||||||
|
def _is_blank(l):
|
||||||
|
return len(l.strip()) == 0
|
||||||
|
|
|
@ -13,7 +13,8 @@ from pygments.token import (Keyword, Name, Comment, String, Error, Number,
|
||||||
Operator, Generic, Whitespace, Token)
|
Operator, Generic, Whitespace, Token)
|
||||||
|
|
||||||
from xonsh.base_shell import BaseShell
|
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.completer import PromptToolkitCompleter
|
||||||
from xonsh.ptk.history import PromptToolkitHistory
|
from xonsh.ptk.history import PromptToolkitHistory
|
||||||
from xonsh.ptk.key_bindings import load_xonsh_bindings
|
from xonsh.ptk.key_bindings import load_xonsh_bindings
|
||||||
|
@ -37,6 +38,27 @@ class PromptToolkitShell(BaseShell):
|
||||||
enable_open_in_editor=True)
|
enable_open_in_editor=True)
|
||||||
load_xonsh_bindings(self.key_bindings_manager)
|
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):
|
def cmdloop(self, intro=None):
|
||||||
"""Enters a loop that reads and execute input from user."""
|
"""Enters a loop that reads and execute input from user."""
|
||||||
if intro:
|
if intro:
|
||||||
|
|
Loading…
Add table
Reference in a new issue