Skip to content

Commit

Permalink
fix: command expansions work for all :commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Axlefublr committed Oct 14, 2024
1 parent 2cf7d51 commit e795601
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ It will look wonky if you do, so it makes the most sense to make a hotkey to tog

### Command expansions

When using actions and commands that let you execute a shell command (`:sh`, `shell_insert_output`, `shell_append_output`, `shell_pipe`, etc), you can now use command expansions.
Supported in: `shell_pipe`, `shell_pipe_to`, `shell_insert_output`, `shell_append_output`, `shell_replace_with_output` and **all** `:command`s. I'm not yet sure if the latter is a good idea, but feel free to `:cd %p` if you wish /j.

Example usage: `:sh echo %p`.

Expand All @@ -106,6 +106,11 @@ Here's what all command expansions would evaluate to:
|`%m` |`%m` |not an expansion, taken literally |
|`%%p` |`%p` |escaped using `%%` to be taken literally|

> [!CAUTION]
> The resulting path is not escaped in any way.
> If it contains spaces, that *may* be a problem, depending on what command you're using the expansion in.
> You may want to quote expansions, in that case.
### Harp

My favorite feature I had in nvim! \
Expand Down
13 changes: 9 additions & 4 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ impl MappableCommand {
jobs: cx.jobs,
scroll: None,
};
let (_, doc) = current!(cx.editor);
let args: Vec<Cow<str>> = args
.iter()
.map(|word| Cow::Owned(expand_expansions(word, doc)))
.collect();
if let Err(e) = (command.fun)(&mut cx, &args[..], PromptEvent::Validate) {
cx.editor.set_error(format!("{}", e));
}
Expand Down Expand Up @@ -5887,8 +5892,6 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id);

let cmd = expand_expansions(cmd, doc);

let mut changes = Vec::with_capacity(selection.len());
let mut ranges = SmallVec::with_capacity(selection.len());
let text = doc.text().slice(..);
Expand All @@ -5900,7 +5903,7 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
output.clone()
} else {
let input = range.slice(text);
match shell_impl(shell, &cmd, pipe.then(|| input.into())) {
match shell_impl(shell, cmd, pipe.then(|| input.into())) {
Ok(mut output) => {
if !input.ends_with("\n") && !output.is_empty() && output.ends_with('\n') {
output.pop();
Expand Down Expand Up @@ -5975,7 +5978,9 @@ fn shell_prompt(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBeha
return;
}

shell(cx, input, &behavior);
let (_, doc) = current!(cx.editor);
let input = expand_expansions(input, doc);
shell(cx, &input, &behavior);
},
);
}
Expand Down
7 changes: 5 additions & 2 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2380,9 +2380,7 @@ fn run_shell_command(
}

let shell = cx.editor.config().shell.clone();
let (_, doc) = current!(cx.editor);
let args = args.join(" ");
let args = expand_expansions(&args, doc);

let callback = async move {
let output = shell_impl_async(&shell, &args, None).await?;
Expand Down Expand Up @@ -3399,6 +3397,11 @@ pub(super) fn command_mode(cx: &mut Context) {
if let Some(cmd) = typed::TYPABLE_COMMAND_MAP.get(parts[0]) {
let shellwords = Shellwords::from(input);
let args = shellwords.words();
let (_, doc) = current!(cx.editor);
let args: Vec<Cow<str>> = args
.iter()
.map(|word| Cow::Owned(expand_expansions(word, doc)))
.collect();

if let Err(e) = (cmd.fun)(cx, &args[1..], event) {
cx.editor.set_error(format!("{}", e));
Expand Down

0 comments on commit e795601

Please sign in to comment.