From b0e60c9cd170a72a25442f26b2e9cb5eb69145cf Mon Sep 17 00:00:00 2001 From: Camille Dejoye Date: Sat, 13 Jun 2020 12:21:03 +0200 Subject: [PATCH] improvement: globally initialize the settings The settings were initialize in an autoload which is not a good practice. So I initialize them in the plugin directly, this way there are initialize only once when the plugin is loaded and they can be access anywhere without having to worry about what the default value. --- autoload/argwrap.vim | 32 ++++++++++--------- .../hooks/filetype/php/200_smart_brace.vim | 9 ++---- plugin/argwrap.vim | 11 ++++++- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/autoload/argwrap.vim b/autoload/argwrap.vim index c620654..35f704c 100644 --- a/autoload/argwrap.vim +++ b/autoload/argwrap.vim @@ -219,30 +219,32 @@ function! argwrap#unwrapContainer(range, container, arguments, padded) exec printf('silent %d,%dd_', a:range.lineStart + 1, a:range.lineEnd) endfunction -function! argwrap#getSetting(name, default) +function! argwrap#getSetting(name) let l:bName = 'b:argwrap_' . a:name let l:gName = 'g:argwrap_' . a:name - if exists(l:bName) - return {l:bName} - elseif exists(l:gName) - return {l:gName} - else - return a:default + return exists(l:bName) ? {l:bName} : {l:gName} +endfunction + +function! argwrap#initSetting(name, value) abort + let l:setting = 'g:argwrap_'.a:name + + if !exists(l:setting) + let {l:setting} = a:value endif endfunction function! argwrap#toggle() let l:cursor = getpos('.') - let l:linePrefix = argwrap#getSetting('line_prefix', '') - let l:padded = argwrap#getSetting('padded_braces', '') - let l:tailComma = argwrap#getSetting('tail_comma', 0) - let l:tailCommaBraces = argwrap#getSetting('tail_comma_braces', '') - let l:tailIndentBraces = argwrap#getSetting('tail_indent_braces', '') - let l:wrapBrace = argwrap#getSetting('wrap_closing_brace', 1) - let l:commaFirst = argwrap#getSetting('comma_first', 0) - let l:commaFirstIndent = argwrap#getSetting('comma_first_indent', 0) + let l:linePrefix = argwrap#getSetting('line_prefix') + let l:padded = argwrap#getSetting('padded_braces') + let l:tailComma = argwrap#getSetting('tail_comma') + let l:tailCommaBraces = argwrap#getSetting('tail_comma_braces') + let l:tailIndentBraces = argwrap#getSetting('tail_indent_braces') + let l:wrapBrace = argwrap#getSetting('wrap_closing_brace') + let l:commaFirst = argwrap#getSetting('comma_first') + let l:commaFirstIndent = argwrap#getSetting('comma_first_indent') let l:range = argwrap#findClosestRange() if !argwrap#validateRange(l:range) diff --git a/autoload/argwrap/hooks/filetype/php/200_smart_brace.vim b/autoload/argwrap/hooks/filetype/php/200_smart_brace.vim index 37cc691..0a2db46 100644 --- a/autoload/argwrap/hooks/filetype/php/200_smart_brace.vim +++ b/autoload/argwrap/hooks/filetype/php/200_smart_brace.vim @@ -18,10 +18,7 @@ function! s:fixMethodOpeningBraceAfterWrap(range, container, arguments) abort " let l:lineEnd = a:range.lineEnd + len(a:arguments) " Add 1 more line if the brace is also wrapped - " TODO define default values on the plugin level so that extension can - " request an option value without having to pass them all as argument or - " having to duplicate the default value - if 0 != argwrap#getSetting('wrap_closing_brace', 1) + if 0 != argwrap#getSetting('wrap_closing_brace') let l:lineEnd += 1 endif @@ -55,13 +52,13 @@ function! argwrap#hooks#filetype#php#200_smart_brace#pre_unwrap(range, container endfunction " }}} function! argwrap#hooks#filetype#php#200_smart_brace#post_wrap(range, container, arguments) abort " {{{ - if argwrap#getSetting('php_smart_brace', 0) + if argwrap#getSetting('php_smart_brace') call s:fixMethodOpeningBraceAfterWrap(a:range, a:container, a:arguments) endif endfunction " }}} function! argwrap#hooks#filetype#php#200_smart_brace#post_unwrap(range, container, arguments) abort " {{{ - if argwrap#getSetting('php_smart_brace', 0) + if argwrap#getSetting('php_smart_brace') call s:fixMethodOpeningBraceAfterUnwrap(a:range, a:container, a:arguments) endif endfunction " }}} diff --git a/plugin/argwrap.vim b/plugin/argwrap.vim index 41ce181..2be1244 100644 --- a/plugin/argwrap.vim +++ b/plugin/argwrap.vim @@ -17,7 +17,16 @@ " IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN " CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -let g:argwrap_filetype_hooks = {} +call argwrap#initSetting('line_prefix', '') +call argwrap#initSetting('padded_braces', '') +call argwrap#initSetting('tail_comma', 0) +call argwrap#initSetting('tail_comma_braces', '') +call argwrap#initSetting('tail_indent_braces', '') +call argwrap#initSetting('wrap_closing_brace', 1) +call argwrap#initSetting('comma_first', 0) +call argwrap#initSetting('comma_first_indent', 0) +call argwrap#initSetting('filetype_hooks', {}) +call argwrap#initSetting('php_smart_brace', 0) command! ArgWrap call argwrap#toggle()