Skip to content

Commit b2dca14

Browse files
committed
Merge pull request #232 from suoto/master
Adding config state handling before/after line comments
2 parents e6e67e9 + 94ecc6c commit b2dca14

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

plugin/NERD_commenter.vim

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,9 +1100,6 @@ endfunction
11001100
" 'Nested', 'ToEOL', 'Append', 'Insert', 'Uncomment', 'Yank'
11011101
function! NERDComment(mode, type) range
11021102
let isVisual = a:mode =~ '[vsx]'
1103-
" we want case sensitivity when commenting
1104-
let oldIgnoreCase = &ignorecase
1105-
set noignorecase
11061103

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

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

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

1198-
let &ignorecase = oldIgnoreCase
1198+
call s:RecoverStateAfterLineComment(state)
11991199

12001200
if isVisual
12011201
let nlines = lastLine - firstLine
@@ -1305,6 +1305,51 @@ function s:RemoveDelimiters(left, right, line)
13051305
return line
13061306
endfunction
13071307

1308+
" Function: s:SetupStateBeforeLineComment(topLine, bottomLine) {{{2
1309+
" Changes ignorecase and foldmethod options before commenting lines and saves
1310+
" their original values in a dict, which is returned as a result
1311+
"
1312+
" Args:
1313+
" topLine: the top line of the visual selection to uncomment
1314+
" bottomLine: the bottom line of the visual selection to uncomment
1315+
"
1316+
" Return: a dict with the state prior to configuration changes
1317+
"
1318+
function s:SetupStateBeforeLineComment(topLine, bottomLine)
1319+
let state = {'foldmethod' : &foldmethod,
1320+
\'ignorecase' : &ignorecase}
1321+
1322+
" Vim's foldmethods are evaluated every time we use 'setline', which can
1323+
" make commenting wide ranges of lines VERY slow. We'll change it to
1324+
" manual, do the commenting stuff and recover it later. To avoid slowing
1325+
" down commenting few lines, we avoid doing this for ranges smaller than
1326+
" 10 lines
1327+
if a:bottomLine - a:topLine >= 10 && &foldmethod != "manual"
1328+
set foldmethod=manual
1329+
endif
1330+
1331+
" we want case sensitivity when commenting
1332+
set noignorecase
1333+
1334+
return state
1335+
endfunction
1336+
1337+
" Function: s:RecoverStateAfterLineComment(state) {{{2
1338+
" Receives the state returned by s:SetupStateBeforeLineComment and restores
1339+
" the state accordingly
1340+
"
1341+
" Args:
1342+
" state: the top line of the visual selection to uncomment
1343+
" bottomLine: the bottom line of the visual selection to uncomment
1344+
function s:RecoverStateAfterLineComment(state)
1345+
if a:state['foldmethod'] != &foldmethod
1346+
let &foldmethod = a:state['foldmethod']
1347+
endif
1348+
if a:state['ignorecase'] != &ignorecase
1349+
let &ignorecase = a:state['ignorecase']
1350+
endif
1351+
endfunction
1352+
13081353
" Function: s:UncommentLines(topLine, bottomLine) {{{2
13091354
" This function uncomments the given lines
13101355
"

0 commit comments

Comments
 (0)