summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Davis2022-11-04 01:58:54 +0000
committerBlaž Hrastnik2022-11-07 04:38:16 +0000
commit3d283b2ca43bb077f52c48fec5e4870cd314b4e3 (patch)
treec95f608404ce5da069688eb0ccf9d196cadbcc31
parent1536a6528968f38adfac2e991b29006f5ded5968 (diff)
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.
-rw-r--r--helix-core/src/shellwords.rs14
-rw-r--r--helix-term/src/commands/typed.rs1
-rw-r--r--helix-term/src/ui/prompt.rs6
3 files changed, 9 insertions, 12 deletions
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<str>) -> Cow<str> {
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\""));
}
}
diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs
index 304b30f9..36080d39 100644
--- a/helix-term/src/commands/typed.rs
+++ b/helix-term/src/commands/typed.rs
@@ -2219,6 +2219,7 @@ pub(super) fn command_mode(cx: &mut Context) {
completer(editor, part)
.into_iter()
.map(|(range, file)| {
+ let file = shellwords::escape(file);
// offset ranges to input
let offset = input.len() - part.len();
let range = (range.start + offset)..;
diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs
index ca2872a7..51ef688d 100644
--- a/helix-term/src/ui/prompt.rs
+++ b/helix-term/src/ui/prompt.rs
@@ -1,6 +1,5 @@
use crate::compositor::{Component, Compositor, Context, Event, EventResult};
use crate::{alt, ctrl, key, shift, ui};
-use helix_core::shellwords;
use helix_view::input::KeyEvent;
use helix_view::keyboard::KeyCode;
use std::{borrow::Cow, ops::RangeFrom};
@@ -336,10 +335,7 @@ impl Prompt {
let (range, item) = &self.completion[index];
- // since we are using shellwords to parse arguments, make sure
- // that whitespace in files is properly escaped.
- let item = shellwords::escape(item);
- self.line.replace_range(range.clone(), &item);
+ self.line.replace_range(range.clone(), item);
self.move_end();
}