| 1 |
use std::{ |
| 2 |
borrow::Cow, |
| 3 |
fs, |
| 4 |
path::{Path, PathBuf}, |
| 5 |
str::FromStr as _, |
| 6 |
sync::Arc, |
| 7 |
}; |
| 8 |
|
| 9 |
use helix_core::{self as core, completion::CompletionProvider, Selection, Transaction}; |
| 10 |
use helix_event::TaskHandle; |
| 11 |
use helix_stdx::path::{self, canonicalize, fold_home_dir, get_path_suffix}; |
| 12 |
use helix_stdx::Url; |
| 13 |
use helix_view::{document::SavePoint, handlers::completion::ResponseContext, Document}; |
| 14 |
|
| 15 |
use crate::handlers::completion::{item::CompletionResponse, CompletionItem, CompletionItems}; |
| 16 |
|
| 17 |
pub(crate) fn path_completion( |
| 18 |
selection: Selection, |
| 19 |
doc: &Document, |
| 20 |
handle: TaskHandle, |
| 21 |
savepoint: Arc<SavePoint>, |
| 22 |
) -> Option<impl FnOnce() -> CompletionResponse> { |
| 23 |
if !doc.path_completion_enabled() { |
| 24 |
return None; |
| 25 |
} |
| 26 |
|
| 27 |
let text = doc.text().clone(); |
| 28 |
let cursor = selection.primary().cursor(text.slice(..)); |
| 29 |
let cur_line = text.char_to_line(cursor); |
| 30 |
let start = text.line_to_char(cur_line).max(cursor.saturating_sub(1000)); |
| 31 |
let line_until_cursor = text.slice(start..cursor); |
| 32 |
|
| 33 |
let (dir_path, typed_file_name) = |
| 34 |
get_path_suffix(line_until_cursor, false).and_then(|matched_path| { |
| 35 |
let matched_path = Cow::from(matched_path); |
| 36 |
let path: Cow<_> = if matched_path.starts_with("file://") { |
| 37 |
Url::from_str(&matched_path) |
| 38 |
.ok() |
| 39 |
.and_then(|url| url.to_file_path().ok())? |
| 40 |
.into() |
| 41 |
} else { |
| 42 |
Path::new(&*matched_path).into() |
| 43 |
}; |
| 44 |
let path = path::expand(&path); |
| 45 |
let parent_dir = doc.path().and_then(|dp| dp.parent()); |
| 46 |
let path = match parent_dir { |
| 47 |
Some(parent_dir) if path.is_relative() => parent_dir.join(&path), |
| 48 |
_ => path.into_owned(), |
| 49 |
}; |
| 50 |
#[cfg(windows)] |
| 51 |
let ends_with_slash = matches!(matched_path.as_bytes().last(), Some(b'/' | b'\\')); |
| 52 |
#[cfg(not(windows))] |
| 53 |
let ends_with_slash = matches!(matched_path.as_bytes().last(), Some(b'/')); |
| 54 |
|
| 55 |
if ends_with_slash { |
| 56 |
Some((PathBuf::from(path.as_path()), None)) |
| 57 |
} else { |
| 58 |
path.parent().map(|parent_path| { |
| 59 |
( |
| 60 |
PathBuf::from(parent_path), |
| 61 |
path.file_name().and_then(|f| f.to_str().map(String::from)), |
| 62 |
) |
| 63 |
}) |
| 64 |
} |
| 65 |
})?; |
| 66 |
|
| 67 |
if handle.is_canceled() { |
| 68 |
return None; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
const PRIORITY: i8 = 1; |
| 73 |
let future = move || { |
| 74 |
let Ok(read_dir) = std::fs::read_dir(&dir_path) else { |
| 75 |
return CompletionResponse { |
| 76 |
items: CompletionItems::Other(Vec::new()), |
| 77 |
provider: CompletionProvider::Path, |
| 78 |
context: ResponseContext { |
| 79 |
is_incomplete: false, |
| 80 |
priority: PRIORITY, |
| 81 |
savepoint, |
| 82 |
}, |
| 83 |
}; |
| 84 |
}; |
| 85 |
|
| 86 |
let edit_diff = typed_file_name |
| 87 |
.as_ref() |
| 88 |
.map(|s| s.chars().count()) |
| 89 |
.unwrap_or_default(); |
| 90 |
|
| 91 |
let res: Vec<_> = read_dir |
| 92 |
.filter_map(Result::ok) |
| 93 |
.filter_map(|dir_entry| { |
| 94 |
dir_entry |
| 95 |
.metadata() |
| 96 |
.ok() |
| 97 |
.and_then(|md| Some((dir_entry.file_name().into_string().ok()?, md))) |
| 98 |
}) |
| 99 |
.map_while(|(file_name, md)| { |
| 100 |
if handle.is_canceled() { |
| 101 |
return None; |
| 102 |
} |
| 103 |
|
| 104 |
let kind = path_kind(&md); |
| 105 |
let documentation = path_documentation(&md, &dir_path.join(&file_name), kind); |
| 106 |
|
| 107 |
let transaction = Transaction::change_by_selection(&text, &selection, |range| { |
| 108 |
let cursor = range.cursor(text.slice(..)); |
| 109 |
(cursor - edit_diff, cursor, Some((&file_name).into())) |
| 110 |
}); |
| 111 |
|
| 112 |
Some(CompletionItem::Other(core::CompletionItem { |
| 113 |
kind: Cow::Borrowed(kind), |
| 114 |
label: file_name.into(), |
| 115 |
transaction, |
| 116 |
documentation: Some(documentation), |
| 117 |
provider: CompletionProvider::Path, |
| 118 |
})) |
| 119 |
}) |
| 120 |
.collect(); |
| 121 |
CompletionResponse { |
| 122 |
items: CompletionItems::Other(res), |
| 123 |
provider: CompletionProvider::Path, |
| 124 |
context: ResponseContext { |
| 125 |
is_incomplete: false, |
| 126 |
priority: PRIORITY, |
| 127 |
savepoint, |
| 128 |
}, |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
Some(future) |
| 133 |
} |
| 134 |
|
| 135 |
#[cfg(unix)] |
| 136 |
fn path_documentation(md: &fs::Metadata, full_path: &Path, kind: &str) -> String { |
| 137 |
let full_path = fold_home_dir(canonicalize(full_path)); |
| 138 |
let full_path_name = full_path.to_string_lossy(); |
| 139 |
|
| 140 |
use std::os::unix::prelude::PermissionsExt; |
| 141 |
let mode = md.permissions().mode(); |
| 142 |
|
| 143 |
let perms = [ |
| 144 |
(libc::S_IRUSR, 'r'), |
| 145 |
(libc::S_IWUSR, 'w'), |
| 146 |
(libc::S_IXUSR, 'x'), |
| 147 |
(libc::S_IRGRP, 'r'), |
| 148 |
(libc::S_IWGRP, 'w'), |
| 149 |
(libc::S_IXGRP, 'x'), |
| 150 |
(libc::S_IROTH, 'r'), |
| 151 |
(libc::S_IWOTH, 'w'), |
| 152 |
(libc::S_IXOTH, 'x'), |
| 153 |
] |
| 154 |
.into_iter() |
| 155 |
.fold(String::with_capacity(9), |mut acc, (p, s)| { |
| 156 |
|
| 157 |
#[allow(clippy::unnecessary_cast)] |
| 158 |
acc.push(if mode & (p as u32) > 0 { s } else { '-' }); |
| 159 |
acc |
| 160 |
}); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
format!( |
| 166 |
"type: `{kind}`\n\ |
| 167 |
permissions: `[{perms}]`\n\ |
| 168 |
full path: `{full_path_name}`", |
| 169 |
) |
| 170 |
} |
| 171 |
|
| 172 |
#[cfg(not(unix))] |
| 173 |
fn path_documentation(_md: &fs::Metadata, full_path: &Path, kind: &str) -> String { |
| 174 |
let full_path = fold_home_dir(canonicalize(full_path)); |
| 175 |
let full_path_name = full_path.to_string_lossy(); |
| 176 |
format!("type: `{kind}`\nfull path: `{full_path_name}`",) |
| 177 |
} |
| 178 |
|
| 179 |
#[cfg(unix)] |
| 180 |
fn path_kind(md: &fs::Metadata) -> &'static str { |
| 181 |
if md.is_symlink() { |
| 182 |
"link" |
| 183 |
} else if md.is_dir() { |
| 184 |
"folder" |
| 185 |
} else { |
| 186 |
use std::os::unix::fs::FileTypeExt; |
| 187 |
if md.file_type().is_block_device() { |
| 188 |
"block" |
| 189 |
} else if md.file_type().is_socket() { |
| 190 |
"socket" |
| 191 |
} else if md.file_type().is_char_device() { |
| 192 |
"char_device" |
| 193 |
} else if md.file_type().is_fifo() { |
| 194 |
"fifo" |
| 195 |
} else { |
| 196 |
"file" |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
#[cfg(not(unix))] |
| 202 |
fn path_kind(md: &fs::Metadata) -> &'static str { |
| 203 |
if md.is_symlink() { |
| 204 |
"link" |
| 205 |
} else if md.is_dir() { |
| 206 |
"folder" |
| 207 |
} else { |
| 208 |
"file" |
| 209 |
} |
| 210 |
} |