-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Add command expansions %key{body} #6979
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this PR we only touch typable commands but we will probably want to re-use expansion elsewhere (for example custom commands (#3393 (comment)) or exposing the file-type in formatter arguments (#5626)). So I think we can formalize expansion a little by moving the replacement function into the editor module in helix-view. I'd also like to see a few unit tests of the regex.
We could also add some integration tests if we add an echo command:
// helix-term/src/commands/typed.rs
fn echo(cx: &mut compositor::Context, args: &[Cow<str>], event: PromptEvent) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}
cx.editor.set_status(format!("{}", args.join(" ")));
Ok(())
}
(for example could be used like :echo %val{filename}
or :echo %sh{echo %{filename}}
)
%sh{body} uses |
helix-view/src/editor.rs
Outdated
space: '·', // U+00B7 | ||
space: '·', // U+00B7 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was recently addressed in #7013 so if you rebase then this line should disappear from the diff
There was a problem hiding this comment.
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 was changed, I'll save the file without formatting that line if you want
957413e
to
e27939f
Compare
I personally would prefer this as a separate module so |
Like this? // helix-view/src/editor.rs
mod variable_expansion;
pub use variable_expansion::expand_variables;
... // helix-view/src/editor/variable_expansion.rs
pub fn expand_variables<'a>(... |
yeah exactly 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍 Just missing some tests
Awesome, looking forward to this! Can we document this somewhere, esp. the exact variable names supported? Also, should this be marked as closing #3134? |
a36e2de
to
d2c6593
Compare
Checking out #7090 I think it's a good idea for users to be able to expand the variables while in |
I just pushed this feature into a new branch of my fork (ce-prompt). Input inside a Prompt (command_mode, file_picker, etc) can be expanded from variables to values using Ctrl + . Screen.Recording.2023-05-22.at.18.58.20.mov |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is looking good. @archseer had the idea of replacing the %val{varname}
syntax with just %varname
, for example %filename
, %dirname
, etc.. %sh{..}
would stay the same. I think Kakoune's syntax makes more sense for Kakoune which mostly integrates with scripting / shell. I don't forsee us needing other %X{..}
s other than maybe one for the plugin system language (though maybe this whole syntax/feature would change with the plugin system). What do you think?
helix-term/src/commands.rs
Outdated
.map(|arg| { | ||
helix_view::editor::expand_variables(cx.editor, arg).map(Cow::from) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.map(|arg| { | |
helix_view::editor::expand_variables(cx.editor, arg).map(Cow::from) | |
}) | |
.map(|arg| helix_view::editor::expand_variables(cx.editor, arg)) |
@ksdrar Today I updated to use this new branch and this command is not working anymore for me.
It opens 4 buffers:
|
I think it's better to leave it be |
d2c6593
to
ceb1b47
Compare
@pascalkuthe @archseer can we get another review? ❤️ (I'm sorry I'm super impatient for this one 😆) |
|
||
match cx.editor.expand_variables(&args) { | ||
Ok(args) => { | ||
let args = args.split_whitespace(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am pretty sure this is not correct. We allow quoting strings so arguments can contain whitespace and a simple split like this will not work. This seems like entirely the wrong place to handle command 3xpansion. This probably needs to be integrated into the command parser properly so that it can correctly interact with whitespace and escapes(without requiring quotes everywhere) so into the shellwords parser somehow. Altough that should likely not actually handle the expansion since it's also used for auto completion. This PR also doens't seem to handle completions at all (:open %{dirname}/
will not offer useful completions). It should be possible to to properly implement that altough it may be tricky (%sh should not be executed for completions tough)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pascalkuthe Is it possible for completions to be implemented in another PR?
What’s the status of this? This is the one feature that is keeping me from using helix, as I can’t bind any key maps to run custom commands involving the current file name. |
@carterols I'm also eagerly awaiting this, but depending on what you want to do, there is a "work around" in which you can parse the current file name via terminal if the one you're using has the functionality (I'm using kitty, the OP used wezterm) |
Hey all! I've been looking for this feature as well (thank you all that have put it together so far!!) and I'm wondering if what we have atm is a bit overkill? I'm thinking we already have the For example, One of the things I really like about helix is that it values a clean code base and doesn't "try to be everything for everyone". As such I'm wondering if we can simplify (or at least consider having a simpler solution and avoid some of the edge cases):
I'm also wondering if What do people think? I've only been using helix for a few months (and loving it! it has pretty much replaced like 12 years of vim) but this is my first time here so really sorry if I'm talking complete rubbish haha! 🙈 edit: one thing to also keep in mind: have we tested this on paths with spaces? As per Pascal's comment above, commands like |
How would I then do something like |
I understand this use case, but maybe this PR can focus on getting basic support for things like As a Helix user, I would much rather have features now that cover most use cases than trying to lump everything together in one PR that takes a long time to get approved. I really could use this feature because at work, I am forced to use Perforce as my SCM, and you have to manually checkout a file to make it writable via a |
I have a case that uses It uses
And I have many commands like this, using
|
The main question is what is delaying this very important feature getting merged? It is not much code, so maybe the implementation is still not meeting the core maintainers vision as it is? Would removing |
@danillos could you not get |
Yes, it works thanks, I don't know why I didn't it before, now I can change to:
|
There is also |
I think it's worth keeping the The main blockers on this seem to be the whitespace handling and completions. My view is that the WS handling is fine as is, especially since the last time I used Both points probably warrant TODOs, but I don't think should hold up and (otherwise) ready feature |
Hope this will be merge soon, it would be a great workaround for many plugin |
I am impatiently waiting for this PR be merged 👀 |
I know this PR has quite a few watchers and I don't want to create noise, but since it has been approved by @the-mikedavis for nearing six months and discussion has seemingly reached a stopping point, can someone say what it needs to be merged? Is the pending review from @archseer what the merge is pending? Or is it actually ready to go? |
Sorry guys, I've been busy with work but I'll try to find some time to finish this PR this week. I'll leave the completions feature for a future PR |
Hey @ksdrar, Would you mind if I (or someone else) try to finish this PR if you don't have time? It's almost ready to land according to the comments, and a lot of people are waiting for it to be completed. I'd like to help move it forward 😁 |
Yeah @tdaron! That would be awesome, thanks for your help! |
Yay ! What's currently missing on this to be ready? |
@pascalkuthe wants to integrate the expansion not in the execution phase but in parsing, but I think that it would require a simple AST or something like that to make the expansion lazy while keeping all the command parsing logic (white space handling, quotes, etc) as it is now. I don't have the path right now, but the structure ShellWords is the parsing phase iirc. |
Could you share your relevant Kitty workaround? |
@quatquatt sure, both my own versions of the wezterm / kitty implementations are here (the wezterm one is in a branch). I implemented them in ruby shell scripts because I don't do much shell scripting and it's way easier to read. |
Based on: #3393