Skip to content

Commit

Permalink
Variable refactoring dependent on cursor point
Browse files Browse the repository at this point in the history
Summary:
By using (thing-at-point 'symbol), the refactoring command becomes
highly dependent on the placement of the point at the time the
refactoring command is called. By using 'sexp, the placement of the
point doesn't matter.

The tests were never catching this because it only shows up when
php-mode is actually enabled. The tests have been updated to cover this
situation. I have also expanded them to verify that we never break snake
case functionality as well.

Fixes #4
  • Loading branch information
keelerm84 committed Nov 24, 2017
1 parent de47bb7 commit d06dabd
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Cask
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

(development
(depends-on "ecukes")
(depends-on "espuds"))
(depends-on "espuds")
(depends-on "php-mode"))
52 changes: 50 additions & 2 deletions features/convert-local.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,59 @@ Feature: Convert Local to Instance Variable
}
}
"""
And I turn on php-mode
And I turn on php-refactor-mode
And I go to word "$localVariable"
And I place the cursor before "$localVariable"
And I press "C-c r lv"
Then I should see "private $localVariable"
And I should not see pattern "^ *$localVariable"

Scenario: A local variable should be converted to an instance variable when point is in middle of variable
When I open temp file "convert-local-to-instance-variable"
And I insert:
"""
<?php
class ConvertLocalToInstanceVariable
{
public function internalMethod()
{
$localVariable = "This needs to be an instance variable";
echo $localVariable;
}
}
"""
And I turn on php-mode
And I turn on php-refactor-mode
And I place the cursor after "$localV"
And I press "C-c r lv"
Then I should see "private $localVariable"
And I should not see pattern "^ *$localVariable"

Scenario: A local variable should be converted to an instance variable when using snake case
When I open temp file "convert-local-to-instance-variable"
And I insert:
"""
<?php
class ConvertLocalToInstanceVariable
{
public function internalMethod()
{
$local_variable = "This needs to be an instance variable";
echo $local_variable;
}
}
"""
And I turn on php-mode
And I turn on php-refactor-mode
And I place the cursor before "$local_variable"
And I press "C-c r lv"
Then I should see "private $local_variable"
And I should not see pattern "^ *$local_variable"

Scenario: Converting local variable is an undoable operation
When I open temp file "convert-local-to-instance-variable"
And I insert:
Expand All @@ -38,8 +85,9 @@ Feature: Convert Local to Instance Variable
}
}
"""
And I turn on php-mode
And I turn on php-refactor-mode
And I go to word "$localVariable"
And I place the cursor before "$localVariable"
And I press "C-c r lv"
And I press "C-_"
Then I should not see "private $localVariable"
Expand Down
6 changes: 4 additions & 2 deletions features/extract-method.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ Feature: Extract a Method
}
}
"""
And I turn on php-mode
And I turn on php-refactor-mode
And I go to word "$localVariable"
And I place the cursor before "$localVariable"
And I set the mark
And I go to word "World"
And I start an action chain
Expand Down Expand Up @@ -65,8 +66,9 @@ Feature: Extract a Method
}
}
"""
And I turn on php-mode
And I turn on php-refactor-mode
And I go to word "$localVariable"
And I place the cursor before "$localVariable"
And I set the mark
And I go to word "World"
And I start an action chain
Expand Down
2 changes: 2 additions & 0 deletions features/optimize-use.feature
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Feature: Optimize Use
}
}
"""
And I turn on php-mode
And I turn on php-refactor-mode
And I press "C-c r ou"
Then I should see "use Top\Level\Domain\Name"
Expand All @@ -42,6 +43,7 @@ Feature: Optimize Use
}
}
"""
And I turn on php-mode
And I turn on php-refactor-mode
And I press "C-c r ou"
And I press "C-_"
Expand Down
58 changes: 56 additions & 2 deletions features/rename-local.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,68 @@ Feature: Rename Local Variable
}
}
"""
And I turn on php-mode
And I turn on php-refactor-mode
And I go to word "$localVariable"
And I place the cursor before "$localVariable"
And I start an action chain
And I press "C-c r rv"
And I type "renamedVariable"
And I execute the action chain
Then I should not see "$localVariable"
And I should see "$renamedVariable"

Scenario: A variable should be renamed when point is in middle of variable
When I open temp file "rename-local-variable"
And I insert:
"""
<?php
class RenameLocalVariable
{
public function internalMethod()
{
$localVariable = "This variable needs renamed.";
echo $localVariable;
}
}
"""
And I turn on php-mode
And I turn on php-refactor-mode
And I place the cursor after "$localV"
And I start an action chain
And I press "C-c r rv"
And I type "renamedVariable"
And I execute the action chain
Then I should not see "$localVariable"
And I should see "$renamedVariable"

Scenario: A variable should be renamed when using snake case
When I open temp file "rename-local-variable"
And I insert:
"""
<?php
class RenameLocalVariable
{
public function internalMethod()
{
$local_variable = "This variable needs renamed.";
echo $local_variable;
}
}
"""
And I turn on php-mode
And I turn on php-refactor-mode
And I place the cursor before "$local_variable"
And I start an action chain
And I press "C-c r rv"
And I type "renamed_variable"
And I execute the action chain
Then I should not see "$local_variable"
And I should see "$renamed_variable"

Scenario: Renaming a variable is an undoable operation
When I open temp file "rename-local-variable"
And I insert:
Expand All @@ -41,8 +94,9 @@ Feature: Rename Local Variable
}
}
"""
And I turn on php-mode
And I turn on php-refactor-mode
And I go to word "$localVariable"
And I place the cursor before "$localVariable"
And I start an action chain
And I press "C-c r rv"
And I type "renamedVariable"
Expand Down
1 change: 1 addition & 0 deletions features/support/env.el
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

(add-to-list 'load-path php-refactor-root-path)

(require 'php-mode)
(require 'php-refactor-mode)
(require 'espuds)
(require 'ert)
Expand Down
4 changes: 2 additions & 2 deletions php-refactor-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"convert-local-to-instance-variable"
(buffer-file-name)
(php-refactor--get-effective-line-number-as-string)
(thing-at-point 'symbol)))
(thing-at-point 'sexp)))

(defun php-refactor--optimize-use ()
"Optimizes the use of Fully qualified names in a file."
Expand Down Expand Up @@ -111,7 +111,7 @@ END is the ending position of the selected region."
"rename-local-variable"
(buffer-file-name)
(php-refactor--get-effective-line-number-as-string)
(thing-at-point 'symbol)
(thing-at-point 'sexp)
renamed)))

(defun php-refactor--get-effective-line-number-as-string ()
Expand Down

0 comments on commit d06dabd

Please sign in to comment.