Skip to content

Commit 528ec04

Browse files
committed
[Resolve #366] Add cljr-rename-ns-alias.
1 parent 9f508bb commit 528ec04

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
## Unreleased
44

5-
- [#402](https://github.com/clojure-emacs/clj-refactor.el/issues/402) cljr-stop-referring: do not alter strings.
6-
- [#380](https://github.com/clojure-emacs/clj-refactor.el/issues/380) clean-ns: fix FileNotFoundException, by trying both the absolute path and the path relative to the project root. This requires a new refactor-nrepl, old versions will only check the absolute path.
5+
- [#366](https://github.com/clojure-emacs/clj-refactor.el/issues/366): Add `cljr-rename-ns-alias`.
6+
- [#402](https://github.com/clojure-emacs/clj-refactor.el/issues/402): `cljr-stop-referring`: do not alter strings.
7+
- [#380](https://github.com/clojure-emacs/clj-refactor.el/issues/380): `cljr-clean-ns`: fix FileNotFoundException, by trying both the absolute path and the path relative to the project root. This requires a new refactor-nrepl, old versions will only check the absolute path.
78

89
## 2.4.0 (2018-08-26)
910

clj-refactor.el

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ Otherwise open the file and do the changes non-interactively."
377377
("ml" . (cljr-move-to-let "Move to let" ?m ("code")))
378378
("pc" . (cljr-project-clean "Project clean" ?c ("project")))
379379
("pf" . (cljr-promote-function "Promote function" ?p ("code" "toplevel-form")))
380+
("ra" . (cljr-rename-ns-alias "Rename namespace alias" ?a ("ns")))
380381
("rf" . (cljr-rename-file-or-dir "Rename file-or-dir" ?r ("project" "toplevel-form")))
381382
("rl" . (cljr-remove-let "Remove let" ?r ("code")))
382383
("rm" . (cljr-require-macro "Add to or extend the require-macros form" ?M ("ns")))
@@ -406,13 +407,14 @@ Otherwise open the file and do the changes non-interactively."
406407
------------------------------------------------------------------------------------------------------------------------------------------------------
407408
_ai_: Add import to ns _am_: Add missing libspec _ap_: Add project dependency
408409
_ar_: Add require to ns _au_: Add use to ns _cn_: Clean ns
409-
_rm_: Require a macro into the ns _sr_: Stop referring
410+
_ra_: Rename namespace alias _rm_: Require a macro into the ns _sr_: Stop referring
410411
_b_: Back to previous Hydra
411412
"
412413
("ai" cljr-add-import-to-ns) ("am" cljr-add-missing-libspec)
413414
("ap" cljr-add-project-dependency) ("ar" cljr-add-require-to-ns)
414415
("au" cljr-add-use-to-ns) ("cn" cljr-clean-ns)
415-
("rm" cljr-require-macro) ("sr" cljr-stop-referring)
416+
("ra" cljr-rename-ns-alias) ("rm" cljr-require-macro)
417+
("sr" cljr-stop-referring)
416418
("b" hydra-cljr-help-menu/body :exit t)
417419
("q" nil "quit"))
418420

@@ -880,6 +882,25 @@ issued, and should be left focused."
880882
(kill-buffer buf))
881883
(find-file (format "%s/%s" new-dir (seq-some (apply-partially same-file active) files)))))
882884

885+
;;;###autoload
886+
(defun cljr-rename-ns-alias ()
887+
"Rename a namespace alias.
888+
889+
See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-rename-ns-alias"
890+
(interactive)
891+
(let ((current-alias (read-from-minibuffer "Current alias: ")))
892+
(save-excursion
893+
(cljr--goto-ns)
894+
(let ((rgx (concat ":as +" current-alias))
895+
(bound (save-excursion (forward-list 1) (point))))
896+
(if (save-excursion (search-forward-regexp rgx bound t))
897+
(let ((new-alias (read-from-minibuffer "New alias: ")))
898+
(save-excursion (replace-regexp rgx (concat ":as " new-alias) nil (point-min) bound))
899+
(save-excursion (replace-regexp (concat current-alias "/") (concat new-alias "/")))
900+
(save-excursion (replace-regexp (concat "#::" current-alias "{") (concat "#::" new-alias "{")))
901+
(message "Successfully renamed alias '%s' to '%s'" current-alias new-alias))
902+
(user-error "Cannot find namespace alias: '%s'" current-alias))))))
903+
883904
;;;###autoload
884905
(defun cljr-rename-file-or-dir (old-path new-path)
885906
"Rename a file or directory of files.

examples/rename-ns-alias.gif

71.4 KB
Loading

features/rename-ns-alias.feature

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Feature: Rename ns alias
2+
3+
Background:
4+
Given I have a project "cljr" in "tmp"
5+
And I have a clojure-file "tmp/src/cljr/core.clj"
6+
And I open file "tmp/src/cljr/core.clj"
7+
And I clear the buffer
8+
9+
Scenario: With :as
10+
When I insert:
11+
"""
12+
(ns cljr.core
13+
(:require [my.lib :as lib]))
14+
15+
(def m #::lib{:kw 1, :n/kw 2, :_/bare 3, 0 4})
16+
17+
(+ (lib/a 1) (b 2))
18+
"""
19+
And I place the cursor before "bare"
20+
And I start an action chain
21+
And I press "C-! ra"
22+
And I type "lib"
23+
And I press "RET"
24+
And I type "foo"
25+
And I press "RET"
26+
And I execute the action chain
27+
Then I should see:
28+
"""
29+
(ns cljr.core
30+
(:require [my.lib :as foo]))
31+
32+
(def m #::foo{:kw 1, :n/kw 2, :_/bare 3, 0 4})
33+
34+
(+ (foo/a 1) (b 2))
35+
"""
36+
37+
Scenario: Without :as
38+
When I insert:
39+
"""
40+
(ns cljr.core
41+
(:require [my.lib :as lib]))
42+
43+
(def m #::lib{:kw 1, :n/kw 2, :_/bare 3, 0 4})
44+
45+
(+ (lib/a 1) (b 2))
46+
"""
47+
And I place the cursor before "bare"
48+
And I start an action chain
49+
And I press "C-! ra"
50+
And I type "foo"
51+
And I press "RET"
52+
Then I should see:
53+
"""
54+
(ns cljr.core
55+
(:require [my.lib :as lib]))
56+
57+
(def m #::lib{:kw 1, :n/kw 2, :_/bare 3, 0 4})
58+
59+
(+ (lib/a 1) (b 2))
60+
"""

0 commit comments

Comments
 (0)