Skip to content

Commit 5189ce9

Browse files
committed
Fix clojure-find-ns when preceded by other forms
1 parent 1dc343f commit 5189ce9

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs fixed
1010

11+
* [#656](https://github.com/clojure-emacs/clojure-mode/issues/656): Fix clojure-find-ns when ns form is preceded by other forms.
12+
1113
* [#593](https://github.com/clojure-emacs/clojure-mode/issues/593): Fix clojure-find-ns when ns form is preceded by whitespace or inside comment form.
1214

1315
## 5.16.2 (2023-08-23)

Diff for: clojure-mode.el

+24-14
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757

5858
;;; Code:
5959

60-
6160
(defvar calculate-lisp-indent-last-sexp)
6261
(defvar delete-pair-blink-delay)
6362
(defvar font-lock-beg)
@@ -717,7 +716,7 @@ If JUSTIFY is non-nil, justify as well as fill the paragraph."
717716
(fill-prefix (clojure-adaptive-fill-function)))
718717
(do-auto-fill)))))
719718

720-
719+
721720
;;; #_ comments font-locking
722721
;; Code heavily borrowed from Slime.
723722
;; https://github.com/slime/slime/blob/master/contrib/slime-fontifying-fu.el#L186
@@ -780,7 +779,7 @@ and `(match-end 1)'."
780779
(scan-error (setq result 'retry))))
781780
result))
782781

783-
782+
784783
;;; General font-locking
785784
(defun clojure-match-next-def ()
786785
"Scans the buffer backwards for the next \"top-level\" definition.
@@ -1889,7 +1888,7 @@ work). To set it from Lisp code, use
18891888
(go-loop 1)
18901889
(thread 0))
18911890

1892-
1891+
18931892

18941893
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18951894
;;
@@ -1944,7 +1943,7 @@ nil."
19441943
(delete-region begin (point))
19451944
result)))
19461945

1947-
1946+
19481947

19491948
(defcustom clojure-cache-project-dir t
19501949
"Whether to cache the results of `clojure-project-dir'."
@@ -1988,7 +1987,7 @@ Return nil if not inside a project."
19881987
"Denormalize PATH by making it relative to the project root."
19891988
(file-relative-name path (clojure-project-dir)))
19901989

1991-
1990+
19921991
;;; ns manipulation
19931992
(defun clojure-expected-ns (&optional path)
19941993
"Return the namespace matching PATH.
@@ -2125,7 +2124,7 @@ content) are considered part of the preceding sexp."
21252124
(make-obsolete-variable 'clojure-namespace-name-regex 'clojure-namespace-regexp "5.12.0")
21262125

21272126
(defconst clojure-namespace-regexp
2128-
(rx line-start (zero-or-more whitespace) "(" (? "clojure.core/") (or "in-ns" "ns" "ns+") symbol-end))
2127+
(rx "(" (? "clojure.core/") (or "in-ns" "ns" "ns+") symbol-end))
21292128

21302129
(defcustom clojure-cache-ns nil
21312130
"Whether to cache the results of `clojure-find-ns'.
@@ -2148,12 +2147,13 @@ DIRECTION is `forward' or `backward'."
21482147
#'search-backward-regexp)))
21492148
(while (and (not candidate)
21502149
(funcall fn clojure-namespace-regexp nil t))
2151-
(let ((end (match-end 0)))
2150+
(let ((start (match-beginning 0))
2151+
(end (match-end 0)))
21522152
(save-excursion
2153-
(save-match-data
2154-
(goto-char end)
2155-
(clojure-forward-logical-sexp)
2156-
(unless (or (clojure--in-string-p) (clojure--in-comment-p) (clojure-top-level-form-p "comment"))
2153+
(when (clojure--is-top-level-form-p start)
2154+
(save-match-data
2155+
(goto-char end)
2156+
(clojure-forward-logical-sexp)
21572157
(setq candidate (string-remove-prefix "'" (thing-at-point 'symbol))))))))
21582158
candidate))
21592159

@@ -2222,7 +2222,7 @@ Returns a list pair, e.g. (\"defn\" \"abc\") or (\"deftest\" \"some-test\")."
22222222
(list (match-string-no-properties 1)
22232223
(match-string-no-properties 2)))))
22242224

2225-
2225+
22262226
;;; Sexp navigation
22272227

22282228
(defun clojure--looking-at-non-logical-sexp ()
@@ -2270,6 +2270,16 @@ This will skip over sexps that don't represent objects, so that ^hints and
22702270
(backward-sexp 1))
22712271
(setq n (1- n))))))
22722272

2273+
(defun clojure--is-top-level-form-p (&optional point)
2274+
"Return truthy if form at POINT is a top level form."
2275+
(save-excursion
2276+
(when point (goto-char point))
2277+
(and (looking-at-p "(")
2278+
(= (point)
2279+
(progn (forward-char)
2280+
(beginning-of-defun-raw)
2281+
(point))))))
2282+
22732283
(defun clojure-top-level-form-p (first-form)
22742284
"Return truthy if the first form matches FIRST-FORM."
22752285
(condition-case nil
@@ -3173,7 +3183,7 @@ Assumes cursor is at beginning of function."
31733183
(clojure--add-arity-reify-internal)))
31743184
(indent-region beg end-marker))))
31753185

3176-
3186+
31773187
;;; Toggle Ignore forms
31783188

31793189
(defun clojure--toggle-ignore-next-sexp (&optional n)

Diff for: test/clojure-mode-sexp-test.el

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@
164164
(expect (equal "baz-quux" (clojure-find-ns))))
165165
(let ((data
166166
'(("\"\n(ns foo-bar)\"\n" "(in-ns 'baz-quux)" "baz-quux")
167-
(";(ns foo-bar)\n" "(in-ns 'baz-quux)" "baz-quux")
167+
(";(ns foo-bar)\n" "(in-ns 'baz-quux2)" "baz-quux2")
168168
("(ns foo-bar)\n" "\"\n(in-ns 'baz-quux)\"" "foo-bar")
169-
("(ns foo-bar)\n" ";(in-ns 'baz-quux)" "foo-bar"))))
169+
("(ns foo-bar2)\n" ";(in-ns 'baz-quux)" "foo-bar2"))))
170170
(pcase-dolist (`(,form1 ,form2 ,expected) data)
171171
(with-clojure-buffer form1
172172
(save-excursion (insert form2))

Diff for: test/clojure-mode-util-test.el

+21
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,27 @@
142142
(with-clojure-buffer "(ns foo)
143143
(ns-unmap *ns* 'map)
144144
(ns.misleading 1 2 3)"
145+
(expect (clojure-find-ns) :to-equal "foo")))
146+
(it "should skip leading garbage"
147+
(with-clojure-buffer " (ns foo)"
148+
(expect (clojure-find-ns) :to-equal "foo"))
149+
(with-clojure-buffer "1(ns foo)"
150+
(expect (clojure-find-ns) :to-equal "foo"))
151+
(with-clojure-buffer "1 (ns foo)"
152+
(expect (clojure-find-ns) :to-equal "foo"))
153+
(with-clojure-buffer "1
154+
(ns foo)"
155+
(expect (clojure-find-ns) :to-equal "foo"))
156+
(with-clojure-buffer "[1]
157+
(ns foo)"
158+
(expect (clojure-find-ns) :to-equal "foo"))
159+
(with-clojure-buffer "[1] (ns foo)"
160+
(expect (clojure-find-ns) :to-equal "foo"))
161+
(with-clojure-buffer "[1](ns foo)"
162+
(expect (clojure-find-ns) :to-equal "foo"))
163+
(with-clojure-buffer "(ns)(ns foo)"
164+
(expect (clojure-find-ns) :to-equal "foo"))
165+
(with-clojure-buffer "(ns )(ns foo)"
145166
(expect (clojure-find-ns) :to-equal "foo"))))
146167

147168
(describe "clojure-sort-ns"

0 commit comments

Comments
 (0)