Skip to content

Commit a1793b1

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

File tree

2 files changed

+65
-46
lines changed

2 files changed

+65
-46
lines changed

radian/key_bindings.py

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -413,78 +413,93 @@ def is_callable(text=""):
413413
return False
414414

415415
@Condition
416-
def accot():
416+
def auto_complete_commit_on_tab():
417417
return settings.auto_complete_commit_on_tab
418418

419+
@Condition
420+
def auto_complete_commit_top_option_on_enter():
421+
return settings.auto_complete_commit_top_option_on_enter
422+
423+
@Condition
424+
def auto_complete_commit_top_option_on_tab():
425+
return settings.auto_complete_commit_top_option_on_tab
426+
427+
@Condition
428+
def auto_complete_commit_only_option_on_tab():
429+
return settings.auto_complete_commit_only_option_on_tab
430+
419431
insert_mode = vi_insert_mode | emacs_insert_mode
420432
focused_insert = insert_mode & has_focus(DEFAULT_BUFFER)
421433
shown_not_selected = has_completions & ~completion_is_selected
422434
alt_enter = [KeyPress(Keys.Escape), KeyPress(Keys.Enter)]
423435

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

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

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)
460+
# apply first completion option with enter when completion menu is showing
461+
@handle('c-j', filter=focused_insert & shown_not_selected & auto_complete_commit_top_option_on_enter)
462+
@handle("enter", filter=focused_insert & shown_not_selected & auto_complete_commit_top_option_on_enter)
454463
def _(event):
455464
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)
465+
text = b.text
466+
completion = b.complete_state.completions[0]
467+
b.apply_completion(completion)
468+
if settings.auto_complete_function_parentheses:
461469
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
462470
b.insert_text("()")
463471
b.cursor_left()
464-
else:
465-
b.start_completion(insert_common_part=True)
472+
if text == b.text:
473+
event.cli.key_processor.feed_multiple(alt_enter)
466474

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

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

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

radian/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def load(self):
3939
self._load_setting("auto_suggest", True, bool)
4040
self._load_setting("emacs_bindings_in_vi_insert_mode", True, bool)
4141
self._load_setting("auto_complete_commit_on_tab", False, bool)
42+
self._load_setting("auto_complete_commit_top_option_on_tab", False, bool)
43+
self._load_setting("auto_complete_commit_only_option_on_tab", False, bool)
44+
self._load_setting("auto_complete_commit_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)