From 53f2fecbf2e85728d550bd0939f775edb613fa66 Mon Sep 17 00:00:00 2001 From: Ilker Mutlu Date: Wed, 2 Nov 2016 12:02:32 +0300 Subject: [PATCH 1/3] Add fluent setter --- README.md | 2 +- doc/refactoring-toolbox.txt | 8 ++++---- plugin/php-refactoring-toolbox.vim | 4 ++++ 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 11f5350..30d853d 100644 --- a/README.md +++ b/README.md @@ -268,7 +268,7 @@ class Foo { } ``` -Hit `sg` and you'll be prompted if you want to create setters and getters for existing properties. +Hit `sg` and you'll be prompted if you want to create setters and getters for existing properties and if you want to make the setter fluent. ``` php ) | \/ | | \/ /_/ > | |( <_> | <_> ) |_| \_\ ( <_> > < + | | \ ___/| | / __ \\ \___| | ( <_> ) | \/ | | \/ /_/ > | |( <_> | <_> ) |_| \_\ ( <_> > < |____|_ /\___ >__| (____ /\___ >__| \____/|__| |__|___| /\___ / |____| \____/ \____/|____/___ /\____/__/\_ \ \/ \/ \/ \/ \//_____/ \/ \/ @@ -229,7 +229,7 @@ class Foo { private $bar; } -Hit sg and you'll be prompted if you want to create setters and getters for existing properties. +Hit `sg` and you'll be prompted if you want to create setters and getters for existing properties and if you want to make the setter fluent. ', 'n') == 0 call s:PhpInsertMethod("public", "set" . l:camelCaseName, ['$' . substitute(l:property, '^_', '', '') ], "$this->" . l:property . " = $" . substitute(l:property, '^_', '', '') . ";\n") + call s:PhpEchoError('Make fluent?') + if inputlist(["0. No", "1. Yes"]) == 1 + exec "normal! jo\return $this;" + endif endif if search(s:php_regex_func_line . "get" . l:camelCaseName . '\>', 'n') == 0 call s:PhpInsertMethod("public", "get" . l:camelCaseName, [], "return $this->" . l:property . ";\n") From b3fd864c3d40d042179e40c4ffa574954a1c3b0b Mon Sep 17 00:00:00 2001 From: Ilker Mutlu Date: Wed, 2 Nov 2016 12:05:04 +0300 Subject: [PATCH 2/3] Update README.md to reflect fluent setter in the code sample. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 30d853d..536cc86 100644 --- a/README.md +++ b/README.md @@ -279,6 +279,8 @@ class Foo { public function setBar($bar) { $this->bar = $bar; + + return $this; // If you opted for a fluent setter at the prompt. } public function getBar() From f91c424280603a01fca856479d016bfdcdcd9bb3 Mon Sep 17 00:00:00 2001 From: Ilker Mutlu Date: Thu, 3 Nov 2016 11:31:03 +0300 Subject: [PATCH 3/3] Add configuration option for fluent setters. This commit adds g:php_refactoring_make_setter_fluent which is by default 0. Possible values are: 0 - Disabled 1 - Enabled 2 - Prompt Other - Error --- README.md | 11 +++++++++++ plugin/php-refactoring-toolbox.vim | 27 ++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 536cc86..fb2d901 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,17 @@ let g:vim_php_refactoring_default_property_visibility = 'private' let g:vim_php_refactoring_default_method_visibility = 'private' ``` +To enable fluent setters add either of these lines to your `~/.vimrc` file +``` +" default is 0 -- disabled + +" to enable for all setters +let g:vim_php_refactoring_fluent_setter = 1 + +" to enable but be prompted when creating the setter +let g:vim_php_refactoring_fluent_setter = 2 +``` + ## Default Mappings diff --git a/plugin/php-refactoring-toolbox.vim b/plugin/php-refactoring-toolbox.vim index fb071d4..d0a436f 100644 --- a/plugin/php-refactoring-toolbox.vim +++ b/plugin/php-refactoring-toolbox.vim @@ -45,6 +45,10 @@ endif if !exists('g:vim_php_refactoring_default_method_visibility') let g:vim_php_refactoring_default_method_visibility = 'private' endif + +if !exists('g:vim_php_refactoring_make_setter_fluent') + let g:vim_php_refactoring_make_setter_fluent = 0 +endif " }}} " Refactoring mapping {{{ @@ -90,6 +94,10 @@ let s:php_regex_fqcn = '[\\_A-Za-z0-9]*' let s:php_regex_cn = '[_A-Za-z0-9]\+' " }}} +" Fluent {{{ +let s:php_fluent_this = "normal! jo\return $this;" +" }}} + function! PhpDocAll() " {{{ if exists("*" . g:vim_php_refactoring_phpdoc) == 0 call s:PhpEchoError(g:vim_php_refactoring_phpdoc . '() vim function doesn''t exists.') @@ -128,9 +136,8 @@ function! PhpCreateSettersAndGetters() " {{{ endif if search(s:php_regex_func_line . "set" . l:camelCaseName . '\>', 'n') == 0 call s:PhpInsertMethod("public", "set" . l:camelCaseName, ['$' . substitute(l:property, '^_', '', '') ], "$this->" . l:property . " = $" . substitute(l:property, '^_', '', '') . ";\n") - call s:PhpEchoError('Make fluent?') - if inputlist(["0. No", "1. Yes"]) == 1 - exec "normal! jo\return $this;" + if g:vim_php_refactoring_make_setter_fluent > 0 + call s:PhpInsertFluent() endif endif if search(s:php_regex_func_line . "get" . l:camelCaseName . '\>', 'n') == 0 @@ -529,3 +536,17 @@ function! s:PhpEchoError(message) " {{{ echohl NONE endfunction " }}} + +function! s:PhpInsertFluent() " {{{ + if g:vim_php_refactoring_make_setter_fluent == 1 + exec s:php_fluent_this + elseif g:vim_php_refactoring_make_setter_fluent == 2 + call s:PhpEchoError('Make fluent?') + if inputlist(["0. No", "1. Yes"]) == 1 + exec s:php_fluent_this + endif + else + echoerr 'Invalid option for g:vim_php_refactoring_make_setter_fluent' + endif +endfunction +" }}}