Skip to content
This repository was archived by the owner on Nov 4, 2025. It is now read-only.

Commit 51f4287

Browse files
committed
remove tail comma for php function declarations
1 parent f3d122d commit 51f4287

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
function! s:dealWithFunctionDeclaration(container) abort " {{{
2+
if a:container.suffix !~ '\v^\)'
3+
return 0
4+
endif
5+
6+
" Take anonymous and arrow functions into account
7+
if a:container.prefix !~? '\v(function|fn)\s*\S*\s*\($'
8+
return 0
9+
endif
10+
11+
return 1
12+
endfunction " }}}
13+
14+
function! argwrap#hooks#filetype#php#200_remove_tail_comma_function_declaration#pre_wrap(range, container, arguments) abort " {{{
15+
" Do nothing but prevent the file to be loaded more than once
16+
" When calling an autoload function that is not define the script that
17+
" should contain it is sourced every time the function is called
18+
endfunction " }}}
19+
20+
function! argwrap#hooks#filetype#php#200_remove_tail_comma_function_declaration#pre_unwrap(range, container, arguments) abort " {{{
21+
" Do nothing but prevent the file to be loaded more than once
22+
" When calling an autoload function that is not define the script that
23+
" should contain it is sourced every time the function is called
24+
endfunction " }}}
25+
26+
function! argwrap#hooks#filetype#php#200_remove_tail_comma_function_declaration#post_wrap(range, container, arguments) abort " {{{
27+
" Don't do anything if the user want a tail comma for function declaration
28+
if 0 == argwrap#getSetting('php_remove_tail_comma_function_declaration')
29+
return
30+
endif
31+
32+
let l:tailComma = argwrap#getSetting('tail_comma')
33+
let l:tailCommaForFunctionDeclaration = argwrap#getSetting('tail_comma_braces') =~ '('
34+
35+
" Don't do anything if the tail comma feature is disabled
36+
if !l:tailComma && !l:tailCommaForFunctionDeclaration
37+
return
38+
endif
39+
40+
" Don't do anything if it's not a function declaration
41+
if !s:dealWithFunctionDeclaration(a:container)
42+
return
43+
endif
44+
45+
let l:lineEnd = a:range.lineEnd + len(a:arguments)
46+
47+
if getline(l:lineEnd) =~ '\v,\s*$'
48+
call setline(l:lineEnd, substitute(getline(l:lineEnd), '\s*,\s*$', '', ''))
49+
endif
50+
endfunction " }}}
51+
52+
function! argwrap#hooks#filetype#php#200_remove_tail_comma_function_declaration#post_unwrap(range, container, arguments) abort " {{{
53+
" Do nothing but prevent the file to be loaded more than once
54+
" When calling an autoload function that is not define the script that
55+
" should contain it is sourced every time the function is called
56+
endfunction " }}}
57+
58+
" vim: ts=2 sw=2 et fdm=marker

doc/argwrap.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,23 @@ file basis using `ftplugin` or `autocmd`. For example, the `argwrap_tail_comma`
197197
int $y
198198
) {
199199
<
200+
* argwrap_php_remove_tail_comma_function_declaration
201+
Removes the tailing comma for PHP function declaration (it's invalid until
202+
PHP 8).
203+
PHP remove tail comma on function declaration disabled (default)
204+
>
205+
public function foo(
206+
int $x,
207+
int $y,
208+
) {
209+
<
210+
PHP remove tail comma on function declaration enabled
211+
>
212+
public function foo(
213+
int $x,
214+
int $y
215+
) {
216+
<
200217

201218
------------------------------------------------------------------------------------------------------------------------
202219
HOOKS *argwrap-hooks*

plugin/argwrap.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ call argwrap#initSetting('comma_first', 0)
2727
call argwrap#initSetting('comma_first_indent', 0)
2828
call argwrap#initSetting('filetype_hooks', {})
2929
call argwrap#initSetting('php_smart_brace', 0)
30+
call argwrap#initSetting('php_remove_tail_comma_function_declaration', 1)
3031

3132
command! ArgWrap call argwrap#toggle()
3233

0 commit comments

Comments
 (0)