helix icon indicating copy to clipboard operation
helix copied to clipboard

Add command expansions %key{body}

Open ksdrar opened this issue 1 year ago • 65 comments

Based on: https://github.com/helix-editor/helix/pull/3393

ksdrar avatar May 06 '23 14:05 ksdrar

%sh{body} uses shell_impl from helix-term/src/commands.rs which cannot be accessed from helix-view, what should be done in this case?

ksdrar avatar May 08 '23 14:05 ksdrar

So I think we can formalize expansion a little by moving the replacement function into the editor module in helix-view.

I personally would prefer this as a separate module so helix-view/src/editor/substitute.rs I think editors.rs is already a bit too much of a kitchen sink and this is pretty orthogonal so it makes more sense as its own module IMO

pascalkuthe avatar May 13 '23 14:05 pascalkuthe

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>(...

ksdrar avatar May 13 '23 15:05 ksdrar

yeah exactly :+1:

pascalkuthe avatar May 13 '23 15:05 pascalkuthe

Awesome, looking forward to this! Can we document this somewhere, esp. the exact variable names supported?

Also, should this be marked as closing https://github.com/helix-editor/helix/issues/3134?

jlebon avatar May 13 '23 16:05 jlebon

Checking out https://github.com/helix-editor/helix/pull/7090 I think it's a good idea for users to be able to expand the variables while in :command mode and maybe even add the variables to the list of suggestions for autocompletion. What do you think?

ksdrar avatar May 21 '23 23:05 ksdrar

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 + .

https://github.com/helix-editor/helix/assets/22417151/25befd25-8e7f-4195-b458-3f7b9c4d6c6a

ksdrar avatar May 23 '23 01:05 ksdrar

@ksdrar Today I updated to use this new branch and this command is not working anymore for me.

s = ':open %sh{~/.dotfiles/support/pipe.rb switch_to_spec %sh{pwd} %val{filename}}'

It opens 4 buffers:

  1. %sh{~/.dotfiles/support/pipe.rb
  2. switch_to_spec
  3. ~/my-project-pwd
  4. }

danillos avatar Jun 22 '23 22:06 danillos

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?

I think it's better to leave it be %val{...} to avoid issues with something like %filename.bak or unintended replacements. Or maybe changing it to %{varname} to avoid val repetition.

ksdrar avatar Jun 23 '23 04:06 ksdrar

@ksdrar Hope you are well. Is this nearly ready to go? I think it just misses some docs and implementing/discussing the-mikedavis's suggested change?

David-Else avatar Aug 24 '23 19:08 David-Else

Hello @David-Else, I just pushed a commit to "cmd-exp-2" branch (it's rebased to latest changes) in my fork changing the expansion method and also uses the proposed %sh{} and %{varname} changes. Can you or someone else try it, please.

ksdrar avatar Aug 25 '23 04:08 ksdrar

@ksdrar I tried the latest pull request here with the :sh echo %val{filename} format and it works great. I use this script and the gh cli command to audition pull requests:

ghpr() { GH_FORCE_TTY=100% gh pr list --limit 300 |
    fzf --ansi --preview 'GH_FORCE_TTY=100% gh pr view {1}' --preview-window 'down,70%' --header-lines 3 |
    awk '{print $1}' |
    xargs gh pr checkout; }

I am afraid I don't know how to pull in your branch https://github.com/ksdrar/helix/tree/cmd-exp-2 using git commands so it creates a new branch in my helix source code repository on my local machine. If you could tell me I would be happy to try it out... or just push the changes here and I will test them :)

David-Else avatar Aug 25 '23 09:08 David-Else

@ksdrar I'll try it today. I'm a heavy user of this feature. But I'm using the previous branch, this one had some bugs the last time I tested, reported here

danillos avatar Aug 25 '23 12:08 danillos

@ksdrar I'll try it today. I'm a heavy user of this feature. But I'm using the previous branch, this one had some bugs the last time I tested, reported here

Thanks!, I tried your command and it works fine (I just tried parsing it as I obviously don't have your ruby script). But if you have any problem, report back please :)

ksdrar avatar Aug 25 '23 12:08 ksdrar

@ksdrar I tried the latest pull request here with the :sh echo %val{filename} format and it works great. I use this script and the gh cli command to audition pull requests:

ghpr() { GH_FORCE_TTY=100% gh pr list --limit 300 |
    fzf --ansi --preview 'GH_FORCE_TTY=100% gh pr view {1}' --preview-window 'down,70%' --header-lines 3 |
    awk '{print $1}' |
    xargs gh pr checkout; }

I am afraid I don't know how to pull in your branch https://github.com/ksdrar/helix/tree/cmd-exp-2 using git commands so it creates a new branch in my helix source code repository on my local machine. If you could tell me I would be happy to try it out... or just push the changes here and I will test them :)

You may try the branch using the following commands. git remote add ksdrar https://github.com/ksdrar/helix git pull ksdrar cmd-exp-2 git switch cmd-exp-2

Or if you don't want to create a branch in your local machine (not recommended). git remote add ksdrar https://github.com/ksdrar/helix git merge ksdrar/cmd-exp-2

ksdrar avatar Aug 25 '23 12:08 ksdrar

@ksdrar I followed your instructions and it works great! For example, this works perfectly:

:sh echo %{filename}:%{linenumber}

@danillos I think you are better qualified to test the expansions are all correct as per your post https://github.com/helix-editor/helix/pull/3393#issuecomment-1520996787

David-Else avatar Aug 25 '23 15:08 David-Else

@ksdrar I did some tests and found these issues:

Issue 1: When executing :sh echo %sh{echo a} it works showing a but the same command in a keybind t = ':sh echo %sh{echo a}' it shows %sh{echo a}

Issue 2: :sh echo %sh{echo %{filename} %sh{pwd}} it works :sh echo %sh{echo %{filename} %sh{pwd} %{filename}} it doesn't works, it seems that any args after the inner %sh breaks it, because :sh echo %sh{echo %{filename} %{filename} %sh{pwd}} works.

danillos avatar Aug 25 '23 16:08 danillos

@danillos I just pushed a commit for the second issue. I tried and it was caused by not trimming the command output. Can you try it please.

About the first issue, the thing is the keybind arguments are parsed by separating the string and saving it as a vector, so the command arguments end up being ["%sh{echo", "a}"]. We would have to join the vector again and then separate it once more for it to work with the expansion. It was previously spotted by @the-mikedavis in https://github.com/helix-editor/helix/pull/6979#discussion_r1191574086. I think we should join them to be parsed.

ksdrar avatar Aug 26 '23 00:08 ksdrar

@ksdrar Confirmed, Issue 2 was fixed.

danillos avatar Aug 26 '23 02:08 danillos

@ksdrar Confirmed, Issue 2 was fixed.

Thanks!. I just pushed a commit addressing the first issue, it should work as expected now.

ksdrar avatar Aug 26 '23 03:08 ksdrar

@ksdrar Issue 1 was fixed too.

But I'm not allowed to save changes, when I attempt to :w, it shows it: CleanShot 2023-08-26 at 06 02 52@2x

danillos avatar Aug 26 '23 09:08 danillos

I have an issue with the latest cmd-exp-2 version. I can't use my shortcut Z = { Z = ":wq", Q = ":q!" }, it just says quit! takes no arguments, probably the same thing as https://github.com/helix-editor/helix/pull/6979#issuecomment-1694238823

David-Else avatar Aug 26 '23 12:08 David-Else

Yeah, I just pushed another commit. Now it should work fine

ksdrar avatar Aug 26 '23 13:08 ksdrar

Yeah, I just pushed another commit. Now it should work fine

Fixed!

David-Else avatar Aug 26 '23 13:08 David-Else

Can this be used in languages.toml? Something like formatter = { command = 'prettier', args = [ "%{filename}" ] } ? I can't really experiment with this until https://github.com/helix-editor/helix/pull/6846 is merged.

David-Else avatar Aug 26 '23 14:08 David-Else

Can this be used in languages.toml? Something like formatter = { command = 'prettier', args = [ "%{filename}" ] } ? I can't really experiment with this until #6846 is merged.

Not really. There's https://github.com/helix-editor/helix/pull/5626 and it's only being held back because the variable declaration syntax (%val{varname} or %{varname}, etc) is not set in stone yet afaik. I think once this PR is merged the formatter one will follow.

ksdrar avatar Aug 26 '23 14:08 ksdrar

@ksdrar, everything is working fine on the branch cmd-exp-2.

danillos avatar Aug 26 '23 14:08 danillos

Currently, filename provides the absolute path, while the dirname command gives the absolute path up until the filename. Is it possible to obtain just the filename? Wouldn't it be more logical for the filename to only provide the file name? We could easily create the absolute path by using dirname filename, and get the bonus of having access to just the file name. Just an idea, I am probably missing some context here.

David-Else avatar Aug 26 '23 15:08 David-Else

Is it possible to obtain just the filename?

You can use basename:

:sh basename %val{filename}

quantonganh avatar Aug 26 '23 15:08 quantonganh

I just added basename and selection variables with the latest commit to "cmd-exp-2". But I think renaming filename to filepath and basename to filename would make their meaning clearer for some users, what do you think?

ksdrar avatar Aug 27 '23 00:08 ksdrar