Skip to content

Update clojure-ts-find-ns to support in-ns and Add tests #38

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
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## main (unreleased)

- Add support for `in-ns` forms in `clojure-ts-find-ns`

## 0.2.2 (2024-02-16)

- [#37]: Fix derived modes broken with [#36].
Expand Down
9 changes: 7 additions & 2 deletions clojure-ts-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -1021,12 +1021,17 @@ See `clojure-ts--font-lock-settings' for usage of MARKDOWN-AVAILABLE."
'(((source (list_lit
:anchor (sym_lit name: (sym_name) @ns)
:anchor (sym_lit name: (sym_name) @ns-name)))
(:equal @ns "ns")))))
(:equal @ns "ns"))
((source (list_lit
:anchor (sym_lit name: (sym_name) @in-ns)
:anchor (quoting_lit
:anchor (sym_lit name: (sym_name) @ns-name))))
(:equal @in-ns "in-ns")))))

(defun clojure-ts-find-ns ()
"Return the name of the current namespace."
(let ((nodes (treesit-query-capture 'clojure clojure-ts--find-ns-query)))
(treesit-node-text (cdr (assoc 'ns-name nodes)))))
(treesit-node-text (cdr (assoc 'ns-name nodes)) t)))

(provide 'clojure-ts-mode)

Expand Down
103 changes: 103 additions & 0 deletions test/clojure-ts-mode-util-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,110 @@

(require 'clojure-ts-mode)
(require 'buttercup)
(require 'test-helper "test/utils/test-helper")

(describe "clojure-ts-mode-version"
(it "should not be nil"
(expect clojure-ts-mode-version)))

(describe "clojure-ts-find-ns"
(it "should find common namespace declarations"
(with-clojure-ts-buffer "(ns foo)"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer "(ns
foo)"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer "(ns foo.baz)"
(expect (clojure-ts-find-ns) :to-equal "foo.baz"))
(with-clojure-ts-buffer "(ns ^:bar foo)"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer "(ns ^:bar ^:baz foo)"
(expect (clojure-ts-find-ns) :to-equal "foo")))

(it "should find namespaces with spaces before ns form"
(with-clojure-ts-buffer " (ns foo)"
(expect (clojure-ts-find-ns) :to-equal "foo")))

(it "should skip namespaces within any comment forms"
(with-clojure-ts-buffer "(comment
(ns foo))"
(expect (clojure-ts-find-ns) :to-equal nil))
(with-clojure-ts-buffer " (ns foo)
(comment
(ns bar))"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer " (comment
(ns foo))
(ns bar)
(comment
(ns baz))"
(expect (clojure-ts-find-ns) :to-equal "bar")))

(it "should find namespace declarations with nested metadata and docstrings"
(with-clojure-ts-buffer "(ns ^{:bar true} foo)"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer "(ns #^{:bar true} foo)"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer "(ns #^{:fail {}} foo)"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer "(ns ^{:fail2 {}} foo.baz)"
(expect (clojure-ts-find-ns) :to-equal "foo.baz"))
(with-clojure-ts-buffer "(ns ^{} foo)"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer "(ns ^{:skip-wiki true}
aleph.netty)"
(expect (clojure-ts-find-ns) :to-equal "aleph.netty"))
(with-clojure-ts-buffer "(ns ^{:foo {:bar :baz} :fake (ns in.meta)} foo
\"docstring
(ns misleading)\")"
(expect (clojure-ts-find-ns) :to-equal "foo")))

(it "should support non-alphanumeric characters"
(with-clojure-ts-buffer "(ns foo+)"
(expect (clojure-ts-find-ns) :to-equal "foo+"))
(with-clojure-ts-buffer "(ns bar**baz$-_quux)"
(expect (clojure-ts-find-ns) :to-equal "bar**baz$-_quux"))
(with-clojure-ts-buffer "(ns aoc-2019.puzzles.day14)"
(expect (clojure-ts-find-ns) :to-equal "aoc-2019.puzzles.day14")))

(it "should support in-ns forms"
(with-clojure-ts-buffer "(in-ns 'bar.baz)"
(expect (clojure-ts-find-ns) :to-equal "bar.baz")))

(it "should take the first ns instead of closest unlike clojure-mode"
Copy link
Contributor Author

@p4v4n p4v4n Feb 19, 2024

Choose a reason for hiding this comment

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

This is the only scenario where it differs from clojure-find-ns after this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Is there any particular reason for this change?

Copy link
Contributor Author

@p4v4n p4v4n Feb 19, 2024

Choose a reason for hiding this comment

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

AFAIU It's debatable what is the correct behaviour here.
clojure-mode finds the closest ns above point and falls back to finding the closest ns below point.

  1. My understanding is that clojure-mode finding the closest ns is more of a result of an implementation detail rather than desired behaviour.

  2. Also I haven't figured out yet how to find all occurrences or do a reverse search with tree-sitter queries.

Copy link
Member

Choose a reason for hiding this comment

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

I recall in the beginning clojure-mode was just looking for a ns form in the beginning of the file, but then we changed it, as per the Clojure specification the current ns was supposed to be whatever ns is closest above the form in question. I don't recall doing a search below as well, but perhaps there was some reason for this.

Copy link
Member

Choose a reason for hiding this comment

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

Anyways, if by first you mean "closest above" (and so it seems looking at the tests), I think that's fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right now it's first from the beginning of file. (point is not used at all).
This is the behaviour before the PR as well. I haven't updated it as it would require more complicated tree-sitter queries.

The below test is matching the 3rd ns only because the first 2 are invalid not because of the point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If clojure-mode behaviour is ideal I would prefer to create a new issue and handle it in a separate PR.(Need more tree-sitter knowledge to handle the queries there.)

Copy link
Member

Choose a reason for hiding this comment

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

That'd be fine by me.

Copy link
Member

Choose a reason for hiding this comment

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

In general that has never been super important for me (to do this by the letter), as usually source files have only the leading ns form. But clearly it was a problem for some users of clojure-mode. :D

Copy link

Choose a reason for hiding this comment

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

@p4v4n If you'd like to chat about tree-sitter-clojure, there is a gitter / matrix channel.

See the bottom of this for links if interested.

(with-clojure-ts-buffer " (ns foo1)

(ns foo2)"
(expect (clojure-ts-find-ns) :to-equal "foo1"))
(with-clojure-ts-buffer-point " (in-ns foo1)
(ns 'foo2)
(in-ns 'foo3)
|
(ns foo4)"
(expect (clojure-ts-find-ns) :to-equal "foo3"))
(with-clojure-ts-buffer "(ns foo)
(ns-unmap *ns* 'map)
(ns.misleading 1 2 3)"
(expect (clojure-ts-find-ns) :to-equal "foo")))

(it "should skip leading garbage"
(with-clojure-ts-buffer " (ns foo)"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer "1(ns foo)"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer "1 (ns foo)"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer "1
(ns foo)"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer "[1]
(ns foo)"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer "[1] (ns foo)"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer "[1](ns foo)"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer "(ns)(ns foo)"
(expect (clojure-ts-find-ns) :to-equal "foo"))
(with-clojure-ts-buffer "(ns 'foo)(ns bar)"
(expect (clojure-ts-find-ns) :to-equal "bar"))))
50 changes: 50 additions & 0 deletions test/utils/test-helper.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
;;; test-helper.el --- Clojure TS Mode: Non-interactive unit-test setup -*- lexical-binding: t; -*-

;; Copyright © 2022-2024 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:

;; Non-interactive test suite setup.

;;; Code:

(defmacro with-clojure-ts-buffer (text &rest body)
"Create a temporary buffer, insert TEXT,switch to clojure-ts-mode.
And evaluate BODY."
(declare (indent 1))
`(with-temp-buffer
(erase-buffer)
(insert ,text)
(clojure-ts-mode)
,@body))

(defmacro with-clojure-ts-buffer-point (text &rest body)
"Run BODY in a temporary clojure buffer with TEXT.

TEXT is a string with a | indicating where point is. The | will be erased
and point left there."
(declare (indent 2))
`(progn
(with-clojure-ts-buffer ,text
(goto-char (point-min))
(re-search-forward "|")
(delete-char -1)
,@body)))

(provide 'test-helper)
;;; test-helper.el ends here