Skip to content

Commit 52ba013

Browse files
committed
add completion settings and improve completion bindings
1 parent f5bad69 commit 52ba013

File tree

2 files changed

+64
-50
lines changed

2 files changed

+64
-50
lines changed

radian/key_bindings.py

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import re
33
import os
44

5-
from prompt_toolkit.completion import CompleteEvent
65
from prompt_toolkit.application.current import get_app
76
from prompt_toolkit.application.run_in_terminal import run_in_terminal
87
from prompt_toolkit.eventloop import From, ensure_future
@@ -413,78 +412,89 @@ def is_callable(text=""):
413412
return False
414413

415414
@Condition
416-
def accot():
417-
return settings.auto_complete_commit_on_tab
415+
def auto_complete_selected_option_on_tab():
416+
return settings.auto_complete_selected_option_on_tab
417+
418+
@Condition
419+
def auto_complete_top_option_on_enter():
420+
return settings.auto_complete_top_option_on_enter
421+
422+
@Condition
423+
def auto_complete_top_option_on_tab():
424+
return settings.auto_complete_top_option_on_tab
425+
426+
@Condition
427+
def auto_complete_only_option_on_tab():
428+
return settings.auto_complete_only_option_on_tab
418429

419430
insert_mode = vi_insert_mode | emacs_insert_mode
420431
focused_insert = insert_mode & has_focus(DEFAULT_BUFFER)
421432
shown_not_selected = has_completions & ~completion_is_selected
422-
alt_enter = [KeyPress(Keys.Escape), KeyPress(Keys.Enter)]
423433

424-
# apply selected completion
425-
@handle('c-j', filter=focused_insert & completion_is_selected & accot)
426-
@handle("enter", filter=focused_insert & completion_is_selected & accot)
434+
# apply selected completion option with enter
435+
@handle('c-j', filter=focused_insert & completion_is_selected)
436+
@handle("enter", filter=focused_insert & completion_is_selected)
427437
def _(event):
428438
b = event.current_buffer
429-
text = b.text
430439
completion = b.complete_state.current_completion
431-
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
432-
b.insert_text("()")
433-
b.cursor_left()
434-
if text == b.text:
435-
event.cli.key_processor.feed_multiple(alt_enter)
440+
b.apply_completion(completion)
441+
if settings.auto_complete_function_parentheses:
442+
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
443+
b.insert_text("()")
444+
b.cursor_left()
436445

437-
# apply first completion option when completion menu is showing
438-
@handle('c-j', filter=focused_insert & shown_not_selected & accot)
439-
@handle("enter", filter=focused_insert & shown_not_selected & accot)
446+
# apply selected completion option with tab
447+
@handle("tab", filter=focused_insert & completion_is_selected & auto_complete_selected_option_on_tab)
448+
@handle("c-space", filter=focused_insert & completion_is_selected & auto_complete_selected_option_on_tab)
440449
def _(event):
441450
b = event.current_buffer
442-
text = b.text
443-
b.complete_next()
444451
completion = b.complete_state.current_completion
445-
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
446-
b.insert_text("()")
447-
b.cursor_left()
448-
if text == b.text:
449-
event.cli.key_processor.feed_multiple(alt_enter)
452+
b.apply_completion(completion)
453+
if settings.auto_complete_function_parentheses:
454+
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
455+
b.insert_text("()")
456+
b.cursor_left()
450457

451-
# apply completion if there is only one option, otherwise start completion
452-
@handle("tab", filter=focused_insert & ~has_completions & accot)
453-
@handle("c-space", filter=focused_insert & ~has_completions & accot)
458+
# apply first completion option with enter when completion menu is showing
459+
@handle('c-j', filter=focused_insert & shown_not_selected & auto_complete_top_option_on_enter)
460+
@handle("enter", filter=focused_insert & shown_not_selected & auto_complete_top_option_on_enter)
454461
def _(event):
455462
b = event.current_buffer
456-
complete_event = CompleteEvent(completion_requested=True)
457-
completions = list(b.completer.get_completions(b.document, complete_event))
458-
if len(completions) == 1:
459-
completion = completions[0]
460-
b.apply_completion(completion)
463+
completion = b.complete_state.completions[0]
464+
b.apply_completion(completion)
465+
if settings.auto_complete_function_parentheses:
461466
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
462467
b.insert_text("()")
463468
b.cursor_left()
464-
else:
465-
b.start_completion(insert_common_part=True)
466469

467-
# apply first completion option if completion menu is showing
468-
@handle("tab", filter=focused_insert & shown_not_selected & accot)
469-
@handle("c-space", filter=focused_insert & shown_not_selected & accot)
470+
# apply first completion option with tab if completion menu is showing
471+
@handle("tab", filter=focused_insert & shown_not_selected & auto_complete_top_option_on_tab)
472+
@handle("c-space", filter=focused_insert & shown_not_selected & auto_complete_top_option_on_tab)
470473
def _(event):
471474
b = event.current_buffer
472-
b.complete_next()
473-
completion = b.complete_state.current_completion
474-
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
475-
b.insert_text("()")
476-
b.cursor_left()
475+
completion = b.complete_state.completions[0]
476+
b.apply_completion(completion)
477+
if settings.auto_complete_function_parentheses:
478+
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
479+
b.insert_text("()")
480+
b.cursor_left()
477481

478-
# apply selected completion option
479-
@handle("tab", filter=focused_insert & completion_is_selected & accot)
480-
@handle("c-space", filter=focused_insert & completion_is_selected & accot)
482+
# apply completion if there is only one option, otherwise start completion
483+
@handle("tab", filter=focused_insert & ~has_completions & auto_complete_only_option_on_tab)
484+
@handle("c-space", filter=focused_insert & ~has_completions & auto_complete_only_option_on_tab)
481485
def _(event):
482486
b = event.current_buffer
483-
completion = b.complete_state.current_completion
484-
b.apply_completion(completion)
485-
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
486-
b.insert_text("()")
487-
b.cursor_left()
487+
b.start_completion()
488+
completions = b.complete_state.completions
489+
if len(completions) == 1:
490+
completion = completions[0]
491+
b.apply_completion(completion)
492+
if settings.auto_complete_function_parentheses:
493+
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
494+
b.insert_text("()")
495+
b.cursor_left()
496+
else:
497+
b.start_completion(insert_common_part=True)
488498

489499
# cancel completion
490500
@handle('c-c', filter=default_focused & has_completions)

radian/settings.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ def _load_prompt(self):
3838
def load(self):
3939
self._load_setting("auto_suggest", True, bool)
4040
self._load_setting("emacs_bindings_in_vi_insert_mode", True, bool)
41-
self._load_setting("auto_complete_commit_on_tab", False, bool)
41+
self._load_setting("auto_complete_selected_option_on_tab", False, bool)
42+
self._load_setting("auto_complete_top_option_on_tab", False, bool)
43+
self._load_setting("auto_complete_only_option_on_tab", False, bool)
44+
self._load_setting("auto_complete_top_option_on_enter", False, bool)
45+
self._load_setting("auto_complete_function_parentheses", False, bool)
4246
self._load_setting("editing_mode", "emacs")
4347
self._load_setting("color_scheme", "native")
4448
self._load_setting("auto_match", True, bool)

0 commit comments

Comments
 (0)