From e5263dc59e22dcdf5c89dd6127927e24e6ab94d9 Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Wed, 28 Sep 2022 01:22:49 +0200 Subject: [PATCH] Add extract variable --- README.md | 39 ++++++++++++++++++++++++ doc/refactoring-toolbox.txt | 48 ++++++++++++++++++++++++++---- playground.php | 13 ++++++++ plugin/php-refactoring-toolbox.vim | 29 ++++++++++++++++++ 4 files changed, 123 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8aa4a0d..55387a6 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ PHP Refactoring Toolbox for VIM * Rename Method * Extract Use * Extract Const +* Extract Variable * Extract Class Property * Extract Method * Create Property @@ -79,6 +80,7 @@ let g:vim_php_refactoring_make_setter_fluent = 2 nnoremap rm :call PhpRenameMethod() nnoremap eu :call PhpExtractUse() vnoremap ec :call PhpExtractConst() + vnoremap ev :call PhpExtractVariable() nnoremap ep :call PhpExtractClassProperty() vnoremap em :call PhpExtractMethod() nnoremap np :call PhpCreateProperty() @@ -250,6 +252,43 @@ class HelloWorld { } ``` +### Extract Variable + +``` php +ev`. +You'll be prompted for a variable name. Enter a variable name and press enter + +``` php +np` will create a new property in your current class. diff --git a/doc/refactoring-toolbox.txt b/doc/refactoring-toolbox.txt index 38e6b03..0570747 100755 --- a/doc/refactoring-toolbox.txt +++ b/doc/refactoring-toolbox.txt @@ -30,6 +30,7 @@ nnoremap rcv :call PhpRenameClassVariable() nnoremap rm :call PhpRenameMethod() nnoremap eu :call PhpExtractUse() vnoremap ec :call PhpExtractConst() +vnoremap ev :call PhpExtractVariable() nnoremap ep :call PhpExtractClassProperty() vnoremap em :call PhpExtractMethod() nnoremap np :call PhpCreateProperty() @@ -47,11 +48,12 @@ Examples *refactoring-to 4. Extract Use Statement........................................|extract-use-statement| 5. Extract Class Property.......................................|extract-class-property| 6. Extract Method...............................................|extract-method| -7. Create Property..............................................|create-property| -8. Detect Unused Use Statements.................................|detect-unused-use| -9. Align assignments............................................|align-assignments| -10. Create Setters and Getters..................................|create-set-get| -11. Document all................................................|document-all| +7. Extract Variable.............................................|extract-variable| +8. Create Property..............................................|create-property| +9. Detect Unused Use Statements.................................|detect-unused-use| +10. Align assignments...........................................|align-assignments| +11. Create Setters and Getters..................................|create-set-get| +12. Document all................................................|document-all| Note: ↑ Is the position of your cursor @@ -191,7 +193,41 @@ class HelloWorld { } =============================================================================== -Create Property *create-property* +Extract Variable *extract-variable* + +ev`. +You'll be prompted for a variable name. Enter a variable name and press enter + +np will create a new property in your current class. diff --git a/playground.php b/playground.php index ffa8a97..f74ca1d 100644 --- a/playground.php +++ b/playground.php @@ -63,6 +63,19 @@ public function testExtractConst() $string = 'FOOBAR'; } + /** + * Select the content you want to place in the content with the visual mode + * (you could use viw on int or va' on string) + * and then press ev to create a variable and replace every occurrences of this + * by the variable usage + */ + public function testExtractVariable() + { + if (!$obj instanceof \Fully\Qualified\Classname) { + Throw new Exception('$obj is not a \Fully\Qualified\Classname'); + } + } + /** * Place your cursor on the "localVariableWanabeAClassVariable" variable * and press ep to promote this variable as class property diff --git a/plugin/php-refactoring-toolbox.vim b/plugin/php-refactoring-toolbox.vim index 36fda35..03fe898 100644 --- a/plugin/php-refactoring-toolbox.vim +++ b/plugin/php-refactoring-toolbox.vim @@ -62,6 +62,7 @@ if g:vim_php_refactoring_use_default_mapping == 1 nnoremap eu :call PhpExtractUse() nnoremap rm :call PhpRenameMethod() vnoremap ec :call PhpExtractConst() + vnoremap ev :call PhpExtractVariable() nnoremap ep :call PhpExtractClassProperty() vnoremap em :call PhpExtractMethod() nnoremap np :call PhpCreateProperty() @@ -252,6 +253,34 @@ function! PhpExtractConst() " {{{ endfunction " }}} +function! PhpExtractVariable() " {{{ + if visualmode() != 'v' + call s:PhpEchoError('Extract variable only works in Visual mode, not in Visual Line or Visual block') + return + endif + let l:name = inputdialog("Name of new variable: ") + " go to select and copy and delete + normal! gvx + " add marker + normal! mr + " type variable name + exec 'normal! i$'.l:name + " go to start on selection + normal! `r + let l:indentChars = indent(line('.')) + " got to line to write assignment + normal! O + " type variable assignment + exec 'normal! i'.repeat(' ', l:indentChars).'$'.l:name.' = ' + " paste selection + normal! pa; + " add empty line after assignment + normal! o + " go to start on selection + normal! `r +endfunction +" }}} + function! PhpExtractClassProperty() " {{{ normal! mr let l:name = substitute(expand(''), '^\$*', '', '')