Skip to content

Commit

Permalink
add completion settings and improve completion bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
marskar committed Oct 17, 2020
1 parent f5bad69 commit 466f36f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 47 deletions.
108 changes: 61 additions & 47 deletions radian/key_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import re
import os

from prompt_toolkit.completion import CompleteEvent
from prompt_toolkit.application.current import get_app
from prompt_toolkit.application.run_in_terminal import run_in_terminal
from prompt_toolkit.eventloop import From, ensure_future
Expand Down Expand Up @@ -413,78 +412,93 @@ def is_callable(text=""):
return False

@Condition
def accot():
def auto_complete_commit_on_tab():
return settings.auto_complete_commit_on_tab

@Condition
def auto_complete_commit_top_option_on_enter():
return settings.auto_complete_commit_top_option_on_enter

@Condition
def auto_complete_commit_top_option_on_tab():
return settings.auto_complete_commit_top_option_on_tab

@Condition
def auto_complete_commit_only_option_on_tab():
return settings.auto_complete_commit_only_option_on_tab

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

# apply selected completion
@handle('c-j', filter=focused_insert & completion_is_selected & accot)
@handle("enter", filter=focused_insert & completion_is_selected & accot)
# apply selected completion option with enter
@handle('c-j', filter=focused_insert & completion_is_selected)
@handle("enter", filter=focused_insert & completion_is_selected)
def _(event):
b = event.current_buffer
text = b.text
completion = b.complete_state.current_completion
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
b.insert_text("()")
b.cursor_left()
if text == b.text:
event.cli.key_processor.feed_multiple(alt_enter)
b.apply_completion(completion)
if settings.auto_complete_function_parentheses:
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
b.insert_text("()")
b.cursor_left()

# apply first completion option when completion menu is showing
@handle('c-j', filter=focused_insert & shown_not_selected & accot)
@handle("enter", filter=focused_insert & shown_not_selected & accot)
# apply selected completion option with tab
@handle("tab", filter=focused_insert & completion_is_selected & auto_complete_commit_on_tab)
@handle("c-space", filter=focused_insert & completion_is_selected & auto_complete_commit_on_tab)
def _(event):
b = event.current_buffer
text = b.text
b.complete_next()
completion = b.complete_state.current_completion
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
b.insert_text("()")
b.cursor_left()
if text == b.text:
event.cli.key_processor.feed_multiple(alt_enter)
b.apply_completion(completion)
if settings.auto_complete_function_parentheses:
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
b.insert_text("()")
b.cursor_left()

# apply completion if there is only one option, otherwise start completion
@handle("tab", filter=focused_insert & ~has_completions & accot)
@handle("c-space", filter=focused_insert & ~has_completions & accot)
# apply first completion option with enter when completion menu is showing
@handle('c-j', filter=focused_insert & shown_not_selected & auto_complete_commit_top_option_on_enter)
@handle("enter", filter=focused_insert & shown_not_selected & auto_complete_commit_top_option_on_enter)
def _(event):
b = event.current_buffer
complete_event = CompleteEvent(completion_requested=True)
completions = list(b.completer.get_completions(b.document, complete_event))
if len(completions) == 1:
completion = completions[0]
b.apply_completion(completion)
text = b.text
completion = b.complete_state.completions[0]
b.apply_completion(completion)
if settings.auto_complete_function_parentheses:
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
b.insert_text("()")
b.cursor_left()
else:
b.start_completion(insert_common_part=True)
if text == b.text:
event.cli.key_processor.feed_multiple(alt_enter)

# apply first completion option if completion menu is showing
@handle("tab", filter=focused_insert & shown_not_selected & accot)
@handle("c-space", filter=focused_insert & shown_not_selected & accot)
# apply first completion option with tab if completion menu is showing
@handle("tab", filter=focused_insert & shown_not_selected & auto_complete_commit_top_option_on_tab)
@handle("c-space", filter=focused_insert & shown_not_selected & auto_complete_commit_top_option_on_tab)
def _(event):
b = event.current_buffer
b.complete_next()
completion = b.complete_state.current_completion
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
b.insert_text("()")
b.cursor_left()
completion = b.complete_state.completions[0]
b.apply_completion(completion)
if settings.auto_complete_function_parentheses:
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
b.insert_text("()")
b.cursor_left()

# apply selected completion option
@handle("tab", filter=focused_insert & completion_is_selected & accot)
@handle("c-space", filter=focused_insert & completion_is_selected & accot)
# apply completion if there is only one option, otherwise start completion
@handle("tab", filter=focused_insert & ~has_completions & auto_complete_commit_only_option_on_tab)
@handle("c-space", filter=focused_insert & ~has_completions & auto_complete_commit_only_option_on_tab)
def _(event):
b = event.current_buffer
completion = b.complete_state.current_completion
b.apply_completion(completion)
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
b.insert_text("()")
b.cursor_left()
b.start_completion()
completions = b.complete_state.completions
if len(completions) == 1:
completion = completions[0]
b.apply_completion(completion)
if settings.auto_complete_function_parentheses:
if is_callable(completion.text) or is_callable(b.document.get_word_under_cursor()):
b.insert_text("()")
b.cursor_left()
else:
b.start_completion(insert_common_part=True)

# cancel completion
@handle('c-c', filter=default_focused & has_completions)
Expand Down
4 changes: 4 additions & 0 deletions radian/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def load(self):
self._load_setting("auto_suggest", True, bool)
self._load_setting("emacs_bindings_in_vi_insert_mode", True, bool)
self._load_setting("auto_complete_commit_on_tab", False, bool)
self._load_setting("auto_complete_commit_top_option_on_tab", False, bool)
self._load_setting("auto_complete_commit_only_option_on_tab", False, bool)
self._load_setting("auto_complete_commit_top_option_on_enter", False, bool)
self._load_setting("auto_complete_function_parentheses", False, bool)
self._load_setting("editing_mode", "emacs")
self._load_setting("color_scheme", "native")
self._load_setting("auto_match", True, bool)
Expand Down

0 comments on commit 466f36f

Please sign in to comment.