Compare commits

..

1 Commits

Author SHA1 Message Date
Adam Stankiewicz
903793ac04 Improve indent heuristics (count increments / decrements), fixes #592 2020-10-19 11:00:16 +02:00

View File

@@ -2634,7 +2634,19 @@ if !has_key(s:disabled_packages, 'autoindent')
" Code below re-implements sleuth for vim-polyglot " Code below re-implements sleuth for vim-polyglot
let g:loaded_sleuth = 1 let g:loaded_sleuth = 1
function! s:guess(lines) abort func! s:get_shiftwidth(indents) abort
let shiftwidth = 0
let max_count = 0
for [indent, indent_count] in items(a:indents)
if indent_count > max_count
let shiftwidth = indent
let max_count = indent_count
endif
endfor
return shiftwidth
endfunc
func! s:guess(lines) abort
let options = {} let options = {}
let ccomment = 0 let ccomment = 0
let podcomment = 0 let podcomment = 0
@@ -2644,12 +2656,15 @@ if !has_key(s:disabled_packages, 'autoindent')
let heredoc = '' let heredoc = ''
let minindent = 10 let minindent = 10
let spaces_minus_tabs = 0 let spaces_minus_tabs = 0
let i = 0 let lineno = 0
let indents = { '2': 0, '3': 0, '4': 0, '6': 0, '8': 0 }
let next_indent_lineno = 1
let prev_indent = 0
for line in a:lines for line in a:lines
let i += 1 let lineno += 1
if !len(line) || line =~# '^\S+$' if line =~# '^\s*$'
continue continue
endif endif
@@ -2713,35 +2728,45 @@ if !has_key(s:disabled_packages, 'autoindent')
let heredoc = herematch[1] . '$' let heredoc = herematch[1] . '$'
endif endif
let spaces_minus_tabs += line[0] == "\t" ? 1 : -1
if line[0] == "\t" if line[0] == "\t"
setlocal noexpandtab let spaces_minus_tabs -= 1
let &l:shiftwidth=&tabstop else
let b:sleuth_culprit .= ':' . i let spaces_minus_tabs += 1
return 1
elseif line[0] == " "
let indent = len(matchstr(line, '^ *')) let indent = len(matchstr(line, '^ *'))
if indent < minindent && index([2, 3, 4, 6, 8], indent) >= 0 let indent_inc = abs(indent - prev_indent)
let minindent = indent
if indent_inc > 0 && lineno == next_indent_lineno
if has_key(indents, indent_inc)
let indents[indent_inc] += 1
endif
endif endif
let next_indent_lineno = lineno + 1
let prev_indent = indent
endif endif
endfor endfor
if minindent < 10 if spaces_minus_tabs < 0
setlocal noexpandtab
let &l:shiftwidth=&tabstop
return 1
endif
let shiftwidth = s:get_shiftwidth(indents)
if shiftwidth > 0
setlocal expandtab setlocal expandtab
let &l:shiftwidth=minindent let &l:shiftwidth=shiftwidth
if &tabstop == 8 if &tabstop == 8
let &l:tabstop=minindent let &l:tabstop=shiftwidth
endif endif
let b:sleuth_culprit .= ':' . i
return 1 return 1
endif endif
return 0 return 0
endfunction endfunc
function! s:detect_indent() abort func! s:detect_indent() abort
if &buftype ==# 'help' if &buftype ==# 'help'
return return
endif endif
@@ -2757,7 +2782,7 @@ if !has_key(s:disabled_packages, 'autoindent')
endif endif
let b:sleuth_culprit = expand("<afile>:p") let b:sleuth_culprit = expand("<afile>:p")
if s:guess(getline(1, 32)) if s:guess(getline(1, 64))
return return
endif endif
if s:guess(getline(1, 1024)) if s:guess(getline(1, 1024))
@@ -2794,12 +2819,18 @@ if !has_key(s:disabled_packages, 'autoindent')
let level -= 1 let level -= 1
endwhile endwhile
unlet b:sleuth_culprit setlocal expandtab
endfunction let &l:shiftwidth = 2
if &tabstop == 8
let &l:tabstop = 2
endif
let b:sleuth_culprit = "default"
endfunc
set smarttab set smarttab
function! SleuthIndicator() abort func! SleuthIndicator() abort
let sw = &shiftwidth ? &shiftwidth : &tabstop let sw = &shiftwidth ? &shiftwidth : &tabstop
if &expandtab if &expandtab
return 'sw='.sw return 'sw='.sw
@@ -2808,7 +2839,7 @@ if !has_key(s:disabled_packages, 'autoindent')
else else
return 'sw='.sw.',ts='.&tabstop return 'sw='.sw.',ts='.&tabstop
endif endif
endfunction endfunc
augroup polyglot-sleuth augroup polyglot-sleuth
au! au!