Anonymous avatar Anonymous committed 03eb366 Draft

initial import

Comments (0)

Files changed (1)

+;(require 'ruby-mode)
+
+;; utils
+(defun rref:current-line ()
+  "Return the vertical position of point..."
+  (+ (count-lines (window-start) (point))
+     (if (= (current-column) 0) 1 0)
+     -1))
+
+;; wrap with module
+(defun rref:wrap-region-with-module ()
+  "Wraps the currently marked region with a module definition. The definition can include :: in which cases nested modules will be created."
+  (interactive)
+  (when (region-active-p)
+    (transient-mark-mode 1)
+    (let* ((modulestr (read-from-minibuffer "Modulename: "))
+	   (modules (mapcar 'rref:make-module-prefix (split-string modulestr "::")))
+	   (prefix  (mapconcat 'identity modules "\n"))
+	   (suffix  (mapconcat (lambda (_) "end") modules "\n")))
+      (save-excursion
+	(let ((result (rref:wrap-region (region-beginning) (region-end) prefix suffix)))
+	  (indent-region (car result) (cdr result)))))))
+
+(defun rref:make-module-prefix (mod)
+  (concat "module " (capitalize mod)))
+
+(defun rref:wrap-region (start end prefix suffix)
+  "wraps a chunk of text with prefix and suffix. It returns the beginning and end of the entire content including the prefix and suffix line"
+  (let ((new-start nil)
+	(new-end nil)
+	(content-size (- end start))
+	;; prefix + newline
+	(prefix-size (+ (length prefix) 1))
+	;; sufffix + newline
+	(suffix-size (+ (length suffix) 1))
+	(deactivate-mark nil))
+
+    ;; insert prefix
+    (goto-char start)
+    (beginning-of-line)
+    (newline)
+    (forward-line -1)
+    (setq new-start (point))
+    (insert prefix)
+
+    ;; insert suffix
+    (goto-char (+ start prefix-size content-size))
+    (newline)
+    (insert suffix)
+    (setq new-end (point))
+
+    (cons new-start new-end)))
+
+
+(setq mark-direction 'f)
+(defun rref:mark-syntactic-construct ()
+  "mark a syntacic contstruct (read sexp)"
+  (interactive)
+  (deactivate-mark nil)
+  (if (eq mark-direction 'f)
+      (progn
+	(ruby-forward-sexp)
+	(set-mark (point))
+	(ruby-backward-sexp)
+	(setq mark-direction 'b))
+    (progn
+      (ruby-backward-sexp 2)
+      (set-mark (point))
+      (ruby-forward-sexp)
+      (setq mark-direction 'f))))
+
+
+(defun rref:extract-method ()
+  (interactive)
+  (let ((method-name (read-from-minibuffer "Methodname: "))
+	(text (buffer-substring (region-beginning) (region-end)))
+	(m-start nil)
+	(m-end  nil))
+    (save-excursion
+      (delete-region (region-beginning) (region-end))
+      (cond
+       ((re-search-forward "private *" nil t 1)
+	(end-of-line)
+	(newline)
+	(insert (rref:build-method method-name text))
+	(ruby-backward-sexp)
+	(setq m-start (point))
+	(ruby-forward-sexp)
+	(indent-region m-start (point)))
+
+       ((search-backward "def ")
+	(beginning-of-line)
+	(ruby-forward-sexp)
+	(newline-and-indent)
+	(insert "private\n")
+	(insert (rref:build-method method-name text))
+	(ruby-backward-sexp)
+	(setq m-start (point))
+	(ruby-forward-sexp)
+	(indent-region m-start (point)))))
+    (insert method-name)
+    (indent-according-to-mode)))
+
+(defun rref:build-method (name body)
+  (format "def %s(*args,&block)\n %s \nend\n" name body))
+
+
+(defvar ruby-refactoring-mode-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map (kbd "C-c C-c e") 'rref:extract-method)
+    (define-key map (kbd "C-c C-c w") 'rref:wrap-region-with-module)
+    (define-key map (kbd "C-c C-c <space> ") 'rref:mark-syntactic-construct)
+    map)
+  "Keymap used in ruby-refactoring-mode.")
+
+
+(define-minor-mode ruby-refactoring
+  "Toggle Ruby Refactoring"
+  nil
+  " Ruby:Refactoring"
+  :keymap ruby-refactoring-mode-map
+  :group 'ruby-refactoring)
+
+
+(provide 'ruby-refactoring)
+
+(require 'ruby-refactoring)
+
+(add-hook 'ruby-mode-hook (lambda () (ruby-refactoring 1)))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.