Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-term/src/handlers/completion/path.rs')
| -rw-r--r-- | helix-term/src/handlers/completion/path.rs | 69 |
1 files changed, 24 insertions, 45 deletions
diff --git a/helix-term/src/handlers/completion/path.rs b/helix-term/src/handlers/completion/path.rs index c2ffa5ef..e92be51c 100644 --- a/helix-term/src/handlers/completion/path.rs +++ b/helix-term/src/handlers/completion/path.rs @@ -3,29 +3,28 @@ use std::{ fs, path::{Path, PathBuf}, str::FromStr as _, - sync::Arc, }; -use helix_core::{self as core, completion::CompletionProvider, Selection, Transaction}; +use futures_util::{future::BoxFuture, FutureExt as _}; +use helix_core as core; +use helix_core::Transaction; use helix_event::TaskHandle; use helix_stdx::path::{self, canonicalize, fold_home_dir, get_path_suffix}; -use helix_view::{document::SavePoint, handlers::completion::ResponseContext, Document}; +use helix_view::Document; use url::Url; -use crate::handlers::completion::{item::CompletionResponse, CompletionItem, CompletionItems}; +use super::item::CompletionItem; pub(crate) fn path_completion( - selection: Selection, + cursor: usize, + text: core::Rope, doc: &Document, handle: TaskHandle, - savepoint: Arc<SavePoint>, -) -> Option<impl FnOnce() -> CompletionResponse> { +) -> Option<BoxFuture<'static, anyhow::Result<Vec<CompletionItem>>>> { if !doc.path_completion_enabled() { return None; } - let text = doc.text().clone(); - let cursor = selection.primary().cursor(text.slice(..)); let cur_line = text.char_to_line(cursor); let start = text.line_to_char(cur_line).max(cursor.saturating_sub(1000)); let line_until_cursor = text.slice(start..cursor); @@ -68,27 +67,12 @@ pub(crate) fn path_completion( return None; } - // TODO: handle properly in the future - const PRIORITY: i8 = 1; - let future = move || { + let future = tokio::task::spawn_blocking(move || { let Ok(read_dir) = std::fs::read_dir(&dir_path) else { - return CompletionResponse { - items: CompletionItems::Other(Vec::new()), - provider: CompletionProvider::Path, - context: ResponseContext { - is_incomplete: false, - priority: PRIORITY, - savepoint, - }, - }; + return Vec::new(); }; - let edit_diff = typed_file_name - .as_ref() - .map(|s| s.chars().count()) - .unwrap_or_default(); - - let res: Vec<_> = read_dir + read_dir .filter_map(Result::ok) .filter_map(|dir_entry| { dir_entry @@ -104,32 +88,27 @@ pub(crate) fn path_completion( let kind = path_kind(&md); let documentation = path_documentation(&md, &dir_path.join(&file_name), kind); - let transaction = Transaction::change_by_selection(&text, &selection, |range| { - let cursor = range.cursor(text.slice(..)); - (cursor - edit_diff, cursor, Some((&file_name).into())) - }); + let edit_diff = typed_file_name + .as_ref() + .map(|f| f.len()) + .unwrap_or_default(); + + let transaction = Transaction::change( + &text, + std::iter::once((cursor - edit_diff, cursor, Some((&file_name).into()))), + ); Some(CompletionItem::Other(core::CompletionItem { kind: Cow::Borrowed(kind), label: file_name.into(), transaction, - documentation: Some(documentation), - provider: CompletionProvider::Path, + documentation, })) }) - .collect(); - CompletionResponse { - items: CompletionItems::Other(res), - provider: CompletionProvider::Path, - context: ResponseContext { - is_incomplete: false, - priority: PRIORITY, - savepoint, - }, - } - }; + .collect::<Vec<_>>() + }); - Some(future) + Some(async move { Ok(future.await?) }.boxed()) } #[cfg(unix)] |