Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

[Improvement] Remove tail comma for php function declarations #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions doc/argwrap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
1 change: 1 addition & 0 deletions plugin/argwrap.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down