Wrote functions to change from camelCase to snake_case.

Need to also do something for dashed case (Lisp case?).
This commit is contained in:
Howard Abrams 2023-07-05 10:18:18 -07:00
parent f0f1834c02
commit 264720be8e
2 changed files with 58 additions and 1 deletions

View file

@ -562,7 +562,7 @@ Cant remember all the shortcuts on the ~g~ key, and =which-key= displays the
;; These go into operator mode, so the key sequence, g U i o
;; upper cases the symbol at point:
"g u" '("downcase" . evil-downcase)
"g U" '("upcase" . evil-case)
"g U" '("upcase" . evil-upcase)
"g ~" '("invert case" . evil-invert-case)
;; Use this ALL the time:

View file

@ -557,6 +557,63 @@ While there are language-specific ways to rename variables and functions, [[http
:config
(ha-leader "s e" '("iedit" . iedit-mode)))
#+end_src
** Case Conversion
Sure we can upper and lowercase stuff, but can we change from Camel to Snake case and back? For instance, =fooBarBaz=, would with ~z s~ keybinding, change to =foo_bar_baz= (and repeating that sequence would return it to =fooBarBaz=).
#+begin_src emacs-lisp
(defun get-bounds-of-symbol ()
"Return a list of the start and end of the current symbol at point.
Return the start and end of region, if the region is active."
(if (region-active-p)
(cons (region-beginning) (region-end))
(bounds-of-thing-at-point 'symbol)))
(defun camel-to-snake-case ()
"Convert the active region or current symbol to snake case.
For instance, fooBarBaz will be foo_bar_baz."
(interactive)
(cl-destructuring-bind (start . end) (get-bounds-of-symbol)
(save-excursion
(goto-char start)
(narrow-to-region start end)
(let ((case-fold-search nil))
(while (re-search-forward (rx (group (one-or-more upper))) nil t)
(replace-match (concat "_" (downcase (match-string 1))) t nil)))
(widen))))
(defun snake-to-camel-case ()
"Convert the active region or current symbol to camel case.
For instance, foo_bar_baz will be fooBarBaz."
(interactive)
(cl-destructuring-bind (start . end) (get-bounds-of-symbol)
(save-excursion
(goto-char start)
(narrow-to-region start end)
(let ((case-fold-search nil))
(while (re-search-forward (rx "_" (group alpha)) nil t)
(replace-match (upcase (match-string 1)) t nil)))
(widen))))
(defun toggle-snake-to-camel-case ()
"Toggle current symbol from snake to camel, and visa-versa."
(interactive)
(cl-destructuring-bind (start . end) (get-bounds-of-symbol)
(save-excursion
(goto-char start)
(if (looking-at (rx (zero-or-more alphanumeric) "_"))
(snake-to-camel-case)
(camel-to-snake-case)))))
#+end_src
And we can put this on the ~z~ keybinding:
#+begin_src emacs-lisp
(use-package evil
:general
(:states '(normal visual motion operator)
"z s" '("toggle snake/camel" . toggle-snake-to-camel-case)
"z S" '("to snake case" . camel-to-snake-case)
"z C" '("to camel case" . snake-to-camel-case)))
#+end_src
I would like to have this bound on the ~g~ sequence, but that is crowded.
** Commenting
I like =comment-dwim= (~M-;~), and I like =comment-box=, but I have an odd personal style that I like to codify: