Permalink
Browse files

Merge pull request #25 from despo/issue23

Fixes for issues 23 and 24
  • Loading branch information...
2 parents 252161d + d86f9e0 commit ca423026368835cd067efd135364a28722b9ef1e @ecomba committed Jun 1, 2011
@@ -20,3 +20,71 @@ Feature: Inline Temp :RInlineTemp
puts 10
"""
+
+ Scenario: Inline a temporary variable to two variables on the same line
+ Given I have the following code:
+ """
+ x = 5
+ y = x and z = x
+ """
+ When I go to the line and execute:
+ """
+ :RInlineTemp
+ """
+ Then I should see:
+ """
+ y = 5 and z = 5
+
+ """
+
+ Scenario: Inline a temporary variable to all variables within the context of a method
+ Given I have the following code:
+ """
+ def some_method
+ x = 5
+ y = x and z = x
+ r = x + 2
+ end
+
+ def some_other_method
+ x = 2
+ y = x + 1
+ end
+ """
+ When I select "x = 5" and execute:
+ """
+ :RInlineTemp
+ """
+ Then I should see:
+ """
+ def some_method
+ y = 5 and z = 5
+ r = 5 + 2
+ end
+
+ def some_other_method
+ x = 2
+ y = x + 1
+ end
+
+ """
+
+ Scenario: Inline a temporary variable to all variables within the context of a method
+ Given I have the following code:
+ """
+ x = 5
+ foo = x + 10
+ x = 10
+ bar = x + 10
+ """
+ When I go to the line and execute:
+ """
+ :RInlineTemp
+ """
+ Then I should see:
+ """
+ foo = 5 + 10
+ x = 10
+ bar = x + 10
+
+ """
@@ -2,3 +2,14 @@
select_method
add_to_commands(command)
end
+
+When /^I select \"x = 5\" and execute:$/ do |command|
+ select_second_line
+ add_to_commands command
+end
+
+def select_second_line
+ @commands = ':normal 2G'
+ add_return_key
+end
+
@@ -3,23 +3,28 @@
function! InlineTemp()
" Copy the variable under the cursor into the 'a' register
" XXX: How do I copy into a variable so I don't pollute the registers?
+ let original_a = @a
normal "ayiw
" It takes 4 diws to get the variable, equal sign, and surrounding
" whitespace. I'm not sure why. diw is different from dw in this
" respect.
normal 4diw
" Delete the expression into the 'b' register
+ let original_b = @b
normal "bd$
" Delete the remnants of the line
normal dd
- " Go to the end of the previous line so we can start our search for the
- " usage of the variable to replace. Doing '0' instead of 'k$' doesn't
- " work; I'm not sure why.
- normal k$
- " Find the next occurence of the variable
- exec '/\<' . @a . '\>'
- " Replace that occurence with the text we yanked
- exec ':.s/\<' . @a . '\>/' . @b
+
+ " Find the start and end of the current block
+ " TODO: tidy up if no matching 'def' found (start would be 0 atm)
+ let [block_start, block_end] = common#get_range_for_block('\<def\>','Wb')
+
+ " Rename the variable within the range of the block
+ call common#gsub_all_in_range(block_start, block_end, '\<' . @a . '\>', @b)
+
+ " Put bck original register contents
+ let @a = original_a
+ let @b = original_b
endfunction

0 comments on commit ca42302

Please sign in to comment.