tpope / vim-commentary

commentary.vim: comment stuff out
http://www.vim.org/scripts/script.php?script_id=3695
5.9k stars 214 forks source link

Commenting is slow in Latex files #110

Closed urbainvaes closed 3 years ago

urbainvaes commented 5 years ago

Hi Tim,

With vimtex folds enabled, commenting long blocks of text with gcc is orders of magnitude slower than a visual block selection followed by I%<esc>. There is perhaps be a way to set foldmethod to manual before running the commenting function, and to restore the previous setting after?

MWE: this tex file and this .vimrc:

call plug#begin('~/.vim/plugged')
Plug 'lervag/vimtex'
Plug 'tpope/vim-commentary'
call plug#end()

let g:vimtex_fold_enabled=1
chrisbra commented 5 years ago

Yeah, it's vimtex foldexpression, that makes commenting of folded sections painfully slow, because it get's re-evaluated on every setline() call.

A workaround would be, to temporarily switch to a manual foldmethod, this patch should do it:

diff --git a/plugin/commentary.vim b/plugin/commentary.vim
index 862ca82..8a24497 100644
--- a/plugin/commentary.vim
+++ b/plugin/commentary.vim
@@ -33,6 +33,7 @@ function! s:go(...) abort
   else
     let [lnum1, lnum2] = [line("'["), line("']")]
   endif
+  let [foldmethod, &fdm] = [&foldmethod, 'manual']

   let [l, r] = s:surroundings()
   let uncomment = 2
@@ -63,7 +64,7 @@ function! s:go(...) abort
     set modelines=0
     silent doautocmd User CommentaryPost
   finally
-    let &modelines = modelines
+    let [&modelines, &foldmethod] = [modelines, foldmethod]
   endtry
   return ''
 endfunction
kiryph commented 5 years ago

I had raised a related issue on the issue tracker of vimtex: https://github.com/lervag/vimtex/issues/944

Using the plugin https://github.com/Konfekt/FastFold or neovim helped me.

urbainvaes commented 4 years ago

@chrisbra Thanks, this patch looks nice! It'd be great if it could be merged in the plugin! :)

tpope commented 3 years ago

My only issue with that patch is I hate to taint :verbose set foldmethod?, but if this is a big problem for people I guess that is an acceptable trade-off.