Skip to content

Commit 5aa01ed

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

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
@@ -2683,17 +2683,21 @@ lists up."
26832683
(insert sexp)
26842684
(clojure--replace-sexps-with-bindings-and-indent)))
26852685

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

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

27712777
(defun clojure--add-arity-defprotocol-internal ()
27722778
"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)