Skip to content

Commit

Permalink
Merge pull request #27 from HParker/test_and_autoselect_post_condition
Browse files Browse the repository at this point in the history
auto select based on current line and test
  • Loading branch information
Andrew J Vargo committed Feb 14, 2016
2 parents 8be821e + 0802e9b commit e6b7125
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
18 changes: 18 additions & 0 deletions features/convert_post_condition.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Feature: The ruby-refactor-convert-post-conditional function

Background:
Given I have loaded my example Ruby file
And I turn on ruby-mode
And I turn on ruby-refactor-mode

Scenario: Should convert post conditional
When I select "some are greater"
And I start an action chain
And I press "C-c C-r o"
And I execute the action chain
Then I should see:
"""
unless long_method.empty?
puts "some are greater than 3"
end
"""
4 changes: 4 additions & 0 deletions features/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def long_method
puts "#{value} is greater than three"
end
end

def has_great_than_three?
puts "some are greater than 3" unless long_method.empty?
end
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions ruby-refactor.el
Original file line number Diff line number Diff line change
Expand Up @@ -431,18 +431,18 @@ If a region is not selected, the transformation uses the current line."
(save-restriction
(save-match-data
(widen)
(let* ((text-begin (region-beginning))
(text-end (region-end))
(let* ((text-begin (line-beginning-position))
(text-end (line-end-position))
(text (ruby-refactor-trim-newline-endings (buffer-substring-no-properties text-begin text-end)))
(conditional
(cond ((string-match-p "if" text) "if")
((string-match-p "unless" text) "unless")
(t (error "You need an `if' or `unless' on the target line"))))
(line-components (ruby-refactor-trim-list (split-string text (format " %s " conditional)))))
(delete-region text-begin text-end)
(insert (format "%s %s" conditional (car line-components)))
(insert (format "%s %s" conditional (cadr line-components)))
(newline-and-indent)
(insert (format "%s" (cadr line-components)))
(insert (format "%s" (car line-components)))
(newline-and-indent)
(insert "end")
(ruby-indent-line)
Expand Down

0 comments on commit e6b7125

Please sign in to comment.