Merge pull request #482 from tomtomjhj/insert

This commit is contained in:
Caleb Maclennan
2022-01-15 21:13:53 +03:00
committed by GitHub
2 changed files with 15 additions and 65 deletions

View File

@@ -1422,46 +1422,20 @@ function! s:PlaceDelimitersAndInsBetween() abort
let left = s:Left({'space': 1}) let left = s:Left({'space': 1})
let right = s:Right({'space': 1}) let right = s:Right({'space': 1})
let theLine = getline('.') " 0. Entered insert normal mode using <C-\><C-O> (:h i_CTRL-\_CTRL-O) to
let lineHasLeadTabs = s:HasLeadingTabs(theLine) || (theLine =~# '^ *$' && !&expandtab) " maintain the cursor position (from <Plug>NERDCommenterInsert).
" 1. Enter insert mode without changing the cursor position.
"convert tabs to spaces and adjust the cursors column to take this into " If the cursor is on EOL (right of the last char), use 'a'.
"account " Otherwise, use 'i'.
let untabbedCol = s:UntabbedCol(theLine, col('.')) let insert = col('.') > strlen(getline('.')) ? 'a' : 'i'
call setline(line('.'), s:ConvertLeadingTabsToSpaces(theLine)) " 2. Insert comment delimiters.
call cursor(line('.'), untabbedCol) " 3. Move the cursor to the left of the closing delimiter, without
" breaking undo sequence.
" get the length of the right delimiter " 4. Enter insert normal mode again without changing the cursor position.
let lenRight = strlen(right) " This ensures that returning to the insert mode after finishing the
" script execution does not move the cursor.
let isDelimOnEOL = col('.') >= strlen(getline('.')) " ( 1 ) ( 2 ) ( 3 ) ( 4 )
execute 'normal!' insert . left . right . repeat("\<C-G>U\<Left>", strchars(right)) . "\<C-\>\<C-O>"
" if the cursor is in the first col then we gotta insert rather than
" append the comment delimiters here
let insOrApp = (col('.')==1 ? 'i' : 'a')
" place the delimiters down. We do it differently depending on whether
" there is a left AND right delimiter
if lenRight > 0
execute ':normal! ' . insOrApp . left . right
execute ':normal! ' . lenRight . 'h'
else
execute ':normal! ' . insOrApp . left
endif
"if needed convert spaces back to tabs and adjust the cursors col
"accordingly
if lineHasLeadTabs
let tabbedCol = s:TabbedCol(getline('.'), col('.'))
call setline(line('.'), s:ConvertLeadingSpacesToTabs(getline('.')))
call cursor(line('.'), tabbedCol)
endif
if isDelimOnEOL && lenRight ==# 0
startinsert!
else
call feedkeys('a', 'ni')
endif
endfunction endfunction
" Function: s:RemoveDelimiters(left, right, line) " Function: s:RemoveDelimiters(left, right, line)
@@ -3022,18 +2996,6 @@ function! s:SwapOuterPlaceHoldersForMultiPartDelims(line) abort
return line return line
endfunction endfunction
" Function: s:TabbedCol(line, col)
" Gets the col number for given line and existing col number. The new col
" number is the col number when all leading spaces are converted to tabs
" Args:
" -line:the line to get the rel col for
" -col: the abs col
function! s:TabbedCol(line, col) abort
let lineTruncated = strpart(a:line, 0, a:col)
let lineSpacesToTabs = substitute(lineTruncated, s:TabSpace(), '\t', 'g')
return strlen(lineSpacesToTabs)
endfunction
"FUNCTION: s:TabSpace() "FUNCTION: s:TabSpace()
"returns a string of spaces equal in length to &tabstop "returns a string of spaces equal in length to &tabstop
function! s:TabSpace() abort function! s:TabSpace() abort
@@ -3054,15 +3016,3 @@ endfunction
function! s:UnEsc(str, escChar) abort function! s:UnEsc(str, escChar) abort
return substitute(a:str, a:escChar, '', 'g') return substitute(a:str, a:escChar, '', 'g')
endfunction endfunction
" Function: s:UntabbedCol(line, col)
" Takes a line and a col and returns the absolute column of col taking into
" account that a tab is worth 3 or 4 (or whatever) spaces.
" Args:
" -line:the line to get the abs col for
" -col: the col that doesn't take into account tabs
function! s:UntabbedCol(line, col) abort
let lineTruncated = strpart(a:line, 0, a:col)
let lineTabsToSpaces = substitute(lineTruncated, '\t', s:TabSpace(), 'g')
return strlen(lineTabsToSpaces)
endfunction

View File

@@ -125,7 +125,7 @@ function! NERDCommentIsCharCommented(line, col)
return nerdcommenter#IsCharCommented(a:line, a:col) return nerdcommenter#IsCharCommented(a:line, a:col)
endfunction endfunction
inoremap <silent> <Plug>NERDCommenterInsert <Space><BS><Esc>:call nerdcommenter#Comment('i', "insert")<CR> inoremap <silent> <Plug>NERDCommenterInsert <C-\><C-O>:call nerdcommenter#Comment('i', "Insert")<CR>
" switch to/from alternative delimiters (does not use wrapper function) " switch to/from alternative delimiters (does not use wrapper function)
nnoremap <Plug>NERDCommenterAltDelims :call nerdcommenter#SwitchToAlternativeDelimiters(1)<CR> nnoremap <Plug>NERDCommenterAltDelims :call nerdcommenter#SwitchToAlternativeDelimiters(1)<CR>