mirror of
https://github.com/gryf/snipmate.vim.git
synced 2026-02-17 22:45:45 +01:00
cleaned up, renamed, and fixed a bug with UpdateSnips() (now UpdateVars)
This commit is contained in:
@@ -222,9 +222,9 @@ fun s:UpdatePlaceholderTabStops()
|
|||||||
unl s:endSnip s:origPos s:origSnipPos
|
unl s:endSnip s:origPos s:origSnipPos
|
||||||
endf
|
endf
|
||||||
|
|
||||||
fun s:UpdateTabStops(...)
|
fun s:UpdateTabStops()
|
||||||
let changeLine = a:0 ? a:1 : s:endSnipLine - g:snipPos[s:curPos][0]
|
let changeLine = s:endSnipLine - g:snipPos[s:curPos][0]
|
||||||
let changeCol = a:0 > 1 ? a:2 : s:endSnip - g:snipPos[s:curPos][1]
|
let changeCol = s:endSnip - g:snipPos[s:curPos][1]
|
||||||
if exists('s:origWordLen')
|
if exists('s:origWordLen')
|
||||||
let changeCol -= s:origWordLen
|
let changeCol -= s:origWordLen
|
||||||
unl s:origWordLen
|
unl s:origWordLen
|
||||||
@@ -295,7 +295,7 @@ endf
|
|||||||
au CursorMovedI * call s:UpdateChangedSnip(0)
|
au CursorMovedI * call s:UpdateChangedSnip(0)
|
||||||
au InsertEnter * call s:UpdateChangedSnip(1)
|
au InsertEnter * call s:UpdateChangedSnip(1)
|
||||||
fun s:UpdateChangedSnip(entering)
|
fun s:UpdateChangedSnip(entering)
|
||||||
if exists('s:update')
|
if exists('s:update') " If modifying a placeholder
|
||||||
if !exists('s:origPos') && s:curPos + 1 < s:snipLen
|
if !exists('s:origPos') && s:curPos + 1 < s:snipLen
|
||||||
" Save the old snippet & word length before it's updated
|
" Save the old snippet & word length before it's updated
|
||||||
" s:startSnip must be saved too, in case text is added
|
" s:startSnip must be saved too, in case text is added
|
||||||
@@ -320,7 +320,7 @@ fun s:UpdateChangedSnip(entering)
|
|||||||
return s:RemoveSnippet()
|
return s:RemoveSnippet()
|
||||||
endif
|
endif
|
||||||
|
|
||||||
call s:UpdateSnip()
|
call s:UpdateVars()
|
||||||
let s:prevLen[1] = col('$')
|
let s:prevLen[1] = col('$')
|
||||||
elseif exists('g:snipPos')
|
elseif exists('g:snipPos')
|
||||||
let col = col('.')
|
let col = col('.')
|
||||||
@@ -344,12 +344,14 @@ fun s:UpdateChangedSnip(entering)
|
|||||||
endif
|
endif
|
||||||
endf
|
endf
|
||||||
|
|
||||||
fun s:UpdateSnip(...)
|
" This updates the variables in a snippet when a placeholder has been edited.
|
||||||
" Using strpart() here avoids a bug if s:endSnip was negative that would
|
" (e.g., each "$1" in "${1:foo} $1bar $1bar")
|
||||||
" happen with the getline('.')[(s:startSnip):(s:endSnip)] syntax
|
fun s:UpdateVars()
|
||||||
let newWordLen = a:0 ? a:1 : s:endSnip - s:startSnip + 1
|
let newWordLen = s:endSnip - s:startSnip + 1
|
||||||
let newWord = strpart(getline('.'), s:startSnip, newWordLen)
|
let newWord = strpart(getline('.'), s:startSnip, newWordLen)
|
||||||
if newWord == s:oldWord | return | endif
|
if newWord == s:oldWord || empty(g:snipPos[s:curPos][3])
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
|
||||||
let changeLen = g:snipPos[s:curPos][2] - newWordLen
|
let changeLen = g:snipPos[s:curPos][2] - newWordLen
|
||||||
let curLine = line('.')
|
let curLine = line('.')
|
||||||
@@ -359,23 +361,29 @@ fun s:UpdateSnip(...)
|
|||||||
let i = 0
|
let i = 0
|
||||||
|
|
||||||
for [lnum, col] in g:snipPos[s:curPos][3]
|
for [lnum, col] in g:snipPos[s:curPos][3]
|
||||||
call setline(lnum, substitute(getline(lnum), '\%'.(col - changeLen).
|
if updateTabStops
|
||||||
\ 'c\V'.escape(s:oldWord, '\'), escape(newWord, '\'), ''))
|
let start = s:startSnip
|
||||||
|
if lnum == curLine && col <= start
|
||||||
if !updateTabStops | continue | endif
|
let s:startSnip -= changeLen
|
||||||
let start = s:startSnip
|
let s:endSnip -= changeLen
|
||||||
if lnum == curLine && col <= start
|
|
||||||
let s:startSnip -= changeLen
|
|
||||||
let s:endSnip -= changeLen
|
|
||||||
endif
|
|
||||||
for nPos in g:snipPos[s:curPos][3][(i):]
|
|
||||||
if nPos[0] > lnum | break | endif " Remember, this part is in order.
|
|
||||||
if nPos[0] == lnum && (nPos[1] > col ||
|
|
||||||
\ (nPos[1] > start && nPos == [curLine, col]))
|
|
||||||
let nPos[1] -= changeLen
|
|
||||||
endif
|
endif
|
||||||
endfor
|
for nPos in g:snipPos[s:curPos][3][(i):]
|
||||||
let i += 1
|
" This list is in ascending order, so quit if we've gone too far.
|
||||||
|
if nPos[0] > lnum | break | endif
|
||||||
|
if nPos[0] == lnum && nPos[1] > col
|
||||||
|
let nPos[1] -= changeLen
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
if lnum == curLine && col > start
|
||||||
|
let col -= changeLen
|
||||||
|
let g:snipPos[s:curPos][3][i][1] = col
|
||||||
|
endif
|
||||||
|
let i += 1
|
||||||
|
endif
|
||||||
|
|
||||||
|
" "Very nomagic" is used here to allow special characters.
|
||||||
|
call setline(lnum, substitute(getline(lnum), '\%'.col.'c\V'.
|
||||||
|
\ escape(s:oldWord, '\'), escape(newWord, '\'), ''))
|
||||||
endfor
|
endfor
|
||||||
if oldStartSnip != s:startSnip
|
if oldStartSnip != s:startSnip
|
||||||
call cursor(0, startCol + s:startSnip - oldStartSnip)
|
call cursor(0, startCol + s:startSnip - oldStartSnip)
|
||||||
|
|||||||
Reference in New Issue
Block a user