From 861e1ab860a98cf5b3a07374e9b50563ca5a6f0d Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Fri, 11 Aug 2023 08:25:32 +0800 Subject: [PATCH] Update buf_tags on BufWritePost --- autoload/clap/api.vim | 33 ++++++++----------- crates/maple_core/src/stdio_server/input.rs | 4 ++- .../src/stdio_server/plugin/ctags.rs | 12 +++---- .../stdio_server/provider/dumb_jump/mod.rs | 20 ++++++----- .../src/stdio_server/provider/mod.rs | 2 +- crates/maple_core/src/stdio_server/vim.rs | 4 --- plugin/clap.vim | 14 ++++---- 7 files changed, 42 insertions(+), 47 deletions(-) diff --git a/autoload/clap/api.vim b/autoload/clap/api.vim index e70e6725b..bdeeeee6b 100644 --- a/autoload/clap/api.vim +++ b/autoload/clap/api.vim @@ -40,27 +40,30 @@ endif let s:api = {} if s:is_nvim - function! clap#api#floating_win_is_valid(winid) abort - return nvim_win_is_valid(a:winid) - endfunction - function! s:api.win_is_valid(winid) abort return nvim_win_is_valid(a:winid) endfunction -else - function! clap#api#floating_win_is_valid(winid) abort - return !empty(popup_getpos(a:winid)) + function! s:api.get_var(name) abort + return nvim_get_var(a:name) endfunction + function! s:api.set_var(name, value) abort + return nvim_set_var(a:name, a:value) + endfunction +else function! s:api.win_is_valid(winid) abort return win_screenpos(a:winid) != [0, 0] endfunction -endif -function! s:api.context_query_or_input() abort - return has_key(g:clap.context, 'query') ? g:clap.context.query : g:clap.input.get() -endfunction + function! s:api.get_var(name) abort + return get(g:, a:name, v:null) + endfunction + + function! s:api.set_var(name, value) abort + execute 'let '.a:name.'= a:value' + endfunction +endif " The leading icon is stripped. function! s:api.display_getcurline() abort @@ -106,14 +109,6 @@ function! s:api.input_set(value) abort call g:clap.input.set(a:value) endfunction -function! s:api.get_var(var) abort - return get(g:, a:var, v:null) -endfunction - -function! s:api.set_var(name, value) abort - execute 'let '.a:name.'= a:value' -endfunction - function! s:api.current_buffer_path() abort return expand('#'.bufnr('%').':p') endfunction diff --git a/crates/maple_core/src/stdio_server/input.rs b/crates/maple_core/src/stdio_server/input.rs index bac5b116a..97bab9568 100644 --- a/crates/maple_core/src/stdio_server/input.rs +++ b/crates/maple_core/src/stdio_server/input.rs @@ -66,9 +66,10 @@ pub enum AutocmdEventType { CursorMoved, InsertEnter, BufEnter, - BufWinEnter, BufLeave, BufDelete, + BufWritePost, + BufWinEnter, BufWinLeave, } @@ -110,6 +111,7 @@ impl Event { "BufEnter" => Self::Autocmd((AutocmdEventType::BufEnter, notification.params)), "BufLeave" => Self::Autocmd((AutocmdEventType::BufLeave, notification.params)), "BufDelete" => Self::Autocmd((AutocmdEventType::BufDelete, notification.params)), + "BufWritePost" => Self::Autocmd((AutocmdEventType::BufWritePost, notification.params)), "BufWinEnter" => Self::Autocmd((AutocmdEventType::BufWinEnter, notification.params)), "BufWinLeave" => Self::Autocmd((AutocmdEventType::BufWinLeave, notification.params)), _ => Self::Action(Action { diff --git a/crates/maple_core/src/stdio_server/plugin/ctags.rs b/crates/maple_core/src/stdio_server/plugin/ctags.rs index 6e677e577..4e9824aff 100644 --- a/crates/maple_core/src/stdio_server/plugin/ctags.rs +++ b/crates/maple_core/src/stdio_server/plugin/ctags.rs @@ -92,7 +92,7 @@ impl CtagsPlugin { #[async_trait::async_trait] impl ClapPlugin for CtagsPlugin { async fn on_plugin_event(&mut self, plugin_event: PluginEvent) -> Result<()> { - use AutocmdEventType::{BufDelete, BufEnter, CursorMoved}; + use AutocmdEventType::{BufDelete, BufEnter, BufWritePost, CursorMoved}; let PluginEvent::Autocmd(autocmd_event) = plugin_event; @@ -105,16 +105,14 @@ impl ClapPlugin for CtagsPlugin { .ok_or_else(|| anyhow::anyhow!("bufnr not found in params"))?; match event_type { - BufEnter => { + BufEnter | BufWritePost => { let file_path: String = self.vim.expand(format!("#{bufnr}:p")).await?; if !Path::new(&file_path).exists() { return Ok(()); } - if let std::collections::hash_map::Entry::Vacant(e) = self.buf_tags.entry(bufnr) { - let buffer_tags = crate::tools::ctags::fetch_buffer_tags(file_path)?; - e.insert(buffer_tags); - self.on_cursor_moved(bufnr).await?; - } + let buffer_tags = crate::tools::ctags::fetch_buffer_tags(file_path)?; + self.buf_tags.insert(bufnr, buffer_tags); + self.on_cursor_moved(bufnr).await?; } BufDelete => { self.buf_tags.remove(&bufnr); diff --git a/crates/maple_core/src/stdio_server/provider/dumb_jump/mod.rs b/crates/maple_core/src/stdio_server/provider/dumb_jump/mod.rs index a7a3aea6a..b84dd2921 100644 --- a/crates/maple_core/src/stdio_server/provider/dumb_jump/mod.rs +++ b/crates/maple_core/src/stdio_server/provider/dumb_jump/mod.rs @@ -5,7 +5,7 @@ use crate::find_usages::{CtagsSearcher, GtagsSearcher, QueryType, Usage, UsageMa use crate::paths::AbsPathBuf; use crate::stdio_server::handler::CachedPreviewImpl; use crate::stdio_server::job; -use crate::stdio_server::provider::{ClapProvider, Context}; +use crate::stdio_server::provider::{BaseArgs, ClapProvider, Context}; use crate::tools::ctags::{get_language, TagsGenerator, CTAGS_EXISTS}; use crate::tools::gtags::GTAGS_EXISTS; use anyhow::Result; @@ -116,6 +116,7 @@ struct SearchResults { #[derive(Debug, Clone)] pub struct DumbJumpProvider { + args: BaseArgs, /// Results from last searching. /// This might be a superset of searching results for the last query. cached_results: SearchResults, @@ -153,13 +154,15 @@ async fn init_gtags(cwd: PathBuf, gtags_regenerated: Arc) { } impl DumbJumpProvider { - pub fn new() -> Self { - Self { + pub async fn new(ctx: &Context) -> Result { + let args = ctx.parse_provider_args().await?; + Ok(Self { + args, cached_results: Default::default(), current_usages: None, ctags_regenerated: Arc::new(false.into()), gtags_regenerated: Arc::new(false.into()), - } + }) } async fn initialize_tags(&self, extension: String, cwd: AbsPathBuf) -> Result<()> { @@ -229,7 +232,7 @@ impl DumbJumpProvider { async fn start_search( &self, search_worker: SearchWorker, - query: String, + query: &str, query_info: QueryInfo, ) -> Result { if query.is_empty() { @@ -295,9 +298,8 @@ impl ClapProvider for DumbJumpProvider { } }); - let query = ctx.vim.context_query_or_input().await?; - if !query.is_empty() { - let query_info = parse_query_info(&query); + if let Some(query) = &self.args.query { + let query_info = parse_query_info(query); let search_worker = SearchWorker { cwd, query_info: query_info.clone(), @@ -384,7 +386,7 @@ impl ClapProvider for DumbJumpProvider { query_info: query_info.clone(), source_file_extension: ctx.start_buffer_extension()?.to_string(), }; - let search_results = self.start_search(search_worker, query, query_info).await?; + let search_results = self.start_search(search_worker, &query, query_info).await?; self.on_new_search_results(search_results, ctx)?; diff --git a/crates/maple_core/src/stdio_server/provider/mod.rs b/crates/maple_core/src/stdio_server/provider/mod.rs index 4e5c53f0d..c1375e894 100644 --- a/crates/maple_core/src/stdio_server/provider/mod.rs +++ b/crates/maple_core/src/stdio_server/provider/mod.rs @@ -57,7 +57,7 @@ pub struct BaseArgs { pub async fn create_provider(ctx: &Context) -> Result> { let provider: Box = match ctx.env.provider_id.as_str() { "blines" => Box::new(blines::BlinesProvider::new(ctx).await?), - "dumb_jump" => Box::new(dumb_jump::DumbJumpProvider::new()), + "dumb_jump" => Box::new(dumb_jump::DumbJumpProvider::new(ctx).await?), "filer" => Box::new(filer::FilerProvider::new(ctx).await?), "files" => Box::new(files::FilesProvider::new(ctx).await?), "grep" => Box::new(grep::GrepProvider::new(ctx).await?), diff --git a/crates/maple_core/src/stdio_server/vim.rs b/crates/maple_core/src/stdio_server/vim.rs index bd2e8cfa9..7b18ed3df 100644 --- a/crates/maple_core/src/stdio_server/vim.rs +++ b/crates/maple_core/src/stdio_server/vim.rs @@ -429,10 +429,6 @@ impl Vim { self.bare_call("clap#rooter#working_dir").await } - pub async fn context_query_or_input(&self) -> Result { - self.bare_call("context_query_or_input").await - } - pub async fn current_buffer_path(&self) -> Result { self.bare_call("current_buffer_path").await } diff --git a/plugin/clap.vim b/plugin/clap.vim index 9bdc5a901..7c79e4b80 100644 --- a/plugin/clap.vim +++ b/plugin/clap.vim @@ -55,12 +55,14 @@ augroup VimClap autocmd BufAdd * call clap#client#notify('note_recent_files', [+expand('')]) if get(g:, 'clap_plugin_experimental', 0) - autocmd InsertEnter * call clap#client#notify('InsertEnter') - autocmd CursorMoved * call clap#client#notify('CursorMoved', [+expand('')]) - autocmd BufEnter * call clap#client#notify('BufEnter', [+expand('')]) - autocmd BufLeave * call clap#client#notify('BufLeave', [+expand('')]) - autocmd BufDelete * call clap#client#notify('BufDelete', [+expand('')]) - autocmd BufWinEnter * call clap#client#notify('BufWinEnter', [+expand('')]) + autocmd InsertEnter * call clap#client#notify('InsertEnter') + autocmd CursorMoved * call clap#client#notify('CursorMoved', [+expand('')]) + autocmd BufEnter * call clap#client#notify('BufEnter', [+expand('')]) + autocmd BufLeave * call clap#client#notify('BufLeave', [+expand('')]) + autocmd BufDelete * call clap#client#notify('BufDelete', [+expand('')]) + autocmd BufWritePost * call clap#client#notify('BufWritePost', [+expand('')]) + autocmd BufWinEnter * call clap#client#notify('BufWinEnter', [+expand('')]) + autocmd BufWinLeave * call clap#client#notify('BufWinLeave', [+expand('')]) endif " yanks provider