Skip to content

Commit c202ef7

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

File tree

2 files changed

+66
-48
lines changed

2 files changed

+66
-48
lines changed

radian/key_bindings.py

Lines changed: 61 additions & 47 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,93 @@ def is_callable(text=""):
413412
return False
414413

415414
@Condition
416-
def accot():
415+
def auto_complete_selected_option_on_tab():
417416
return settings.auto_complete_commit_on_tab
418417

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
429+
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
422433
alt_enter = [KeyPress(Keys.Escape), KeyPress(Keys.Enter)]
423434

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)
435+
# apply selected completion option with enter
436+
@handle('c-j', filter=focused_insert & completion_is_selected)
437+
@handle("enter", filter=focused_insert & completion_is_selected)
427438
def _(event):
428439
b = event.current_buffer
429-
text = b.text
430440
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)
441+
b.apply_completion(completion)
442+
if settings.auto_complete_function_parentheses:
443+
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
444+
b.insert_text("()")
445+
b.cursor_left()
436446

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)
447+
# apply selected completion option with tab
448+
@handle("tab", filter=focused_insert & completion_is_selected & auto_complete_selected_option_on_tab)
449+
@handle("c-space", filter=focused_insert & completion_is_selected & auto_complete_selected_option_on_tab)
440450
def _(event):
441451
b = event.current_buffer
442-
text = b.text
443-
b.complete_next()
444452
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)
453+
b.apply_completion(completion)
454+
if settings.auto_complete_function_parentheses:
455+
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
456+
b.insert_text("()")
457+
b.cursor_left()
450458

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)
459+
# apply first completion option with enter when completion menu is showing
460+
@handle('c-j', filter=focused_insert & shown_not_selected & auto_complete_top_option_on_enter)
461+
@handle("enter", filter=focused_insert & shown_not_selected & auto_complete_top_option_on_enter)
454462
def _(event):
455463
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)
464+
text = b.text
465+
completion = b.complete_state.completions[0]
466+
b.apply_completion(completion)
467+
if settings.auto_complete_function_parentheses:
461468
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
462469
b.insert_text("()")
463470
b.cursor_left()
464-
else:
465-
b.start_completion(insert_common_part=True)
471+
if text == b.text:
472+
event.cli.key_processor.feed_multiple(alt_enter)
466473

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)
474+
# apply first completion option with tab if completion menu is showing
475+
@handle("tab", filter=focused_insert & shown_not_selected & auto_complete_top_option_on_tab)
476+
@handle("c-space", filter=focused_insert & shown_not_selected & auto_complete_top_option_on_tab)
470477
def _(event):
471478
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()
479+
completion = b.complete_state.completions[0]
480+
b.apply_completion(completion)
481+
if settings.auto_complete_function_parentheses:
482+
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
483+
b.insert_text("()")
484+
b.cursor_left()
477485

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)
486+
# apply completion if there is only one option, otherwise start completion
487+
@handle("tab", filter=focused_insert & ~has_completions & auto_complete_only_option_on_tab)
488+
@handle("c-space", filter=focused_insert & ~has_completions & auto_complete_only_option_on_tab)
481489
def _(event):
482490
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()
491+
b.start_completion()
492+
completions = b.complete_state.completions
493+
if len(completions) == 1:
494+
completion = completions[0]
495+
b.apply_completion(completion)
496+
if settings.auto_complete_function_parentheses:
497+
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
498+
b.insert_text("()")
499+
b.cursor_left()
500+
else:
501+
b.start_completion(insert_common_part=True)
488502

489503
# cancel completion
490504
@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)