1
0
Fork 0

fixup! fixup! Handle a multi-line query on Enter key-press (fixes #1031)

This commit is contained in:
Owen Stephens 2019-10-16 22:26:25 +01:00
parent 6763433a68
commit 629a931aed
4 changed files with 17 additions and 11 deletions

View File

@ -15,6 +15,7 @@ Bug fixes:
* History navigation bindings in multiline queries (#1004) (Thanks: `Pedro Ferrari`_)
* Can't connect to pgbouncer database (#1093). (Thanks: `Irina Truong`_)
* Fix broken multi-line history search (#1031). (Thanks: `Owen Stephens`_)
* Fix slow typing/movement when multi-line query ends in a semicolon (#994). (Thanks: `Owen Stephens`_)
Internal:
---------

View File

@ -10,7 +10,7 @@ from prompt_toolkit.filters import (
has_selection,
)
from .pgbuffer import multi_line_buffer_should_be_handled
from .pgbuffer import buffer_should_be_handled
_logger = logging.getLogger(__name__)
@ -102,8 +102,7 @@ def pgcli_bindings(pgcli):
# history search, and one of several conditions are True
@kb.add(
"enter",
filter=~(completion_is_selected | is_searching)
& multi_line_buffer_should_be_handled(pgcli),
filter=~(has_completions | is_searching) & buffer_should_be_handled(pgcli),
)
def _(event):
event.current_buffer.validate_and_handle()

View File

@ -35,7 +35,7 @@ from prompt_toolkit.completion import DynamicCompleter, ThreadedCompleter
from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode
from prompt_toolkit.shortcuts import PromptSession, CompleteStyle
from prompt_toolkit.document import Document
from prompt_toolkit.filters import HasFocus, IsDone, Condition
from prompt_toolkit.filters import HasFocus, IsDone
from prompt_toolkit.lexers import PygmentsLexer
from prompt_toolkit.layout.processors import (
ConditionalProcessor,
@ -809,7 +809,11 @@ class PGCli(object):
],
auto_suggest=AutoSuggestFromHistory(),
tempfile_suffix=".sql",
multiline=Condition(lambda: self.multi_line),
# N.b. pgcli's multi-line mode controls submit-on-Enter (which
# overrides the default behaviour of prompt_toolkit) and is
# distinct from prompt_toolkit's multiline mode here, which
# controls layout/display of the prompt/buffer
multiline=True,
history=history,
completer=ThreadedCompleter(DynamicCompleter(lambda: self.completer)),
complete_while_typing=True,

View File

@ -14,17 +14,19 @@ def _is_complete(sql):
"""
Returns True if the input mode is multi-line using psql mode, and the main
buffer's contents indicate that it should be handled, False otherwise. This
method is required since by default prompt_toolkit would not handle a buffer on
Enter keypress when in multi-line mode.
Returns True if the buffer contents should be handled (i.e. the query/command
executed) immediately. This is necessary as we use prompt_toolkit in multiline
mode, which by default will insert new lines on Enter.
"""
def multi_line_buffer_should_be_handled(pgcli):
def buffer_should_be_handled(pgcli):
@Condition
def cond():
if not pgcli.multi_line or pgcli.multiline_mode == "safe":
if not pgcli.multi_line:
return True
if pgcli.multiline_mode == "safe":
return False
doc = get_app().layout.get_buffer_by_name(DEFAULT_BUFFER).document