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

Only complete appropriate arguments for typed commands. #5966

Conversation

askreet
Copy link
Contributor

@askreet askreet commented Feb 13, 2023

Edit: This doesn't fix #2772, it does enable the fix. It fixes my issue that was closed as duplicate, though.

I think there's a bigger change we can make here to address some other code duplication that looks something like setting argument types of commands within the TypableCommand struct and using that data both for completion and for verification of the number of arguments. Currently, some commands have checks for argument length and emit errors while others silently ignore trailing arguments.

I'm not thrilled about having variadic length completers represented as different enum variants but as we statically allocate all the command configuration I couldn't use a dynamic container. I've also left the implementation of arity 2 completers for set-option, even though I haven't implemented a completer for option values (I've got another patch I'm working on that will dive into option metadata for #5939 and can tie that in).

Edit: I realized after reading my own PR that I didn't need different variants for variadic completers, but I do need lifetime annotations to use slices. I'm probably irrationally allergic to lifetime annotations, but this does seem a bit cleaner.

Copy link
Member

@pascalkuthe pascalkuthe left a comment

Choose a reason for hiding this comment

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

This is a nice UX Improvement and has potential for even netter followup improvements. I like that this is quite small in scope. The diff is only a bit larger because you and to.change the comlleteres for all typable commands 👍

}

#[derive(Clone)]
pub enum CompletionConfig<'a> {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think an enum is an amazing fit here. NoComoletions is just a subset of positional and this approach doesn't allow commands which have a couple positional arguments followed by varags. Instead you should simply use a list of positional arguments followed by an optional vararg completed:

strict CommandSignature{
   postional_args: &'static [Option<Completer>],
   var_arcs: Option<Completer>
} 

You can get the compler for field n with:

self.positional_arts.get(n).unwrap_or(self.var_args)

Note that my suggestion uses a 'ststic slice. Tgis removes the lifetime argument you were unaopy with and is a good fit here because currently all tupable commands are static anyway and if we move away from that we need to heap allocate anyway for that cade a cow would be used)

},
TypableCommand {
name: "encoding",
aliases: &[],
doc: "Set encoding. Based on `https://encoding.spec.whatwg.org`.",
fun: set_encoding,
completer: None,
completer: CompletionConfig::NoCompletion,
Copy link
Member

Choose a reason for hiding this comment

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

Probably not in this this PR but in the future having an enum completer would be nice for cases like this. An autoamtic way to do that would also be useful for #4411 but I am not sure if serde allows that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I want to have more metadata about options in general, some discussion in #5939.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that should be left to future work, just something that came to mind while reviewing

@askreet
Copy link
Contributor Author

askreet commented Feb 18, 2023

Thanks for the tips @pascalkuthe, pushed up some updates. I think your suggestion is much cleaner, especially using 'static scope.

Copy link
Member

@pascalkuthe pascalkuthe left a comment

Choose a reason for hiding this comment

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

I had a couple more ideas to make the API even more compact

Comment on lines 38 to 41
positional_args: &'static [Option<Completer>],

// All remaining arguments will use this completion method, if set.
var_args: Option<Completer>,
Copy link
Member

Choose a reason for hiding this comment

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

Actually thinking about it more I don't think we need to use Option<Completer> here we can just use ui::completers::none instead.
This will allow creating some utility functions (see below) to make the API a bit cleaner.

Comment on lines 45 to 63
const fn none() -> Self {
Self {
positional_args: &[],
var_args: None,
}
}
Copy link
Member

@pascalkuthe pascalkuthe Feb 18, 2023

Choose a reason for hiding this comment

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

I think adding a couple more utility constructs will go a long way towards keeping the command list short and readable.

Suggested change
const fn none() -> Self {
Self {
positional_args: &[],
var_args: None,
}
}
const fn none() -> Self {
Self {
positional_args: &[],
var_args: completers::none,
}
}
const fn single(completer: &'static Completer) -> Self {
Self {
positional_args: slice::from_ref(completer),
var_args: completers::none,
}
}
const fn repeat(completer: Completer) -> Self {
Self {
positional_args: &[],
var_args: completer,
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You know it's funny I actually wrote basically the same thing first and wasn't sure if people would be comfortable with the layers of indirection. I agree and will put it back! :-)

@pascalkuthe pascalkuthe added C-enhancement Category: Improvements E-easy Call for participation: Experience needed to fix: Easy / not much S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 18, 2023
@askreet
Copy link
Contributor Author

askreet commented Feb 18, 2023

@pascalkuthe Updates ready! :)

@pascalkuthe
Copy link
Member

@pascalkuthe Updates ready! :)

Ah I actually mean that the positional_arguments should also be just a Completer instead of Option<Completer> would remove all the Some(..) wrapping (and you can always use completers:;none instead of None)

@askreet
Copy link
Contributor Author

askreet commented Feb 18, 2023 via email

pascalkuthe
pascalkuthe previously approved these changes Feb 19, 2023
Copy link
Member

@pascalkuthe pascalkuthe left a comment

Choose a reason for hiding this comment

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

LGTM now 👍

@pascalkuthe pascalkuthe added S-waiting-on-review Status: Awaiting review from a maintainer. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 19, 2023
@askreet askreet force-pushed the issue-2772/set-option-completion-arguments branch from 4a23125 to 09e210b Compare March 6, 2023 10:48
@archseer archseer merged commit 27aa919 into helix-editor:master Mar 14, 2023
@the-mikedavis the-mikedavis mentioned this pull request Mar 15, 2023
sagnibak pushed a commit to sagnibak/helix that referenced this pull request Mar 21, 2023
wes-adams pushed a commit to wes-adams/helix that referenced this pull request Jul 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Improvements E-easy Call for participation: Experience needed to fix: Easy / not much S-waiting-on-review Status: Awaiting review from a maintainer.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

set-option completion shown after first argument is set
3 participants