Skip to content

Commit

Permalink
add a RetabSnip command which fixes indentation automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcWeber committed Jan 21, 2010
1 parent d9f239c commit ff95f28
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
37 changes: 37 additions & 0 deletions autoload/snipMate.vim
Expand Up @@ -545,5 +545,42 @@ fun! snipMate#GetSnippets(scopes, trigger)
return result
endf

" adds leading tab
" and replaces leading spaces by tabs
" see ftplugin/snippet.vim
fun! snipMate#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:noet:sw=4:ts=4:ft=vim
5 changes: 3 additions & 2 deletions doc/snipMate.txt
Expand Up @@ -57,8 +57,9 @@ Note that the first hard tab after the snippet trigger is required, and not
expanded in the actual snippet. The syntax for *.snippet files is the same,
only without the trigger declaration and starting indentation.

Also note that snippets must be defined using hard tabs. They can be expanded
to spaces later if desired (see |snipMate-indenting|).
Also note that indentation within snippets must be defined using hard tabs.
They can be expanded to spaces later if desired (see |snipMate-indenting|).
You can retab a snippet by visually selecting the lines, then press <cr>.

"#" is used as a line-comment character in *.snippets files; however, they can
only be used outside of a snippet declaration. E.g.: >
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/snippet.vim
@@ -0,0 +1,2 @@
command -buffer -range=% RetabSnip <line1>,<line2>call snipMate#RetabSnip()
vnoremap <cr> :RetabSnip<cr>

0 comments on commit ff95f28

Please sign in to comment.