From 3d283b2ca43bb077f52c48fec5e4870cd314b4e3 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Thu, 3 Nov 2022 20:58:54 -0500 Subject: Escape filenames in command completion This changes the completion items to be rendered with shellword escaping, so a file `a b.txt` is rendered as `a\ b.txt` which matches how it should be inputted. --- helix-core/src/shellwords.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'helix-core/src') diff --git a/helix-core/src/shellwords.rs b/helix-core/src/shellwords.rs index 3375bef1..7742896c 100644 --- a/helix-core/src/shellwords.rs +++ b/helix-core/src/shellwords.rs @@ -1,9 +1,9 @@ use std::borrow::Cow; /// Auto escape for shellwords usage. -pub fn escape(input: &str) -> Cow<'_, str> { +pub fn escape(input: Cow) -> Cow { if !input.chars().any(|x| x.is_ascii_whitespace()) { - Cow::Borrowed(input) + input } else if cfg!(unix) { Cow::Owned(input.chars().fold(String::new(), |mut buf, c| { if c.is_ascii_whitespace() { @@ -311,15 +311,15 @@ mod test { #[test] #[cfg(unix)] fn test_escaping_unix() { - assert_eq!(escape("foobar"), Cow::Borrowed("foobar")); - assert_eq!(escape("foo bar"), Cow::Borrowed("foo\\ bar")); - assert_eq!(escape("foo\tbar"), Cow::Borrowed("foo\\\tbar")); + assert_eq!(escape("foobar".into()), Cow::Borrowed("foobar")); + assert_eq!(escape("foo bar".into()), Cow::Borrowed("foo\\ bar")); + assert_eq!(escape("foo\tbar".into()), Cow::Borrowed("foo\\\tbar")); } #[test] #[cfg(windows)] fn test_escaping_windows() { - assert_eq!(escape("foobar"), Cow::Borrowed("foobar")); - assert_eq!(escape("foo bar"), Cow::Borrowed("\"foo bar\"")); + assert_eq!(escape("foobar".into()), Cow::Borrowed("foobar")); + assert_eq!(escape("foo bar".into()), Cow::Borrowed("\"foo bar\"")); } } -- cgit v1.2.3-70-g09d2