Romain Arnal 2025-06-16 10:17:25 -05:00 committed by GitHub
commit 4bfbd5f8b7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 8 deletions

View File

@ -210,7 +210,7 @@ pub fn get_truncated_path(path: impl AsRef<Path>) -> PathBuf {
fn path_component_regex(windows: bool) -> String {
// TODO: support backslash path escape on windows (when using git bash for example)
let space_escape = if windows { r"[\^`]\s" } else { r"[\\]\s" };
// partially baesd on what's allowed in an url but with some care to avoid
// partially based on what's allowed in an url but with some care to avoid
// false positives (like any kind of brackets or quotes)
r"[\w@.\-+#$%?!,;~&]|".to_owned() + space_escape
}

View File

@ -14,6 +14,14 @@ use url::Url;
use crate::handlers::completion::{item::CompletionResponse, CompletionItem, CompletionItems};
fn unescape_spaces(string: &str) -> String {
string.replace("\\ ", " ").to_owned()
}
fn escape_spaces(string: &str) -> String {
string.replace(' ', "\\ ").to_owned()
}
pub(crate) fn path_completion(
selection: Selection,
doc: &Document,
@ -32,14 +40,14 @@ pub(crate) fn path_completion(
let (dir_path, typed_file_name) =
get_path_suffix(line_until_cursor, false).and_then(|matched_path| {
let matched_path = Cow::from(matched_path);
let matched_path = unescape_spaces(&Cow::from(matched_path));
let path: Cow<_> = if matched_path.starts_with("file://") {
Url::from_str(&matched_path)
Url::from_str(matched_path.as_str())
.ok()
.and_then(|url| url.to_file_path().ok())?
.into()
} else {
Path::new(&*matched_path).into()
Path::new(matched_path.as_str()).into()
};
let path = path::expand(&path);
let parent_dir = doc.path().and_then(|dp| dp.parent());
@ -91,10 +99,12 @@ pub(crate) fn path_completion(
let res: Vec<_> = read_dir
.filter_map(Result::ok)
.filter_map(|dir_entry| {
dir_entry
.metadata()
.ok()
.and_then(|md| Some((dir_entry.file_name().into_string().ok()?, md)))
dir_entry.metadata().ok().and_then(|md| {
Some((
escape_spaces(dir_entry.file_name().into_string().ok()?.as_str()),
md,
))
})
})
.map_while(|(file_name, md)| {
if handle.is_canceled() {