Skip to content

Commit da56a69

Browse files
rrudakovbbatsov
authored andcommitted
Extend completion to complete ns aliases and required symbols
Completions for: - namespace aliases required with `:as` - imported functions required with `:refer` - imported Java classes
1 parent 7c33f29 commit da56a69

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
- [#113](https://github.com/clojure-emacs/clojure-ts-mode/pull/113): Fix non-working refactoring commands for Emacs-30.
77
- [#114](https://github.com/clojure-emacs/clojure-ts-mode/pull/114): Extend built-in completion to complete keywords and local bindings in
88
`for` and `doseq` forms.
9+
- [#116](https://github.com/clojure-emacs/clojure-ts-mode/pull/116): Extend built-in completion to complete all imported symbols from an `ns`
10+
form.
911

1012
## 0.5.1 (2025-06-17)
1113

clojure-ts-mode.el

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2592,6 +2592,30 @@ before DELIM-OPEN."
25922592
:anchor ((sym_lit) @defun-candidate)))))
25932593
"Query that matches top-level definitions.")
25942594

2595+
(defconst clojure-ts--completion-query-ns
2596+
(treesit-query-compile
2597+
'clojure
2598+
'(((source (list_lit
2599+
:anchor [(comment) (meta_lit) (old_meta_lit)] :*
2600+
:anchor (sym_lit name: (sym_name) @sym)
2601+
;; require
2602+
(list_lit
2603+
:anchor ((kwd_lit) @kwd (:equal ":require" @kwd))
2604+
(vec_lit
2605+
:anchor (sym_lit)
2606+
[(sym_lit) @ns-alias-candidate
2607+
(vec_lit (sym_lit) @defun-candidate)]))))
2608+
(:equal "ns" @sym))
2609+
((source (list_lit
2610+
:anchor [(comment) (meta_lit) (old_meta_lit)] :*
2611+
:anchor (sym_lit name: (sym_name) @sym)
2612+
;; import
2613+
(((list_lit
2614+
:anchor ((kwd_lit) @kwd (:equal ":import" @kwd))
2615+
(list_lit :anchor (sym_lit) (sym_lit) @import-candidate))))))
2616+
(:equal "ns" @sym))))
2617+
"Query that matches all imported symbols in a Clojure ns form.")
2618+
25952619
(defconst clojure-ts--completion-query-keywords
25962620
(treesit-query-compile 'clojure '((kwd_lit) @keyword-candidate))
25972621
"Query that matches any Clojure keyword.")
@@ -2634,7 +2658,9 @@ bindings vector as well as destructuring syntax.")
26342658
(defconst clojure-ts--completion-annotations
26352659
(list 'defun-candidate " Definition"
26362660
'local-candidate " Local variable"
2637-
'keyword-candidate " Keyword")
2661+
'keyword-candidate " Keyword"
2662+
'ns-alias-candidate " Namespace alias"
2663+
'import-candidate " Class")
26382664
"Property list of completion candidate type and annotation string.")
26392665

26402666
(defun clojure-ts--completion-annotation-function (candidate)
@@ -2691,6 +2717,7 @@ all let bindings found along the way."
26912717
(source (treesit-buffer-root-node 'clojure))
26922718
(nodes (append (treesit-query-capture source clojure-ts--completion-query-defuns)
26932719
(treesit-query-capture source clojure-ts--completion-query-keywords)
2720+
(treesit-query-capture source clojure-ts--completion-query-ns)
26942721
(clojure-ts--completion-fn-args-nodes)
26952722
(clojure-ts--completion-let-locals-nodes))))
26962723
(list (car bounds)

test/clojure-ts-mode-completion.el

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,33 @@ u|"
196196
(expect (nth 2 (clojure-ts-completion-at-point-function))
197197
:to-equal '((":let" . keyword-candidate)
198198
("digit" . local-candidate)
199-
("prefixed-digit" . local-candidate))))))
199+
("prefixed-digit" . local-candidate)))))
200+
201+
(it "should complete all imported symbols from a ns form"
202+
(with-clojure-ts-buffer-point "
203+
(ns completion
204+
(:require
205+
[clojure.string :as str]
206+
[clojure.test :as test :refer [deftest testing is]])
207+
(:import
208+
(java.time Instant LocalDate)))
209+
210+
s|"
211+
(expect (nth 2 (clojure-ts-completion-at-point-function))
212+
:to-equal '(("completion" . defun-candidate)
213+
(":require" . keyword-candidate)
214+
(":as" . keyword-candidate)
215+
(":refer" . keyword-candidate)
216+
(":import" . keyword-candidate)
217+
(":require" . kwd)
218+
("str" . ns-alias-candidate)
219+
("test" . ns-alias-candidate)
220+
("deftest" . defun-candidate)
221+
("testing" . defun-candidate)
222+
("is" . defun-candidate)
223+
(":import" . kwd)
224+
("Instant" . import-candidate)
225+
("LocalDate" . import-candidate))))))
200226

201227
(provide 'clojure-ts-mode-completion)
202228
;;; clojure-ts-mode-completion.el ends here

test/samples/completion.clj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
(ns completion)
1+
(ns completion
2+
(:require
3+
[clojure.string :as str]
4+
[clojure.test :as test :refer [deftest testing is]])
5+
(:import
6+
(java.time Instant LocalDate)))
27

38
(def my-var "Hello")
49
(def my-another-var "World")

0 commit comments

Comments
 (0)