pangloss / vim-javascript

Vastly improved Javascript indentation and syntax support in Vim.
http://www.vim.org/scripts/script.php?script_id=4452
3.79k stars 358 forks source link

Performance suggestion for foldmethod=syntax #1110

Open tandrewnichols opened 6 years ago

tandrewnichols commented 6 years ago

I spent some time tracking down sudden sluggishness in javascript files to discover it was this plugin (and then of course read the README where it actually says the performance might degrade 🤦‍♂️), but I think I might have found a workaround for the best of both worlds (I haven't experimented heavily with this, but it seems to be working for folding based on syntax without the normal sluggishness). I'm opening as an Issue rather than a PR just to make sure there aren't flaws in this method that I haven't spotted yet. I'm mapping <CR> to za to fold in normal mode (although the principle here would hold regardless of what mapping you're using), and I set it up like this:

function! s:SyntaxFold(map)
  let prev_method = &fdm
  set fdm=syntax
  exec "normal!" a:map
  exec "set fdm=" . prev_method
endfunction

nnoremap <CR> :call <SID>SyntaxFold('za')<CR>
nnoremap <leader><CR> :call <SID>SyntaxFold('zA')<CR>

So basically turn on syntax-based folding, do the folding, then turn it back off again. Thoughts or any flaws with this approach?

heisian commented 6 years ago

Was wondering why my vim was being so... slow... I like this solution, the only caveat being that one has to remap all of the z(x) commands if one wants the full gamut.

amadeus commented 6 years ago

Also worth checking out:

https://github.com/Konfekt/FastFold

ivangeorgiew commented 6 years ago

This is my setup for folding for some months now. It is extremely fast and I haven't found any bugs. https://github.com/ivangeorgiew/dotfiles/blob/master/.vimrc

You will need https://github.com/Konfekt/FastFold and the function FoldExprJS() from my vimrc. Lastly you will need to change s:tabstop to whatever spacing you use.

If you have questions, let me know. This function was extremely hard to think of btw :D