Skip to content

Extend completion to complete ns aliases and required symbols #116

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 1 commit into from
Jun 26, 2025
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 @@ -6,6 +6,8 @@
- [#113](https://github.com/clojure-emacs/clojure-ts-mode/pull/113): Fix non-working refactoring commands for Emacs-30.
- [#114](https://github.com/clojure-emacs/clojure-ts-mode/pull/114): Extend built-in completion to complete keywords and local bindings in
`for` and `doseq` forms.
- [#116](https://github.com/clojure-emacs/clojure-ts-mode/pull/116): Extend built-in completion to complete all imported symbols from an `ns`
form.

## 0.5.1 (2025-06-17)

Expand Down
29 changes: 28 additions & 1 deletion clojure-ts-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2592,6 +2592,30 @@ before DELIM-OPEN."
:anchor ((sym_lit) @defun-candidate)))))
"Query that matches top-level definitions.")

(defconst clojure-ts--completion-query-ns
(treesit-query-compile
'clojure
'(((source (list_lit
:anchor [(comment) (meta_lit) (old_meta_lit)] :*
:anchor (sym_lit name: (sym_name) @sym)
;; require
(list_lit
:anchor ((kwd_lit) @kwd (:equal ":require" @kwd))
(vec_lit
:anchor (sym_lit)
[(sym_lit) @ns-alias-candidate
(vec_lit (sym_lit) @defun-candidate)]))))
(:equal "ns" @sym))
((source (list_lit
:anchor [(comment) (meta_lit) (old_meta_lit)] :*
:anchor (sym_lit name: (sym_name) @sym)
;; import
(((list_lit
:anchor ((kwd_lit) @kwd (:equal ":import" @kwd))
(list_lit :anchor (sym_lit) (sym_lit) @import-candidate))))))
(:equal "ns" @sym))))
"Query that matches all imported symbols in a Clojure ns form.")

(defconst clojure-ts--completion-query-keywords
(treesit-query-compile 'clojure '((kwd_lit) @keyword-candidate))
"Query that matches any Clojure keyword.")
Expand Down Expand Up @@ -2634,7 +2658,9 @@ bindings vector as well as destructuring syntax.")
(defconst clojure-ts--completion-annotations
(list 'defun-candidate " Definition"
'local-candidate " Local variable"
'keyword-candidate " Keyword")
'keyword-candidate " Keyword"
'ns-alias-candidate " Namespace alias"
'import-candidate " Class")
"Property list of completion candidate type and annotation string.")

(defun clojure-ts--completion-annotation-function (candidate)
Expand Down Expand Up @@ -2691,6 +2717,7 @@ all let bindings found along the way."
(source (treesit-buffer-root-node 'clojure))
(nodes (append (treesit-query-capture source clojure-ts--completion-query-defuns)
(treesit-query-capture source clojure-ts--completion-query-keywords)
(treesit-query-capture source clojure-ts--completion-query-ns)
(clojure-ts--completion-fn-args-nodes)
(clojure-ts--completion-let-locals-nodes))))
(list (car bounds)
Expand Down
28 changes: 27 additions & 1 deletion test/clojure-ts-mode-completion.el
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,33 @@ u|"
(expect (nth 2 (clojure-ts-completion-at-point-function))
:to-equal '((":let" . keyword-candidate)
("digit" . local-candidate)
("prefixed-digit" . local-candidate))))))
("prefixed-digit" . local-candidate)))))

(it "should complete all imported symbols from a ns form"
(with-clojure-ts-buffer-point "
(ns completion
(:require
[clojure.string :as str]
[clojure.test :as test :refer [deftest testing is]])
(:import
(java.time Instant LocalDate)))

s|"
(expect (nth 2 (clojure-ts-completion-at-point-function))
:to-equal '(("completion" . defun-candidate)
(":require" . keyword-candidate)
(":as" . keyword-candidate)
(":refer" . keyword-candidate)
(":import" . keyword-candidate)
(":require" . kwd)
("str" . ns-alias-candidate)
("test" . ns-alias-candidate)
("deftest" . defun-candidate)
("testing" . defun-candidate)
("is" . defun-candidate)
(":import" . kwd)
("Instant" . import-candidate)
("LocalDate" . import-candidate))))))

(provide 'clojure-ts-mode-completion)
;;; clojure-ts-mode-completion.el ends here
7 changes: 6 additions & 1 deletion test/samples/completion.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
(ns completion)
(ns completion
(:require
[clojure.string :as str]
[clojure.test :as test :refer [deftest testing is]])
(:import
(java.time Instant LocalDate)))

(def my-var "Hello")
(def my-another-var "World")
Expand Down