Skip to content

Commit

Permalink
support retabbing snipmate snippet files
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcWeber committed May 19, 2013
1 parent 9a14439 commit 221af0f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
41 changes: 41 additions & 0 deletions autoload/UltiSnips.vim
Expand Up @@ -176,4 +176,45 @@ fun! UltiSnips#ResetOmniFunc(s)
return "\<c-n>\<c-p>"
endf


" adds leading tab
" and replaces leading spaces by tabs
" see ftplugin/snippet.vim
"
" this implementation is copied from snipmate
fun! UltiSnips#RetabSnip() range
let leadingTab = expand('%:e') == 'snippets'

let lines = getline(a:firstline, a:lastline)

" remove leading "\t"
let allIndented = 1
for l in lines
if l[0] != '\t' | let allIndented = 0 | endif
endfor

" retab
if allIndented
call map(lines, 'v:val[1:]')
endif

let leadingSp = filter(map(copy(lines),'matchstr(v:val,"^\\s*") '),'v:val !=""')
if !empty(leadingSp)
" lines containing leading spaces found
let smallestInd = len(sort(leadingSp)[-1])
let ind = input('retab, spaces per tab: ', smallestInd)
for i in range(0, len(lines)-1)
let ml = matchlist(lines[i], '^\(\s*\)\(.*\)')
let lines[i] = repeat("\t", len(ml[1]) / ind)
\ . repeat( " ", len(ml[1]) % ind)
\ . ml[2]
endfor
endif
" readd tab
let tab = leadingTab ? "\t" : ""
for i in range(0,len(lines)-1)
call setline(a:firstline + i, tab.lines[i])
endfor
endf

" vim: ts=8 sts=4 sw=4 expandtab
5 changes: 5 additions & 0 deletions ftplugin/snippets.vim
Expand Up @@ -12,3 +12,8 @@ if exists("loaded_matchit") && !exists("b:match_words")
let b:match_ignorecase = 0
let b:match_words = '^snippet\>:^endsnippet\>,^global\>:^endglobal\>,\${:}'
endif


" for snipmate snippets
command! -buffer -range=% RetabSnip <line1>,<line2>call UltiSnips#RetabSnip()
vnoremap <buffer> <cr> :RetabSnip<cr>
4 changes: 4 additions & 0 deletions plugin/UltiSnips.vim
Expand Up @@ -200,4 +200,8 @@ endf

call UltiSnips_MapKeys()

augroup UltiSnips
autocm BufRead,BufNewFile snippets/*.snippets set filetype=snippets
augroup end

" vim: ts=8 sts=4 sw=4 expandtab

0 comments on commit 221af0f

Please sign in to comment.