Skip to content

Commit

Permalink
Merge pull request #232 from suoto/master
Browse files Browse the repository at this point in the history
Adding config state handling before/after line comments
  • Loading branch information
alerque committed May 23, 2016
2 parents e6e67e9 + 94ecc6c commit b2dca14
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions plugin/NERD_commenter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1100,9 +1100,6 @@ endfunction
" 'Nested', 'ToEOL', 'Append', 'Insert', 'Uncomment', 'Yank'
function! NERDComment(mode, type) range
let isVisual = a:mode =~ '[vsx]'
" we want case sensitivity when commenting
let oldIgnoreCase = &ignorecase
set noignorecase

if !exists("g:did_load_ftplugin") || g:did_load_ftplugin != 1
call s:NerdEcho("filetype plugins should be enabled. See :help NERDComInstallation and :help :filetype-plugin-on", 0)
Expand All @@ -1117,6 +1114,9 @@ function! NERDComment(mode, type) range
let firstLine = a:firstline
let lastLine = a:lastline
endif
"
" Save options we need to change so we can recover them later
let state = s:SetupStateBeforeLineComment(firstLine, lastLine)

let countWasGiven = (!isVisual && firstLine != lastLine)

Expand Down Expand Up @@ -1195,7 +1195,7 @@ function! NERDComment(mode, type) range
execute firstLine .','. lastLine .'call NERDComment("'. a:mode .'", "Comment")'
endif

let &ignorecase = oldIgnoreCase
call s:RecoverStateAfterLineComment(state)

if isVisual
let nlines = lastLine - firstLine
Expand Down Expand Up @@ -1305,6 +1305,51 @@ function s:RemoveDelimiters(left, right, line)
return line
endfunction

" Function: s:SetupStateBeforeLineComment(topLine, bottomLine) {{{2
" Changes ignorecase and foldmethod options before commenting lines and saves
" their original values in a dict, which is returned as a result
"
" Args:
" topLine: the top line of the visual selection to uncomment
" bottomLine: the bottom line of the visual selection to uncomment
"
" Return: a dict with the state prior to configuration changes
"
function s:SetupStateBeforeLineComment(topLine, bottomLine)
let state = {'foldmethod' : &foldmethod,
\'ignorecase' : &ignorecase}

" Vim's foldmethods are evaluated every time we use 'setline', which can
" make commenting wide ranges of lines VERY slow. We'll change it to
" manual, do the commenting stuff and recover it later. To avoid slowing
" down commenting few lines, we avoid doing this for ranges smaller than
" 10 lines
if a:bottomLine - a:topLine >= 10 && &foldmethod != "manual"
set foldmethod=manual
endif

" we want case sensitivity when commenting
set noignorecase

return state
endfunction

" Function: s:RecoverStateAfterLineComment(state) {{{2
" Receives the state returned by s:SetupStateBeforeLineComment and restores
" the state accordingly
"
" Args:
" state: the top line of the visual selection to uncomment
" bottomLine: the bottom line of the visual selection to uncomment
function s:RecoverStateAfterLineComment(state)
if a:state['foldmethod'] != &foldmethod
let &foldmethod = a:state['foldmethod']
endif
if a:state['ignorecase'] != &ignorecase
let &ignorecase = a:state['ignorecase']
endif
endfunction

" Function: s:UncommentLines(topLine, bottomLine) {{{2
" This function uncomments the given lines
"
Expand Down

0 comments on commit b2dca14

Please sign in to comment.