Support default tabstop

Closes #1.
This commit is contained in:
Tim Pope 2013-01-02 23:14:59 -05:00
parent 80c5b309da
commit dd0f3349a2
2 changed files with 17 additions and 2 deletions

View File

@ -27,6 +27,8 @@ then simply copy and paste:
hierarchy until a match is found. This means, for example, the indent for
the first file in a brand new Ruby project might very well be derived from
your `.irbrc`. I consider this a feature.
* If your file is consistently indented hard tabs, `'shiftwidth'` will be set
to your `'tabstop'` Otherwise, a `'tabstop'` of 8 is enforced.
* The algorithm is rolled from scratch, fairly simplistic, and only lightly
battle tested. It's probably not (yet) as good as [DetectIndent][].
Let me know what it fails on for you.

View File

@ -10,6 +10,7 @@ let g:loaded_sleuth = 1
function! s:guess(lines) abort
let options = {}
let heuristics = {'spaces': 0, 'hard': 0, 'soft': 0}
let ccomment = 0
for line in a:lines
@ -30,9 +31,12 @@ function! s:guess(lines) abort
let softtab = repeat(' ', 8)
if line =~# '^\t'
let options.expandtab = 0
let heuristics.hard += 1
elseif line =~# '^' . softtab
let options.expandtab = 1
let heuristics.soft += 1
endif
if line =~# '^ '
let heuristics.spaces += 1
endif
let indent = len(matchstr(substitute(line, '\t', softtab, 'g'), '^ *'))
if indent > 1 && get(options, 'shiftwidth', 99) > indent
@ -41,6 +45,15 @@ function! s:guess(lines) abort
endfor
if heuristics.hard && !heuristics.spaces
return {'expandtab': 0, 'shiftwidth': &tabstop}
elseif heuristics.soft != heuristics.hard
let options.expandtab = heuristics.soft > heuristics.spaces
if heuristics.hard
let options.tabstop = 8
endif
endif
return options
endfunction