correct handling of commands without args inside of MappableCommands

pull/11164/head
Théo Daron 4 months ago
parent 5bda25cf30
commit 0a655d9978

@ -217,15 +217,19 @@ impl MappableCommand {
pub fn execute(&self, cx: &mut Context) {
match &self {
Self::Typable { name, args, doc: _ } => {
let mut args: Vec<Cow<str>> = 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();
let args: Vec<Cow<str>> = 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,
@ -233,12 +237,13 @@ impl MappableCommand {
scroll: None,
};
if let Err(e) = (command.fun)(&mut cx, &args[..], PromptEvent::Validate) {
if let Err(e) =
(command.fun)(&mut cx, &expanded_args[..], PromptEvent::Validate)
{
cx.editor.set_error(format!("{}", e));
}
}
}
}
Self::Static { fun, .. } => (fun)(cx),
}
}

@ -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));

@ -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<Cow<'a, str>>,
) -> anyhow::Result<Vec<Cow<'a, str>>> {
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<Cow<'a, str>> {
pub fn expand_variable_in_string<'a>(&self, input: &'a str) -> anyhow::Result<Cow<'a, str>> {
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 {

Loading…
Cancel
Save