Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Convert Post Conditional refactoring #23

Merged
merged 1 commit into from
Aug 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

Ruby refactor is inspired by the Vim plugin vim-refactoring-ruby, currently found at https://github.com/ecomba/vim-ruby-refactoring.

I've implemented 5 refactorings
These are the refactorings available
- Extract to Method (C-c C-r e)
- Extract Local Variable (C-c C-r v)
- Extract Constant (C-c C-r c)
- Add Parameter (C-c C-r p)
- Extract to Let (C-c C-r l)
- Convert Post Conditional (C-c C-r o)

# Install
To install manually, add ruby-refactor.el to your load path, then:
Expand Down Expand Up @@ -104,11 +105,28 @@ Oh, if you invoke with a prefix arg (`C-u`, etc.), it'll swap the placement
of the let. If you have location as top, a prefix argument will place
it closest. I kinda got nutty with this one.

## Convert Post Conditional:
Select a region of text and invoke `ruby-refactor-convert-post-conditional`.
This simply moves the expression inside of an 'if' or 'unless' block.

So this:

```ruby
do_some_stuff('blah') if condition
```

becomes

```ruby
if condition
do_some_stuff('blah')
end
```


## TODO
From the vim plugin, these remain to be done (I don't plan to do them all.)
- remove inline temp (sexy!)
- convert post conditional


# License
Expand Down
22 changes: 20 additions & 2 deletions ruby-refactor.el
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
;; ## TODO
;; From the vim plugin, these remain to be done (I don't plan to do them all.)
;; - remove inline temp (sexy!)
;; - convert post conditional

;;; Code:

Expand Down Expand Up @@ -163,6 +162,7 @@ being altered."
(define-key prefix-map (kbd "l") 'ruby-refactor-extract-to-let)
(define-key prefix-map (kbd "v") 'ruby-refactor-extract-local-variable)
(define-key prefix-map (kbd "c") 'ruby-refactor-extract-constant)
(define-key prefix-map (kbd "o") 'ruby-refactor-convert-post-conditional)
(define-key map ruby-refactor-keymap-prefix prefix-map))
map)
"Keymap to use in ruby refactor minor mode.")
Expand Down Expand Up @@ -428,7 +428,25 @@ If a region is not selected, the transformation uses the current line."
(defun ruby-refactor-convert-post-conditional()
"Convert post conditional expression to conditional expression"
(interactive)
(error "Not Yet Implemented"))
(save-restriction
(save-match-data
(widen)
(let* ((text-begin (region-beginning))
(text-end (region-end))
(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)))
(newline-and-indent)
(insert (format "%s" (cadr line-components)))
(newline-and-indent)
(insert "end")
(ruby-indent-line)
(search-backward conditional)))))

;;;###autoload
(define-minor-mode ruby-refactor-mode
Expand Down