1
Fork 0

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.
This commit is contained in:
Camille Dejoye 2020-06-13 12:21:03 +02:00
parent eff4334368
commit b0e60c9cd1
3 changed files with 30 additions and 22 deletions

View File

@ -219,30 +219,32 @@ function! argwrap#unwrapContainer(range, container, arguments, padded)
exec printf('silent %d,%dd_', a:range.lineStart + 1, a:range.lineEnd) exec printf('silent %d,%dd_', a:range.lineStart + 1, a:range.lineEnd)
endfunction endfunction
function! argwrap#getSetting(name, default) function! argwrap#getSetting(name)
let l:bName = 'b:argwrap_' . a:name let l:bName = 'b:argwrap_' . a:name
let l:gName = 'g:argwrap_' . a:name let l:gName = 'g:argwrap_' . a:name
if exists(l:bName) return exists(l:bName) ? {l:bName} : {l:gName}
return {l:bName} endfunction
elseif exists(l:gName)
return {l:gName} function! argwrap#initSetting(name, value) abort
else let l:setting = 'g:argwrap_'.a:name
return a:default
if !exists(l:setting)
let {l:setting} = a:value
endif endif
endfunction endfunction
function! argwrap#toggle() function! argwrap#toggle()
let l:cursor = getpos('.') let l:cursor = getpos('.')
let l:linePrefix = argwrap#getSetting('line_prefix', '') let l:linePrefix = argwrap#getSetting('line_prefix')
let l:padded = argwrap#getSetting('padded_braces', '') let l:padded = argwrap#getSetting('padded_braces')
let l:tailComma = argwrap#getSetting('tail_comma', 0) let l:tailComma = argwrap#getSetting('tail_comma')
let l:tailCommaBraces = argwrap#getSetting('tail_comma_braces', '') let l:tailCommaBraces = argwrap#getSetting('tail_comma_braces')
let l:tailIndentBraces = argwrap#getSetting('tail_indent_braces', '') let l:tailIndentBraces = argwrap#getSetting('tail_indent_braces')
let l:wrapBrace = argwrap#getSetting('wrap_closing_brace', 1) let l:wrapBrace = argwrap#getSetting('wrap_closing_brace')
let l:commaFirst = argwrap#getSetting('comma_first', 0) let l:commaFirst = argwrap#getSetting('comma_first')
let l:commaFirstIndent = argwrap#getSetting('comma_first_indent', 0) let l:commaFirstIndent = argwrap#getSetting('comma_first_indent')
let l:range = argwrap#findClosestRange() let l:range = argwrap#findClosestRange()
if !argwrap#validateRange(l:range) if !argwrap#validateRange(l:range)

View File

@ -18,10 +18,7 @@ function! s:fixMethodOpeningBraceAfterWrap(range, container, arguments) abort "
let l:lineEnd = a:range.lineEnd + len(a:arguments) let l:lineEnd = a:range.lineEnd + len(a:arguments)
" Add 1 more line if the brace is also wrapped " Add 1 more line if the brace is also wrapped
" TODO define default values on the plugin level so that extension can if 0 != argwrap#getSetting('wrap_closing_brace')
" 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)
let l:lineEnd += 1 let l:lineEnd += 1
endif endif
@ -55,13 +52,13 @@ function! argwrap#hooks#filetype#php#200_smart_brace#pre_unwrap(range, container
endfunction " }}} endfunction " }}}
function! argwrap#hooks#filetype#php#200_smart_brace#post_wrap(range, container, arguments) abort " {{{ 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) call s:fixMethodOpeningBraceAfterWrap(a:range, a:container, a:arguments)
endif endif
endfunction " }}} endfunction " }}}
function! argwrap#hooks#filetype#php#200_smart_brace#post_unwrap(range, container, arguments) abort " {{{ 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) call s:fixMethodOpeningBraceAfterUnwrap(a:range, a:container, a:arguments)
endif endif
endfunction " }}} endfunction " }}}

View File

@ -17,7 +17,16 @@
" IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN " 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. " 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() command! ArgWrap call argwrap#toggle()