From b2d2dcb4040341a32c6909090afc1db4e93b7619 Mon Sep 17 00:00:00 2001 From: zhouguoqiang Date: Wed, 30 Oct 2019 18:09:28 +0800 Subject: [PATCH 1/6] A simple way to implement tagbar async call --- autoload/tagbar.vim | 54 +++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 21 deletions(-) mode change 100644 => 100755 autoload/tagbar.vim diff --git a/autoload/tagbar.vim b/autoload/tagbar.vim old mode 100644 new mode 100755 index 813c40b1..563dc552 --- a/autoload/tagbar.vim +++ b/autoload/tagbar.vim @@ -2413,12 +2413,24 @@ endfunction " Helper functions {{{1 " s:AutoUpdate() {{{2 +" use timer_start to let tagbar async, this can increase vim open file performance and +" fix windows blink when open some file. function! s:AutoUpdate(fname, force, ...) abort - call tagbar#debug#log('AutoUpdate called [' . a:fname . ']') + let g:tagbar_update_fname = a:fname + let g:tagbar_update_force = a:force + let g:tagbar_no_display = a:0 > 0 ? a:1 : 0 + if has('win32') " windows use timer_start will call bug + call AutoUpdate_CB(0) + else + call timer_start(0, 'AutoUpdate_CB') + endif +endfunc - " Whether we want to skip actually displaying the tags in Tagbar and only - " update the fileinfo - let no_display = a:0 > 0 ? a:1 : 0 +function! AutoUpdate_CB(channel, ...) abort + " call tagbar#debug#log('AutoUpdate called [' . fname . ']') + let l:fname = g:tagbar_update_fname + let l:force = g:tagbar_update_force + let no_display = g:tagbar_no_display " This file is being loaded due to a quickfix command like vimgrep, so " don't process it @@ -2436,7 +2448,7 @@ function! s:AutoUpdate(fname, force, ...) abort endif " Get the filetype of the file we're about to process - let bufnr = bufnr(a:fname) + let bufnr = bufnr(l:fname) let ftype = getbufvar(bufnr, '&filetype') " Don't do anything if we're in the tagbar window @@ -2451,7 +2463,7 @@ function! s:AutoUpdate(fname, force, ...) abort \ "sanitized filetype: '" . sftype . "'") " Don't do anything if the file isn't supported - if !s:IsValidFile(a:fname, sftype) + if !s:IsValidFile(l:fname, sftype) call tagbar#debug#log('Not a valid file, stopping processing') let s:nearby_disabled = 1 return @@ -2463,20 +2475,20 @@ function! s:AutoUpdate(fname, force, ...) abort " Testing the mtime of the file is necessary in case it got changed " outside of Vim, for example by checking out a different version from a " VCS. - if s:known_files.has(a:fname) - let curfile = s:known_files.get(a:fname) - " if a:force || getbufvar(curfile.bufnr, '&modified') || - if a:force || empty(curfile) || curfile.ftype != sftype || - \ (filereadable(a:fname) && getftime(a:fname) > curfile.mtime) - call tagbar#debug#log('File data outdated, updating [' . a:fname . ']') - call s:ProcessFile(a:fname, sftype) + if s:known_files.has(l:fname) + let curfile = s:known_files.get(l:fname) + " if l:force || getbufvar(curfile.bufnr, '&modified') || + if l:force || empty(curfile) || curfile.ftype != sftype || + \ (filereadable(l:fname) && getftime(l:fname) > curfile.mtime) + call tagbar#debug#log('File data outdated, updating [' . l:fname . ']') + call s:ProcessFile(l:fname, sftype) let updated = 1 else - call tagbar#debug#log('File data seems up to date [' . a:fname . ']') + call tagbar#debug#log('File data seems up to date [' . l:fname . ']') endif - elseif !s:known_files.has(a:fname) - call tagbar#debug#log('New file, processing [' . a:fname . ']') - call s:ProcessFile(a:fname, sftype) + elseif !s:known_files.has(l:fname) + call tagbar#debug#log('New file, processing [' . l:fname . ']') + call s:ProcessFile(l:fname, sftype) let updated = 1 endif @@ -2484,12 +2496,12 @@ function! s:AutoUpdate(fname, force, ...) abort return endif - let fileinfo = s:known_files.get(a:fname) + let fileinfo = s:known_files.get(l:fname) " If we don't have an entry for the file by now something must have gone " wrong, so don't change the tagbar content if empty(fileinfo) - call tagbar#debug#log('fileinfo empty after processing [' . a:fname . ']') + call tagbar#debug#log('fileinfo empty after processing [' . l:fname . ']') return endif @@ -2498,14 +2510,14 @@ function! s:AutoUpdate(fname, force, ...) abort if bufwinnr(s:TagbarBufName()) != -1 && !s:paused && \ (s:new_window || updated || \ (!empty(tagbar#state#get_current_file(0)) && - \ a:fname != tagbar#state#get_current_file(0).fpath)) + \ l:fname != tagbar#state#get_current_file(0).fpath)) call s:RenderContent(fileinfo) endif " Call setCurrent after rendering so RenderContent can check whether the " same file is being redisplayed if !empty(fileinfo) - call tagbar#debug#log('Setting current file [' . a:fname . ']') + call tagbar#debug#log('Setting current file [' . l:fname . ']') call tagbar#state#set_current_file(fileinfo) let s:nearby_disabled = 0 endif From 7c31c57897c5cdeb97f2bc60f49252e348645a39 Mon Sep 17 00:00:00 2001 From: raven42 Date: Fri, 25 Sep 2020 15:23:12 -0500 Subject: [PATCH 2/6] Use lambda function pointer to do async call --- autoload/tagbar.vim | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/autoload/tagbar.vim b/autoload/tagbar.vim index 2aef8b37..fc3bd47b 100755 --- a/autoload/tagbar.vim +++ b/autoload/tagbar.vim @@ -2649,21 +2649,18 @@ endfunction " use timer_start to let tagbar async, this can increase vim open file performance and " fix windows blink when open some file. function! s:AutoUpdate(fname, force, ...) abort - let g:tagbar_update_fname = a:fname - let g:tagbar_update_force = a:force - let g:tagbar_no_display = a:0 > 0 ? a:1 : 0 - if has('win32') " windows use timer_start will call bug - call AutoUpdate_CB(0) + if !has('win32') && has('lambda') + call timer_start(0, { -> AutoUpdate_CB(a:fname, a:force, a:0 < 0 ? a:1 : '0')}) else - call timer_start(0, 'AutoUpdate_CB') + call AutoUpdate_CB(a:fname, a:force, a:0 < 0 ? a:1 : '0') endif endfunc -function! AutoUpdate_CB(channel, ...) abort - " call tagbar#debug#log('AutoUpdate called [' . fname . ']') - let l:fname = g:tagbar_update_fname - let l:force = g:tagbar_update_force - let no_display = g:tagbar_no_display +function! AutoUpdate_CB(fname, force, ...) abort + let l:fname = a:fname + let l:force = a:force + let no_display = a:0 < 0 ? a:1 : 0 + call tagbar#debug#log('AutoUpdate_CB(' . a:fname . ', ' . a:force . ', ' . a:0 . ')') " This file is being loaded due to a quickfix command like vimgrep, so " don't process it From 117eb8cc8eb99aae8dab0cce94f92f57c85633d6 Mon Sep 17 00:00:00 2001 From: raven42 Date: Fri, 25 Sep 2020 15:27:46 -0500 Subject: [PATCH 3/6] revert variable naming --- autoload/tagbar.vim | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/autoload/tagbar.vim b/autoload/tagbar.vim index fc3bd47b..879b14c3 100755 --- a/autoload/tagbar.vim +++ b/autoload/tagbar.vim @@ -2650,16 +2650,14 @@ endfunction " fix windows blink when open some file. function! s:AutoUpdate(fname, force, ...) abort if !has('win32') && has('lambda') - call timer_start(0, { -> AutoUpdate_CB(a:fname, a:force, a:0 < 0 ? a:1 : '0')}) + call timer_start(0, { -> AutoUpdate_CB(a:fname, a:force, a:0 > 0 ? a:1 : '0')}) else call AutoUpdate_CB(a:fname, a:force, a:0 < 0 ? a:1 : '0') endif endfunc function! AutoUpdate_CB(fname, force, ...) abort - let l:fname = a:fname - let l:force = a:force - let no_display = a:0 < 0 ? a:1 : 0 + let no_display = a:0 > 0 ? a:1 : 0 call tagbar#debug#log('AutoUpdate_CB(' . a:fname . ', ' . a:force . ', ' . a:0 . ')') " This file is being loaded due to a quickfix command like vimgrep, so @@ -2678,7 +2676,7 @@ function! AutoUpdate_CB(fname, force, ...) abort endif " Get the filetype of the file we're about to process - let bufnr = bufnr(l:fname) + let bufnr = bufnr(a:fname) let ftype = getbufvar(bufnr, '&filetype') " Don't do anything if we're in the tagbar window @@ -2693,7 +2691,7 @@ function! AutoUpdate_CB(fname, force, ...) abort \ "sanitized filetype: '" . sftype . "'") " Don't do anything if the file isn't supported - if !s:IsValidFile(l:fname, sftype) + if !s:IsValidFile(a:fname, sftype) call tagbar#debug#log('Not a valid file, stopping processing') let s:nearby_disabled = 1 return @@ -2705,20 +2703,20 @@ function! AutoUpdate_CB(fname, force, ...) abort " Testing the mtime of the file is necessary in case it got changed " outside of Vim, for example by checking out a different version from a " VCS. - if s:known_files.has(l:fname) - let curfile = s:known_files.get(l:fname) - " if l:force || getbufvar(curfile.bufnr, '&modified') || - if l:force || empty(curfile) || curfile.ftype != sftype || - \ (filereadable(l:fname) && getftime(l:fname) > curfile.mtime) - call tagbar#debug#log('File data outdated, updating [' . l:fname . ']') - call s:ProcessFile(l:fname, sftype) + if s:known_files.has(a:fname) + let curfile = s:known_files.get(a:fname) + " if a:force || getbufvar(curfile.bufnr, '&modified') || + if a:force || empty(curfile) || curfile.ftype != sftype || + \ (filereadable(a:fname) && getftime(a:fname) > curfile.mtime) + call tagbar#debug#log('File data outdated, updating [' . a:fname . ']') + call s:ProcessFile(a:fname, sftype) let updated = 1 else - call tagbar#debug#log('File data seems up to date [' . l:fname . ']') + call tagbar#debug#log('File data seems up to date [' . a:fname . ']') endif - elseif !s:known_files.has(l:fname) - call tagbar#debug#log('New file, processing [' . l:fname . ']') - call s:ProcessFile(l:fname, sftype) + elseif !s:known_files.has(a:fname) + call tagbar#debug#log('New file, processing [' . a:fname . ']') + call s:ProcessFile(a:fname, sftype) let updated = 1 endif @@ -2726,12 +2724,12 @@ function! AutoUpdate_CB(fname, force, ...) abort return endif - let fileinfo = s:known_files.get(l:fname) + let fileinfo = s:known_files.get(a:fname) " If we don't have an entry for the file by now something must have gone " wrong, so don't change the tagbar content if empty(fileinfo) - call tagbar#debug#log('fileinfo empty after processing [' . l:fname . ']') + call tagbar#debug#log('fileinfo empty after processing [' . a:fname . ']') return endif @@ -2740,14 +2738,14 @@ function! AutoUpdate_CB(fname, force, ...) abort if bufwinnr(s:TagbarBufName()) != -1 && !s:paused && \ (s:new_window || updated || \ (!empty(tagbar#state#get_current_file(0)) && - \ l:fname != tagbar#state#get_current_file(0).fpath)) + \ a:fname != tagbar#state#get_current_file(0).fpath)) call s:RenderContent(fileinfo) endif " Call setCurrent after rendering so RenderContent can check whether the " same file is being redisplayed if !empty(fileinfo) - call tagbar#debug#log('Setting current file [' . l:fname . ']') + call tagbar#debug#log('Setting current file [' . a:fname . ']') call tagbar#state#set_current_file(fileinfo) let s:nearby_disabled = 0 endif From ae61b609cc69639aee4a031910f93583055b5f96 Mon Sep 17 00:00:00 2001 From: raven42 Date: Fri, 25 Sep 2020 15:32:24 -0500 Subject: [PATCH 4/6] fix no_display --- autoload/tagbar.vim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/autoload/tagbar.vim b/autoload/tagbar.vim index 879b14c3..91e877c8 100755 --- a/autoload/tagbar.vim +++ b/autoload/tagbar.vim @@ -2649,16 +2649,16 @@ endfunction " use timer_start to let tagbar async, this can increase vim open file performance and " fix windows blink when open some file. function! s:AutoUpdate(fname, force, ...) abort + let no_display = a:0 > 0 ? a:1 : 0 + if !has('win32') && has('lambda') - call timer_start(0, { -> AutoUpdate_CB(a:fname, a:force, a:0 > 0 ? a:1 : '0')}) + call timer_start(0, { -> AutoUpdate_CB(a:fname, a:force, no_display)}) else call AutoUpdate_CB(a:fname, a:force, a:0 < 0 ? a:1 : '0') endif endfunc -function! AutoUpdate_CB(fname, force, ...) abort - let no_display = a:0 > 0 ? a:1 : 0 - call tagbar#debug#log('AutoUpdate_CB(' . a:fname . ', ' . a:force . ', ' . a:0 . ')') +function! AutoUpdate_CB(fname, force, no_display) abort " This file is being loaded due to a quickfix command like vimgrep, so " don't process it @@ -2720,7 +2720,7 @@ function! AutoUpdate_CB(fname, force, ...) abort let updated = 1 endif - if no_display + if a:no_display return endif From 3971510ba73473af0fa05e2fd8fb465964263358 Mon Sep 17 00:00:00 2001 From: raven42 Date: Fri, 25 Sep 2020 15:33:50 -0500 Subject: [PATCH 5/6] revert log --- autoload/tagbar.vim | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/autoload/tagbar.vim b/autoload/tagbar.vim index 91e877c8..1c77c186 100755 --- a/autoload/tagbar.vim +++ b/autoload/tagbar.vim @@ -2649,6 +2649,10 @@ endfunction " use timer_start to let tagbar async, this can increase vim open file performance and " fix windows blink when open some file. function! s:AutoUpdate(fname, force, ...) abort + call tagbar#debug#log('AutoUpdate called [' . a:fname . ']') + + " Whether we want to skip actually displaying the tags in Tagbar and only + " update the fileinfo let no_display = a:0 > 0 ? a:1 : 0 if !has('win32') && has('lambda') From bd306cf6fc27f0b1f915014b3b51edbe7aecbc7c Mon Sep 17 00:00:00 2001 From: raven42 Date: Fri, 25 Sep 2020 15:49:44 -0500 Subject: [PATCH 6/6] Correct no_display for sync call --- autoload/tagbar.vim | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/autoload/tagbar.vim b/autoload/tagbar.vim index 1c77c186..11d5b26c 100755 --- a/autoload/tagbar.vim +++ b/autoload/tagbar.vim @@ -2655,10 +2655,12 @@ function! s:AutoUpdate(fname, force, ...) abort " update the fileinfo let no_display = a:0 > 0 ? a:1 : 0 - if !has('win32') && has('lambda') + if !has('win32') && has('lambda') && has('timers') + call tagbar#debug#log('Performing async call to AutoUpdate_CB') call timer_start(0, { -> AutoUpdate_CB(a:fname, a:force, no_display)}) else - call AutoUpdate_CB(a:fname, a:force, a:0 < 0 ? a:1 : '0') + call tagbar#debug#log('Performing sync call to AutoUpdate_CB') + call AutoUpdate_CB(a:fname, a:force, no_display) endif endfunc