diff --git a/CHANGELOG.md b/CHANGELOG.md index 2504e4c..c3ee46f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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]. diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index 48a3945..0297501 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -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) diff --git a/test/clojure-ts-mode-util-test.el b/test/clojure-ts-mode-util-test.el index 9d02bcc..b4c2e13 100644 --- a/test/clojure-ts-mode-util-test.el +++ b/test/clojure-ts-mode-util-test.el @@ -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" + (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")))) diff --git a/test/utils/test-helper.el b/test/utils/test-helper.el new file mode 100644 index 0000000..b7ac0d4 --- /dev/null +++ b/test/utils/test-helper.el @@ -0,0 +1,50 @@ +;;; test-helper.el --- Clojure TS Mode: Non-interactive unit-test setup -*- lexical-binding: t; -*- + +;; Copyright © 2022-2024 Bozhidar Batsov <bozhidar@batsov.dev> + +;; 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