Skip to content

Commit b133d81

Browse files
authored
Merge pull request #22 from ilkermutlu/fluent-setter
Fluent setter
2 parents 435c0e0 + f91c424 commit b133d81

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ let g:vim_php_refactoring_default_property_visibility = 'private'
5252
let g:vim_php_refactoring_default_method_visibility = 'private'
5353
```
5454

55+
To enable fluent setters add either of these lines to your `~/.vimrc` file
56+
```
57+
" default is 0 -- disabled
58+
59+
" to enable for all setters
60+
let g:vim_php_refactoring_fluent_setter = 1
61+
62+
" to enable but be prompted when creating the setter
63+
let g:vim_php_refactoring_fluent_setter = 2
64+
```
65+
5566

5667
## Default Mappings
5768

@@ -268,7 +279,7 @@ class Foo {
268279
}
269280
```
270281

271-
Hit `<Leader>sg` and you'll be prompted if you want to create setters and getters for existing properties.
282+
Hit `<Leader>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.
272283

273284
``` php
274285
<?php
@@ -279,6 +290,8 @@ class Foo {
279290
public function setBar($bar)
280291
{
281292
$this->bar = $bar;
293+
294+
return $this; // If you opted for a fluent setter at the prompt.
282295
}
283296

284297
public function getBar()

doc/refactoring-toolbox.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
*refactoring-toolbox*
1+
*refactoring-toolbox*
22

3-
__________ _____ __ .__ ___________ .__ ___.
3+
__________ _____ __ .__ ___________ .__ ___.
44
\______ \ _____/ ____\____ _____/ |_ ___________|__| ____ ____ \__ ___/___ ____ | |\_ |__ _______ ___
55
| _// __ \ __\\__ \ _/ ___\ __\/ _ \_ __ \ |/ \ / ___\ | | / _ \ / _ \| | | __ \ / _ \ \/ /
6-
| | \ ___/| | / __ \\ \___| | ( <_> ) | \/ | | \/ /_/ > | |( <_> | <_> ) |_| \_\ ( <_> > <
6+
| | \ ___/| | / __ \\ \___| | ( <_> ) | \/ | | \/ /_/ > | |( <_> | <_> ) |_| \_\ ( <_> > <
77
|____|_ /\___ >__| (____ /\___ >__| \____/|__| |__|___| /\___ / |____| \____/ \____/|____/___ /\____/__/\_ \
88
\/ \/ \/ \/ \//_____/ \/ \/
99

@@ -229,7 +229,7 @@ class Foo {
229229
private $bar;
230230
}
231231

232-
Hit <Leader>sg and you'll be prompted if you want to create setters and getters for existing properties.
232+
Hit `<Leader>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.
233233

234234
<?php
235235

plugin/php-refactoring-toolbox.vim

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ endif
4545
if !exists('g:vim_php_refactoring_default_method_visibility')
4646
let g:vim_php_refactoring_default_method_visibility = 'private'
4747
endif
48+
49+
if !exists('g:vim_php_refactoring_make_setter_fluent')
50+
let g:vim_php_refactoring_make_setter_fluent = 0
51+
endif
4852
" }}}
4953

5054
" Refactoring mapping {{{
@@ -90,6 +94,10 @@ let s:php_regex_fqcn = '[\\_A-Za-z0-9]*'
9094
let s:php_regex_cn = '[_A-Za-z0-9]\+'
9195
" }}}
9296

97+
" Fluent {{{
98+
let s:php_fluent_this = "normal! jo\<CR>return $this;"
99+
" }}}
100+
93101
function! PhpDocAll() " {{{
94102
if exists("*" . g:vim_php_refactoring_phpdoc) == 0
95103
call s:PhpEchoError(g:vim_php_refactoring_phpdoc . '() vim function doesn''t exists.')
@@ -128,6 +136,9 @@ function! PhpCreateSettersAndGetters() " {{{
128136
endif
129137
if search(s:php_regex_func_line . "set" . l:camelCaseName . '\>', 'n') == 0
130138
call s:PhpInsertMethod("public", "set" . l:camelCaseName, ['$' . substitute(l:property, '^_', '', '') ], "$this->" . l:property . " = $" . substitute(l:property, '^_', '', '') . ";\n")
139+
if g:vim_php_refactoring_make_setter_fluent > 0
140+
call s:PhpInsertFluent()
141+
endif
131142
endif
132143
if search(s:php_regex_func_line . "get" . l:camelCaseName . '\>', 'n') == 0
133144
call s:PhpInsertMethod("public", "get" . l:camelCaseName, [], "return $this->" . l:property . ";\n")
@@ -525,3 +536,17 @@ function! s:PhpEchoError(message) " {{{
525536
echohl NONE
526537
endfunction
527538
" }}}
539+
540+
function! s:PhpInsertFluent() " {{{
541+
if g:vim_php_refactoring_make_setter_fluent == 1
542+
exec s:php_fluent_this
543+
elseif g:vim_php_refactoring_make_setter_fluent == 2
544+
call s:PhpEchoError('Make fluent?')
545+
if inputlist(["0. No", "1. Yes"]) == 1
546+
exec s:php_fluent_this
547+
endif
548+
else
549+
echoerr 'Invalid option for g:vim_php_refactoring_make_setter_fluent'
550+
endif
551+
endfunction
552+
" }}}

0 commit comments

Comments
 (0)