1
Fork 0

improvement: add a filetype hook for PHP

This solve the issue #19
I put it there as an example, it might be better in its own repository.
This way a user will be able to choose to use it or not, which will
allow them to be able to provide their own implementation.

This attempt only deal with methods.
Functions are not part of the PSR-2.
Closures should always have the opening brace on the same line as the
closing parenthesis, this extension is not a CS fixer and therefore as
no reason to deal with them.
This commit is contained in:
Camille Dejoye 2020-06-06 20:02:48 +02:00
parent f7e39155de
commit 18621cafcb
3 changed files with 79 additions and 8 deletions

View File

@ -260,18 +260,12 @@ function! argwrap#toggle()
call argwrap#wrapContainer(l:range, l:container, l:arguments, l:wrapBrace, l:tailComma, l:tailCommaBraces, l:tailIndentBraces, l:linePrefix, l:commaFirst, l:commaFirstIndent)
let l:cursor[1] = l:range.lineStart + 1
let l:filetypeHook = printf('argwrap#hooks#%s#post_wrap', &filetype)
if exists('*'.l:filetypeHook)
call call(l:filetypeHook, [l:range, l:container, l:arguments])
endif
silent! call argwrap#hooks#{&filetype}#post_wrap(l:range, l:container, l:arguments)
else
call argwrap#unwrapContainer(l:range, l:container, l:arguments, l:padded)
let l:cursor[1] = l:range.lineStart
let l:filetypeHook = printf('argwrap#hooks#%s#post_unwrap', &filetype)
if exists('*'.l:filetypeHook)
call call(l:filetypeHook, [l:range, l:container, l:arguments])
endif
silent! call argwrap#hooks#{&filetype}#post_unwrap(l:range, l:container, l:arguments)
endif
call setpos('.', l:cursor)

View File

@ -0,0 +1,60 @@
function! s:dealWithMethodArguments(container) abort " {{{
if a:container.suffix !~ '\v^\)'
return 0
endif
if a:container.prefix !~? '\v^%(public|protected|private)\s+function\s+\S+\s*\($'
return 0
endif
return 1
endfunction " }}}
function! argwrap#hooks#php#post_wrap(range, container, arguments) abort " {{{
if argwrap#getSetting('php_smart_brace', 0)
call s:fixMethodOpeningBraceAfterWrap(a:range, a:container, a:arguments)
endif
endfunction " }}}
function! argwrap#hooks#php#post_unwrap(range, container, arguments) abort " {{{
if argwrap#getSetting('php_smart_brace', 0)
call s:fixMethodOpeningBraceAfterUnwrap(a:range, a:container, a:arguments)
endif
endfunction " }}}
function! s:fixMethodOpeningBraceAfterWrap(range, container, arguments) abort " {{{
if !s:dealWithMethodArguments(a:container)
return
endif
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)
let l:lineEnd += 1
endif
if getline(l:lineEnd + 1) =~ '\v^\s*\{'
execute printf('undojoin | normal! %dGJ', l:lineEnd)
endif
endfunction " }}}
function! s:fixMethodOpeningBraceAfterUnwrap(range, container, arguments) abort " {{{
if !s:dealWithMethodArguments(a:container)
return
endif
if a:container.suffix !~ '\v^\)\s*\{'
return
endif
" +1 to get the position after the closing parenthesis
let l:col = stridx(getline(a:range.lineStart), a:container.suffix) + 1
execute printf("undojoin | normal! %dG0%dlct{\<CR>", a:range.lineStart, l:col)
endfunction " }}}
" vim: ts=2 sw=2 et fdm=marker

View File

@ -179,6 +179,23 @@ file basis using `ftplugin` or `autocmd`. For example, the `argwrap_tail_comma`
, wubble
)
<
* argwrap_php_smart_brace
Specifies if the opening brace of PHP methods should be wrap/unwrap as well.
PHP smart brace disabled (default)
>
public function foo(
int $x,
int $y
)
{
<
PHP smart brace enabled ()
>
public function foo(
int $x,
int $y
) {
<
------------------------------------------------------------------------------------------------------------------------
USAGE *argwrap-usage*