correct handling of commands without args inside of MappableCommands

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

@ -217,26 +217,31 @@ impl MappableCommand {
pub fn execute(&self, cx: &mut Context) { pub fn execute(&self, cx: &mut Context) {
match &self { match &self {
Self::Typable { name, args, doc: _ } => { Self::Typable { name, args, doc: _ } => {
let mut args: Vec<Cow<str>> = args.iter().map(Cow::from).collect(); let args: Vec<Cow<str>> = args.iter().map(Cow::from).collect();
let joined_args = vec![Cow::from(args.join(" "))]; let mut joined_args = args.join(" ");
if let Ok(expanded_args) = cx.editor.expand_variables(&joined_args) { let expanded_args = match args.len() {
args = expanded_args 0 => vec![],
.first() _ => {
.unwrap() if let Ok(expanded) = cx.editor.expand_variable_in_string(&joined_args) {
.split(' ') joined_args = expanded.to_string();
.map(Cow::from) joined_args.split(' ').map(Cow::from).collect()
.collect(); } else {
if let Some(command) = typed::TYPABLE_COMMAND_MAP.get(name.as_str()) { args
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));
} }
} }
};
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), Self::Static { fun, .. } => (fun)(cx),

@ -3244,7 +3244,7 @@ pub(super) fn command_mode(cx: &mut Context) {
let shellwords = Shellwords::from(input); let shellwords = Shellwords::from(input);
let words = shellwords.words().to_vec(); let words = shellwords.words().to_vec();
let args = if event == PromptEvent::Validate { let args = if event == PromptEvent::Validate {
match cx.editor.expand_variables(&words) { match cx.editor.expand_variables_in_vec(&words) {
Ok(args) => args, Ok(args) => args,
Err(e) => { Err(e) => {
cx.editor.set_error(format!("{}", e)); cx.editor.set_error(format!("{}", e));

@ -2,20 +2,20 @@ use crate::Editor;
use std::borrow::Cow; use std::borrow::Cow;
impl Editor { impl Editor {
pub fn expand_variables<'a>( pub fn expand_variables_in_vec<'a>(
&self, &self,
args: &'a Vec<Cow<'a, str>>, args: &'a Vec<Cow<'a, str>>,
) -> anyhow::Result<Vec<Cow<'a, str>>> { ) -> anyhow::Result<Vec<Cow<'a, str>>> {
let mut output = Vec::with_capacity(args.len()); let mut output = Vec::with_capacity(args.len());
for arg in args { 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); output.push(s);
} }
} }
Ok(output) 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 (view, doc) = current_ref!(self);
let shell = &self.config().shell; let shell = &self.config().shell;
@ -94,7 +94,9 @@ impl Editor {
} }
if let Some(o) = output.as_mut() { 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 || { let output = tokio::task::block_in_place(move || {
helix_lsp::block_on(async move { helix_lsp::block_on(async move {

Loading…
Cancel
Save