Skip to content

Commit 2b22f03

Browse files
committed
Rename ns aliases in region
1 parent a959a1b commit 2b22f03

File tree

1 file changed

+73
-44
lines changed

1 file changed

+73
-44
lines changed

clojure-mode.el

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

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

0 commit comments

Comments
 (0)