Merge pull request #24 from ervandew/master

Default alignment option + ability to align at index 0.
This commit is contained in:
Caleb Maclennan 2016-05-23 19:08:24 +03:00
commit 34e0115ffa
2 changed files with 54 additions and 8 deletions

View File

@ -433,6 +433,9 @@ then the script would do a sexy comment on the last visual selection.
uncommenting. uncommenting.
|'NERDCompactSexyComs'| Specifies whether to use the compact |'NERDCompactSexyComs'| Specifies whether to use the compact
style sexy comments. style sexy comments.
|'NERDDefaultAlign'| Specifies the default alignment to use,
one of 'none', 'left', 'start', or
'both'.
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
4.3 Options details *NERDComOptionsDetails* 4.3 Options details *NERDComOptionsDetails*
@ -623,6 +626,14 @@ Otherwise they would become: >
int baz = foo + bar int baz = foo + bar
< <
Note: When using 'start' as the default alignment, the enabling of
NERDRemoveExtraSpaces will still result in the removal of a space after the
delimiter. This can be undesirable since aligning the delimiters at the very
start of the line (index 0) will usually result in spaces between the comment
delimiters and the text which probably shouldn't be removed. So when using
'start' as the default alignment, take care to also disable
NERDRemoveExtraSpaces.
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
*'NERDLPlace'* *'NERDLPlace'*
*'NERDRPlace'* *'NERDRPlace'*
@ -681,6 +692,17 @@ If you want spaces to be added then set NERDSpaceDelims to 1 in your vimrc.
See also |'NERDRemoveExtraSpaces'|. See also |'NERDRemoveExtraSpaces'|.
------------------------------------------------------------------------------
*'NERDDefaultAlign'*
Values: 'none', 'left', 'start', 'both'
Default 'none'.
Specifies the default alignment to use when inserting comments.
Note: When using 'start' as the default alignment be sure to disable
NERDRemoveExtraSpaces. See the note at the bottom of |NERDRemoveExtraSpaces|
for more details.
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
*'NERDCompactSexyComs'* *'NERDCompactSexyComs'*
Values: 0 or 1. Values: 0 or 1.

View File

@ -61,6 +61,7 @@ call s:InitVariable("g:NERDRemoveAltComs", 1)
call s:InitVariable("g:NERDRemoveExtraSpaces", 0) call s:InitVariable("g:NERDRemoveExtraSpaces", 0)
call s:InitVariable("g:NERDRPlace", "<]") call s:InitVariable("g:NERDRPlace", "<]")
call s:InitVariable("g:NERDSpaceDelims", 0) call s:InitVariable("g:NERDSpaceDelims", 0)
call s:InitVariable("g:NERDDefaultAlign", "none")
let s:NERDFileNameEscape="[]#*$%'\" ?`!&();<>\\" let s:NERDFileNameEscape="[]#*$%'\" ?`!&();<>\\"
@ -469,6 +470,15 @@ function s:SetUpForNewFiletype(filetype, forceReset)
endfunction endfunction
function s:CreateDelimMapFromCms() function s:CreateDelimMapFromCms()
if &ft == '' && exists('g:NERDDefaultDelims')
let delims = g:NERDDefaultDelims
for i in ['left', 'leftAlt', 'right', 'rightAlt']
if !has_key(delims, i)
let delims[i] = ''
endif
endfor
return delims
endif
return { return {
\ 'left': substitute(&commentstring, '\([^ \t]*\)\s*%s.*', '\1', ''), \ 'left': substitute(&commentstring, '\([^ \t]*\)\s*%s.*', '\1', ''),
\ 'right': substitute(&commentstring, '.*%s\s*\(.*\)', '\1', 'g'), \ 'right': substitute(&commentstring, '.*%s\s*\(.*\)', '\1', 'g'),
@ -679,13 +689,13 @@ endfunction
" Args: " Args:
" -forceNested: a flag indicating whether the called is requesting the comment " -forceNested: a flag indicating whether the called is requesting the comment
" to be nested if need be " to be nested if need be
" -align: should be "left" or "both" or "none" " -align: should be "left", "start", "both" or "none"
" -firstLine/lastLine: the top and bottom lines to comment " -firstLine/lastLine: the top and bottom lines to comment
function s:CommentLines(forceNested, align, firstLine, lastLine) function s:CommentLines(forceNested, align, firstLine, lastLine)
" we need to get the left and right indexes of the leftmost char in the " we need to get the left and right indexes of the leftmost char in the
" block of of lines and the right most char so that we can do alignment of " block of of lines and the right most char so that we can do alignment of
" the delimiters if the user has specified " the delimiters if the user has specified
let leftAlignIndx = s:LeftMostIndx(a:forceNested, 0, a:firstLine, a:lastLine) let leftAlignIndx = a:align == "start" ? 0 : s:LeftMostIndx(a:forceNested, 0, a:firstLine, a:lastLine)
let rightAlignIndx = s:RightMostIndx(a:forceNested, 0, a:firstLine, a:lastLine) let rightAlignIndx = s:RightMostIndx(a:forceNested, 0, a:firstLine, a:lastLine)
" gotta add the length of the left delimiter onto the rightAlignIndx cos " gotta add the length of the left delimiter onto the rightAlignIndx cos
@ -713,7 +723,7 @@ function s:CommentLines(forceNested, align, firstLine, lastLine)
" check if we can comment this line " check if we can comment this line
if !isCommented || g:NERDUsePlaceHolders || s:Multipart() if !isCommented || g:NERDUsePlaceHolders || s:Multipart()
if a:align == "left" || a:align == "both" if a:align == "left" || a:align == "start" || a:align == "both"
let theLine = s:AddLeftDelimAligned(s:Left({'space': 1}), theLine, leftAlignIndx) let theLine = s:AddLeftDelimAligned(s:Left({'space': 1}), theLine, leftAlignIndx)
else else
let theLine = s:AddLeftDelim(s:Left({'space': 1}), theLine) let theLine = s:AddLeftDelim(s:Left({'space': 1}), theLine)
@ -917,6 +927,12 @@ endfunction
" -firstLine/lastLine: the top and bottom lines to comment " -firstLine/lastLine: the top and bottom lines to comment
function s:CommentLinesToggle(forceNested, firstLine, lastLine) function s:CommentLinesToggle(forceNested, firstLine, lastLine)
let currentLine = a:firstLine let currentLine = a:firstLine
let align = g:NERDDefaultAlign
let leftAlignIndx = align == "start" ? 0 : s:LeftMostIndx(a:forceNested, 0, a:firstLine, a:lastLine)
let rightAlignIndx = s:RightMostIndx(a:forceNested, 0, a:firstLine, a:lastLine)
let rightAlignIndx = rightAlignIndx + strlen(s:Left({'space': 1}))
while currentLine <= a:lastLine while currentLine <= a:lastLine
" get the next line, check commentability and convert spaces to tabs " get the next line, check commentability and convert spaces to tabs
@ -931,8 +947,16 @@ function s:CommentLinesToggle(forceNested, firstLine, lastLine)
let theLine = s:SwapOutterMultiPartDelimsForPlaceHolders(theLine) let theLine = s:SwapOutterMultiPartDelimsForPlaceHolders(theLine)
endif endif
let theLine = s:AddLeftDelim(s:Left({'space': 1}), theLine) if align == 'left' || align == 'start' || align == 'both'
let theLine = s:AddRightDelim(s:Right({'space': 1}), theLine) let theLine = s:AddLeftDelimAligned(s:Left({'space': 1}), theLine, leftAlignIndx)
else
let theLine = s:AddLeftDelim(s:Left({'space': 1}), theLine)
endif
if align == "both"
let theLine = s:AddRightDelimAligned(s:Right({'space': 1}), theLine, rightAlignIndx)
else
let theLine = s:AddRightDelim(s:Right({'space': 1}), theLine)
endif
endif endif
" restore leading tabs if appropriate " restore leading tabs if appropriate
@ -979,7 +1003,7 @@ function s:CommentRegion(topLine, topCol, bottomLine, bottomCol, forceNested)
let topOfRange = a:topLine+1 let topOfRange = a:topLine+1
let bottomOfRange = a:bottomLine-1 let bottomOfRange = a:bottomLine-1
if topOfRange <= bottomOfRange if topOfRange <= bottomOfRange
call s:CommentLines(a:forceNested, "none", topOfRange, bottomOfRange) call s:CommentLines(a:forceNested, g:NERDDefaultAlign, topOfRange, bottomOfRange)
endif endif
"comment the bottom line "comment the bottom line
@ -1076,7 +1100,7 @@ function! NERDComment(mode, type) range
elseif isVisual && visualmode() == "v" && (g:NERDCommentWholeLinesInVMode==0 || (g:NERDCommentWholeLinesInVMode==2 && s:HasMultipartDelims())) elseif isVisual && visualmode() == "v" && (g:NERDCommentWholeLinesInVMode==0 || (g:NERDCommentWholeLinesInVMode==2 && s:HasMultipartDelims()))
call s:CommentRegion(firstLine, firstCol, lastLine, lastCol, forceNested) call s:CommentRegion(firstLine, firstCol, lastLine, lastCol, forceNested)
else else
call s:CommentLines(forceNested, "none", firstLine, lastLine) call s:CommentLines(forceNested, g:NERDDefaultAlign, firstLine, lastLine)
endif endif
elseif a:type ==? 'AlignLeft' || a:type ==? 'AlignBoth' elseif a:type ==? 'AlignLeft' || a:type ==? 'AlignBoth'
@ -1095,7 +1119,7 @@ function! NERDComment(mode, type) range
try try
call s:CommentLinesSexy(firstLine, lastLine) call s:CommentLinesSexy(firstLine, lastLine)
catch /NERDCommenter.Delimiters/ catch /NERDCommenter.Delimiters/
call s:CommentLines(forceNested, "none", firstLine, lastLine) call s:CommentLines(forceNested, g:NERDDefaultAlign, firstLine, lastLine)
catch /NERDCommenter.Nesting/ catch /NERDCommenter.Nesting/
call s:NerdEcho("Sexy comment aborted. Nested sexy cannot be nested", 0) call s:NerdEcho("Sexy comment aborted. Nested sexy cannot be nested", 0)
endtry endtry