From ebebc16b159bbf9d7abb6b1a131d0df715ca9fdb Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Tue, 28 May 2024 15:31:24 +0800 Subject: [PATCH] Extract search_with_spinner() in Vim --- .../maple_core/src/stdio_server/provider/impls/blines.rs | 7 +++---- .../maple_core/src/stdio_server/provider/impls/files.rs | 6 +++--- .../maple_core/src/stdio_server/provider/impls/grep.rs | 5 ++--- .../maple_core/src/stdio_server/provider/impls/igrep.rs | 4 +++- .../src/stdio_server/provider/impls/tagfiles.rs | 5 ++++- crates/maple_core/src/stdio_server/service.rs | 5 ++++- crates/maple_core/src/stdio_server/vim.rs | 7 +++++++ crates/types/src/source_item.rs | 9 ++------- 8 files changed, 28 insertions(+), 20 deletions(-) diff --git a/crates/maple_core/src/stdio_server/provider/impls/blines.rs b/crates/maple_core/src/stdio_server/provider/impls/blines.rs index 361fe5feb..89659894f 100644 --- a/crates/maple_core/src/stdio_server/provider/impls/blines.rs +++ b/crates/maple_core/src/stdio_server/provider/impls/blines.rs @@ -109,10 +109,9 @@ impl BlinesProvider { let search_context = ctx.search_context(stop_signal.clone()); tokio::spawn(async move { - let _ = vim.bare_exec("clap#spinner#set_busy"); - crate::searcher::file::search(query, source_file, matcher, search_context) - .await; - let _ = vim.bare_exec("clap#spinner#set_idle"); + let future = + crate::searcher::file::search(query, source_file, matcher, search_context); + vim.search_with_spinner(future).await; }) }; diff --git a/crates/maple_core/src/stdio_server/provider/impls/files.rs b/crates/maple_core/src/stdio_server/provider/impls/files.rs index 1b056b64b..34b276c9f 100644 --- a/crates/maple_core/src/stdio_server/provider/impls/files.rs +++ b/crates/maple_core/src/stdio_server/provider/impls/files.rs @@ -86,9 +86,9 @@ impl FilesProvider { let vim = ctx.vim.clone(); let hidden = self.args.hidden; tokio::spawn(async move { - let _ = vim.bare_exec("clap#spinner#set_busy"); - crate::searcher::files::search(query, hidden, matcher, search_context).await; - let _ = vim.bare_exec("clap#spinner#set_idle"); + let future = + crate::searcher::files::search(query, hidden, matcher, search_context); + vim.search_with_spinner(future).await; }) }; diff --git a/crates/maple_core/src/stdio_server/provider/impls/grep.rs b/crates/maple_core/src/stdio_server/provider/impls/grep.rs index 11edd9291..2df58ea63 100644 --- a/crates/maple_core/src/stdio_server/provider/impls/grep.rs +++ b/crates/maple_core/src/stdio_server/provider/impls/grep.rs @@ -58,9 +58,8 @@ impl GrepProvider { search_context.paths.extend_from_slice(&self.args.paths); } let join_handle = tokio::spawn(async move { - let _ = vim.bare_exec("clap#spinner#set_busy"); - crate::searcher::grep::search(query, matcher, search_context).await; - let _ = vim.bare_exec("clap#spinner#set_idle"); + let future = crate::searcher::grep::search(query, matcher, search_context); + vim.search_with_spinner(future).await; }); let new_control = SearcherControl { diff --git a/crates/maple_core/src/stdio_server/provider/impls/igrep.rs b/crates/maple_core/src/stdio_server/provider/impls/igrep.rs index 0a1c6ed68..a613a9ed3 100644 --- a/crates/maple_core/src/stdio_server/provider/impls/igrep.rs +++ b/crates/maple_core/src/stdio_server/provider/impls/igrep.rs @@ -46,11 +46,13 @@ impl Grepper { let new_control = { let stop_signal = Arc::new(AtomicBool::new(false)); + let vim = ctx.vim.clone(); let mut search_context = ctx.search_context(stop_signal.clone()); search_context.paths = vec![path]; let join_handle = tokio::spawn(async move { - crate::searcher::grep::search(query, matcher, search_context).await + let future = crate::searcher::grep::search(query, matcher, search_context); + vim.search_with_spinner(future).await }); SearcherControl { diff --git a/crates/maple_core/src/stdio_server/provider/impls/tagfiles.rs b/crates/maple_core/src/stdio_server/provider/impls/tagfiles.rs index 65f1b7a2d..64537d2a2 100644 --- a/crates/maple_core/src/stdio_server/provider/impls/tagfiles.rs +++ b/crates/maple_core/src/stdio_server/provider/impls/tagfiles.rs @@ -34,9 +34,12 @@ impl TagfilesProvider { let join_handle = { let search_context = ctx.search_context(stop_signal.clone()); let cwd = ctx.cwd.to_path_buf(); + let vim = ctx.vim.clone(); tokio::spawn(async move { - crate::searcher::tagfiles::search(query, cwd, matcher, search_context).await; + let future = + crate::searcher::tagfiles::search(query, cwd, matcher, search_context); + vim.search_with_spinner(future).await; }) }; diff --git a/crates/maple_core/src/stdio_server/service.rs b/crates/maple_core/src/stdio_server/service.rs index 3e6c798fa..0b445faf0 100644 --- a/crates/maple_core/src/stdio_server/service.rs +++ b/crates/maple_core/src/stdio_server/service.rs @@ -204,7 +204,8 @@ impl ProviderSession { maybe_event = self.provider_events.recv() => { match maybe_event { Some(event) => { - tracing::trace!(debounce = true, "[{}] Recv debounced event: {event:?}", self.id); + let event_display = format!("{event:?}"); + tracing::trace!(debounce = true, "[{}] Recv debounced event: {event_display}", self.id); match event { ProviderEvent::Internal(internal_event) => { @@ -216,6 +217,7 @@ impl ProviderSession { } } } + tracing::trace!("[{}] Processed event: {event_display}", self.id); } ProviderEvent::OnMove(params) => { on_move.replace(params); @@ -229,6 +231,7 @@ impl ProviderSession { if let Err(err) = self.provider.on_key_event(&mut self.ctx, key_event).await { tracing::error!(?err, "Failed to process key_event"); } + tracing::trace!("[{}] Processed event: {event_display}", self.id); } ProviderEvent::Exit => { self.handle_exit(); diff --git a/crates/maple_core/src/stdio_server/vim.rs b/crates/maple_core/src/stdio_server/vim.rs index 70bd878c0..5516ae799 100644 --- a/crates/maple_core/src/stdio_server/vim.rs +++ b/crates/maple_core/src/stdio_server/vim.rs @@ -1,4 +1,5 @@ use crate::stdio_server::provider::ProviderId; +use futures::Future; use once_cell::sync::{Lazy, OnceCell}; use paths::AbsPathBuf; use rpc::vim::RpcClient; @@ -536,6 +537,12 @@ impl Vim { let value: Value = self.call("buf_is_valid", [buf]).await?; Ok(from_vim_bool(value)) } + + pub async fn search_with_spinner(&self, future: impl Future) { + let _ = self.bare_exec("clap#spinner#set_busy"); + future.await; + let _ = self.bare_exec("clap#spinner#set_idle"); + } } #[cfg(test)] diff --git a/crates/types/src/source_item.rs b/crates/types/src/source_item.rs index 689a375b2..db2b1d08b 100644 --- a/crates/types/src/source_item.rs +++ b/crates/types/src/source_item.rs @@ -41,8 +41,9 @@ impl<'a> FuzzyText<'a> { /// The location that a match should look in. /// /// Given a query, the match scope can refer to a full string or a substring. -#[derive(Debug, Clone, Copy)] +#[derive(Default, Debug, Clone, Copy)] pub enum MatchScope { + #[default] Full, /// `:Clap tags`, `:Clap proj_tags` TagName, @@ -52,12 +53,6 @@ pub enum MatchScope { GrepLine, } -impl Default for MatchScope { - fn default() -> Self { - Self::Full - } -} - impl std::str::FromStr for MatchScope { type Err = (); fn from_str(s: &str) -> Result {