Skip to content

Fix clojure-find-ns #661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bugs fixed

* [#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.

## 5.16.2 (2023-08-23)

### Changes
Expand Down
6 changes: 4 additions & 2 deletions clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2125,7 +2125,7 @@ content) are considered part of the preceding sexp."
(make-obsolete-variable 'clojure-namespace-name-regex 'clojure-namespace-regexp "5.12.0")

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

(defcustom clojure-cache-ns nil
"Whether to cache the results of `clojure-find-ns'.
Expand Down Expand Up @@ -2153,7 +2153,7 @@ DIRECTION is `forward' or `backward'."
(save-match-data
(goto-char end)
(clojure-forward-logical-sexp)
(unless (or (clojure--in-string-p) (clojure--in-comment-p))
(unless (or (clojure--in-string-p) (clojure--in-comment-p) (clojure-top-level-form-p "comment"))
(setq candidate (string-remove-prefix "'" (thing-at-point 'symbol))))))))
candidate))

Expand Down Expand Up @@ -2275,6 +2275,8 @@ This will skip over sexps that don't represent objects, so that ^hints and
(condition-case nil
(save-excursion
(beginning-of-defun)
(clojure-forward-logical-sexp 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should add some comments here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change is to handle whitespace before comment form. Not sure if this is the best way to do it though.
Added a couple of comments now to make the intent more clear.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My point was mainly that it looks a bit weird that we're going forward and backwards back-to-back. If that's used often in the code we can make a function I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. But I am not sure what is the correct abstraction anymore as it's more of a hack.

This is the third occurrence in the codebase but there are other places where similar logic was used with different functions and some places where edge cases are not handled. Even my current change will still break if there are multiple expressions in the same line.

Looks like all these extra steps are only needed in many places because of beginning-of-defun behaving 'incorrectly'.
For ex:

1 (ns abc) (defn abc []
             (comment
               (println "try (clojure-beginning-of-defun-function) from here"))
             "abc")

Now running (clojure-beginning-of-defun-function) or (beginning-of-defun) from any of the inner expressions takes the cursur to the beginning of the line (before 1).
The expected behaviour IMO from the name would be having the cursor before (defn.
The docstring mentions this but there is no way to easy way to control the end location of the cursor.

clojure-mode uses beginning-of-defun a lot so there are a lot of unhandled edge cases.
For example take these two code blocks:

(defn s1 [x] (str x)) (+ 1 2)

(defn s2
    [x] (str x))
(defn s1 [x] (str x)) 
(+ 1 2)
(defn s2
    [x] (str x))

Run M-x cider-eval-defun-at-point(C-c C-c) inside (+ 1 2) and the result is different in the two scenarios.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tldr: IIUC bad choice of abstraction from beginning-of-defun fn has propagated many edge cases possibly in all lisp modes.
Maybe I am missing something basic or someone already solved this issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bbatsov I have raised a separate PR for replacing beginning-of-defun fn completely here.
#663
After merging that PR these two lines here can be removed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for diving deep into this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged latest master and removed the extra lines here.
Will wait for @vemv to resolve the other conversation.

(clojure-backward-logical-sexp 1)
(forward-char 1)
(clojure-forward-logical-sexp 1)
(clojure-backward-logical-sexp 1)
Expand Down
15 changes: 15 additions & 0 deletions test/clojure-mode-util-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@
(expect (clojure-find-ns) :to-equal "foo"))
(with-clojure-buffer "(ns ^:bar ^:baz foo)"
(expect (clojure-find-ns) :to-equal "foo")))
(it "should find namespaces with spaces before ns form"
(with-clojure-buffer " (ns foo)"
(expect (clojure-find-ns) :to-equal "foo")))
(it "should skip namespaces within any comment forms"
(with-clojure-buffer " (ns foo)
(comment
(ns bar))"
(expect (clojure-find-ns) :to-equal "foo"))
(with-clojure-buffer " (comment
(ns foo))
(ns bar)
(comment
(ns baz))"
(expect (clojure-find-ns) :to-equal "bar"))
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(expect (clojure-find-ns) :to-equal "bar"))
)
(expect (clojure-find-ns) :to-equal "bar")))

(it "should find namespace declarations with nested metadata and docstrings"
(with-clojure-buffer "(ns ^{:bar true} foo)"
(expect (clojure-find-ns) :to-equal "foo"))
Expand Down