-
Notifications
You must be signed in to change notification settings - Fork 280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Include parentheses when tab completing functions #387
Comments
I implemented this feature in my Click here to see the code:from jedi import Interpreter
from prompt_toolkit import filters
from prompt_toolkit.enums import DEFAULT_BUFFER
from prompt_toolkit.completion import CompleteEvent
def configure(repl):
focused_insert = filters.has_focus(DEFAULT_BUFFER) & filters.vi_insert_mode
focused_insert_and_completion = focused_insert & filters.has_completions
def is_callable(text=""):
completions = Interpreter(text, [locals()]).complete()
match = next((i for i in completions if i.name == text), None)
return match.type in ("class", "function") if match else None
@repl.add_key_binding("tab", filter=focused_insert)
@repl.add_key_binding("c-space", filter=focused_insert)
def _(event):
b = event.current_buffer
if b.completer is None:
return
complete_event = CompleteEvent(completion_requested=True)
completions = list(b.completer.get_completions(b.document, complete_event))
if len(completions) == 1: # only one possible completion
completion = completions[0]
if completion.start_position:
b.delete_before_cursor(-completion.start_position)
elif not b.complete_state: # no completion menu
b.start_completion(insert_common_part=True)
completion = None
elif b.complete_state.current_completion: # completion menu and selection
completion = b.complete_state.current_completion
else: # completion menu, but no selection
b.complete_next()
completion = b.complete_state.current_completion
if completion:
b.apply_completion(completion)
if is_callable(completion.text):
b.insert_text("()")
b.cursor_left()
@repl.add_key_binding("enter", filter=focused_insert_and_completion)
def _(event):
b = event.current_buffer
if b.completer is None:
return
if b.complete_state.current_completion: # completion menu and selection
completion = b.complete_state.current_completion
else: # completion menu, but no selection
b.complete_next()
completion = b.complete_state.current_completion
if completion:
b.apply_completion(completion)
if is_callable(completion.text):
b.insert_text("()")
b.cursor_left() Note: This also completely changes how completion works:
Enter:
I decided the best solution in the end will be to use |
I submitted a pull request that implements the changes described above: #390. |
When completing a function,
ptpython does not add parentheses after the function name.
I would like tab completion of functions to also add parentheses and put my cursor inside the parentheses:
Other examples:
Tab completion in IPython shows functions with parentheses in the completion menu:
but when I tab complete a function the parentheses are not included:
It would be better if IPython would add parentheses and put my cursor inside the parentheses:
Adding parentheses to completed functions is the default behavior in
And it can be enabled in the Python language server protocol (LSP).
I think this feature would be widely appreciated by users and should be the default behavior for tab completion of functions. Some users may prefer the current behavior, so ideally it would be controlled via a setting in
ptpython/config.py
.The text was updated successfully, but these errors were encountered: