vim-refact
vim-refact copied to clipboard
Some refactoring stuff for Vim
Refactor plugin for Vim.
You can use this plugin to make some refactoring on your code. It was originally written to work with the Ruby language, but it's flexible (I hope, I'll add some more languages later) to work with another languages.
Examples
Extracting a method
Let's say we have this Ruby code:
- module Test
- puts "bye"
- puts "tchau"
- puts "hasta la vista"
-
test class
- class Test
-
puts "hi"
-
puts "oi"
-
puts "hola"
-
def initialize
-
@foo = "bar"
-
end
-
# test method
-
def test
-
puts "test!"
-
puts "bye!"
-
c = 0
-
while c < 10
-
puts "c = #{c}"
-
c += 1
-
end
-
end
- end
- end
If the cursor is positioned over the line 2, and the lines are selected till the line 4, we can type:
:Rem
or, in visual mode, typing the new method name when asked
rem
to extract those lines to a
:Rem say_bye<CR>
or on visual mode
rem (typing say_bye)
will turn our code to this:
- module Test
-
test class
- class Test
-
puts "hi"
-
puts "oi"
-
puts "hola"
-
def initialize
-
@foo = "bar"
-
end
-
# test method
-
def test
-
puts "test!"
-
puts "bye!"
-
c = 0
-
while c < 10
-
puts "c = #{c}"
-
c += 1
-
end
-
end
- end
- def say_bye
-
puts "bye"
-
puts "tchau"
-
puts "hasta la vista"
- end
- end
where a module method was created with the selected content.
Now, selecting from line 4 to 6, extracting the code to a method inside the class Test, called say_hi, with
:Rem say_hi
or, in visual mode, typing say_hi when asked for a new method name
rem
and the code now is
- module Test
-
test class
- class Test
-
def initialize
-
@foo = "bar"
-
end
-
# test method
-
def test
-
puts "test!"
-
puts "bye!"
-
c = 0
-
while c < 10
-
puts "c = #{c}"
-
c += 1
-
end
-
end
-
def say_hi
-
puts "hi"
-
puts "oi"
-
puts "hola"
-
end
- end
- def say_bye
-
puts "bye"
-
puts "tchau"
-
puts "hasta la vista"
- end
- end
We can remove content from inside a method to another one. Let's say we want to remove lines 8 and 9 to a method called another_test. Select those 2 lines and
:Rem another_test
or, in visual mode, typing another_test when asked about the new method name
rem
and now the code is
- module Test
-
test class
- class Test
-
def initialize
-
@foo = "bar"
-
end
-
# test method
-
def test
-
another_test
-
c = 0
-
while c < 10
-
puts "c = #{c}"
-
c += 1
-
end
-
end
-
def another_test
-
puts "test!"
-
puts "bye!"
-
end
-
def say_hi
-
puts "hi"
-
puts "oi"
-
puts "hola"
-
end
- end
- def say_bye
-
puts "bye"
-
puts "tchau"
-
puts "hasta la vista"
- end
- end
Now we have the option to extract lines 10 to 14 or 12 to 13. Let's try the first option, extracting the code to a method called count. Select from lines 10 to 14 and
:Rem count
or, on visual mode, typing count when asked about the new method name
rem
and now the code is
- module Test
-
test class
- class Test
-
def initialize
-
@foo = "bar"
-
end
-
# test method
-
def test
-
another_test
-
count
-
end
-
def count
-
c = 0
-
while c < 10
-
puts "c = #{c}"
-
c += 1
-
end
-
end
-
def another_test
-
puts "test!"
-
puts "bye!"
-
end
-
def say_hi
-
puts "hi"
-
puts "oi"
-
puts "hola"
-
end
- end
- def say_bye
-
puts "bye"
-
puts "tchau"
-
puts "hasta la vista"
- end
- end
Or we can extract only from line 12 (see code before the last change) to a method called show_count, creating a c parameter and sending the c variable to the method, selecting line 12 and
:Rem show_count c
or on visual mode, typing show_count c when asked about the new method name
rem
and now the code is
- module Test
-
test class
- class Test
-
def initialize
-
@foo = "bar"
-
end
-
# test method
-
def test
-
another_test
-
c = 0
-
while c < 10
-
show_count(c)
-
c += 1
-
end
-
end
-
def show_count(c)
-
puts "c = #{c}"
-
end
-
def another_test
-
puts "test!"
-
puts "bye!"
-
end
-
def say_hi
-
puts "hi"
-
puts "oi"
-
puts "hola"
-
end
- end
- def say_bye
-
puts "bye"
-
puts "tchau"
-
puts "hasta la vista"
- end
- end
Renaming a variable on method scope
If we have this piece of code:
-
test method, with foo argument
- def some_method(foo)
- foo.inspect
- puts "foo converted to string: #{foo}"
- end
and position the cursor on rows 3 or 4 and use
:Rrv foo bar
or, more easier, put the cursor over the foo word and type
rrv (typing the new variable name when asked)
we changed all the ocurrences of foo to bar inside the method:
-
test method, with foo argument
- def some_method(bar)
- bar.inspect
- puts "bar converted to string: #{bar}"
- end
if the cursor position is on line 1, all the other ocurrences of the foo var above (ok, we don't have more lines above here but you got the point) will be also changed.
Renaming an attribute
If we have this piece of code:
- class Test
-
def initialize
-
@foo = "bar"
-
end
-
def print_contents
-
# foo contents
-
puts "#{@foo}"
-
end
- end
and wants to change all the ocurrences of the foo attribute (@foo) to @bar, we can use, inside the class scope:
:Rra foo bar
or, more easier, put the cursor over the foo word and type
rra (typing the new attribute name when asked)
so we can get
- class Test
-
def initialize
-
@bar = "bar"
-
end
-
def print_contents
-
# foo contents
-
puts "#{@bar}"
-
end
- end
Align assigment operators
Suppose we have this code:
a = 1 ab += 2 abc -= 3 abcd *= 4 abcde /= 2 abcdef =~ /foo/ abcdefg != /bar/
Ugly, right? Select all the lines and type
raa
and then
a = 1 ab += 2 abc -= 3 abcd *= 4 abcde /= 2 abcdef =~ /foo/ abcdefg != /bar/
Better, uh?