1
0
mirror of https://github.com/gryf/snipmate.vim.git synced 2025-12-22 05:47:59 +01:00

cleaned up s:Update(TabStops|PlaceholderTabStops|Snip) functions

This commit is contained in:
Michael Sanders
2009-04-10 14:56:57 -04:00
parent 9f9d948fc8
commit ae337edd40

View File

@@ -77,8 +77,8 @@ fun s:ProcessSnippet()
" This helps tell the position of the tab stops later. " This helps tell the position of the tab stops later.
let g:snippet = substitute(g:snippet, '${\d:\(.\{-}\)}', '&\1', 'g') let g:snippet = substitute(g:snippet, '${\d:\(.\{-}\)}', '&\1', 'g')
" Update the g:snippet so that all the $# become " Update the g:snippet so that all the $# become the text after
" the text after the colon in their associated ${#}. " the colon in their associated ${#}.
" (e.g. "${1:foo}" turns all "$1"'s into "foo") " (e.g. "${1:foo}" turns all "$1"'s into "foo")
let i = 1 let i = 1
wh stridx(g:snippet, '${'.i) != -1 wh stridx(g:snippet, '${'.i) != -1
@@ -178,106 +178,89 @@ fun snipMate#jumpTabStop()
endf endf
fun s:UpdatePlaceholderTabStops() fun s:UpdatePlaceholderTabStops()
let changeLen = s:origWordLen - g:snipPos[s:curPos][2]
unl s:startSnip s:origWordLen s:update
if !exists('s:origPos') | return | endif
" Update tab stops in snippet if text has been added via "$#", " Update tab stops in snippet if text has been added via "$#",
" e.g. in "${1:foo}bar$1${2}" " e.g. in "${1:foo}bar$1${2}"
if exists('s:origPos')
let changeLen = s:origWordLen - g:snipPos[s:curPos][2]
" This could probably be more efficent...
if changeLen != 0 if changeLen != 0
let lnum = line('.') let curLine = line('.')
let len = len(s:origPos)
for pos in g:snipPos[s:curPos + 1:] for pos in g:snipPos[s:curPos + 1:]
let i = 0 | let j = 0 | let k = 0 let changed = pos[0] == curLine && pos[1] > s:origSnipPos ? 1 : 0
let endSnip = pos[2] + pos[1] - 1 let changedVars = 0
" Subtract changeLen to each tab stop that was after any of let endPlaceholder = pos[2] - 1 + pos[1]
" Subtract changeLen from each tab stop that was after any of
" the current tab stop's placeholders. " the current tab stop's placeholders.
wh i < len && s:origPos[i][0] <= pos[0] for [lnum, col] in s:origPos
if pos[0] == s:origPos[i][0] if lnum > pos[0] | break | endif
if pos[1] > s:origPos[i][1] if pos[0] == lnum
\ || (pos[2] == -1 && pos[1] == s:origPos[i][1]) if pos[1] > col || (pos[2] == -1 && pos[1] == col)
let j += 1 let changed += 1
elseif s:origPos[i][1] < endSnip elseif col < endPlaceholder
let k += 1 let changedVars += 1
endif endif
endif endif
let i += 1
endw
if pos[0] == lnum && pos[1] > s:origSnipPos
let j += 1
endif
let pos[1] -= changeLen*j
let pos[2] -= changeLen*k " Parse variables within placeholders
" Do the same to any placeholders in the other tab stops.
if pos[2] != -1
for nPos in pos[3]
let i = 0 | let j = 0
wh i < len && s:origPos[i][0] <= nPos[0]
if nPos[0] == s:origPos[i][0] && nPos[1] > s:origPos[i][1]
let j += 1
endif
let i += 1
endw
if nPos[0] == lnum && nPos[1] > s:origSnipPos
let j += 1
endif
let nPos[1] -= changeLen*j
endfor endfor
let pos[1] -= changeLen * changed
let pos[2] -= changeLen * changedVars " Parse variables within placeholders
" e.g., "${1:foo} ${2:$1bar}"
if pos[2] == -1 | continue | endif
" Do the same to any placeholders in the other tab stops.
for nPos in pos[3]
let changed = nPos[0] == curLine && nPos[1] > s:origSnipPos ? 1 : 0
for [lnum, col] in s:origPos
if nPos[0] == lnum && nPos[1] > col
let changed += 1
endif endif
endfor endfor
let nPos[1] -= changeLen * changed
endfor
endfor
endif endif
unl s:endSnip s:origPos s:origSnipPos unl s:endSnip s:origPos s:origSnipPos
endif
unl s:startSnip s:origWordLen s:update
endf endf
fun s:UpdateTabStops(...) fun s:UpdateTabStops(...)
let changeLine = a:0 ? a:1 : s:endSnipLine - g:snipPos[s:curPos][0] let changeLine = a:0 ? a:1 : s:endSnipLine - g:snipPos[s:curPos][0]
let changeCol = a:0 > 1 ? a:2 : s:endSnip - g:snipPos[s:curPos][1] let changeCol = a:0 > 1 ? a:2 : s:endSnip - g:snipPos[s:curPos][1]
if exists('s:origWordLen') if exists('s:origWordLen')
let changeCol -= s:origWordLen | unl s:origWordLen let changeCol -= s:origWordLen
unl s:origWordLen
endif endif
" There's probably a more efficient way to do this as well...
let lnum = g:snipPos[s:curPos][0] let lnum = g:snipPos[s:curPos][0]
let col = g:snipPos[s:curPos][1] let col = g:snipPos[s:curPos][1]
" Update the line number of all proceeding tab stops if <cr> has " Update the line number of all proceeding tab stops if <cr> has
" been inserted. " been inserted.
if changeLine != 0 if changeLine != 0
for pos in g:snipPos[(s:curPos + 1):] for pos in g:snipPos[s:curPos + 1:]
if pos[0] >= lnum if pos[0] >= lnum
if pos[0] == lnum if pos[0] == lnum | let pos[1] += changeCol | endif
let pos[1] += changeCol
endif
let pos[0] += changeLine let pos[0] += changeLine
endif endif
if pos[2] != -1 if pos[2] == -1 | continue | endif
for nPos in pos[3] for nPos in pos[3]
if nPos[0] >= lnum if nPos[0] >= lnum
if nPos[0] == lnum if nPos[0] == lnum | let nPos[1] += changeCol | endif
let nPos[1] += changeCol
endif
let nPos[0] += changeLine let nPos[0] += changeLine
endif endif
endfor endfor
endif
endfor endfor
elseif changeCol != 0 elseif changeCol != 0
" Update the column of all proceeding tab stops if text has " Update the column of all proceeding tab stops if text has
" been inserted/deleted in the current line. " been inserted/deleted in the current line.
for pos in g:snipPos[(s:curPos + 1):] for pos in g:snipPos[s:curPos + 1:]
if pos[1] >= col && pos[0] == lnum if pos[1] >= col && pos[0] == lnum
let pos[1] += changeCol let pos[1] += changeCol
endif endif
if pos[2] != -1 if pos[2] == -1 | continue | endif
for nPos in pos[3] for nPos in pos[3]
if nPos[0] > lnum | break | endif if nPos[0] > lnum | break | endif
if nPos[0] == lnum && nPos[1] >= col if nPos[0] == lnum && nPos[1] >= col
let nPos[1] += changeCol let nPos[1] += changeCol
endif endif
endfor endfor
endif
endfor endfor
endif endif
endf endf
@@ -366,40 +349,38 @@ fun s:UpdateSnip(...)
" happen with the getline('.')[(s:startSnip):(s:endSnip)] syntax " happen with the getline('.')[(s:startSnip):(s:endSnip)] syntax
let newWordLen = a:0 ? a:1 : s:endSnip - s:startSnip + 1 let newWordLen = a:0 ? a:1 : s:endSnip - s:startSnip + 1
let newWord = strpart(getline('.'), s:startSnip, newWordLen) let newWord = strpart(getline('.'), s:startSnip, newWordLen)
if newWord != s:oldWord if newWord == s:oldWord | return | endif
let changeLen = g:snipPos[s:curPos][2] - newWordLen let changeLen = g:snipPos[s:curPos][2] - newWordLen
let curLine = line('.') let curLine = line('.')
let startCol = col('.') let startCol = col('.')
let oldStartSnip = s:startSnip let oldStartSnip = s:startSnip
let updateSnip = changeLen != 0 let updateTabStops = changeLen != 0
let i = 0 let i = 0
for pos in g:snipPos[s:curPos][3] for [lnum, col] in g:snipPos[s:curPos][3]
if updateSnip call setline(lnum, substitute(getline(lnum), '\%'.(col - changeLen).
\ 'c\V'.escape(s:oldWord, '\'), escape(newWord, '\'), ''))
if !updateTabStops | continue | endif
let start = s:startSnip let start = s:startSnip
if pos[0] == curLine && pos[1] <= start if lnum == curLine && col <= start
let s:startSnip -= changeLen let s:startSnip -= changeLen
let s:endSnip -= changeLen let s:endSnip -= changeLen
endif endif
for nPos in g:snipPos[s:curPos][3][(i):] for nPos in g:snipPos[s:curPos][3][(i):]
if nPos[0] == pos[0] if nPos[0] > lnum | break | endif " Remember, this part is in order.
if nPos[1] > pos[1] || (nPos == [curLine, pos[1]] && if nPos[0] == lnum && (nPos[1] > col ||
\ nPos[1] > start) \ (nPos[1] > start && nPos == [curLine, col]))
let nPos[1] -= changeLen let nPos[1] -= changeLen
endif endif
elseif nPos[0] > pos[0] | break | endif
endfor endfor
let i += 1 let i += 1
endif
call setline(pos[0], substitute(getline(pos[0]), '\%'.pos[1].'c\V'.
\ escape(s:oldWord, '\'), escape(newWord, '\'), ''))
endfor endfor
if oldStartSnip != s:startSnip if oldStartSnip != s:startSnip
call cursor('.', startCol + s:startSnip - oldStartSnip) call cursor(0, startCol + s:startSnip - oldStartSnip)
endif endif
let s:oldWord = newWord let s:oldWord = newWord
let g:snipPos[s:curPos][2] = newWordLen let g:snipPos[s:curPos][2] = newWordLen
endif
endf endf