Skip to content

Commit

Permalink
Update buf_tags on BufWritePost
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Aug 11, 2023
1 parent 137d086 commit 861e1ab
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 47 deletions.
33 changes: 14 additions & 19 deletions autoload/clap/api.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion crates/maple_core/src/stdio_server/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ pub enum AutocmdEventType {
CursorMoved,
InsertEnter,
BufEnter,
BufWinEnter,
BufLeave,
BufDelete,
BufWritePost,
BufWinEnter,
BufWinLeave,
}

Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 5 additions & 7 deletions crates/maple_core/src/stdio_server/plugin/ctags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
20 changes: 11 additions & 9 deletions crates/maple_core/src/stdio_server/provider/dumb_jump/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -153,13 +154,15 @@ async fn init_gtags(cwd: PathBuf, gtags_regenerated: Arc<AtomicBool>) {
}

impl DumbJumpProvider {
pub fn new() -> Self {
Self {
pub async fn new(ctx: &Context) -> Result<Self> {
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<()> {
Expand Down Expand Up @@ -229,7 +232,7 @@ impl DumbJumpProvider {
async fn start_search(
&self,
search_worker: SearchWorker,
query: String,
query: &str,
query_info: QueryInfo,
) -> Result<SearchResults> {
if query.is_empty() {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/maple_core/src/stdio_server/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub struct BaseArgs {
pub async fn create_provider(ctx: &Context) -> Result<Box<dyn ClapProvider>> {
let provider: Box<dyn ClapProvider> = 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?),
Expand Down
4 changes: 0 additions & 4 deletions crates/maple_core/src/stdio_server/vim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,6 @@ impl Vim {
self.bare_call("clap#rooter#working_dir").await
}

pub async fn context_query_or_input(&self) -> Result<String> {
self.bare_call("context_query_or_input").await
}

pub async fn current_buffer_path(&self) -> Result<String> {
self.bare_call("current_buffer_path").await
}
Expand Down
14 changes: 8 additions & 6 deletions plugin/clap.vim
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ augroup VimClap
autocmd BufAdd * call clap#client#notify('note_recent_files', [+expand('<abuf>')])

if get(g:, 'clap_plugin_experimental', 0)
autocmd InsertEnter * call clap#client#notify('InsertEnter')
autocmd CursorMoved * call clap#client#notify('CursorMoved', [+expand('<abuf>')])
autocmd BufEnter * call clap#client#notify('BufEnter', [+expand('<abuf>')])
autocmd BufLeave * call clap#client#notify('BufLeave', [+expand('<abuf>')])
autocmd BufDelete * call clap#client#notify('BufDelete', [+expand('<abuf>')])
autocmd BufWinEnter * call clap#client#notify('BufWinEnter', [+expand('<abuf>')])
autocmd InsertEnter * call clap#client#notify('InsertEnter')
autocmd CursorMoved * call clap#client#notify('CursorMoved', [+expand('<abuf>')])
autocmd BufEnter * call clap#client#notify('BufEnter', [+expand('<abuf>')])
autocmd BufLeave * call clap#client#notify('BufLeave', [+expand('<abuf>')])
autocmd BufDelete * call clap#client#notify('BufDelete', [+expand('<abuf>')])
autocmd BufWritePost * call clap#client#notify('BufWritePost', [+expand('<abuf>')])
autocmd BufWinEnter * call clap#client#notify('BufWinEnter', [+expand('<abuf>')])
autocmd BufWinLeave * call clap#client#notify('BufWinLeave', [+expand('<abuf>')])
endif

" yanks provider
Expand Down

0 comments on commit 861e1ab

Please sign in to comment.