Skip to content

Commit

Permalink
Extend existing functionality and add string conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
rejeep committed Apr 6, 2012
1 parent 7125099 commit 5da8c48
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 20 deletions.
45 changes: 45 additions & 0 deletions features/string-conversion.feature
@@ -0,0 +1,45 @@
Feature: String conversion
As a ruby-tools-mode user
I want to turn single quote strings to double quote string and reverse

Scenario: Turn single quote string to double quote string
When I insert "'foo'"
And I turn on ruby-mode
And I place the cursor on the string "foo"
And I press "C-""
Then I should see ""foo""
Scenario: Turn single quote string to double quote string in method call
When I insert "foo('bar')"
And I turn on ruby-mode
And I place the cursor on the string "bar"
And I press "C-""
Then I should see "foo("bar")"

Scenario: Do not turn to single quote string when on single quote string
When I insert "'foo'"
And I turn on ruby-mode
And I place the cursor on the string "foo"
And I press "C-'"
Then I should see "'foo'"

Scenario: Turn double quote string to single quote string
When I insert ""foo""
And I turn on ruby-mode
And I place the cursor on the string "foo"
And I press "C-'"
Then I should see "'foo'"

Scenario: Turn double quote string to single quote string in method call
When I insert "foo("bar")"
And I turn on ruby-mode
And I place the cursor on the string "bar"
And I press "C-'"
Then I should see "foo('bar')"

Scenario: Do not turn to double quote string when on double quote string
When I insert ""foo""
And I turn on ruby-mode
And I place the cursor on the string "foo"
And I press "C-""
Then I should see ""foo""
14 changes: 14 additions & 0 deletions features/string-to-symbol.feature
Expand Up @@ -36,6 +36,20 @@ Feature: String To Symbol
And I place the cursor on the string "foo_bar"
And I press "C-:"
Then I should see ":foo_bar"

Scenario: Turn string to symbol when at beginning of string
When I insert "'foo'"
And I turn on ruby-mode
And I go to point "2"
And I press "C-:"
Then I should see ":foo"

Scenario: Turn string to symbol when at end of string
When I insert "'foo'"
And I turn on ruby-mode
And I go to point "4"
And I press "C-:"
Then I should see ":foo"

Scenario: Do not turn symbol to string when not on a string
When I insert "foo('bar')"
Expand Down
21 changes: 21 additions & 0 deletions features/symbol-to-string.feature
Expand Up @@ -29,10 +29,31 @@ Feature: Symbol To String
And I place the cursor on the symbol "bar"
And I press "C-""
Then I should see "foo("bar")"

Scenario: Turn symbol in to string when at beginning of symbol
When I insert "foo(:bar)"
And I turn on ruby-mode
And I go to point "6"
And I press "C-'"
Then I should see "foo('bar')"

Scenario: Turn symbol in to string when at end of symbol
When I insert "foo(:bar)"
And I turn on ruby-mode
And I go to point "8"
And I press "C-'"
Then I should see "foo('bar')"

Scenario: Do not turn symbol to string when not on symbol
When I insert "foo(:bar)"
And I turn on ruby-mode
And I place the cursor on "foo"
And I press "C-'"
Then I should see "foo(:bar)"

Scenario: Do not turn symbol to string when symbol in string
When I insert "'foo :bar baz'"
And I turn on ruby-mode
And I place the cursor on "bar"
And I press "C-'"
Then I should see "'foo :bar baz'"
72 changes: 52 additions & 20 deletions ruby-tools.el
Expand Up @@ -50,27 +50,27 @@

(defvar ruby-tools-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-'") 'ruby-tools-symbol-to-single-quote-string)
(define-key map (kbd "C-\"") 'ruby-tools-symbol-to-double-quote-string)
(define-key map (kbd "C-'") 'ruby-tools-to-single-quote-string)
(define-key map (kbd "C-\"") 'ruby-tools-to-double-quote-string)
(define-key map (kbd "C-:") 'ruby-tools-string-to-symbol)
map)
"Keymap for `ruby-tools-mode'.")

(defun ruby-tools-symbol-to-single-quote-string ()
(defun ruby-tools-to-single-quote-string ()
"Turn symbol at point to a single quote string."
(interactive)
(ruby-tools-symbol-to-string "'"))
(ruby-tools-to-string "'"))

(defun ruby-tools-symbol-to-double-quote-string ()
(defun ruby-tools-to-double-quote-string ()
"Turn symbol at point to a double quote string."
(interactive)
(ruby-tools-symbol-to-string "\""))
(ruby-tools-to-string "\""))

(defun ruby-tools-string-to-symbol ()
"Turn string at point to symbol."
(interactive)
(if (ruby-tools-string-at-point-p)
(let* ((region (ruby-tools-keyword-region))
(let* ((region (ruby-tools-string-region))
(min (nth 0 region))
(max (nth 1 region))
(region (buffer-substring-no-properties min max)))
Expand All @@ -83,34 +83,66 @@

(defun ruby-tools-symbol-at-point-p ()
"Check if cursor is at a symbol or not."
(memq 'font-lock-constant-face (text-properties-at (point))))
(and
(not (ruby-tools-string-at-point-p))
(or
(memq 'font-lock-constant-face (text-properties-at (point)))
(and
(looking-at "[A-Za-z0-9_]+")
(looking-back ":[A-Za-z0-9_]*")))))

(defun ruby-tools-string-at-point-p ()
"Check if cursor is at a string or not."
(memq 'font-lock-string-face (text-properties-at (point))))
(or
(memq 'font-lock-string-face (text-properties-at (point)))
(and
(looking-at "[^\"']+['\"]")
(looking-back "['\"][^\"']*"))))

(defun ruby-tools-keyword-region ()
"Return min and max points (as a list) for the keyword at point."
(defun ruby-tools-symbol-region ()
(list
(or
(previous-single-property-change (point) 'face)
(point-min))
(or
(next-single-property-change (point) 'face)
(point-max))))
(save-excursion
(search-backward ":" (line-beginning-position) t))
(save-excursion
(if (re-search-forward "[^A-Za-z0-9_]" (line-end-position) t)
(1- (point))
(line-end-position)))))

(defun ruby-tools-string-region ()
(list
(save-excursion
(re-search-backward "['\"][^\"']*" (line-beginning-position) t))
(save-excursion
(re-search-forward "[^\"']+['\"]" (line-end-position) t))))

(defun ruby-tools-symbol-to-string (string-quote)
(defun ruby-tools-to-string (string-quote)
"Turn symbol at point to a STRING-QUOTE string."
(if (ruby-tools-symbol-at-point-p)
(let* ((region (ruby-tools-keyword-region))
(let* ((region (ruby-tools-symbol-region))
(min (nth 0 region))
(max (nth 1 region)))
(save-excursion
(delete-region min (1+ min))
(goto-char min)
(insert string-quote)
(goto-char max)
(insert string-quote)))))
(insert string-quote)))
(if (ruby-tools-string-at-point-p)
(let* ((region (ruby-tools-string-region))
(min (nth 0 region))
(max (nth 1 region))
(string-char
(char-to-string (char-after min)))
(other-string-char
(if (equal string-char "'") "\"" "'")))
(unless (equal string-quote string-char)
(save-excursion
(delete-region min (1+ min))
(goto-char min)
(insert other-string-char)
(goto-char max)
(delete-region max (1- max))
(insert other-string-char)))))))

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

0 comments on commit 5da8c48

Please sign in to comment.