Skip to content

Commit

Permalink
Complete local bindings for ClojureScript files
Browse files Browse the repository at this point in the history
When editing a ClojureScript file, cider-nrepl doesn't offer any local
binding names (the function name and argument names if inside the
function, the let-like block bindings if inside a let-like block,
etc.) as completion candidates, like it does when editing a Clojure
file.

The root of the problem is that
`cider.nrepl.middleware.complete/cljs-sources` only includes
`::suitable-sources/cljs-source` as a possible source. And `suitable`
doesn't perform any local binding analysis.

Given that the local binding analysis done in
`compliment.sources.local-bindings` namespace doesn't perform any
evaluation or execution of the context form (thus, it is independent
of the actual host platform differences), we can use that same source
for ClojureScript local bindings completion.

[Closes: clojure-emacs#766]
  • Loading branch information
iarenaza committed Jan 31, 2023
1 parent 804eec7 commit a48c634
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master (unreleased)

* [#766](https://github.com/clojure-emacs/cider-nrepl/issues/766) Complete local bindings for ClojureScript files.

## 0.29.0 (2022-12-05)

* [#758](https://github.com/clojure-emacs/cider-nrepl/pull/758) Add nREPL op to parse stacktraces into data (`analyze-stacktrace`), rename `stacktrace` op to `analyze-last-stacktrace` and deprecate `stacktrace`.
Expand Down
3 changes: 2 additions & 1 deletion src/cider/nrepl/middleware/complete.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@

(def cljs-sources
"A list of ClojureScript completion sources for compliment."
[::suitable-sources/cljs-source])
[::suitable-sources/cljs-source
:compliment.sources.local-bindings/local-bindings])

(defn complete
[{:keys [ns prefix symbol context extra-metadata enhanced-cljs-completion?] :as msg}]
Expand Down
13 changes: 12 additions & 1 deletion test/cljs/cider/nrepl/middleware/cljs_complete_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,18 @@
:ns "cljs.user"
:prefix "js/Ob"})
candidates (:completions response)]
(is (empty? candidates)))))
(is (empty? candidates))))
(testing "local bindings"
(let [response (session/message {:op "complete"
:ns "cljs.user"
:prefix "ba"
;; Including `quux :quux` helps ensuring that only
;; bindings with the specified prefix (ba*) will be
;; suggested, instead of all the local bindings.
:context "(defn foo [bar] (let [baz :baz, quux :quux] (str __prefix__)))"
:enhanced-cljs-completion? "t"})
candidates (:completions response)]
(is (= [{:candidate "bar", :type "local"}, {:candidate "baz", :type "local"}] candidates)))))

(deftest cljs-complete-doc-test
(testing "no suitable documentation can be found"
Expand Down

0 comments on commit a48c634

Please sign in to comment.