mirror of
https://github.com/gryf/snipmate.vim.git
synced 2026-01-03 20:34:18 +01:00
got rid of some ugly global variables
This commit is contained in:
@@ -8,21 +8,19 @@ fun s:RemoveSnippet()
|
|||||||
unl g:snipPos s:curPos s:snipLen s:endSnip s:endSnipLine s:prevLen
|
unl g:snipPos s:curPos s:snipLen s:endSnip s:endSnipLine s:prevLen
|
||||||
endf
|
endf
|
||||||
|
|
||||||
fun snipMate#expandSnip(col)
|
fun snipMate#expandSnip(snip, col)
|
||||||
let lnum = line('.') | let col = a:col
|
let lnum = line('.') | let col = a:col
|
||||||
|
|
||||||
call s:ProcessSnippet()
|
let snippet = s:ProcessSnippet(a:snip)
|
||||||
if g:snippet == ''
|
if snippet == '' | return '' | endif
|
||||||
unl g:snippet | return '' " Avoid an error if the snippet is now empty
|
|
||||||
endif
|
|
||||||
|
|
||||||
let snip = split(substitute(g:snippet, '$\d\+\|${\d\+.\{-}}', '', 'g'), "\n", 1)
|
let snipLines = split(substitute(snippet, '$\d\+\|${\d\+.\{-}}', '', 'g'), "\n", 1)
|
||||||
|
|
||||||
let line = getline(lnum)
|
let line = getline(lnum)
|
||||||
let afterCursor = strpart(line, col - 1)
|
let afterCursor = strpart(line, col - 1)
|
||||||
if afterCursor != "\t" && afterCursor != ' '
|
if afterCursor != "\t" && afterCursor != ' '
|
||||||
let line = strpart(line, 0, col - 1)
|
let line = strpart(line, 0, col - 1)
|
||||||
let snip[-1] .= afterCursor
|
let snipLines[-1] .= afterCursor
|
||||||
else
|
else
|
||||||
let afterCursor = ''
|
let afterCursor = ''
|
||||||
" For some reason the cursor needs to move one right after this
|
" For some reason the cursor needs to move one right after this
|
||||||
@@ -31,18 +29,16 @@ fun snipMate#expandSnip(col)
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
call setline(lnum, line.snip[0])
|
call setline(lnum, line.snipLines[0])
|
||||||
|
|
||||||
" 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
|
||||||
call append(lnum, map(snip[1:], "'".strpart(line, 0, indent - 1)."'.v:val"))
|
call append(lnum, map(snipLines[1:], "'".strpart(line, 0, indent - 1)."'.v:val"))
|
||||||
if &fen | sil! exe lnum.','.(lnum + len(snip) - 1).'foldopen' | endif
|
if &fen | sil! exe lnum.','.(lnum + len(snipLines) - 1).'foldopen' | endif
|
||||||
|
|
||||||
let snipLen = s:BuildTabStops(lnum, col - indent, indent)
|
let [g:snipPos, s:snipLen] = s:BuildTabStops(snippet, lnum, col - indent, indent)
|
||||||
unl g:snippet
|
|
||||||
|
|
||||||
if snipLen
|
if s:snipLen
|
||||||
let s:snipLen = snipLen
|
|
||||||
let s:curPos = 0
|
let s:curPos = 0
|
||||||
let s:endSnip = g:snipPos[s:curPos][1]
|
let s:endSnip = g:snipPos[s:curPos][1]
|
||||||
let s:endSnipLine = g:snipPos[s:curPos][0]
|
let s:endSnipLine = g:snipPos[s:curPos][0]
|
||||||
@@ -51,48 +47,49 @@ fun snipMate#expandSnip(col)
|
|||||||
let s:prevLen = [line('$'), col('$')]
|
let s:prevLen = [line('$'), col('$')]
|
||||||
if g:snipPos[s:curPos][2] != -1 | return s:SelectWord() | endif
|
if g:snipPos[s:curPos][2] != -1 | return s:SelectWord() | endif
|
||||||
else
|
else
|
||||||
unl g:snipPos
|
unl g:snipPos s:snipLen
|
||||||
" 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(snipLines) - 1
|
||||||
call cursor(lnum + newlines, indent + len(snip[-1]) - len(afterCursor)
|
call cursor(lnum + newlines, indent + len(snipLines[-1]) - len(afterCursor)
|
||||||
\ + (newlines ? 0: col - 1))
|
\ + (newlines ? 0: col - 1))
|
||||||
endif
|
endif
|
||||||
return ''
|
return ''
|
||||||
endf
|
endf
|
||||||
|
|
||||||
fun s:ProcessSnippet()
|
fun s:ProcessSnippet(snip)
|
||||||
|
let snippet = a:snip
|
||||||
" Evaluate eval (`...`) expressions.
|
" Evaluate eval (`...`) expressions.
|
||||||
" Using a loop here instead of a regex fixes a bug with nested "\=".
|
" Using a loop here instead of a regex fixes a bug with nested "\=".
|
||||||
if stridx(g:snippet, '`') != -1
|
if stridx(snippet, '`') != -1
|
||||||
wh match(g:snippet, '`.\{-}`') != -1
|
wh match(snippet, '`.\{-}`') != -1
|
||||||
let g:snippet = substitute(g:snippet, '`.\{-}`',
|
let snippet = substitute(snippet, '`.\{-}`',
|
||||||
\ substitute(eval(matchstr(g:snippet, '`\zs.\{-}\ze`')),
|
\ substitute(eval(matchstr(snippet, '`\zs.\{-}\ze`')),
|
||||||
\ "\n\\%$", '', ''), '')
|
\ "\n\\%$", '', ''), '')
|
||||||
endw
|
endw
|
||||||
let g:snippet = substitute(g:snippet, "\r", "\n", 'g')
|
let snippet = substitute(snippet, "\r", "\n", 'g')
|
||||||
endif
|
endif
|
||||||
|
|
||||||
" Place all text after a colon in a tab stop after the tab stop
|
" Place all text after a colon in a tab stop after the tab stop
|
||||||
" (e.g. "${#:foo}" becomes "${:foo}foo").
|
" (e.g. "${#:foo}" becomes "${:foo}foo").
|
||||||
" 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 snippet = substitute(snippet, '${\d\+:\(.\{-}\)}', '&\1', 'g')
|
||||||
|
|
||||||
" Update the g:snippet so that all the $# become the text after
|
" Update the a:snip so that all the $# become 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(snippet, '${'.i) != -1
|
||||||
let s = matchstr(g:snippet, '${'.i.':\zs.\{-}\ze}')
|
let s = matchstr(snippet, '${'.i.':\zs.\{-}\ze}')
|
||||||
if s != ''
|
if s != ''
|
||||||
let g:snippet = substitute(g:snippet, '$'.i, '&'.s, 'g')
|
let snippet = substitute(snippet, '$'.i, '&'.s, 'g')
|
||||||
endif
|
endif
|
||||||
let i += 1
|
let i += 1
|
||||||
endw
|
endw
|
||||||
|
|
||||||
if &et " Expand tabs to spaces if 'expandtab' is set.
|
if &et " Expand tabs to spaces if 'expandtab' is set.
|
||||||
let g:snippet = substitute(g:snippet, '\t',
|
return substitute(snippet, '\t', repeat(' ', &sts ? &sts : &sw), 'g')
|
||||||
\ repeat(' ', &sts ? &sts : &sw), 'g')
|
|
||||||
endif
|
endif
|
||||||
|
return snippet
|
||||||
endf
|
endf
|
||||||
|
|
||||||
fun s:Count(haystack, needle)
|
fun s:Count(haystack, needle)
|
||||||
@@ -117,30 +114,30 @@ endf
|
|||||||
" the matches of "$#", to be replaced with the placeholder. This list is
|
" the matches of "$#", to be replaced with the placeholder. This list is
|
||||||
" composed the same way as the parent; the first item is the line number,
|
" composed the same way as the parent; the first item is the line number,
|
||||||
" and the second is the column.
|
" and the second is the column.
|
||||||
fun s:BuildTabStops(lnum, col, indent)
|
fun s:BuildTabStops(snip, lnum, col, indent)
|
||||||
let g:snipPos = []
|
let snipPos = []
|
||||||
let i = 1
|
let i = 1
|
||||||
let withoutVars = substitute(g:snippet, '$\d\+', '', 'g')
|
let withoutVars = substitute(a:snip, '$\d\+', '', 'g')
|
||||||
wh stridx(g:snippet, '${'.i) != -1
|
wh stridx(a:snip, '${'.i) != -1
|
||||||
let beforeTabStop = matchstr(withoutVars, '^.*\ze${'.i.'\D')
|
let beforeTabStop = matchstr(withoutVars, '^.*\ze${'.i.'\D')
|
||||||
let withoutOthers = substitute(withoutVars, '${'.i.'\@!\d\+.\{-}}', '', 'g')
|
let withoutOthers = substitute(withoutVars, '${'.i.'\@!\d\+.\{-}}', '', 'g')
|
||||||
let g:snipPos += [[a:lnum + s:Count(beforeTabStop, "\n"),
|
let snipPos += [[a:lnum + s:Count(beforeTabStop, "\n"),
|
||||||
\ a:indent + len(matchstr(withoutOthers,
|
\ a:indent + len(matchstr(withoutOthers,
|
||||||
\ "^.*\\(\n\\|^\\)\\zs.*\\ze${".i.'\D')), -1]]
|
\ "^.*\\(\n\\|^\\)\\zs.*\\ze${".i.'\D')), -1]]
|
||||||
if g:snipPos[i-1][0] == a:lnum
|
if snipPos[i-1][0] == a:lnum
|
||||||
let g:snipPos[i-1][1] += a:col
|
let snipPos[i-1][1] += a:col
|
||||||
endif
|
endif
|
||||||
|
|
||||||
" Get all $# matches in another list, if ${#:name} is given
|
" Get all $# matches in another list, if ${#:name} is given
|
||||||
if stridx(withoutVars, '${'.i.':') != -1
|
if stridx(withoutVars, '${'.i.':') != -1
|
||||||
let j = i - 1
|
let j = i - 1
|
||||||
let g:snipPos[j][2] = len(matchstr(withoutVars, '${'.i.':\zs.\{-}\ze}'))
|
let snipPos[j][2] = len(matchstr(withoutVars, '${'.i.':\zs.\{-}\ze}'))
|
||||||
let g:snipPos[j] += [[]]
|
let snipPos[j] += [[]]
|
||||||
let withoutOthers = substitute(g:snippet, '${\d\+.\{-}}\|$'.i.'\@!\d\+', '', 'g')
|
let withoutOthers = substitute(a:snip, '${\d\+.\{-}}\|$'.i.'\@!\d\+', '', 'g')
|
||||||
wh match(withoutOthers, '$'.i.'\D') != -1
|
wh match(withoutOthers, '$'.i.'\D') != -1
|
||||||
let beforeMark = matchstr(withoutOthers, '^.\{-}\ze$'.i.'\D')
|
let beforeMark = matchstr(withoutOthers, '^.\{-}\ze$'.i.'\D')
|
||||||
let linecount = a:lnum + s:Count(beforeMark, "\n")
|
let linecount = a:lnum + s:Count(beforeMark, "\n")
|
||||||
let g:snipPos[j][3] += [[linecount,
|
let snipPos[j][3] += [[linecount,
|
||||||
\ a:indent + (linecount > a:lnum
|
\ a:indent + (linecount > a:lnum
|
||||||
\ ? len(matchstr(beforeMark, "^.*\n\\zs.*"))
|
\ ? len(matchstr(beforeMark, "^.*\n\\zs.*"))
|
||||||
\ : a:col + len(beforeMark))]]
|
\ : a:col + len(beforeMark))]]
|
||||||
@@ -149,7 +146,7 @@ fun s:BuildTabStops(lnum, col, indent)
|
|||||||
endif
|
endif
|
||||||
let i += 1
|
let i += 1
|
||||||
endw
|
endw
|
||||||
return i - 1
|
return [snipPos, i - 1]
|
||||||
endf
|
endf
|
||||||
|
|
||||||
fun snipMate#jumpTabStop()
|
fun snipMate#jumpTabStop()
|
||||||
@@ -179,8 +176,8 @@ fun s:UpdatePlaceholderTabStops()
|
|||||||
let changeLen = s:origWordLen - g:snipPos[s:curPos][2]
|
let changeLen = s:origWordLen - g:snipPos[s:curPos][2]
|
||||||
unl s:startSnip s:origWordLen s:update
|
unl s:startSnip s:origWordLen s:update
|
||||||
if !exists('s:origPos') | return | endif
|
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 changeLen != 0
|
if changeLen != 0
|
||||||
let curLine = line('.')
|
let curLine = line('.')
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
" File: snipMate.vim
|
" File: snipMate.vim
|
||||||
" Author: Michael Sanders
|
" Author: Michael Sanders
|
||||||
" Version: 0.78
|
" Version: 0.79
|
||||||
" Description: snipMate.vim implements some of TextMate's snippets features in
|
" Description: snipMate.vim implements some of TextMate's snippets features in
|
||||||
" Vim. A snippet is a piece of often-typed text that you can
|
" Vim. A snippet is a piece of often-typed text that you can
|
||||||
" insert into your document using a trigger word followed by a "<tab>".
|
" insert into your document using a trigger word followed by a "<tab>".
|
||||||
@@ -142,19 +142,17 @@ fun! TriggerSnippet()
|
|||||||
|
|
||||||
let word = matchstr(getline('.'), '\S\+\%'.col('.').'c')
|
let word = matchstr(getline('.'), '\S\+\%'.col('.').'c')
|
||||||
for scope in [bufnr('%')] + split(&ft, '\.') + ['_']
|
for scope in [bufnr('%')] + split(&ft, '\.') + ['_']
|
||||||
let trigger = s:GetSnippet(word, scope)
|
let [trigger, snippet] = s:GetSnippet(word, scope)
|
||||||
if exists('g:snippet') | break | endif
|
" If word is a trigger for a snippet, delete the trigger & expand
|
||||||
|
" the snippet.
|
||||||
|
if snippet != ''
|
||||||
|
let col = col('.') - len(trigger)
|
||||||
|
sil exe 's/\V'.escape(trigger, '/').'\%#//'
|
||||||
|
return snipMate#expandSnip(snippet, col)
|
||||||
|
endif
|
||||||
endfor
|
endfor
|
||||||
|
|
||||||
" If word is a trigger for a snippet, delete the trigger & expand the snippet.
|
if exists('SuperTabKey')
|
||||||
if exists('g:snippet')
|
|
||||||
if g:snippet == '' " If user cancelled a multi snippet, quit.
|
|
||||||
unl g:snippet | return ''
|
|
||||||
endif
|
|
||||||
let col = col('.') - len(trigger)
|
|
||||||
sil exe 's/'.escape(trigger, '.^$/\*[]').'\%#//'
|
|
||||||
return snipMate#expandSnip(col)
|
|
||||||
elseif exists('SuperTabKey')
|
|
||||||
call feedkeys(SuperTabKey)
|
call feedkeys(SuperTabKey)
|
||||||
return ''
|
return ''
|
||||||
endif
|
endif
|
||||||
@@ -164,18 +162,18 @@ endf
|
|||||||
" Check if word under cursor is snippet trigger; if it isn't, try checking if
|
" Check if word under cursor is snippet trigger; if it isn't, try checking if
|
||||||
" the text after non-word characters is (e.g. check for "foo" in "bar.foo")
|
" the text after non-word characters is (e.g. check for "foo" in "bar.foo")
|
||||||
fun s:GetSnippet(word, scope)
|
fun s:GetSnippet(word, scope)
|
||||||
let word = a:word
|
let word = a:word | let snippet = ''
|
||||||
wh !exists('g:snippet')
|
wh snippet == ''
|
||||||
if exists('s:snippets["'.a:scope.'"]["'.escape(word, '\"').'"]')
|
if exists('s:snippets["'.a:scope.'"]["'.escape(word, '\"').'"]')
|
||||||
let g:snippet = s:snippets[a:scope][word]
|
let snippet = s:snippets[a:scope][word]
|
||||||
elseif exists('s:multi_snips["'.a:scope.'"]["'.escape(word, '\"').'"]')
|
elseif exists('s:multi_snips["'.a:scope.'"]["'.escape(word, '\"').'"]')
|
||||||
let g:snippet = s:ChooseSnippet(a:scope, word)
|
let snippet = s:ChooseSnippet(a:scope, word)
|
||||||
else
|
else
|
||||||
if match(word, '\W') == -1 | break | endif
|
if match(word, '\W') == -1 | break | endif
|
||||||
let word = substitute(word, '.\{-}\W', '', '')
|
let word = substitute(word, '.\{-}\W', '', '')
|
||||||
endif
|
endif
|
||||||
endw
|
endw
|
||||||
return word
|
return [word, snippet]
|
||||||
endf
|
endf
|
||||||
|
|
||||||
fun s:ChooseSnippet(scope, trigger)
|
fun s:ChooseSnippet(scope, trigger)
|
||||||
|
|||||||
Reference in New Issue
Block a user