From 6b44c1596b9eab7277461952c193804cd0bc13a6 Mon Sep 17 00:00:00 2001 From: Camille Dejoye Date: Sun, 22 Nov 2020 14:02:01 +0100 Subject: [PATCH] remove tail comma for php function declarations --- ...remove_tail_comma_function_declaration.vim | 58 +++++++++++++++++++ doc/argwrap.txt | 17 ++++++ plugin/argwrap.vim | 1 + 3 files changed, 76 insertions(+) create mode 100644 autoload/argwrap/hooks/filetype/php/200_remove_tail_comma_function_declaration.vim diff --git a/autoload/argwrap/hooks/filetype/php/200_remove_tail_comma_function_declaration.vim b/autoload/argwrap/hooks/filetype/php/200_remove_tail_comma_function_declaration.vim new file mode 100644 index 0000000..6087555 --- /dev/null +++ b/autoload/argwrap/hooks/filetype/php/200_remove_tail_comma_function_declaration.vim @@ -0,0 +1,58 @@ +function! s:dealWithFunctionDeclaration(container) abort " {{{ + if a:container.suffix !~ '\v^\)' + return 0 + endif + + " Take anonymous and arrow functions into account + if a:container.prefix !~? '\v(function|fn)\s*\S*\s*\($' + return 0 + endif + + return 1 +endfunction " }}} + +function! argwrap#hooks#filetype#php#200_remove_tail_comma_function_declaration#pre_wrap(range, container, arguments) abort " {{{ + " Do nothing but prevent the file to be loaded more than once + " When calling an autoload function that is not define the script that + " should contain it is sourced every time the function is called +endfunction " }}} + +function! argwrap#hooks#filetype#php#200_remove_tail_comma_function_declaration#pre_unwrap(range, container, arguments) abort " {{{ + " Do nothing but prevent the file to be loaded more than once + " When calling an autoload function that is not define the script that + " should contain it is sourced every time the function is called +endfunction " }}} + +function! argwrap#hooks#filetype#php#200_remove_tail_comma_function_declaration#post_wrap(range, container, arguments) abort " {{{ + " Don't do anything if the user want a tail comma for function declaration + if 0 == argwrap#getSetting('php_remove_tail_comma_function_declaration') + return + endif + + let l:tailComma = argwrap#getSetting('tail_comma') + let l:tailCommaForFunctionDeclaration = argwrap#getSetting('tail_comma_braces') =~ '(' + + " Don't do anything if the tail comma feature is disabled + if !l:tailComma && !l:tailCommaForFunctionDeclaration + return + endif + + " Don't do anything if it's not a function declaration + if !s:dealWithFunctionDeclaration(a:container) + return + endif + + let l:lineEnd = a:range.lineEnd + len(a:arguments) + + if getline(l:lineEnd) =~ '\v,\s*$' + call setline(l:lineEnd, substitute(getline(l:lineEnd), '\s*,\s*$', '', '')) + endif +endfunction " }}} + +function! argwrap#hooks#filetype#php#200_remove_tail_comma_function_declaration#post_unwrap(range, container, arguments) abort " {{{ + " Do nothing but prevent the file to be loaded more than once + " When calling an autoload function that is not define the script that + " should contain it is sourced every time the function is called +endfunction " }}} + +" vim: ts=2 sw=2 et fdm=marker diff --git a/doc/argwrap.txt b/doc/argwrap.txt index d5dffe7..03a6543 100644 --- a/doc/argwrap.txt +++ b/doc/argwrap.txt @@ -197,6 +197,23 @@ file basis using `ftplugin` or `autocmd`. For example, the `argwrap_tail_comma` int $y ) { < +* argwrap_php_remove_tail_comma_function_declaration + Removes the tailing comma for PHP function declaration (it's invalid until + PHP 8). + PHP remove tail comma on function declaration disabled (default) +> + public function foo( + int $x, + int $y, + ) { +< + PHP remove tail comma on function declaration enabled +> + public function foo( + int $x, + int $y + ) { +< ------------------------------------------------------------------------------------------------------------------------ HOOKS *argwrap-hooks* diff --git a/plugin/argwrap.vim b/plugin/argwrap.vim index 2be1244..0867372 100644 --- a/plugin/argwrap.vim +++ b/plugin/argwrap.vim @@ -27,6 +27,7 @@ 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) +call argwrap#initSetting('php_remove_tail_comma_function_declaration', 0) command! ArgWrap call argwrap#toggle()