-
Notifications
You must be signed in to change notification settings - Fork 9
/
README
362 lines (295 loc) · 7.14 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
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:
01. module Test
02. puts "bye"
03. puts "tchau"
04. puts "hasta la vista"
05. # test class
06. class Test
07. puts "hi"
08. puts "oi"
09. puts "hola"
10. def initialize
11. @foo = "bar"
12. end
13. # test method
14. def test
15. puts "test!"
16. puts "bye!"
17. c = 0
18. while c < 10
19. puts "c = #{c}"
20. c += 1
21. end
22. end
23. end
24. end
If the cursor is positioned over the line 2, and the lines are selected
till the line 4, we can type:
:Rem <method> (<p1>, <p2>)
or, in visual mode, typing the new method name when asked
rem
to extract those lines to a <method> with some optionally parameters. So,
:Rem say_bye<CR>
or on visual mode
rem (typing say_bye)
will turn our code to this:
01. module Test
02. # test class
03. class Test
04. puts "hi"
05. puts "oi"
06. puts "hola"
07. def initialize
08. @foo = "bar"
09. end
10. # test method
11. def test
12. puts "test!"
13. puts "bye!"
14. c = 0
15. while c < 10
16. puts "c = #{c}"
17. c += 1
18. end
19. end
20. end
21. def say_bye
22. puts "bye"
23. puts "tchau"
24. puts "hasta la vista"
25. end
26. 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
00. module Test
01. # test class
02. class Test
03. def initialize
04. @foo = "bar"
05. end
06. # test method
07. def test
08. puts "test!"
09. puts "bye!"
10. c = 0
11. while c < 10
12. puts "c = #{c}"
13. c += 1
14. end
15. end
16. def say_hi
17. puts "hi"
18. puts "oi"
19. puts "hola"
20. end
21. end
22. def say_bye
23. puts "bye"
24. puts "tchau"
25. puts "hasta la vista"
26. end
27. 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
01. module Test
02. # test class
03. class Test
04. def initialize
05. @foo = "bar"
06. end
07. # test method
08. def test
09. another_test
10. c = 0
11. while c < 10
12. puts "c = #{c}"
13. c += 1
14. end
15. end
16. def another_test
17. puts "test!"
18. puts "bye!"
19. end
20. def say_hi
21. puts "hi"
22. puts "oi"
23. puts "hola"
24. end
25. end
26. def say_bye
27. puts "bye"
28. puts "tchau"
29. puts "hasta la vista"
30. end
31. 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
01. module Test
02. # test class
03. class Test
04. def initialize
05. @foo = "bar"
06. end
07. # test method
08. def test
09. another_test
10. count
11. end
12. def count
13. c = 0
14. while c < 10
15. puts "c = #{c}"
16. c += 1
17. end
18. end
19. def another_test
20. puts "test!"
21. puts "bye!"
22. end
23. def say_hi
24. puts "hi"
25. puts "oi"
26. puts "hola"
27. end
28. end
29. def say_bye
30. puts "bye"
31. puts "tchau"
32. puts "hasta la vista"
33. end
34. 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
01. module Test
02. # test class
03. class Test
04. def initialize
05. @foo = "bar"
06. end
07. # test method
08. def test
09. another_test
10. c = 0
11. while c < 10
12. show_count(c)
13. c += 1
14. end
15. end
16. def show_count(c)
17. puts "c = #{c}"
18. end
19. def another_test
20. puts "test!"
21. puts "bye!"
22. end
23. def say_hi
24. puts "hi"
25. puts "oi"
26. puts "hola"
27. end
28. end
29. def say_bye
30. puts "bye"
31. puts "tchau"
32. puts "hasta la vista"
33. end
34. end
Renaming a variable on method scope
-----------------------------------
If we have this piece of code:
01. # test method, with foo argument
02. def some_method(foo)
03. foo.inspect
04. puts "foo converted to string: #{foo}"
05. 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*:
01. # test method, with foo argument
02. def some_method(bar)
03. bar.inspect
04. puts "bar converted to string: #{bar}"
05. 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:
01. class Test
02. def initialize
03. @foo = "bar"
04. end
05. def print_contents
06. # foo contents
07. puts "#{@foo}"
08. end
09. 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
01. class Test
02. def initialize
03. @bar = "bar"
04. end
05. def print_contents
06. # foo contents
07. puts "#{@bar}"
08. end
09. 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?