From 18621cafcb1a73858862921b45791f514f447b25 Mon Sep 17 00:00:00 2001 From: Camille Dejoye Date: Sat, 6 Jun 2020 20:02:48 +0200 Subject: [PATCH] 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. --- autoload/argwrap.vim | 10 ++---- autoload/argwrap/hooks/php.vim | 60 ++++++++++++++++++++++++++++++++++ doc/argwrap.txt | 17 ++++++++++ 3 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 autoload/argwrap/hooks/php.vim diff --git a/autoload/argwrap.vim b/autoload/argwrap.vim index c7b5049..dcb71bc 100644 --- a/autoload/argwrap.vim +++ b/autoload/argwrap.vim @@ -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) diff --git a/autoload/argwrap/hooks/php.vim b/autoload/argwrap/hooks/php.vim new file mode 100644 index 0000000..e34902f --- /dev/null +++ b/autoload/argwrap/hooks/php.vim @@ -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{\", a:range.lineStart, l:col) +endfunction " }}} + +" vim: ts=2 sw=2 et fdm=marker diff --git a/doc/argwrap.txt b/doc/argwrap.txt index 4805046..e76b1b8 100644 --- a/doc/argwrap.txt +++ b/doc/argwrap.txt @@ -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*