From 912ed5976bb463996a940799bfa5291c7f3ec96a Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 29 May 2020 23:48:54 +1200 Subject: Commit first pass at indent heuristic I don't like this approach very much, and am about to discard it. --- vim/plugin/detect_indent.vim | 56 ++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 26 deletions(-) (limited to 'vim') diff --git a/vim/plugin/detect_indent.vim b/vim/plugin/detect_indent.vim index baee0db5..cbd2cf91 100644 --- a/vim/plugin/detect_indent.vim +++ b/vim/plugin/detect_indent.vim @@ -1,51 +1,55 @@ function Count() abort + let spaces = { - \ 'histogram': {}, + \ 'hist': {}, \ 'total': 0, \} let tabs = 0 let total = line('$') + for lnum in range(1, total) let line = getline(lnum) let tabs += matchstr(line, '^\t*') != '' - let ls = strlen(matchstr(line, '^ *')) - if ls == 0 + let indent = strlen(matchstr(line, '^ *')) + if indent == 0 continue endif - if !has_key(spaces['histogram'], ls) - let spaces['histogram'][ls] = 0 + if !has_key(spaces['hist'], indent) + let spaces['hist'][indent] = 0 endif - let spaces['histogram'][ls] += 1 + let spaces['hist'][indent] += 1 let spaces['total'] += 1 endfor - setlocal - echo spaces - echo tabs - - if tabs > spaces['total'] * 5 - echo 'TABS' - setlocal noexpandtab shiftwidth=0 softtabstop=-1 - elseif spaces['total'] > tabs * 5 - echo 'SPACES' + if &expandtab && tabs > spaces['total'] * 5 + setlocal noexpandtab softtabstop=0 + let &l:shiftwidth = &tabstop + setlocal expandtab? + elseif !&expandtab && spaces['total'] > tabs * 5 let set = [] - for ls in keys(spaces['histogram']) - if spaces['histogram'][ls] * 100 / spaces['total'] >= 10 - call add(set, ls) + for indent in keys(spaces['hist']) + if spaces['hist'][indent] * 100 / spaces['total'] >= 5 + call add(set, indent) endif endfor - for div in range(1, max(set)) - for ls in set - if ls % div != 0 + let shiftwidth = 1 + let continue = 1 + for divisor in range(2, max(set)) + for indent in set + if indent % divisor != 0 + let continue = 0 break endif - let gcd = div endfor + if !continue + break + endif + let gcd = div endfor - echo gcd - echo set - else - echo 'UNCLEAR' + setlocal expandtab + let &l:shiftwidth = gcd + let &l:softtabstop = gcd + setlocal expandtab? shiftwidth? endif endfunction -- cgit v1.2.3