mirror of
https://github.com/preservim/nerdcommenter.git
synced 2025-11-09 02:13:45 -05:00
Merge pull request #232 from suoto/master
Adding config state handling before/after line comments
This commit is contained in:
@@ -1100,9 +1100,6 @@ endfunction
|
|||||||
" 'Nested', 'ToEOL', 'Append', 'Insert', 'Uncomment', 'Yank'
|
" 'Nested', 'ToEOL', 'Append', 'Insert', 'Uncomment', 'Yank'
|
||||||
function! NERDComment(mode, type) range
|
function! NERDComment(mode, type) range
|
||||||
let isVisual = a:mode =~ '[vsx]'
|
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
|
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)
|
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
|
|||||||
let firstLine = a:firstline
|
let firstLine = a:firstline
|
||||||
let lastLine = a:lastline
|
let lastLine = a:lastline
|
||||||
endif
|
endif
|
||||||
|
"
|
||||||
|
" Save options we need to change so we can recover them later
|
||||||
|
let state = s:SetupStateBeforeLineComment(firstLine, lastLine)
|
||||||
|
|
||||||
let countWasGiven = (!isVisual && firstLine != lastLine)
|
let countWasGiven = (!isVisual && firstLine != lastLine)
|
||||||
|
|
||||||
@@ -1195,7 +1195,7 @@ function! NERDComment(mode, type) range
|
|||||||
execute firstLine .','. lastLine .'call NERDComment("'. a:mode .'", "Comment")'
|
execute firstLine .','. lastLine .'call NERDComment("'. a:mode .'", "Comment")'
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let &ignorecase = oldIgnoreCase
|
call s:RecoverStateAfterLineComment(state)
|
||||||
|
|
||||||
if isVisual
|
if isVisual
|
||||||
let nlines = lastLine - firstLine
|
let nlines = lastLine - firstLine
|
||||||
@@ -1305,6 +1305,51 @@ function s:RemoveDelimiters(left, right, line)
|
|||||||
return line
|
return line
|
||||||
endfunction
|
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
|
" Function: s:UncommentLines(topLine, bottomLine) {{{2
|
||||||
" This function uncomments the given lines
|
" This function uncomments the given lines
|
||||||
"
|
"
|
||||||
|
|||||||
Reference in New Issue
Block a user