changeset 267:6030953258fe

On Windows, kill ongoing jobs before leaving so we can clean-up temp files
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 12 May 2022 09:20:21 -0700
parents 1b74fb3819e1
children 6b3ab48ea3c0
files autoload/gutentags.vim autoload/gutentags/cscope.vim autoload/gutentags/ctags.vim autoload/gutentags/gtags_cscope.vim plugin/gutentags.vim
diffstat 5 files changed, 53 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/autoload/gutentags.vim	Fri Feb 25 22:12:42 2022 +0800
+++ b/autoload/gutentags.vim	Thu May 12 09:20:21 2022 -0700
@@ -354,6 +354,24 @@
     let g:__gutentags_vim_is_leaving = 1
 endfunction
 
+function! gutentags#on_vim_leave() abort
+    if has('win32') && !has('nvim')
+        " Vim8 doesn't seem to be killing child processes soon enough for
+        " us to clean things up inside this plugin, so do it ourselves.
+        " TODO: test other platforms and other vims
+        for module in g:gutentags_modules
+            for upd_info in s:update_in_progress[module]
+                let l:job = upd_info[1]
+                call job_stop(l:job, "term")
+                let l:status = job_status(l:job)
+                if l:status == "run"
+                    call job_stop(l:job, "kill")
+                endif
+            endfor
+        endfor
+    endif
+endfunction
+
 " }}}
 
 "  Job Management {{{
@@ -366,10 +384,14 @@
     let s:update_in_progress[module] = []
 endfor
 
+" Adds a started job to the list of ongoing updates.
+" Must pass the tags file being created/updated, and the job data as
+" returned by the gutentags#start_job function
 function! gutentags#add_job(module, tags_file, data) abort
     call add(s:update_in_progress[a:module], [a:tags_file, a:data])
 endfunction
 
+" Finds an ongoing job by tags file
 function! gutentags#find_job_index_by_tags_file(module, tags_file) abort
     let l:idx = -1
     for upd_info in s:update_in_progress[a:module]
@@ -381,6 +403,7 @@
     return -1
 endfunction
 
+" Finds an ongoing job by job data
 function! gutentags#find_job_index_by_data(module, data) abort
     let l:idx = -1
     for upd_info in s:update_in_progress[a:module]
@@ -392,16 +415,19 @@
     return -1
 endfunction
 
+" Gets the tags file of a given job
 function! gutentags#get_job_tags_file(module, job_idx) abort
     return s:update_in_progress[a:module][a:job_idx][0]
 endfunction
 
+" Gets the job data of the i-th job
 function! gutentags#get_job_data(module, job_idx) abort
     return s:update_in_progress[a:module][a:job_idx][1]
 endfunction
 
+" Removes the i-th job from the ongoing jobs
 function! gutentags#remove_job(module, job_idx) abort
-    let l:tags_file = s:update_in_progress[a:module][a:job_idx][0]
+    let [l:tags_file, l:job_data] = s:update_in_progress[a:module][a:job_idx]
     call remove(s:update_in_progress[a:module], a:job_idx)
 
     " Run the user callback for finished jobs.
@@ -431,11 +457,14 @@
     else
         call gutentags#trace("Finished ".a:module." job.")
     endif
+
+    return [l:tags_file, l:job_data]
 endfunction
 
+" Removes the job from the ongoing jobs given its job data
 function! gutentags#remove_job_by_data(module, data) abort
     let l:idx = gutentags#find_job_index_by_data(a:module, a:data)
-    call gutentags#remove_job(a:module, l:idx)
+    return gutentags#remove_job(a:module, l:idx)
 endfunction
 
 " }}}
--- a/autoload/gutentags/cscope.vim	Fri Feb 25 22:12:42 2022 +0800
+++ b/autoload/gutentags/cscope.vim	Thu May 12 09:20:21 2022 -0700
@@ -87,6 +87,12 @@
                     \"cscope job failed, returned: ".
                     \string(a:exit_val))
     endif
+    if has('win32') && g:__gutentags_vim_is_leaving
+        " The process got interrupted because Vim is quitting.
+        " Remove the db file on Windows because there's no `trap`
+        " statement in the update script.
+        try | call delete(l:dbfile_path) | endtry
+    endif
 endfunction
 
 " }}}
--- a/autoload/gutentags/ctags.vim	Fri Feb 25 22:12:42 2022 +0800
+++ b/autoload/gutentags/ctags.vim	Thu May 12 09:20:21 2022 -0700
@@ -214,12 +214,20 @@
 endfunction
 
 function! gutentags#ctags#on_job_exit(job, exit_val) abort
-    call gutentags#remove_job_by_data('ctags', a:job)
+    let [l:tags_file, l:job_data] = gutentags#remove_job_by_data('ctags', a:job)
 
     if a:exit_val != 0 && !g:__gutentags_vim_is_leaving
         call gutentags#warning("ctags job failed, returned: ".
                     \string(a:exit_val))
     endif
+    if has('win32') && g:__gutentags_vim_is_leaving
+        " The process got interrupted because Vim is quitting.
+        " Remove the tags and lock files on Windows because there's no `trap`
+        " statement in update script.
+        try | call delete(l:tags_file) | endtry
+        try | call delete(l:tags_file.'.temp') | endtry
+        try | call delete(l:tags_file.'.lock') | endtry
+    endif
 endfunction
 
 " }}}
--- a/autoload/gutentags/gtags_cscope.vim	Fri Feb 25 22:12:42 2022 +0800
+++ b/autoload/gutentags/gtags_cscope.vim	Thu May 12 09:20:21 2022 -0700
@@ -117,6 +117,12 @@
                     \"gtags-cscope job failed, returned: ".
                     \string(a:exit_val))
     endif
+    if has('win32') && g:__gutentags_vim_is_leaving
+        " The process got interrupted because Vim is quitting.
+        " Remove the db file on Windows because there's no `trap`
+        " statement in the update script.
+        try | call delete(l:dbfile_path) | endtry
+    endif
 endfunction
 
 " }}}
--- a/plugin/gutentags.vim	Fri Feb 25 22:12:42 2022 +0800
+++ b/plugin/gutentags.vim	Thu May 12 09:20:21 2022 -0700
@@ -98,6 +98,7 @@
     autocmd BufNewFile,BufReadPost *  call gutentags#setup_gutentags()
     autocmd VimEnter               *  if expand('<amatch>')==''|call gutentags#setup_gutentags()|endif
     autocmd VimLeavePre            *  call gutentags#on_vim_leave_pre()
+    autocmd VimLeave               *  call gutentags#on_vim_leave()
 augroup end
 
 " }}}