Skip to content

Commit 8affea4

Browse files
committed
Rename ns aliases in region
1 parent a959a1b commit 8affea4

File tree

1 file changed

+71
-44
lines changed

1 file changed

+71
-44
lines changed

clojure-mode.el

+71-44
Original file line numberDiff line numberDiff line change
@@ -2670,38 +2670,6 @@ lists up."
26702670
(insert sexp)
26712671
(clojure--replace-sexps-with-bindings-and-indent)))
26722672

2673-
(defun clojure-collect-ns-aliases (ns-form)
2674-
"Collect all namespace aliases in NS-FORM."
2675-
(with-temp-buffer
2676-
(delay-mode-hooks
2677-
(clojure-mode)
2678-
(insert ns-form)
2679-
(goto-char (point-min))
2680-
(let ((end (point-max))
2681-
(rgx (rx ":as" (+ space)
2682-
(group-n 1 (+ (not (in " ,]\n"))))))
2683-
(res ()))
2684-
(while (re-search-forward rgx end 'noerror)
2685-
(unless (or (clojure--in-string-p) (clojure--in-comment-p))
2686-
(push (match-string-no-properties 1) res)))
2687-
res))))
2688-
2689-
(defun clojure--rename-ns-alias-internal (current-alias new-alias)
2690-
"Rename a namespace alias CURRENT-ALIAS to NEW-ALIAS."
2691-
(clojure--find-ns-in-direction 'backward)
2692-
(let ((rgx (concat ":as +" (regexp-quote current-alias) "\\_>"))
2693-
(bound (save-excursion (forward-list 1) (point))))
2694-
(when (search-forward-regexp rgx bound t)
2695-
(replace-match (concat ":as " new-alias))
2696-
(save-excursion
2697-
(while (re-search-forward (concat (regexp-quote current-alias) "/") nil t)
2698-
(when (not (nth 3 (syntax-ppss)))
2699-
(replace-match (concat new-alias "/")))))
2700-
(save-excursion
2701-
(while (re-search-forward (concat "#::" (regexp-quote current-alias) "{") nil t)
2702-
(replace-match (concat "#::" new-alias "{"))))
2703-
(message "Successfully renamed alias '%s' to '%s'" current-alias new-alias))))
2704-
27052673
;;;###autoload
27062674
(defun clojure-let-backward-slurp-sexp (&optional n)
27072675
"Slurp the s-expression before the let form into the let form.
@@ -2745,21 +2713,80 @@ With a numeric prefix argument the let is introduced N lists up."
27452713
(interactive)
27462714
(clojure--move-to-let-internal (read-from-minibuffer "Name of bound symbol: ")))
27472715

2716+
2717+
;;; Renaming ns aliases
2718+
2719+
(defun clojure--alias-usage-regexp (alias)
2720+
"Regexp for matching usages of ALIAS in qualified symbols, keywords and maps.
2721+
When nil, match all namespace usages.
2722+
The first match-group is the alias."
2723+
(let ((alias (if alias (regexp-quote alias) clojure--sym-regexp)))
2724+
(concat "#::\\(?1:" alias "\\)[ ,\r\n\t]*{"
2725+
"\\|"
2726+
"\\(?1:" alias "\\)/")))
2727+
2728+
(defun clojure--rename-ns-alias-usages (current-alias new-alias beg end)
2729+
"Rename all usages of CURRENT-ALIAS in region BEG to END with NEW-ALIAS."
2730+
(let ((rgx (clojure--alias-usage-regexp current-alias)))
2731+
(save-excursion
2732+
(goto-char end)
2733+
(setq end (point-marker))
2734+
(goto-char beg)
2735+
(while (re-search-forward rgx end 'noerror)
2736+
(when (not (clojure--in-string-p)) ;; replace in comments, but not strings
2737+
(goto-char (match-beginning 1))
2738+
(delete-region (point) (match-end 1))
2739+
(insert new-alias))))))
2740+
2741+
(defun clojure--collect-ns-aliases (beg end ns-form-p)
2742+
"Collect all aliases between BEG and END.
2743+
When NS-FORM-P is non-nil, treat the region as a ns form
2744+
and pick up aliases from [... :as alias] forms,
2745+
otherwise pick up alias usages from keywords / symbols."
2746+
(let ((res ()))
2747+
(save-excursion
2748+
(let ((rgx (if ns-form-p
2749+
(rx ":as" (+ space)
2750+
(group-n 1 (+ (not (in " ,]\n")))))
2751+
(clojure--alias-usage-regexp nil))))
2752+
(goto-char beg)
2753+
(while (re-search-forward rgx end 'noerror)
2754+
(unless (or (clojure--in-string-p) (clojure--in-comment-p))
2755+
(cl-pushnew (match-string-no-properties 1) res
2756+
:test #'equal)))
2757+
(reverse res)))))
2758+
2759+
(defun clojure--rename-ns-alias-internal (current-alias new-alias)
2760+
"Rename a namespace alias CURRENT-ALIAS to NEW-ALIAS.
2761+
Assume point is at the start of ns form."
2762+
(clojure--find-ns-in-direction 'backward)
2763+
(let ((rgx (concat ":as +" (regexp-quote current-alias) "\\_>"))
2764+
(bound (save-excursion (forward-list 1) (point-marker))))
2765+
(when (search-forward-regexp rgx bound t)
2766+
(replace-match (concat ":as " new-alias))
2767+
(clojure--rename-ns-alias-usages current-alias new-alias bound (point-max)))))
2768+
27482769
;;;###autoload
27492770
(defun clojure-rename-ns-alias ()
2750-
"Rename a namespace alias."
2771+
"Rename a namespace alias.
2772+
If a region is active, only pick up and rename aliases within the region."
27512773
(interactive)
2752-
(save-excursion
2753-
(clojure--find-ns-in-direction 'backward)
2754-
(let* ((current-alias (completing-read "Current alias: "
2755-
(clojure-collect-ns-aliases
2756-
(thing-at-point 'list))))
2757-
(rgx (concat ":as +" (regexp-quote current-alias) "\\_>"))
2758-
(bound (save-excursion (forward-list 1) (point))))
2759-
(if (save-excursion (search-forward-regexp rgx bound t))
2760-
(let ((new-alias (read-from-minibuffer "New alias: ")))
2761-
(clojure--rename-ns-alias-internal current-alias new-alias))
2762-
(message "Cannot find namespace alias: '%s'" current-alias)))))
2774+
(if (use-region-p)
2775+
(let* ((beg (region-beginning))
2776+
(end (region-end))
2777+
(current-alias (completing-read "Current alias: "
2778+
(clojure--collect-ns-aliases
2779+
beg end nil)))
2780+
(new-alias (read-from-minibuffer (format "Replace %s with: " current-alias))))
2781+
(clojure--rename-ns-alias-usages current-alias new-alias beg end))
2782+
(save-excursion
2783+
(clojure--find-ns-in-direction 'backward)
2784+
(let* ((bounds (bounds-of-thing-at-point 'list))
2785+
(current-alias (completing-read "Current alias: "
2786+
(clojure--collect-ns-aliases
2787+
(car bounds) (cdr bounds) t)))
2788+
(new-alias (read-from-minibuffer (format "Replace %s with: " current-alias))))
2789+
(clojure--rename-ns-alias-internal current-alias new-alias)))))
27632790

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

0 commit comments

Comments
 (0)