From 487d26290162e417328bc39205c9ae508f779cfd Mon Sep 17 00:00:00 2001 From: Liam Fleming Date: Tue, 18 Sep 2018 02:04:35 +0100 Subject: [PATCH] wordcount: formatter: code quality improvements - Rename new formatting function: transform() -> to_string() - Optimise separator selection - Other quality improvements --- autoload/airline/extensions/wordcount.vim | 6 ++-- .../wordcount/formatters/default.vim | 35 +++++++++---------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/autoload/airline/extensions/wordcount.vim b/autoload/airline/extensions/wordcount.vim index 4a937e83..07426efd 100644 --- a/autoload/airline/extensions/wordcount.vim +++ b/autoload/airline/extensions/wordcount.vim @@ -11,7 +11,7 @@ if exists('*wordcount') else function! s:get_wordcount(type) " 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 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 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)) endfunction @@ -39,7 +39,7 @@ endfunction " fall back to default if s:formatter !=# 'default' 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') let s:formatter = 'default' else diff --git a/autoload/airline/extensions/wordcount/formatters/default.vim b/autoload/airline/extensions/wordcount/formatters/default.vim index 846b4e06..813fe611 100644 --- a/autoload/airline/extensions/wordcount/formatters/default.vim +++ b/autoload/airline/extensions/wordcount/formatters/default.vim @@ -6,33 +6,30 @@ scriptencoding utf-8 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) -function! airline#extensions#wordcount#formatters#default#transform(text) - if empty(a:text) +if match(get(v:, 'lang', ''), '\v\cC|en') > -1 + 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 endif - let result = g:airline_symbols.space . g:airline_right_alt_sep . g:airline_symbols.space if winwidth(0) >= 80 - let separator = s:get_decimal_group() - if a:text > 999 && !empty(separator) + if a:wordcount > 999 " 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 - let text = a:text + let wordcount = a:wordcount endif - let result = printf(s:fmt, a:text). result + let str = printf(s:fmt, wordcount) else - let result = printf(s:fmt_short, a:text). result + let str = printf(s:fmt_short, a:wordcount) endif - return result -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 '' + return str . g:airline_symbols.space . g:airline_right_alt_sep . g:airline_symbols.space endfunction