diff --git a/README.markdown b/README.markdown index 874844c..b341f7e 100644 --- a/README.markdown +++ b/README.markdown @@ -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. diff --git a/plugin/sleuth.vim b/plugin/sleuth.vim index 8cb3730..d6695e2 100644 --- a/plugin/sleuth.vim +++ b/plugin/sleuth.vim @@ -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