Skip to content

Commit 48dee72

Browse files
committed
Refactor collect-ns-aliases
Make into public API, remove cons cell of positions from return value.
1 parent dd3151a commit 48dee72

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

clojure-mode.el

+26-20
Original file line numberDiff line numberDiff line change
@@ -2684,17 +2684,21 @@ lists up."
26842684
(insert sexp)
26852685
(clojure--replace-sexps-with-bindings-and-indent)))
26862686

2687-
(defun clojure--collect-ns-aliases ()
2688-
"Collect all aliases in current ns form."
2689-
(save-excursion
2690-
(clojure--find-ns-in-direction 'backward)
2691-
(let ((end (cdr (bounds-of-thing-at-point 'list)))
2692-
(rgx (rx ":as" (+ space)
2693-
(group-n 1 (+ (not (in " ,]\n"))))))
2694-
(res ()))
2695-
(while (re-search-forward rgx end 'noerror)
2696-
(push (match-string-no-properties 1) res))
2697-
res)))
2687+
(defun clojure-collect-ns-aliases (ns-form)
2688+
"Collect all namespace aliases in NS-FORM."
2689+
(with-temp-buffer
2690+
(delay-mode-hooks
2691+
(clojure-mode)
2692+
(insert ns-form)
2693+
(goto-char (point-min))
2694+
(let ((end (point-max))
2695+
(rgx (rx ":as" (+ space)
2696+
(group-n 1 (+ (not (in " ,]\n"))))))
2697+
(res ()))
2698+
(while (re-search-forward rgx end 'noerror)
2699+
(unless (or (clojure--in-string-p) (clojure--in-comment-p))
2700+
(push (match-string-no-properties 1) res)))
2701+
res))))
26982702

26992703
(defun clojure--rename-ns-alias-internal (current-alias new-alias)
27002704
"Rename a namespace alias CURRENT-ALIAS to NEW-ALIAS."
@@ -2759,15 +2763,17 @@ With a numeric prefix argument the let is introduced N lists up."
27592763
(defun clojure-rename-ns-alias ()
27602764
"Rename a namespace alias."
27612765
(interactive)
2762-
(let ((current-alias (completing-read "Current alias: " (clojure--collect-ns-aliases))))
2763-
(save-excursion
2764-
(clojure--find-ns-in-direction 'backward)
2765-
(let ((rgx (concat ":as +" current-alias))
2766-
(bound (save-excursion (forward-list 1) (point))))
2767-
(if (save-excursion (search-forward-regexp rgx bound t))
2768-
(let ((new-alias (read-from-minibuffer "New alias: ")))
2769-
(clojure--rename-ns-alias-internal current-alias new-alias))
2770-
(message "Cannot find namespace alias: '%s'" current-alias))))))
2766+
(save-excursion
2767+
(clojure--find-ns-in-direction 'backward)
2768+
(let* ((current-alias (completing-read "Current alias: "
2769+
(clojure-collect-ns-aliases
2770+
(save-excursion (thing-at-point 'list)))))
2771+
(rgx (concat ":as +" current-alias))
2772+
(bound (save-excursion (forward-list 1) (point))))
2773+
(if (save-excursion (search-forward-regexp rgx bound t))
2774+
(let ((new-alias (read-from-minibuffer "New alias: ")))
2775+
(clojure--rename-ns-alias-internal current-alias new-alias))
2776+
(message "Cannot find namespace alias: '%s'" current-alias)))))
27712777

27722778
(defun clojure--add-arity-defprotocol-internal ()
27732779
"Add an arity to a signature inside a defprotocol.

test/clojure-mode-refactor-rename-ns-alias-test.el

+7-7
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,18 @@
110110
(m*/operator 1 (math.-/subtract 2 3))"
111111
(clojure--rename-ns-alias-internal "math.*" "m*"))
112112

113-
(it "should offer completions"
114-
(with-clojure-buffer
115-
"(ns test.ns
113+
(it "should offer completions"
114+
(expect
115+
(clojure-collect-ns-aliases
116+
"(ns test.ns
116117
(:require [my.math.subtraction :as math.-]
117118
[my.math.multiplication :as math.*]
118-
[clojure.spec.alpha as s]
119+
[clojure.spec.alpha :as s]
119120
;; [clojure.spec.alpha2 :as s2]
120121
[symbols :as abc123.-$#.%*+!@]))
121122
122-
(math.*/operator 1 (math.-/subtract 2 3))"
123-
(expect (clojure--collect-ns-aliases)
124-
:to-equal '("abc123.-$#.%*+!@" "s" "math.*" "math.-")))))
123+
(math.*/operator 1 (math.-/subtract 2 3))")
124+
:to-equal '("abc123.-$#.%*+!@" "s" "math.*" "math.-"))))
125125

126126
(provide 'clojure-mode-refactor-rename-ns-alias-test)
127127

0 commit comments

Comments
 (0)