Skip to content

Commit 279e0c9

Browse files
committed
fix #518 - ns form inside strings and comments
1 parent 396a15b commit 279e0c9

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [#508](https://github.com/clojure-emacs/clojure-mode/issues/508): Fix font lock for namespaces with metadata
1010
* [#506](https://github.com/clojure-emacs/clojure-mode/issues/506): `clojure-mode-display-version` correctly displays the package's version
1111
* [#445](https://github.com/clojure-emacs/clojure-mode/issues/445), [#405](https://github.com/clojure-emacs/clojure-mode/issues/405), [#469](https://github.com/clojure-emacs/clojure-mode/issues/469): Correct font-lock on string definitions with docstrings, e.g: `(def foo "doc" "value")`. Correct indentation as well.
12+
* [#518](https://github.com/clojure-emacs/clojure-mode/issues/518): Fix clojure-find-ns when there's an `ns` form inside a string
1213

1314
## 5.10.0 (2019-01-05)
1415

clojure-mode.el

+20-6
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,19 @@ the cached value will be updated automatically."
18861886
(defvar-local clojure-cached-ns nil
18871887
"A buffer ns cache used to speed up ns-related operations.")
18881888

1889+
(defun clojure--find-ns-in-direction (direction)
1890+
"Return the nearest namespace in a specific DIRECTION.
1891+
DIRECTION is `forward' or `backward'."
1892+
(let ((candidate)
1893+
(fn (if (eq direction 'forward)
1894+
#'search-forward-regexp
1895+
#'search-backward-regexp)))
1896+
(while (and (not candidate)
1897+
(funcall fn clojure-namespace-name-regex nil t))
1898+
(unless (or (clojure--in-string-p) (clojure--in-comment-p))
1899+
(setq candidate (match-string-no-properties 4))))
1900+
candidate))
1901+
18891902
(defun clojure-find-ns ()
18901903
"Return the namespace of the current Clojure buffer.
18911904
Return the namespace closest to point and above it. If there are
@@ -1901,12 +1914,9 @@ The results will be cached if `clojure-cache-ns' is set to t."
19011914
;; Move to top-level to avoid searching from inside ns
19021915
(ignore-errors (while t (up-list nil t t)))
19031916

1904-
;; The closest ns form above point.
1905-
(when (or (re-search-backward clojure-namespace-name-regex nil t)
1906-
;; Or any form at all.
1907-
(and (goto-char (point-min))
1908-
(re-search-forward clojure-namespace-name-regex nil t)))
1909-
(match-string-no-properties 4))))))
1917+
;; Move to closest ns form above point.
1918+
(or (clojure--find-ns-in-direction 'backward)
1919+
(clojure--find-ns-in-direction 'forward))))))
19101920
(setq clojure-cached-ns ns)
19111921
ns)))
19121922

@@ -2384,6 +2394,10 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-cycle-privacy"
23842394
"Check whether the point is currently in a string."
23852395
(nth 3 (syntax-ppss)))
23862396

2397+
(defun clojure--in-comment-p ()
2398+
"Check whether the point is currently in a comment."
2399+
(nth 4 (syntax-ppss)))
2400+
23872401
(defun clojure--goto-if ()
23882402
"Find the first surrounding if or if-not expression."
23892403
(when (clojure--in-string-p)

test/clojure-mode-sexp-test.el

+16-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,22 @@ and point left there."
162162

163163
;; From inside second ns's name
164164
(goto-char 42)
165-
(should (equal "baz-quux" (clojure-find-ns))))))
165+
(should (equal "baz-quux" (clojure-find-ns))))
166+
(let ((data
167+
'(("\"\n(ns foo-bar)\"\n" "(in-ns 'baz-quux)" "baz-quux")
168+
(";(ns foo-bar)\n" "(in-ns 'baz-quux)" "baz-quux")
169+
("(ns foo-bar)\n" "\"\n(in-ns 'baz-quux)\"" "foo-bar")
170+
("(ns foo-bar)\n" ";(in-ns 'baz-quux)" "foo-bar"))))
171+
(pcase-dolist (`(,form1 ,form2 ,expected) data)
172+
(with-temp-buffer
173+
(insert form1)
174+
(save-excursion (insert form2))
175+
(clojure-mode)
176+
;; Between the two namespaces
177+
(should (equal expected (clojure-find-ns)))
178+
;; After both namespaces
179+
(goto-char (point-max))
180+
(should (equal expected (clojure-find-ns))))))))
166181

167182
(provide 'clojure-mode-sexp-test)
168183

0 commit comments

Comments
 (0)