Skip to content

Commit

Permalink
Added RenamelocalvariableCommand and Convertlocalvariabletoinstanceva…
Browse files Browse the repository at this point in the history
…riableCommand commands
  • Loading branch information
hgraca committed May 2, 2013
1 parent f035ba6 commit 23b4ce8
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 32 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
1.2.0
=====
* Implementation of:
* Rename Local Variable
* Convert Local Variable To Instance Variable

1.0.0
=====
* Initial Version
* Implementation of:
* Extract method
8 changes: 7 additions & 1 deletion Context.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
"children":
[
{ "command": "extract", "args": {"execute": false}, "caption": "Extract Method - Diff" },
{ "command": "extract", "args": {"execute": true}, "caption": "Extract Method - Patch" }
{ "command": "extract", "args": {"execute": true}, "caption": "Extract Method - Patch" },
{ "caption": "-" },
{ "command": "renamelocalvariable", "args": {"execute": false}, "caption": "Rename Local Var - Diff" },
{ "command": "renamelocalvariable", "args": {"execute": true}, "caption": "Rename Local Var - Patch" },
{ "caption": "-" },
{ "command": "convertlocalvariabletoinstancevariable", "args": {"execute": false}, "caption": "Convert Local Var to Instance - Diff" },
{ "command": "convertlocalvariabletoinstancevariable", "args": {"execute": true}, "caption": "Convert Local Var to Instance - Patch" }
]
}
]
6 changes: 5 additions & 1 deletion Default.sublime-keymap
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
[
{ "keys": ["ctrl+shift+r", "ctrl+shift+1"], "command": "extract", "args": {"execute": false} },
{ "keys": ["ctrl+shift+r", "ctrl+shift+2"], "command": "extract", "args": {"execute": true} }
{ "keys": ["ctrl+shift+r", "ctrl+shift+2"], "command": "extract", "args": {"execute": true} },
{ "keys": ["ctrl+shift+r", "ctrl+shift+3"], "command": "renamelocalvariable", "args": {"execute": false} },
{ "keys": ["ctrl+shift+r", "ctrl+shift+4"], "command": "renamelocalvariable", "args": {"execute": true} },
{ "keys": ["ctrl+shift+r", "ctrl+shift+5"], "command": "convertlocalvariabletoinstancevariable", "args": {"execute": false} },
{ "keys": ["ctrl+shift+r", "ctrl+shift+6"], "command": "convertlocalvariabletoinstancevariable", "args": {"execute": true} }
]
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
## Future developments

Developed by PHP Refactoring Browser
Implemented in this plugin
Wraped by this plugin
▼ ▼
- [x] [x] Extract Method
- [x] [ ] Rename Local Variable
- [x] [x] Rename Local Variable
- [ ] [ ] Optimize use statements
- [x] [ ] Convert Local Variable to Instance Variable
- [x] [x] Convert Local Variable to Instance Variable
- [ ] [ ] Convert Magic Value to Constant
- [ ] [ ] Rename Method
- [ ] [ ] Rename Instance Variable
Expand All @@ -42,4 +42,8 @@
## Usefull sublime console lines

view.run_command('extract', {'execute': False})
view.run_command('extract', {'execute': True})
view.run_command('extract', {'execute': True})
view.run_command('renamelocalvariable', {'execute': False})
view.run_command('renamelocalvariable', {'execute': true})
view.run_command('convertlocalvariabletoinstancevariable', {'execute': False})
view.run_command('convertlocalvariabletoinstancevariable', {'execute': true})
1 change: 1 addition & 0 deletions messages.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"install": "messages/install.md",
"1.0.0": "messages/1.0.0.md"
"1.2.0": "messages/1.2.0.md"
}
13 changes: 13 additions & 0 deletions messages/1.2.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

New on 1.2.0
============
* Implementation of:
* Rename Local Variable
* Convert Local Variable To Instance Variable


If you have any questions or found a bug fell free to contact me at [email protected]

For bugs open an issue on github https://github.com/hgraca/sublime-text-2-php-refactor/issues

Hope you enjoy it.
4 changes: 2 additions & 2 deletions packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"platforms": {
"*": [
{
"version": "1.0.0",
"url": "https://github.com/hgraca/sublime-text-2-php-refactor/archive/1.0.0.zip"
"version": "1.2.0",
"url": "https://github.com/hgraca/sublime-text-2-php-refactor/archive/1.2.0.zip"
}
]
}
Expand Down
10 changes: 9 additions & 1 deletion sandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ class Calculator

public $test = 0;

public function calculate($a, $b, $op)
public function calculate1($a, $b, $op)
{
if ($op == '+'){
$result = $a + $b;
}
return $result;
}

public function calculate2($a, $b, $op)
{
if ($op == '+'){
$result = $a + $b;
Expand Down
84 changes: 61 additions & 23 deletions sublime-text-2-php-refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ def confirm(self, on_confirm):
else:
window.show_quick_panel([yes, no], on_confirm)

def patch(self, execute, filePath):
patch = ''
if (True == execute):
backup = ' --no-backup-if-mismatch'
if (True == Prefs.backup):
backup = ' -b'
patch = '|patch' + backup + ' ' + filePath
return patch


'''
Extract a range of lines into a new method and call this method from the original location.
Expand All @@ -104,6 +113,7 @@ def confirm(self, on_confirm):
class ExtractCommand(sublime_plugin.TextCommand, Refactor):

def run(self, edit, execute=False):
msg("ExtractCommand")
window = sublime.active_window()
sels = self.view.sel()
for sel in sels:
Expand All @@ -113,14 +123,9 @@ def run(self, edit, execute=False):
return ''

def runCommandLine(self, filePath, fromLine, toLine, newFcName, execute=False):
patch = ''
if (True == execute):
backup = ' --no-backup-if-mismatch'
if (True == Prefs.backup):
backup = ' -b'
patch = '|patch' + backup + ' ' + filePath
patch = self.patch(execute, filePath)

command = "php " + self.REFACTOR + " extract-method " + self.view.file_name() + " " + fromLine + "-" + toLine + " " + newFcName + patch
command = "php " + self.REFACTOR + " extract-method " + filePath + " " + fromLine + "-" + toLine + " " + newFcName + patch

if ((True == execute) and (True == Prefs.confirm)):
self.confirm(lambda x: self.execute('extract_' + newFcName, command, execute))
Expand All @@ -135,9 +140,37 @@ def runCommandLine(self, filePath, fromLine, toLine, newFcName, execute=False):
'''


class RenameLocalVariableCommand(sublime_plugin.TextCommand):
class RenamelocalvariableCommand(sublime_plugin.TextCommand, Refactor):

def run(self, edit, execute=False):
msg("RenameLocalVariableCommand")
window = sublime.active_window()
sels = self.view.sel()
for sel in sels:
line = str(self.view.rowcol(sel.begin())[0] + 1)
selection = self.view.substr(sel).strip("$")
window.show_input_panel("What is the variable new name?", '', lambda newVarName: self.runCommandLine(self.view.file_name(), line, selection, newVarName.strip("$"), execute), None, None)
return ''

def runCommandLine(self, filePath, line, oldVarName, newVarName, execute=False):
patch = self.patch(execute, filePath)

command = "php " + self.REFACTOR + " rename-local-variable " + filePath + " " + line + " " + oldVarName + " " + newVarName + patch

if ((True == execute) and (True == Prefs.confirm)):
self.confirm(lambda x: self.execute('rename-local-var_' + oldVarName + newVarName, command, execute))
else:
self.execute('rename-local-var_' + oldVarName + '_' + newVarName, command, execute)


'''
'''


class OptimizeuseCommand(sublime_plugin.TextCommand, Refactor):
def run(self, edit):
msg("php refactor.phar rename-local-variable " + self.view.file_name() + " 8 $oldName $newName|colordiff")
msg("OptimizeUseCommand")


'''
Expand All @@ -147,27 +180,32 @@ def run(self, edit):
'''


class OptimizeUseCommand(sublime_plugin.TextCommand):
def run(self, edit):
msg("php refactor.phar convert-local-to-instance-variable " + self.view.file_name() + " 8 $variable|colordiff")

class ConvertlocalvariabletoinstancevariableCommand(sublime_plugin.TextCommand, Refactor):

'''
def run(self, edit, execute=False):
sels = self.view.sel()
for sel in sels:
line = str(self.view.rowcol(sel.begin())[0] + 1)
selection = self.view.substr(sel).strip("$")
self.runCommandLine(self.view.file_name(), line, selection, execute)

'''
def runCommandLine(self, filePath, line, varName, execute=False):
patch = self.patch(execute, filePath)

command = "php " + self.REFACTOR + " convert-local-to-instance-variable " + filePath + " " + line + " " + varName + patch

class ConvertLocalVariableToInstanceVariableCommand(sublime_plugin.TextCommand):
def run(self, edit):
msg("ConvertLocalVariableToInstanceVariableCommand")
if ((True == execute) and (True == Prefs.confirm)):
self.confirm(lambda x: self.execute('local-var-to-instance_' + varName, command, execute))
else:
self.execute('local-var-to-instance_' + varName, command, execute)


'''
'''


class ConvertMagicValueToConstantCommand(sublime_plugin.TextCommand):
class ConvertmagicvaluetoconstantCommand(sublime_plugin.TextCommand, Refactor):
def run(self, edit):
msg("ConvertMagicValueToConstantCommand")

Expand All @@ -177,7 +215,7 @@ def run(self, edit):
'''


class RenameMethodCommand(sublime_plugin.TextCommand):
class RenamemethodCommand(sublime_plugin.TextCommand, Refactor):
def run(self, edit):
msg("RenameMethodCommand")

Expand All @@ -187,7 +225,7 @@ def run(self, edit):
'''


class RenameInstanceVariableCommand(sublime_plugin.TextCommand):
class RenameinstancevariableCommand(sublime_plugin.TextCommand, Refactor):
def run(self, edit):
msg("RenameInstanceVariableCommand")

Expand All @@ -197,7 +235,7 @@ def run(self, edit):
'''


class RenameClassCommand(sublime_plugin.TextCommand):
class RenameclassCommand(sublime_plugin.TextCommand, Refactor):
def run(self, edit):
msg("RenameClassCommand")

Expand All @@ -207,6 +245,6 @@ def run(self, edit):
'''


class RenameNamespaceCommand(sublime_plugin.TextCommand):
class RenamenamespaceCommand(sublime_plugin.TextCommand, Refactor):
def run(self, edit):
msg("RenameNamespaceCommand")
Binary file modified sublime-text-2-php-refactor.pyc
Binary file not shown.

0 comments on commit 23b4ce8

Please sign in to comment.