@@ -2634,7 +2634,19 @@ if !has_key(s:disabled_packages, 'autoindent')
2634
2634
" Code below re-implements sleuth for vim-polyglot
2635
2635
let g: loaded_sleuth = 1
2636
2636
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
2638
2650
let options = {}
2639
2651
let ccomment = 0
2640
2652
let podcomment = 0
@@ -2644,12 +2656,15 @@ if !has_key(s:disabled_packages, 'autoindent')
2644
2656
let heredoc = ' '
2645
2657
let minindent = 10
2646
2658
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
2648
2663
2649
2664
for line in a: lines
2650
- let i += 1
2665
+ let lineno += 1
2651
2666
2652
- if ! len ( line ) || line = ~# ' ^\S+ $'
2667
+ if line = ~# ' ^\s* $'
2653
2668
continue
2654
2669
endif
2655
2670
@@ -2713,35 +2728,45 @@ if !has_key(s:disabled_packages, 'autoindent')
2713
2728
let heredoc = herematch[1 ] . ' $'
2714
2729
endif
2715
2730
2716
- let spaces_minus_tabs += line [0 ] == " \t " ? 1 : -1
2717
-
2718
2731
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
2724
2735
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
2727
2742
endif
2743
+
2744
+ let next_indent_lineno = lineno + 1
2745
+ let prev_indent = indent
2728
2746
endif
2729
2747
endfor
2730
2748
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
2732
2758
setlocal expandtab
2733
- let &l: shiftwidth= minindent
2759
+ let &l: shiftwidth= shiftwidth
2734
2760
if &tabstop == 8
2735
- let &l: tabstop= minindent
2761
+ let &l: tabstop= shiftwidth
2736
2762
endif
2737
- let b: sleuth_culprit .= ' :' . i
2738
2763
return 1
2739
2764
endif
2740
2765
2741
2766
return 0
2742
- endfunction
2767
+ endfunc
2743
2768
2744
- function ! s: detect_indent () abort
2769
+ func ! s: detect_indent () abort
2745
2770
if &buftype == # ' help'
2746
2771
return
2747
2772
endif
@@ -2757,7 +2782,7 @@ if !has_key(s:disabled_packages, 'autoindent')
2757
2782
endif
2758
2783
2759
2784
let b: sleuth_culprit = expand (" <afile>:p" )
2760
- if s: guess (getline (1 , 32 ))
2785
+ if s: guess (getline (1 , 64 ))
2761
2786
return
2762
2787
endif
2763
2788
if s: guess (getline (1 , 1024 ))
@@ -2794,12 +2819,18 @@ if !has_key(s:disabled_packages, 'autoindent')
2794
2819
let level -= 1
2795
2820
endwhile
2796
2821
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
2799
2830
2800
2831
set smarttab
2801
2832
2802
- function ! SleuthIndicator () abort
2833
+ func ! SleuthIndicator () abort
2803
2834
let sw = &shiftwidth ? &shiftwidth : &tabstop
2804
2835
if &expandtab
2805
2836
return ' sw=' .sw
@@ -2808,7 +2839,7 @@ if !has_key(s:disabled_packages, 'autoindent')
2808
2839
else
2809
2840
return ' sw=' .sw .' ,ts=' .&tabstop
2810
2841
endif
2811
- endfunction
2842
+ endfunc
2812
2843
2813
2844
augroup polyglot - sleuth
2814
2845
au !
0 commit comments