Skip to content
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

Provide option to have tab apply completion #229

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mskar
Copy link

@mskar mskar commented Oct 10, 2020

In #224, I requested a feature that adds parentheses after functions completed with Tab or Enter.

I decided the best solution will be to use rchitect to identify functions based on the string returned by the completer.

This pull request implements the feature I requested (function completion with added parentheses), but also strives to make autocompletion work like in RStudio or the VSCode R extension. In VSCode and RStudio, tab applies completion instead of cycling through completion options, like in bash or zsh.

Currently, this feature is disabled by default, but it has to be enabled by adding

options(radian.auto_complete_commit_on_tab = TRUE)

to .radian_profile.

For more information, please take a look at this comment in #387.

To install radian with the tab_apply_completion feature, run
python -m pip install git+https://github.com/mskar/radian.git@tab_apply_completion

Note: This also completely changes how completion works:
Tab:

  • does not cycle thru completion options and instead
  • accepts completion if there is only one completion option
  • starts completion if there are multiple options and the completion menu is not showing
  • accepts the selected completion option if an option is selected
  • accepts the first completion option if the completion menu is showing and no option is selected

Enter:

  • accepts the selected completion option if an option is selected
  • accepts the first completion option if the completion menu is showing and no option is selected

TLDR:

previous behavior:

  • tab cycles completion
  • function completion does not add parentheses

proposed behavior:

  • tab applies completion
  • function completion adds parentheses

Changes:

Changed: when triggering completion with Tab, insert text that all options have in common
Changed: use Tab to accept only option or accept selected
Changed: do not move through options with not Tab and Shift-Tab
Unchanged: use Enter to accept if completion menu is showing
Unchanged: move through options with c-n and c-p
Unchanged: cancel completion with c-g

@randy3k
Copy link
Owner

randy3k commented Oct 10, 2020

I don't mind having the feature of committing completion with tab, but I don't think it is a good idea to set it as default because it is a breaking change. By the way, could you rename the setting as auto_complete_commit_on_tab which is the setting name inspired by Sublime Text?

@randy3k
Copy link
Owner

randy3k commented Oct 10, 2020

By the way, thank you for the contribution.

try:
return rcopy(rcall("class", reval(text))) == "function"
except:
return False
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is a robust way to detect if a token is a function. We may need to poke into how the token is determined: https://github.com/randy3k/rchitect/blob/master/rchitect/completion.py#L18

Copy link
Author

@mskar mskar Oct 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this part of the code could be improved, but so far the parentheses are added without fail. In other words, I have not found a situation in which this doesn't work.
I can't find much in the way of documentation on completeToken. It is used by the completeme package. I looked at the source code for completeToken and it seems like completeToken guesses which function it is inside for argument completion, but I don't know how this might help us to identify the token as a function.
I'm hoping that you might have some ideas regarding the best way to determine if a token is a function😄

@mskar
Copy link
Author

mskar commented Oct 11, 2020

@randy3k, thanks for the great feedback.
With 65210ee, the feature is no longer enabled by default and the setting is renamed to auto_complete_commit_on_tab.
If there are any other changes that need to be made, please let me know.

@mskar
Copy link
Author

mskar commented Oct 11, 2020

@randy3k, thanks for merging in #227.
I pulled the latest changes from the master branch into this PR.

@randy3k
Copy link
Owner

randy3k commented Oct 12, 2020

I feel like tab to commit completion and adding parentheses after functions are two separate features. We should be able to enable one and disable the other and now you have tied the two features together.

@mskar
Copy link
Author

mskar commented Oct 12, 2020

Yes, I agree with you, @randy3k, it will be better to have two independent features controlled by two independent settings.

I will edit this PR to

  • maintain the current behavior of tab if auto_complete_commit_on_tab is disabled
  • have tab to commit completion without adding parentheses after functions if auto_complete_commit_on_tab is enabled
  • include a new setting for adding parentheses after functions that works regardless of whether auto_complete_commit_on_tab is enabled or disabled.

What should the new setting be called?
Ideally, the two settings would have similar names, e.g.

  • auto_complete_commit_on_tab
  • auto_complete_function_parentheses

@randy3k
Copy link
Owner

randy3k commented Oct 14, 2020

The plan looks good to me.

@mskar
Copy link
Author

mskar commented Oct 17, 2020

@randy3k, I split the original feature into 5 independent features.
Each feature has its own setting to allow for granular control of completion behavior:

  1. auto_complete_function_parentheses:
  • Tab: Unchanged unless combined with one of the settings that end in _option_on_tab.
  • Enter: Accepts selected completion and add parentheses to functions
  1. auto_complete_selected_option_on_tab:
  • Tab: Accepts the selected completion option if an option is selected
  • Enter: Unchanged
  1. auto_complete_top_option_on_enter:
  • Tab: Unchanged
  • Enter: Accepts the first completion option if the completion menu is showing
  1. auto_complete_top_option_on_tab:
  • Tab: Accepts the first completion option if the completion menu is showing and no option is selected
  • Enter: Unchanged
  1. auto_complete_only_option_on_tab:
  • Tab: Accepts completion if there is only one completion option or starts completion if there are multiple options
  • Enter: Unchanged

These features can be combined.
Enabling all of the features makes completion behave like in RStudio.

Is 5 settings too many?
Would it be a good idea to add another setting that combined 2-5 (auto_complete_[selected,top,only]_option_on_[tab,enter])?

Please let me know if any of the settings should be renamed.

@mskar mskar force-pushed the tab_apply_completion branch 7 times, most recently from c202ef7 to 386c50f Compare October 17, 2020 23:37
@psobolewskiPhD
Copy link

psobolewskiPhD commented Feb 26, 2021

So I can't break the habit of trying tab for completions.
Any advice to apply these changes to my local radian or a local repo, but then how to compile it?

@randy3k randy3k force-pushed the master branch 2 times, most recently from b161529 to ac0e9cb Compare April 30, 2021 05:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants