Permalink
Browse files

Add function to toggle ruby hash syntax, adapted from TextMate

  • Loading branch information...
1 parent 101589c commit a306a0136a0b6d62f01626fd9d6e233e3b58a487 @purcell committed Mar 6, 2013
Showing with 17 additions and 0 deletions.
  1. +17 −0 init-ruby-mode.el
View
@@ -101,4 +101,21 @@
(add-hook 'ruby-mode-hook (lambda () (local-set-key [f6] 'recompile)))
+;;----------------------------------------------------------------------------
+;; Ruby - handy helpers
+;;----------------------------------------------------------------------------
+
+;; Borrowed from https://github.com/textmate/ruby.tmbundle/blob/master/Commands/Convert%20Ruby%20hash%20to%201_9%20syntax.tmCommand
+(defun sanityinc/ruby-toggle-hash-syntax (beg end)
+ "Toggle between ruby 1.8 and 1.9 hash styles."
+ (interactive "r")
+ (save-excursion
+ (goto-char beg)
+ (cond
+ ((save-excursion (search-forward "=>" end t))
+ (replace-regexp ":\\(\\w+\\) +=> +" "\\1: " nil beg end))
+ ((save-excursion (re-search-forward "\\w+:" end t))
+ (replace-regexp "\\(\\w+\\):\\( *\\(?:\"\\(?:\\\"\\|[^\"]\\)*\"\\|'\\(?:\\'\\|[^']\\)*'\\|\\w+([^)]*)\\|[^,]+\\)\\)" ":\\1 =>\\2" nil beg end)))))
+
+
(provide 'init-ruby-mode)

0 comments on commit a306a01

Please sign in to comment.