@@ -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.
18911904Return the namespace closest to point and above it. If there are
@@ -1901,12 +1914,8 @@ 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+ (or (clojure--find-ns-in-direction 'backward )
1918+ (clojure--find-ns-in-direction 'forward ))))))
19101919 (setq clojure-cached-ns ns)
19111920 ns)))
19121921
@@ -2384,6 +2393,10 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-cycle-privacy"
23842393 " Check whether the point is currently in a string."
23852394 (nth 3 (syntax-ppss )))
23862395
2396+ (defun clojure--in-comment-p ()
2397+ " Check whether the point is currently in a comment."
2398+ (nth 4 (syntax-ppss )))
2399+
23872400(defun clojure--goto-if ()
23882401 " Find the first surrounding if or if-not expression."
23892402 (when (clojure--in-string-p)
0 commit comments