wordcount: formatter: code quality improvements

- Rename new formatting function: transform() -> to_string()
- Optimise separator selection
- Other quality improvements
This commit is contained in:
Liam Fleming 2018-09-18 02:04:35 +01:00
parent 8715d13cc5
commit 487d262901
2 changed files with 19 additions and 22 deletions

View File

@ -11,7 +11,7 @@ if exists('*wordcount')
else else
function! s:get_wordcount(type) function! s:get_wordcount(type)
" index to retrieve from whitespace-separated output of g_CTRL-G " index to retrieve from whitespace-separated output of g_CTRL-G
" 11 - words, 5 - visual words (in visual mode) " 11 : words, 5 : visual words (in visual mode)
let idx = (a:type == 'words') ? 11 : 5 let idx = (a:type == 'words') ? 11 : 5
let save_status = v:statusmsg let save_status = v:statusmsg
@ -31,7 +31,7 @@ let s:formatter = get(g:, 'airline#extensions#wordcount#formatter', 'default')
" wrapper function for compatibility; redefined below for old-style formatters " wrapper function for compatibility; redefined below for old-style formatters
function! s:format_wordcount(type) function! s:format_wordcount(type)
return airline#extensions#wordcount#formatters#{s:formatter}#transform( return airline#extensions#wordcount#formatters#{s:formatter}#to_string(
\ s:get_wordcount(a:type)) \ s:get_wordcount(a:type))
endfunction endfunction
@ -39,7 +39,7 @@ endfunction
" fall back to default " fall back to default
if s:formatter !=# 'default' if s:formatter !=# 'default'
execute 'runtime! autoload/airline/extensions/wordcount/formatters/'.s:formatter execute 'runtime! autoload/airline/extensions/wordcount/formatters/'.s:formatter
if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#transform') if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#to_string')
if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#format') if !exists('*airline#extensions#wordcount#formatters#{s:formatter}#format')
let s:formatter = 'default' let s:formatter = 'default'
else else

View File

@ -6,33 +6,30 @@ scriptencoding utf-8
let s:fmt = get(g:, 'airline#extensions#wordcount#formatter#default#fmt', '%s words') let s:fmt = get(g:, 'airline#extensions#wordcount#formatter#default#fmt', '%s words')
let s:fmt_short = get(g:, 'airline#extensions#wordcount#formatter#default#fmt_short', s:fmt == '%s words' ? '%sW' : s:fmt) let s:fmt_short = get(g:, 'airline#extensions#wordcount#formatter#default#fmt_short', s:fmt == '%s words' ? '%sW' : s:fmt)
function! airline#extensions#wordcount#formatters#default#transform(text) if match(get(v:, 'lang', ''), '\v\cC|en') > -1
if empty(a:text) let s:decimal_group = ','
elseif match(get(v:, 'lang', ''), '\v\cde|dk|fr|pt') > -1
let s:decimal_group = '.'
else
let s:decimal_group = ''
endif
function! airline#extensions#wordcount#formatters#default#to_string(wordcount)
if empty(a:wordcount)
return return
endif endif
let result = g:airline_symbols.space . g:airline_right_alt_sep . g:airline_symbols.space
if winwidth(0) >= 80 if winwidth(0) >= 80
let separator = s:get_decimal_group() if a:wordcount > 999
if a:text > 999 && !empty(separator)
" Format number according to locale, e.g. German: 1.245 or English: 1,245 " Format number according to locale, e.g. German: 1.245 or English: 1,245
let text = substitute(a:text, '\d\@<=\(\(\d\{3\}\)\+\)$', separator.'&', 'g') let wordcount = substitute(a:wordcount, '\d\@<=\(\(\d\{3\}\)\+\)$', s:decimal_group.'&', 'g')
else else
let text = a:text let wordcount = a:wordcount
endif endif
let result = printf(s:fmt, a:text). result let str = printf(s:fmt, wordcount)
else else
let result = printf(s:fmt_short, a:text). result let str = printf(s:fmt_short, a:wordcount)
endif endif
return result return str . g:airline_symbols.space . g:airline_right_alt_sep . g:airline_symbols.space
endfunction
function! s:get_decimal_group()
if match(get(v:, 'lang', ''), '\v\cC|en') > -1
return ','
elseif match(get(v:, 'lang', ''), '\v\cde|dk|fr|pt') > -1
return '.'
endif
return ''
endfunction endfunction