aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2020-05-29 23:48:54 +1200
committerTom Ryder <tom@sanctum.geek.nz>2020-05-29 23:48:54 +1200
commit912ed5976bb463996a940799bfa5291c7f3ec96a (patch)
tree7dc8454c6d3e3ca46b057495bb328f7d1c700929
parentWorking on a plugin for detecting tab indent (diff)
downloaddotfiles-912ed5976bb463996a940799bfa5291c7f3ec96a.tar.gz
dotfiles-912ed5976bb463996a940799bfa5291c7f3ec96a.zip
Commit first pass at indent heuristic
I don't like this approach very much, and am about to discard it.
-rw-r--r--vim/plugin/detect_indent.vim56
1 files changed, 30 insertions, 26 deletions
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