From 0a655d9978ebeafb2221ddd3509db66f7a6cdd02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Daron?= Date: Wed, 31 Jul 2024 14:34:31 +0200 Subject: [PATCH] correct handling of commands without args inside of MappableCommands --- helix-term/src/commands.rs | 41 ++++++++++++--------- helix-term/src/commands/typed.rs | 2 +- helix-view/src/editor/variable_expansion.rs | 10 +++-- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 661c10560..564e46e70 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -217,26 +217,31 @@ impl MappableCommand { pub fn execute(&self, cx: &mut Context) { match &self { Self::Typable { name, args, doc: _ } => { - let mut args: Vec> = args.iter().map(Cow::from).collect(); - let joined_args = vec![Cow::from(args.join(" "))]; - if let Ok(expanded_args) = cx.editor.expand_variables(&joined_args) { - args = expanded_args - .first() - .unwrap() - .split(' ') - .map(Cow::from) - .collect(); - if let Some(command) = typed::TYPABLE_COMMAND_MAP.get(name.as_str()) { - let mut cx = compositor::Context { - editor: cx.editor, - jobs: cx.jobs, - scroll: None, - }; - - if let Err(e) = (command.fun)(&mut cx, &args[..], PromptEvent::Validate) { - cx.editor.set_error(format!("{}", e)); + let args: Vec> = args.iter().map(Cow::from).collect(); + let mut joined_args = args.join(" "); + let expanded_args = match args.len() { + 0 => vec![], + _ => { + if let Ok(expanded) = cx.editor.expand_variable_in_string(&joined_args) { + joined_args = expanded.to_string(); + joined_args.split(' ').map(Cow::from).collect() + } else { + args } } + }; + if let Some(command) = typed::TYPABLE_COMMAND_MAP.get(name.as_str()) { + let mut cx = compositor::Context { + editor: cx.editor, + jobs: cx.jobs, + scroll: None, + }; + + if let Err(e) = + (command.fun)(&mut cx, &expanded_args[..], PromptEvent::Validate) + { + cx.editor.set_error(format!("{}", e)); + } } } Self::Static { fun, .. } => (fun)(cx), diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 50d63fdd7..2e56382e1 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -3244,7 +3244,7 @@ pub(super) fn command_mode(cx: &mut Context) { let shellwords = Shellwords::from(input); let words = shellwords.words().to_vec(); let args = if event == PromptEvent::Validate { - match cx.editor.expand_variables(&words) { + match cx.editor.expand_variables_in_vec(&words) { Ok(args) => args, Err(e) => { cx.editor.set_error(format!("{}", e)); diff --git a/helix-view/src/editor/variable_expansion.rs b/helix-view/src/editor/variable_expansion.rs index f79d24c0e..675c9a5a0 100644 --- a/helix-view/src/editor/variable_expansion.rs +++ b/helix-view/src/editor/variable_expansion.rs @@ -2,20 +2,20 @@ use crate::Editor; use std::borrow::Cow; impl Editor { - pub fn expand_variables<'a>( + pub fn expand_variables_in_vec<'a>( &self, args: &'a Vec>, ) -> anyhow::Result>> { let mut output = Vec::with_capacity(args.len()); for arg in args { - if let Ok(s) = self.expand_arg(arg) { + if let Ok(s) = self.expand_variable_in_string(arg) { output.push(s); } } Ok(output) } - fn expand_arg<'a>(&self, input: &'a str) -> anyhow::Result> { + pub fn expand_variable_in_string<'a>(&self, input: &'a str) -> anyhow::Result> { let (view, doc) = current_ref!(self); let shell = &self.config().shell; @@ -94,7 +94,9 @@ impl Editor { } if let Some(o) = output.as_mut() { - let body = self.expand_arg(&input[index + 4..end])?; + let body = self.expand_variable_in_string( + &input[index + 4..end], + )?; let output = tokio::task::block_in_place(move || { helix_lsp::block_on(async move {