Skip to content

Commit 62e13ca

Browse files
anthonygaleabbatsov
authored andcommitted
Add refactoring command clojure-rename-ns-alias (#529)
Originally requested in clj-refactor: clojure-emacs/clj-refactor.el#366.
1 parent a9cbe6a commit 62e13ca

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### New features
66

77
* [#496](https://github.com/clojure-emacs/clojure-mode/issues/496): Highlight `[[wikilinks]]` in comments.
8+
* [#366](https://github.com/clojure-emacs/clj-refactor.el/issues/366): Add support for renaming ns aliases.
89

910
### Bugs fixed
1011

clojure-mode.el

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,6 +2659,23 @@ lists up."
26592659
(insert sexp)
26602660
(clojure--replace-sexps-with-bindings-and-indent)))
26612661

2662+
(defun clojure--rename-ns-alias-internal (current-alias new-alias)
2663+
"Rename a namespace alias CURRENT-ALIAS to NEW-ALIAS."
2664+
(clojure--find-ns-in-direction 'backward)
2665+
(let ((rgx (concat ":as +" current-alias))
2666+
(bound (save-excursion (forward-list 1) (point))))
2667+
(when (save-excursion (search-forward-regexp rgx bound t))
2668+
(save-excursion
2669+
(while (re-search-forward rgx bound t)
2670+
(replace-match (concat ":as " new-alias))))
2671+
(save-excursion
2672+
(while (re-search-forward (concat current-alias "/") nil t)
2673+
(replace-match (concat new-alias "/"))))
2674+
(save-excursion
2675+
(while (re-search-forward (concat "#::" current-alias "{") nil t)
2676+
(replace-match (concat "#::" new-alias "{"))))
2677+
(message "Successfully renamed alias '%s' to '%s'" current-alias new-alias))))
2678+
26622679
;;;###autoload
26632680
(defun clojure-let-backward-slurp-sexp (&optional n)
26642681
"Slurp the s-expression before the let form into the let form.
@@ -2701,6 +2718,20 @@ With a numeric prefix argument the let is introduced N lists up."
27012718
(interactive)
27022719
(clojure--move-to-let-internal (read-from-minibuffer "Name of bound symbol: ")))
27032720

2721+
;;;###autoload
2722+
(defun clojure-rename-ns-alias ()
2723+
"Rename a namespace alias."
2724+
(interactive)
2725+
(let ((current-alias (read-from-minibuffer "Current alias: ")))
2726+
(save-excursion
2727+
(clojure--find-ns-in-direction 'backward)
2728+
(let ((rgx (concat ":as +" current-alias))
2729+
(bound (save-excursion (forward-list 1) (point))))
2730+
(if (save-excursion (search-forward-regexp rgx bound t))
2731+
(let ((new-alias (read-from-minibuffer "New alias: ")))
2732+
(clojure--rename-ns-alias-internal current-alias new-alias))
2733+
(message "Cannot find namespace alias: '%s'" current-alias))))))
2734+
27042735

27052736
;;; ClojureScript
27062737
(defconst clojurescript-font-lock-keywords
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
;;; clojure-mode-refactor-rename-ns-alias-test.el --- Clojure Mode: refactor rename ns alias -*- lexical-binding: t; -*-
2+
3+
;; This file is not part of GNU Emacs.
4+
5+
;; This program is free software; you can redistribute it and/or modify
6+
;; it under the terms of the GNU General Public License as published by
7+
;; the Free Software Foundation, either version 3 of the License, or
8+
;; (at your option) any later version.
9+
10+
;; This program is distributed in the hope that it will be useful,
11+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
;; GNU General Public License for more details.
14+
15+
;; You should have received a copy of the GNU General Public License
16+
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
;;; Code:
19+
20+
(require 'clojure-mode)
21+
(require 'ert)
22+
23+
(def-refactor-test test-rename-ns-alias
24+
"(ns cljr.core
25+
(:require [my.lib :as lib]))
26+
27+
(def m #::lib{:kw 1, :n/kw 2, :_/bare 3, 0 4})
28+
29+
(+ (lib/a 1) (b 2))"
30+
"(ns cljr.core
31+
(:require [my.lib :as foo]))
32+
33+
(def m #::foo{:kw 1, :n/kw 2, :_/bare 3, 0 4})
34+
35+
(+ (foo/a 1) (b 2))"
36+
(clojure--rename-ns-alias-internal "lib" "foo"))
37+
38+
(def-refactor-test test-rename-ns-alias-with-missing-as
39+
"(ns cljr.core
40+
(:require [my.lib :as lib]))
41+
42+
(def m #::lib{:kw 1, :n/kw 2, :_/bare 3, 0 4})
43+
44+
(+ (lib/a 1) (b 2))"
45+
"(ns cljr.core
46+
(:require [my.lib :as lib]))
47+
48+
(def m #::lib{:kw 1, :n/kw 2, :_/bare 3, 0 4})
49+
50+
(+ (lib/a 1) (b 2))"
51+
(clojure--rename-ns-alias-internal "foo" "bar"))
52+
53+
(provide 'clojure-mode-refactor-rename-ns-alias-test)
54+
55+
;;; clojure-mode-refactor-rename-ns-alias-test.el ends here

0 commit comments

Comments
 (0)