Skip to content

Commit 9a32fd2

Browse files
fcyingalerque
authored andcommitted
Add NERDToggleCheckAllLines option to NERDCommenterToggle (#334)
1 parent e679d8a commit 9a32fd2

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ let g:NERDCommentEmptyLines = 1
9494
9595
" Enable trimming of trailing whitespace when uncommenting
9696
let g:NERDTrimTrailingWhitespace = 1
97+
98+
" Enable NERDCommenterToggle to check all selected lines is commented or not
99+
let g:NERDToggleCheckAllLines = 1
97100
```
98101

99102
### Default mappings

doc/NERD_commenter.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,9 @@ change the filetype back: >
488488
one of 'none', 'left', 'start', or
489489
'both'.
490490

491+
|'NERDToggleCheckAllLines'| Enable NERDCommenterToggle to check
492+
all selected lines is commented or not.
493+
491494
------------------------------------------------------------------------------
492495
4.3 Options details *NERDComOptionsDetails*
493496

@@ -800,6 +803,15 @@ When this option is set to 1, comments are nested automatically. That is, if
800803
you hit |<Leader>|cc on a line that is already commented it will be commented
801804
again.
802805

806+
------------------------------------------------------------------------------
807+
*'NERDToggleCheckAllLines'*
808+
Values: 0 or 1.
809+
Default 0.
810+
811+
When this option is set to 1, NERDCommenterToggle will check all selected line,
812+
if there have oneline not be commented, then comment all lines.
813+
814+
803815
------------------------------------------------------------------------------
804816
3.3 Default delimiter customisation *NERDComDefaultDelims*
805817

plugin/NERD_commenter.vim

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ call s:InitVariable("g:NERDRPlace", "<]")
6565
call s:InitVariable("g:NERDSpaceDelims", 0)
6666
call s:InitVariable("g:NERDDefaultAlign", "none")
6767
call s:InitVariable("g:NERDTrimTrailingWhitespace", 0)
68+
call s:InitVariable("g:NERDToggleCheckAllLines", 0)
6869

6970
let s:NERDFileNameEscape="[]#*$%'\" ?`!&();<>\\"
7071

@@ -1258,12 +1259,29 @@ function! NERDComment(mode, type) range
12581259
endtry
12591260

12601261
elseif a:type ==? 'Toggle'
1261-
let theLine = getline(firstLine)
1262-
1263-
if s:IsInSexyComment(firstLine) || s:IsCommentedFromStartOfLine(s:Left(), theLine) || s:IsCommentedFromStartOfLine(s:Left({'alt': 1}), theLine)
1264-
call s:UncommentLines(firstLine, lastLine)
1262+
if g:NERDToggleCheckAllLines ==# 0
1263+
let theLine = getline(firstLine)
1264+
if s:IsInSexyComment(firstLine) || s:IsCommentedFromStartOfLine(s:Left(), theLine) || s:IsCommentedFromStartOfLine(s:Left({'alt': 1}), theLine)
1265+
call s:UncommentLines(firstLine, lastLine)
1266+
else
1267+
call s:CommentLinesToggle(forceNested, firstLine, lastLine)
1268+
endif
12651269
else
1270+
let l:commentAllLines = 0
1271+
for i in range(firstLine, lastLine)
1272+
let theLine = getline(i)
1273+
" if have one line no comment, then comment all lines
1274+
if !s:IsInSexyComment(firstLine) && !s:IsCommentedFromStartOfLine(s:Left(), theLine) && !s:IsCommentedFromStartOfLine(s:Left({'alt': 1}), theLine)
1275+
let l:commentAllLines = 1
1276+
break
1277+
else
1278+
endif
1279+
endfor
1280+
if l:commentAllLines ==# 1
12661281
call s:CommentLinesToggle(forceNested, firstLine, lastLine)
1282+
else
1283+
call s:UncommentLines(firstLine, lastLine)
1284+
endif
12671285
endif
12681286

12691287
elseif a:type ==? 'Minimal'

0 commit comments

Comments
 (0)