From d398a0278c889248d4250856508b45994ade91bb Mon Sep 17 00:00:00 2001 From: Jaehwang Jerry Jung Date: Sat, 31 Jul 2021 00:43:43 +0900 Subject: [PATCH 1/4] =?UTF-8?q?[=20\t]=20=E2=86=92=20\s,=20[^=20\t]=20?= =?UTF-8?q?=E2=86=92=20\S?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autoload/nerdcommenter.vim | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/autoload/nerdcommenter.vim b/autoload/nerdcommenter.vim index 721923d..3e11e2b 100644 --- a/autoload/nerdcommenter.vim +++ b/autoload/nerdcommenter.vim @@ -506,7 +506,7 @@ function! s:CreateDelimMapFromCms() abort return delims endif return { - \ 'left': substitute(&commentstring, '\([^ \t]*\)\s*%s.*', '\1', ''), + \ 'left': substitute(&commentstring, '\(\S*\)\s*%s.*', '\1', ''), \ 'right': substitute(&commentstring, '.*%s\s*\(.*\)', '\1', 'g'), \ 'nested': 0, \ 'leftAlt': '', @@ -1091,7 +1091,7 @@ function! s:CommentRegion(topLine, topCol, bottomLine, bottomCol, forceNested) a "comment the bottom line let bottom = getline(a:bottomLine) - let numLeadingSpacesTabs = strlen(substitute(bottom, '^\([ \t]*\).*$', '\1', '')) + let numLeadingSpacesTabs = strlen(substitute(bottom, '^\(\s*\).*$', '\1', '')) call s:CommentBlock(a:bottomLine, a:bottomLine, numLeadingSpacesTabs+1, a:bottomCol, a:forceNested) endif @@ -1236,7 +1236,7 @@ function! nerdcommenter#Comment(mode, type) range abort for i in range(firstLine, lastLine) let theLine = getline(i) " if have one line no comment(not include blank/whitespace-only lines), then comment all lines - if theLine =~# '[^ \t]\+' && !s:IsInSexyComment(firstLine) && !s:IsCommentedFromStartOfLine(s:Left(), theLine) && !s:IsCommentedFromStartOfLine(s:Left({'alt': 1}), theLine) + if theLine =~# '\S\+' && !s:IsInSexyComment(firstLine) && !s:IsCommentedFromStartOfLine(s:Left(), theLine) && !s:IsCommentedFromStartOfLine(s:Left({'alt': 1}), theLine) let l:commentAllLines = 1 break else @@ -1671,7 +1671,7 @@ function! s:UncommentLinesSexy(topline, bottomline) abort let theLine = getline(a:topline) " if the first line contains only the left delimiter then just delete it - if theLine =~# '^[ \t]*' . left . '[ \t]*$' && !g:NERDCompactSexyComs + if theLine =~# '^\s*' . left . '\s*$' && !g:NERDCompactSexyComs call cursor(a:topline, 1) normal! dd let bottomline = bottomline - 1 @@ -1695,7 +1695,7 @@ function! s:UncommentLinesSexy(topline, bottomline) abort let theLine = getline(bottomline) " if the bottomline contains only the right delimiter then just delete it - if theLine =~# '^[ \t]*' . right . '[ \t]*$' + if theLine =~# '^\s*' . right . '\s*$' call cursor(bottomline, 1) normal! dd @@ -1712,7 +1712,7 @@ function! s:UncommentLinesSexy(topline, bottomline) abort " if the last line also starts with a sexy comment marker then we " remove this as well - if theLine =~# '^[ \t]*' . sexyComMarker + if theLine =~# '^\s*' . sexyComMarker " remove the sexyComMarker. If there is a space after it then " remove that too @@ -1836,7 +1836,7 @@ endfunction " Function: s:AddLeftDelim(delim, theLine) " Args: function! s:AddLeftDelim(delim, theLine) abort - return substitute(a:theLine, '^\([ \t]*\)', '\1' . a:delim, '') + return substitute(a:theLine, '^\(\s*\)', '\1' . a:delim, '') endfunction " Function: s:AddLeftDelimAligned(delim, theLine) @@ -1906,7 +1906,7 @@ function! s:CanCommentLine(forceNested, lineNum) abort " make sure we don't comment lines that are just spaces or tabs or empty, " unless configured otherwise - if g:NERDCommentEmptyLines ==# 0 && theLine =~# "^[ \t]*$" + if g:NERDCommentEmptyLines ==# 0 && theLine =~# "^\s*$" return 0 endif @@ -1973,7 +1973,7 @@ function! s:CanToggleCommentLine(forceNested, lineNum) abort " make sure we don't comment lines that are just spaces or tabs or empty, " unless configured otherwise - if g:NERDCommentEmptyLines ==# 0 && theLine =~# "^[ \t]*$" + if g:NERDCommentEmptyLines ==# 0 && theLine =~# "^\s*$" return 0 endif @@ -2194,16 +2194,16 @@ function! s:FindBoundingLinesOfSexyCom(lineNum) abort let theLine = getline(currentLine) "check if the current line is the top of the sexy comment - if currentLine <= a:lineNum && theLine =~# '^[ \t]*' . left && theLine !~# '.*' . right && currentLine < s:NumLinesInBuf() + if currentLine <= a:lineNum && theLine =~# '^\s*' . left && theLine !~# '.*' . right && currentLine < s:NumLinesInBuf() let top = currentLine let currentLine = a:lineNum "check if the current line is the bottom of the sexy comment - elseif theLine =~# '^[ \t]*' . right && theLine !~# '.*' . left && currentLine > 1 + elseif theLine =~# '^\s*' . right && theLine !~# '.*' . left && currentLine > 1 let bottom = currentLine "the right delimiter is on the same line as the last sexyComMarker - elseif theLine =~# '^[ \t]*' . sexyComMarker . '.*' . right + elseif theLine =~# '^\s*' . sexyComMarker . '.*' . right let bottom = currentLine "we have not found the top or bottom line so we assume currentLine is an @@ -2212,7 +2212,7 @@ function! s:FindBoundingLinesOfSexyCom(lineNum) abort "if the line doesn't start with a sexyComMarker then it is not a sexy "comment - if theLine !~# '^[ \t]*' . sexyComMarker + if theLine !~# '^\s*' . sexyComMarker return [] endif @@ -2552,7 +2552,7 @@ function! s:IsDelimValid(delimiter, delIndx, line) abort "if the delimiter is on the very first char of the line or is the "first non-tab/space char on the line then it is a valid comment delimiter - if a:delIndx ==# 0 || a:line =~# "^[ \t]\\{" . a:delIndx . "\\}\".*$" + if a:delIndx ==# 0 || a:line =~# "^\s\\{" . a:delIndx . "\\}\".*$" return 1 endif @@ -2653,7 +2653,7 @@ function! s:IsSexyComment(topline, bottomline) abort endif "if the top line doesn't begin with a left delimiter then the comment isn't sexy - if getline(a:topline) !~# '^[ \t]*' . left + if getline(a:topline) !~# '^\s*' . left return 0 endif @@ -2681,7 +2681,7 @@ function! s:IsSexyComment(topline, bottomline) abort while currentLine < a:bottomline let theLine = getline(currentLine) - if theLine !~# '^[ \t]*' . sexyComMarker + if theLine !~# '^\s*' . sexyComMarker return 0 endif @@ -2780,12 +2780,12 @@ function! s:LeftMostIndx(countCommentedLines, countEmptyLines, topline, bottomli " get the next line and if it is allowed to be commented, or is not " commented, check it let theLine = getline(currentLine) - if a:countEmptyLines || theLine !~# '^[ \t]*$' + if a:countEmptyLines || theLine !~# '^\s*$' if a:countCommentedLines || (!s:IsCommented(s:Left(), s:Right(), theLine) && !s:IsCommented(s:Left({'alt': 1}), s:Right({'alt': 1}), theLine)) " convert spaces to tabs and get the number of leading spaces for " this line and update leftMostIndx if need be let theLine = s:ConvertLeadingTabsToSpaces(theLine) - let leadSpaceOfLine = strlen( substitute(theLine, '\(^[ \t]*\).*$','\1','') ) + let leadSpaceOfLine = strlen( substitute(theLine, '\(^\s*\).*$','\1','') ) if leadSpaceOfLine < leftMostIndx let leftMostIndx = leadSpaceOfLine endif @@ -2963,7 +2963,7 @@ function! s:RightMostIndx(countCommentedLines, countEmptyLines, topline, bottoml " get the next line and see if it is commentable, otherwise it doesn't " count let theLine = getline(currentLine) - if a:countEmptyLines || theLine !~# '^[ \t]*$' + if a:countEmptyLines || theLine !~# '^\s*$' if a:countCommentedLines || (!s:IsCommented(s:Left(), s:Right(), theLine) && !s:IsCommented(s:Left({'alt': 1}), s:Right({'alt': 1}), theLine)) From ed591cb2a20b9daa9c6945c40d51a6d279305c00 Mon Sep 17 00:00:00 2001 From: Jaehwang Jerry Jung Date: Sat, 31 Jul 2021 01:11:39 +0900 Subject: [PATCH 2/4] =?UTF-8?q?substitute(..,=20'..\(pat\)..',=20'\1')=20?= =?UTF-8?q?=E2=86=92=20matchstr()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autoload/nerdcommenter.vim | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/autoload/nerdcommenter.vim b/autoload/nerdcommenter.vim index 3e11e2b..40475fb 100644 --- a/autoload/nerdcommenter.vim +++ b/autoload/nerdcommenter.vim @@ -506,8 +506,8 @@ function! s:CreateDelimMapFromCms() abort return delims endif return { - \ 'left': substitute(&commentstring, '\(\S*\)\s*%s.*', '\1', ''), - \ 'right': substitute(&commentstring, '.*%s\s*\(.*\)', '\1', 'g'), + \ 'left': matchstr(&commentstring, '^\S*\ze\s*%s'), + \ 'right': matchstr(&commentstring, '%s\s*\zs.*$'), \ 'nested': 0, \ 'leftAlt': '', \ 'rightAlt': '', @@ -1091,7 +1091,7 @@ function! s:CommentRegion(topLine, topCol, bottomLine, bottomCol, forceNested) a "comment the bottom line let bottom = getline(a:bottomLine) - let numLeadingSpacesTabs = strlen(substitute(bottom, '^\(\s*\).*$', '\1', '')) + let numLeadingSpacesTabs = strlen(matchstr(bottom, '^\s*')) call s:CommentBlock(a:bottomLine, a:bottomLine, numLeadingSpacesTabs+1, a:bottomCol, a:forceNested) endif @@ -2450,7 +2450,7 @@ endfunction " -left: the left delimiter to check for function! s:IsCommentedFromStartOfLine(left, line) abort let theLine = s:ConvertLeadingTabsToSpaces(a:line) - let numSpaces = strlen(substitute(theLine, '^\( *\).*$', '\1', '')) + let numSpaces = strlen(matchstr(theLine, '^ *')) let delimIndx = s:FindDelimiterIndex(a:left, theLine) return delimIndx ==# numSpaces endfunction @@ -2785,7 +2785,7 @@ function! s:LeftMostIndx(countCommentedLines, countEmptyLines, topline, bottomli " convert spaces to tabs and get the number of leading spaces for " this line and update leftMostIndx if need be let theLine = s:ConvertLeadingTabsToSpaces(theLine) - let leadSpaceOfLine = strlen( substitute(theLine, '\(^\s*\).*$','\1','') ) + let leadSpaceOfLine = strlen(matchstr(theLine, '^\s*')) if leadSpaceOfLine < leftMostIndx let leftMostIndx = leadSpaceOfLine endif @@ -2833,7 +2833,7 @@ endfunction " Function: s:NumberOfLeadingTabs(s) " returns the number of leading tabs in the given string function! s:NumberOfLeadingTabs(s) abort - return strlen(substitute(a:s, '^\(\t*\).*$', '\1', '')) + return strlen(matchstr(a:s, '^\t*')) endfunction " Function: s:NumLinesInBuf() From 576bd64482df3caafadb5852c985421fb0dc32ae Mon Sep 17 00:00:00 2001 From: Jaehwang Jerry Jung Date: Sun, 1 Aug 2021 00:43:34 +0900 Subject: [PATCH 3/4] =?UTF-8?q?{Save,Restore}ScreenState=20=E2=86=92=20win?= =?UTF-8?q?{save,rest}view?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autoload/nerdcommenter.vim | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/autoload/nerdcommenter.vim b/autoload/nerdcommenter.vim index 40475fb..b022a9e 100644 --- a/autoload/nerdcommenter.vim +++ b/autoload/nerdcommenter.vim @@ -1259,9 +1259,9 @@ function! nerdcommenter#Comment(mode, type) range abort endtry elseif a:type ==? 'ToEOL' - call s:SaveScreenState() + let view = winsaveview() call s:CommentBlock(firstLine, firstLine, col('.'), col('$')-1, 1) - call s:RestoreScreenState() + call winrestview(view) elseif a:type ==? 'Append' call s:AppendCommentToLine() @@ -2907,21 +2907,6 @@ function! s:ReplaceRightMostDelim(toReplace, replacor, str) abort return line endfunction -"FUNCTION: s:RestoreScreenState() -" -"Sets the screen state back to what it was when s:SaveScreenState was last -"called. -" -function! s:RestoreScreenState() abort - if !exists('t:NERDComOldTopLine') || !exists('t:NERDComOldPos') - throw 'NERDCommenter exception: cannot restore screen' - endif - - call cursor(t:NERDComOldTopLine, 0) - normal! zt - call setpos('.', t:NERDComOldPos) -endfunction - " Function: s:Right(...) " returns right delimiter data function! s:Right(...) abort @@ -2983,14 +2968,6 @@ function! s:RightMostIndx(countCommentedLines, countEmptyLines, topline, bottoml return rightMostIndx endfunction -"FUNCTION: s:SaveScreenState() -"Saves the current cursor position in the current buffer and the window -"scroll position -function! s:SaveScreenState() abort - let t:NERDComOldPos = getpos('.') - let t:NERDComOldTopLine = line('w0') -endfunction - " Function: s:SwapOuterMultiPartDelimsForPlaceHolders(line) " This function takes a line and swaps the outer most multi-part delimiters for " place holders From f7333809b6e8bd28ddddba8c877c5ac4a8576af6 Mon Sep 17 00:00:00 2001 From: Jaehwang Jerry Jung Date: Sun, 1 Aug 2021 00:57:34 +0900 Subject: [PATCH 4/4] Prefer single quoted strings --- autoload/nerdcommenter.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autoload/nerdcommenter.vim b/autoload/nerdcommenter.vim index b022a9e..c005fa8 100644 --- a/autoload/nerdcommenter.vim +++ b/autoload/nerdcommenter.vim @@ -1906,7 +1906,7 @@ function! s:CanCommentLine(forceNested, lineNum) abort " make sure we don't comment lines that are just spaces or tabs or empty, " unless configured otherwise - if g:NERDCommentEmptyLines ==# 0 && theLine =~# "^\s*$" + if g:NERDCommentEmptyLines ==# 0 && theLine =~# '^\s*$' return 0 endif @@ -1973,7 +1973,7 @@ function! s:CanToggleCommentLine(forceNested, lineNum) abort " make sure we don't comment lines that are just spaces or tabs or empty, " unless configured otherwise - if g:NERDCommentEmptyLines ==# 0 && theLine =~# "^\s*$" + if g:NERDCommentEmptyLines ==# 0 && theLine =~# '^\s*$' return 0 endif