branch: elpa/clojure-mode
commit d69839084fc01e09870b53bc57e6bb07a87a64f9
Author: Bozhidar Batsov <[email protected]>
Commit: Bozhidar Batsov <[email protected]>
Fix clojure-update-ns and add tests
clojure-update-ns was broken because it used replace-match with
group 4 from the old clojure-namespace-name-regex, but clojure-find-ns
uses clojure-namespace-regexp which has no capture groups. Replace the
broken replace-match approach with direct sexp navigation to find and
replace the namespace name.
---
clojure-mode.el | 11 +++++-
test/clojure-mode-update-ns-test.el | 77 +++++++++++++++++++++++++++++++++++++
2 files changed, 87 insertions(+), 1 deletion(-)
diff --git a/clojure-mode.el b/clojure-mode.el
index 07e4ab57df..721ad6085c 100644
--- a/clojure-mode.el
+++ b/clojure-mode.el
@@ -2116,7 +2116,16 @@ Useful if a file has been renamed."
(save-match-data
(if (clojure-find-ns)
(progn
- (replace-match nsname nil nil nil 4)
+ ;; Move to end of the ns/in-ns keyword, then forward
+ ;; to the namespace name sexp and replace it.
+ (goto-char (match-end 0))
+ (clojure-forward-logical-sexp)
+ (let ((end (point)))
+ (backward-sexp)
+ ;; Skip past quote in (in-ns 'foo)
+ (skip-chars-forward "'")
+ (delete-region (point) end)
+ (insert nsname))
(message "ns form updated to `%s'" nsname)
(setq clojure-cached-ns nsname))
(user-error "Can't find ns form")))))))
diff --git a/test/clojure-mode-update-ns-test.el
b/test/clojure-mode-update-ns-test.el
new file mode 100644
index 0000000000..092379f048
--- /dev/null
+++ b/test/clojure-mode-update-ns-test.el
@@ -0,0 +1,77 @@
+;;; clojure-mode-update-ns-test.el --- Clojure Mode: update-ns tests -*-
lexical-binding: t; -*-
+
+;; Copyright (C) 2026 Bozhidar Batsov <[email protected]>
+
+;; This file is not part of GNU Emacs.
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Tests for clojure-update-ns.
+
+;;; Code:
+
+(require 'clojure-mode)
+(require 'buttercup)
+(require 'test-helper "test/utils/test-helper")
+
+(describe "clojure-update-ns"
+
+ (it "should update a simple ns form"
+ (with-clojure-buffer "(ns old.name)"
+ (let ((clojure-expected-ns-function (lambda () "new.name")))
+ (clojure-update-ns)
+ (expect (buffer-string) :to-equal "(ns new.name)"))))
+
+ (it "should update ns with metadata"
+ (with-clojure-buffer "(ns ^:bar old.name)"
+ (let ((clojure-expected-ns-function (lambda () "new.name")))
+ (clojure-update-ns)
+ (expect (buffer-string) :to-equal "(ns ^:bar new.name)"))))
+
+ (it "should update ns with map metadata"
+ (with-clojure-buffer "(ns ^{:doc \"hello\"} old.name)"
+ (let ((clojure-expected-ns-function (lambda () "new.name")))
+ (clojure-update-ns)
+ (expect (buffer-string) :to-equal "(ns ^{:doc \"hello\"} new.name)"))))
+
+ (it "should update ns with require forms"
+ (with-clojure-buffer "(ns old.name
+ (:require [clojure.string :as str]))"
+ (let ((clojure-expected-ns-function (lambda () "new.name")))
+ (clojure-update-ns)
+ (expect (buffer-string) :to-equal "(ns new.name
+ (:require [clojure.string :as str]))"))))
+
+ (it "should update in-ns forms"
+ (with-clojure-buffer "(in-ns 'old.name)"
+ (let ((clojure-expected-ns-function (lambda () "new.name")))
+ (clojure-update-ns)
+ (expect (buffer-string) :to-equal "(in-ns 'new.name)"))))
+
+ (it "should update the cached namespace"
+ (with-clojure-buffer "(ns old.name)"
+ (let ((clojure-expected-ns-function (lambda () "new.name")))
+ (clojure-update-ns)
+ (expect clojure-cached-ns :to-equal "new.name"))))
+
+ (it "should signal an error when no ns form is found"
+ (with-clojure-buffer "(defn foo [] 1)"
+ (let ((clojure-expected-ns-function (lambda () "new.name")))
+ (expect (clojure-update-ns) :to-throw 'user-error)))))
+
+(provide 'clojure-mode-update-ns-test)
+
+;;; clojure-mode-update-ns-test.el ends here