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

fixed bug with updating tab stops after a nested snippet is inserted

This commit is contained in:
Michael Sanders
2009-02-28 16:52:33 -05:00
parent b5999ba5cc
commit 892b4a90fb
2 changed files with 42 additions and 42 deletions

View File

@@ -11,3 +11,18 @@ snor <bs> b<bs>
snor ' b<bs>' snor ' b<bs>'
snor <right> <esc>a snor <right> <esc>a
snor <left> <esc>bi snor <left> <esc>bi
" By default load snippets in ~/.vim/snippets/<filetype>
if !exists('snippets_dir')
let snippets_dir = $HOME.(has('win16') || has('win32') || has('win64') ?
\ '\vimfiles\snippets' : '/.vim/snippets/')
endif
if isdirectory(snippets_dir)
if isdirectory(snippets_dir)
call ExtractSnips(snippets_dir, '_')
endif
au FileType * if !exists('did_ft_'.&ft) &&
\ isdirectory(snippets_dir.&ft)
\| cal ExtractSnips(snippets_dir.&ft, &ft)
\| endif
endif

View File

@@ -8,7 +8,9 @@
" For more help see snipMate.txt; you can do this by using: " For more help see snipMate.txt; you can do this by using:
" :helptags ~/.vim/doc " :helptags ~/.vim/doc
" :h snipMate.txt " :h snipMate.txt
" Last Modified: February 26, 2009. " Last Modified: February 28, 2009.
" TO FIX: - filetype snippets
" - nesting
if exists('loaded_snips') || &cp || version < 700 if exists('loaded_snips') || &cp || version < 700
finish finish
@@ -21,23 +23,6 @@ com! -nargs=+ -bang GlobalSnip call s:MakeSnippet(<q-args>, '_', <bang>0)
let s:snippets = {} | let s:multi_snips = {} let s:snippets = {} | let s:multi_snips = {}
"read in file based snippets for each filetype as we encounter the filetype
au FileType * call s:CheckForSnippets()
fun! s:CheckForSnippets()
if !exists('s:did_'.&ft) && isdirectory($HOME.'/.vim/snippets/'.&ft)
cal ExtractSnips($HOME.'/.vim/snippets/'.&ft, &ft)
let s:did_{&ft} = 1
en
endf
"read in the global file based snippets after vim starts
au VimEnter * call s:ReadGlobalSnippets()
fun! s:ReadGlobalSnippets()
if isdirectory($HOME.'/.vim/snippets/_')
call ExtractSnips($HOME.'/.vim/snippets/_', '_')
endif
endf
fun! Filename(...) fun! Filename(...)
let filename = expand('%:t:r') let filename = expand('%:t:r')
if filename == '' | return a:0 == 2 ? a:2 : '' | endif if filename == '' | return a:0 == 2 ? a:2 : '' | endif
@@ -81,7 +66,7 @@ fun! ExtractSnips(dir, ft)
for path in split(globpath(a:dir, '*'), "\n") for path in split(globpath(a:dir, '*'), "\n")
if isdirectory(path) if isdirectory(path)
for snipFile in split(globpath(path, '*.snippet'), "\n") for snipFile in split(globpath(path, '*.snippet'), "\n")
call s:ProcessFile(snipFile, a:ft, call s:ProcessFile(snipFile, a:ft,
\ strpart(path, strridx(path, s:slash)+1)) \ strpart(path, strridx(path, s:slash)+1))
endfor endfor
else else
@@ -89,7 +74,7 @@ fun! ExtractSnips(dir, ft)
endif endif
endfor endfor
unl s:slash unl s:slash
let s:did_{a:ft} = 1 let g:did_ft_{a:ft} = 1
endf endf
" Processes a snippet file; optionally add the name of the parent directory " Processes a snippet file; optionally add the name of the parent directory
@@ -202,6 +187,9 @@ fun s:ExpandSnippet(col)
let snip = split(substitute(s:snippet, '$\d\|${\d.\{-}}', '', 'g'), "\n", 1) let snip = split(substitute(s:snippet, '$\d\|${\d.\{-}}', '', 'g'), "\n", 1)
if afterCursor != '' | let snip[-1] .= afterCursor | endif if afterCursor != '' | let snip[-1] .= afterCursor | endif
call setline(lnum, line.snip[0]) call setline(lnum, line.snip[0])
if exists('s:snipPos')
call s:UpdateTabStops(len(snip)-1, len(snip[-1])-len(afterCursor))
endif
" autoindent snippet according to previous indentation " autoindent snippet according to previous indentation
let indent = matchend(line, '^.\{-}\ze\(\S\|$\)')+1 let indent = matchend(line, '^.\{-}\ze\(\S\|$\)')+1
@@ -212,7 +200,7 @@ fun s:ExpandSnippet(col)
if snipLen if snipLen
if exists('s:snipLen') if exists('s:snipLen')
let s:snipLen += snipLen-1 | let s:curPos += 1 let s:snipLen += snipLen | let s:curPos += 1
else else
let s:snipLen = snipLen | let s:curPos = 0 let s:snipLen = snipLen | let s:curPos = 0
endif endif
@@ -227,7 +215,7 @@ fun s:ExpandSnippet(col)
" place cursor at end of snippet if no tab stop is given " place cursor at end of snippet if no tab stop is given
let newlines = len(snip)-1 let newlines = len(snip)-1
call cursor(lnum + newlines, indent + len(snip[-1]) - len(afterCursor) call cursor(lnum + newlines, indent + len(snip[-1]) - len(afterCursor)
\ + (newlines ? 0: col)) \ + (newlines ? -1: col))
endif endif
return '' return ''
endf endf
@@ -277,7 +265,7 @@ fun s:Count(haystack, needle)
return counter return counter
endf endf
" Sorry, this next section is a bit convoluted... " Sorry, this next function is a bit convoluted...
" This function builds a list of a list of each tab stop in the snippet " This function builds a list of a list of each tab stop in the snippet
" containing: " containing:
" 1.) The number of the current line plus the number of "\n"s (line " 1.) The number of the current line plus the number of "\n"s (line
@@ -335,23 +323,15 @@ endf
fun s:JumpTabStop() fun s:JumpTabStop()
if exists('s:update') if exists('s:update')
call s:UpdatePlaceholderTabStops() call s:UpdatePlaceholderTabStops()
let changeLine = 0 | let changeCol = 0 let updated = 1
else
let changeLine = s:endSnipLine - s:snipPos[s:curPos][0]
let changeCol = s:endSnip - s:snipPos[s:curPos][1]
if exists('s:origWordLen')
let changeCol -= s:origWordLen
unl s:origWordLen
endif
endif endif
let s:curPos += 1 let s:curPos += 1
if s:curPos == s:snipLen if s:curPos == s:snipLen
let sMode = s:endSnip == s:snipPos[s:curPos-1][1]+s:snipPos[s:curPos-1][2] let sMode = s:endSnip == s:snipPos[s:curPos-1][1]+s:snipPos[s:curPos-1][2]
call s:RemoveSnippet() call s:RemoveSnippet()
return sMode ? "\<tab>" : TriggerSnippet() return sMode ? "\<tab>" : TriggerSnippet()
endif endif
call s:UpdateTabStops(changeLine, changeCol) if !exists('updated') | call s:UpdateTabStops() | endif
call cursor(s:snipPos[s:curPos][0], s:snipPos[s:curPos][1]) call cursor(s:snipPos[s:curPos][0], s:snipPos[s:curPos][1])
@@ -415,43 +395,48 @@ fun s:UpdatePlaceholderTabStops()
unl s:startSnip s:origWordLen s:update unl s:startSnip s:origWordLen s:update
endf endf
fun s:UpdateTabStops(changeLine, changeCol) fun s:UpdateTabStops(...)
let changeLine = a:0 ? a:1 : s:endSnipLine - s:snipPos[s:curPos-1][0]
let changeCol = a:0 > 1 ? a:2 : s:endSnip - s:snipPos[s:curPos-1][1]
if exists('s:origWordLen')
let changeCol -= s:origWordLen | unl s:origWordLen
endif
" there's probably a more efficient way to do this as well... " there's probably a more efficient way to do this as well...
let lnum = s:snipPos[s:curPos-1][0] let lnum = s:snipPos[s:curPos-1][0]
let col = s:snipPos[s:curPos-1][1] let col = s:snipPos[s:curPos-1][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 a:changeLine != 0 if changeLine != 0
for pos in s:snipPos[(s:curPos):] for pos in s:snipPos[(s:curPos):]
if pos[0] >= lnum if pos[0] >= lnum
if pos[0] == lnum if pos[0] == lnum
let pos[1] += a:changeCol let pos[1] += changeCol
endif endif
let pos[0] += a:changeLine let pos[0] += changeLine
endif endif
if pos[2] != -1 if pos[2] != -1
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] += a:changeCol let nPos[1] += changeCol
endif endif
let nPos[0] += a:changeLine let nPos[0] += changeLine
endif endif
endfor endfor
endif endif
endfor endfor
elseif a: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 s:snipPos[(s:curPos):] for pos in s:snipPos[(s:curPos):]
if pos[1] >= col && pos[0] == lnum if pos[1] >= col && pos[0] == lnum
let pos[1] += a:changeCol let pos[1] += changeCol
endif endif
if pos[2] != -1 if pos[2] != -1
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] += a:changeCol let nPos[1] += changeCol
endif endif
endfor endfor
endif endif