Files
dotemacs/qwan/qwan-refactor.el
T

67 lines
2.8 KiB
EmacsLisp

;;; qwan/qwan-refactor.el -*- lexical-binding: t; -*-
;;;
(provide 'qwan-refactor)
(defun qwan-echo (new-name)
(interactive "SRename function to:")
(message "Will rename to %s" new-name))
;; from @tbf@functional.cafe
(defun movd (from to)
(fset to (symbol-function from))
(fset from nil)
to)
;; from @tbf@functional.cafe
(defmacro frename (from to)
`(movd ',from ',to))
(defun qrefactor/rename (new-name)
"Rename function at point. You must modify references yourself"
(interactive "MRename function to:")
(let ((bounds (bounds-of-thing-at-point 'symbol)))
(if bounds
(progn
(let* ((begin (car bounds))
(end (cdr bounds))
(old-name (buffer-substring-no-properties begin end))
(new-function-name (as-function-name new-name)))
(message "Will rename to %s" new-function-name)
;; should use as-function-name here
(movd (intern old-name) (intern new-function-name))
(ert-make-test-unbound (intern old-name))
(delete-region begin end)
(insert new-function-name)
(message "Renamed %s to %s." old-name new-name)))
(message "No symbol under cursor."))))
(defun qrefactor/remove-test ()
"Renmove test at point from memory.
See also ert-delete-test if you want to delete from a list of existing tests."
(interactive)
(let ((bounds (bounds-of-thing-at-point 'symbol)))
(if bounds
(progn
(let* ((begin (car bounds))
(end (cdr bounds))
(old-name (buffer-substring-no-properties begin end)))
(ert-make-test-unbound (intern old-name))
(message "Undefined %s." old-name))))
(message "No symbol under cursor.")))
;; tests are better than literate programming when you want to triangulate on the same function
;; org only shows the last return value by default, you can return a list of course, but still
(defun as-function-name (candidate-name)
"Take the string and make it suitable to be a function name.
Replaces space with dash and make string all lowercase."
(declare (side-effect-free t))
(s-join "-" (mapcar 'downcase (split-string candidate-name " " t))))
;; (s-join "-" (mapcar 'downcase (s-split-words s))))
(ert-deftest refactor-rename-should-leave-forward-slash ()
(should (equal (as-function-name "qwan/echo") "qwan/echo")))
(ert-deftest refactor-rename-should-replace-space-with-dash ()
(should (equal (as-function-name "qwan/echo chamber") "qwan/echo-chamber")))
(ert-deftest refactor-rename-replaces-uppercase-with-lowercase ()
(should (equal (as-function-name "Echo-chamber") "echo-chamber")))
(ert-deftest refactor-renamed-thrice ()
(should (equal 1 1)))
(ert-make-test-unbound 'refactor-rename-test-for-fun)
;; (ert "refactor.*")