Skip to content

Commit

Permalink
Indent $VISUAL expansions properly
Browse files Browse the repository at this point in the history
See #177 and #178
  • Loading branch information
ajzafar committed Jan 23, 2015
1 parent fa67acd commit cc9e6e4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
31 changes: 23 additions & 8 deletions autoload/snipmate/parse.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function! s:new_parser(text)
let ret.input = a:text
let ret.len = strlen(ret.input)
let ret.pos = -1
let ret.indent = 0
let ret.value = []
let ret.vars = {}
call ret.advance()
Expand Down Expand Up @@ -123,9 +124,11 @@ function! s:parser_text(...) dict
elseif self.next == "\n"
call add(res, val)
let val = ''
let self.indent = 0
call self.advance()
elseif self.next == "\t" && &expandtab
let val .= repeat(' ', (&sts > 0) ? &sts : &sw)
elseif self.next == "\t"
let self.indent += 1
let val .= s:indent(1)
call self.advance()
else
let val .= self.next
Expand All @@ -144,12 +147,13 @@ function! s:parser_parse(...) dict
let var = self.var()
if !empty(var)
if var[0] is# 'VISUAL'
let add_to = s:visual_placeholder(var)
let add_to = s:visual_placeholder(var, self.indent)
if !empty(ret) && type(ret[-1]) == type('')
let ret[-1] .= add_to
let ret[-1] .= add_to[0]
else
call add(ret, add_to)
call add(ret, add_to[0])
endif
call extend(ret, add_to[1:-1])
elseif var[0] >= 0
call add(ret, var)
call self.add_var(var)
Expand Down Expand Up @@ -182,13 +186,24 @@ call extend(s:parser_proto, snipmate#util#add_methods(s:sfile(), 'parser',
\ [ 'advance', 'same', 'id', 'add_var', 'var', 'varend',
\ 'placeholder', 'subst', 'expr', 'text', 'parse' ]), 'error')

function! s:visual_placeholder(var)
function! s:indent(count)
if &expandtab
let shift = repeat(' ', (&sts > 0) ? &sts : &sw)
else
let shift = "\t"
endif
return repeat(shift, a:count)
endfunction

function! s:visual_placeholder(var, indent)
let dict = get(a:var, 1, {})
let pat = get(dict, 'pat', '')
let sub = get(dict, 'sub', '')
let flags = get(dict, 'flags', '')
let ret = substitute(get(b:, 'snipmate_visual', ''), pat, sub, flags)
return ret
let content = split(substitute(get(b:, 'snipmate_visual', ''), pat, sub, flags), "\n", 1)
let indent = s:indent(a:indent)
call map(content, '(v:key != 0) ? indent . v:val : v:val')
return content
endfunction

function! snipmate#parse#snippet(text)
Expand Down
7 changes: 7 additions & 0 deletions t/parser.vim
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,11 @@ describe 'snippet parser'
Expect Parse("x\n$1x") == ['x', '', [1], 'x']
end

it 'expands $VISUAL placeholders with any indents'
Expect Parse("x$VISUALx") == ['xtestvisualx']
let b:snipmate_visual = " foo\nbar\n baz"
setl noet
Expect Parse("\tx\n\t$VISUAL\nx") == ["\tx", "\t foo", "\tbar", "\t baz", "x"]
end

end

0 comments on commit cc9e6e4

Please sign in to comment.