From 296871afdecc8723085640c832423950aaf05a3d Mon Sep 17 00:00:00 2001 From: Ilker Mutlu Date: Fri, 4 Nov 2016 11:15:02 +0300 Subject: [PATCH] Functionality for creating only getters, closes #18 --- README.md | 32 ++++++++++++++++++++++++++++++ plugin/php-refactoring-toolbox.vim | 27 +++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/README.md b/README.md index fb2d901..6b5f3f7 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,12 @@ If you want to disable the user validation at the getter/setter creation, just a let g:vim_php_refactoring_auto_validate_sg = 1 ``` +If you want to disable the user validation at getter only creation, just add this line in your `~/.vimrc` file + +``` +let g:vim_php_refactoring_auto_validate_g = 1 +``` + If you want to disable the user validation for all rename features, just add this line in your `~/.vimrc` file ``` @@ -77,6 +83,7 @@ let g:vim_php_refactoring_fluent_setter = 2 nnoremap du :call PhpDetectUnusedUseStatements() vnoremap == :call PhpAlignAssigns() nnoremap sg :call PhpCreateSettersAndGetters() + nnoremap cog :call PhpCreateGetters() nnoremap da :call PhpDocAll() ## Playground.php @@ -301,6 +308,31 @@ class Foo { } ``` +### Create only getters + +``` php +cog` and you will be prompted if you want only getters for existing properties + +``` php +bar; + } +} +``` + ### Document all `da` will call your documentation plugin (by default Php Documentor for vim https://github.com/tobyS/pdv) for every uncommented classes, methods, functions and properties. diff --git a/plugin/php-refactoring-toolbox.vim b/plugin/php-refactoring-toolbox.vim index d0a436f..c0a0c4c 100644 --- a/plugin/php-refactoring-toolbox.vim +++ b/plugin/php-refactoring-toolbox.vim @@ -30,6 +30,10 @@ if !exists('g:vim_php_refactoring_auto_validate_sg') let g:vim_php_refactoring_auto_validate_sg = g:vim_php_refactoring_auto_validate endif +if !exists('g:vim_php_refactoring_auto_validate_g') + let g:vim_php_refactoring_auto_validate_g = g:vim_php_refactoring_auto_validate +endif + if !exists('g:vim_php_refactoring_auto_validate_rename') let g:vim_php_refactoring_auto_validate_rename = g:vim_php_refactoring_auto_validate endif @@ -64,6 +68,7 @@ if g:vim_php_refactoring_use_default_mapping == 1 nnoremap du :call PhpDetectUnusedUseStatements() vnoremap == :call PhpAlignAssigns() nnoremap sg :call PhpCreateSettersAndGetters() + nnoremap cog :call PhpCreateGetters() nnoremap da :call PhpDocAll() endif " }}} @@ -119,6 +124,28 @@ function! PhpDocAll() " {{{ endfunction " }}} +function! PhpCreateGetters() " {{{ + normal gg + let l:properties = [] + while search(s:php_regex_member_line, 'eW') > 0 + normal w"xye + call add(l:properties, @x) + endwhile + for l:property in l:properties + let l:camelCaseName = substitute(l:property, '^_\?\(.\)', '\U\1', '') + if g:vim_php_refactoring_auto_validate_g == 0 + call s:PhpEchoError('Create get' . l:camelCaseName . '()') + if inputlist(["0. No", "1. Yes"]) == 0 + continue + 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") + endif + endfor +endfunction +" }}} + function! PhpCreateSettersAndGetters() " {{{ normal gg let l:properties = []