Skip to content

Commit 903793a

Browse files
committed
Improve indent heuristics (count increments / decrements), fixes #592
1 parent 71a21a1 commit 903793a

File tree

1 file changed

+55
-24
lines changed

1 file changed

+55
-24
lines changed

ftdetect/polyglot.vim

+55-24
Original file line numberDiff line numberDiff line change
@@ -2634,7 +2634,19 @@ if !has_key(s:disabled_packages, 'autoindent')
26342634
" Code below re-implements sleuth for vim-polyglot
26352635
let g:loaded_sleuth = 1
26362636

2637-
function! s:guess(lines) abort
2637+
func! s:get_shiftwidth(indents) abort
2638+
let shiftwidth = 0
2639+
let max_count = 0
2640+
for [indent, indent_count] in items(a:indents)
2641+
if indent_count > max_count
2642+
let shiftwidth = indent
2643+
let max_count = indent_count
2644+
endif
2645+
endfor
2646+
return shiftwidth
2647+
endfunc
2648+
2649+
func! s:guess(lines) abort
26382650
let options = {}
26392651
let ccomment = 0
26402652
let podcomment = 0
@@ -2644,12 +2656,15 @@ if !has_key(s:disabled_packages, 'autoindent')
26442656
let heredoc = ''
26452657
let minindent = 10
26462658
let spaces_minus_tabs = 0
2647-
let i = 0
2659+
let lineno = 0
2660+
let indents = { '2': 0, '3': 0, '4': 0, '6': 0, '8': 0 }
2661+
let next_indent_lineno = 1
2662+
let prev_indent = 0
26482663

26492664
for line in a:lines
2650-
let i += 1
2665+
let lineno += 1
26512666

2652-
if !len(line) || line =~# '^\S+$'
2667+
if line =~# '^\s*$'
26532668
continue
26542669
endif
26552670

@@ -2713,35 +2728,45 @@ if !has_key(s:disabled_packages, 'autoindent')
27132728
let heredoc = herematch[1] . '$'
27142729
endif
27152730

2716-
let spaces_minus_tabs += line[0] == "\t" ? 1 : -1
2717-
27182731
if line[0] == "\t"
2719-
setlocal noexpandtab
2720-
let &l:shiftwidth=&tabstop
2721-
let b:sleuth_culprit .= ':' . i
2722-
return 1
2723-
elseif line[0] == " "
2732+
let spaces_minus_tabs -= 1
2733+
else
2734+
let spaces_minus_tabs += 1
27242735
let indent = len(matchstr(line, '^ *'))
2725-
if indent < minindent && index([2, 3, 4, 6, 8], indent) >= 0
2726-
let minindent = indent
2736+
let indent_inc = abs(indent - prev_indent)
2737+
2738+
if indent_inc > 0 && lineno == next_indent_lineno
2739+
if has_key(indents, indent_inc)
2740+
let indents[indent_inc] += 1
2741+
endif
27272742
endif
2743+
2744+
let next_indent_lineno = lineno + 1
2745+
let prev_indent = indent
27282746
endif
27292747
endfor
27302748

2731-
if minindent < 10
2749+
if spaces_minus_tabs < 0
2750+
setlocal noexpandtab
2751+
let &l:shiftwidth=&tabstop
2752+
return 1
2753+
endif
2754+
2755+
let shiftwidth = s:get_shiftwidth(indents)
2756+
2757+
if shiftwidth > 0
27322758
setlocal expandtab
2733-
let &l:shiftwidth=minindent
2759+
let &l:shiftwidth=shiftwidth
27342760
if &tabstop == 8
2735-
let &l:tabstop=minindent
2761+
let &l:tabstop=shiftwidth
27362762
endif
2737-
let b:sleuth_culprit .= ':' . i
27382763
return 1
27392764
endif
27402765

27412766
return 0
2742-
endfunction
2767+
endfunc
27432768

2744-
function! s:detect_indent() abort
2769+
func! s:detect_indent() abort
27452770
if &buftype ==# 'help'
27462771
return
27472772
endif
@@ -2757,7 +2782,7 @@ if !has_key(s:disabled_packages, 'autoindent')
27572782
endif
27582783

27592784
let b:sleuth_culprit = expand("<afile>:p")
2760-
if s:guess(getline(1, 32))
2785+
if s:guess(getline(1, 64))
27612786
return
27622787
endif
27632788
if s:guess(getline(1, 1024))
@@ -2794,12 +2819,18 @@ if !has_key(s:disabled_packages, 'autoindent')
27942819
let level -= 1
27952820
endwhile
27962821

2797-
unlet b:sleuth_culprit
2798-
endfunction
2822+
setlocal expandtab
2823+
let &l:shiftwidth = 2
2824+
if &tabstop == 8
2825+
let &l:tabstop = 2
2826+
endif
2827+
2828+
let b:sleuth_culprit = "default"
2829+
endfunc
27992830

28002831
set smarttab
28012832

2802-
function! SleuthIndicator() abort
2833+
func! SleuthIndicator() abort
28032834
let sw = &shiftwidth ? &shiftwidth : &tabstop
28042835
if &expandtab
28052836
return 'sw='.sw
@@ -2808,7 +2839,7 @@ if !has_key(s:disabled_packages, 'autoindent')
28082839
else
28092840
return 'sw='.sw.',ts='.&tabstop
28102841
endif
2811-
endfunction
2842+
endfunc
28122843

28132844
augroup polyglot-sleuth
28142845
au!

0 commit comments

Comments
 (0)