From a48c6349eb5752bd811dc6972f7edff983511e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Arenaza?= Date: Mon, 30 Jan 2023 16:49:55 +0100 Subject: [PATCH] Complete local bindings for ClojureScript files 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: #766] --- CHANGELOG.md | 2 ++ src/cider/nrepl/middleware/complete.clj | 3 ++- .../cider/nrepl/middleware/cljs_complete_test.clj | 13 ++++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fab70917b..fb3863212 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/src/cider/nrepl/middleware/complete.clj b/src/cider/nrepl/middleware/complete.clj index 2ef657187..981013a2b 100644 --- a/src/cider/nrepl/middleware/complete.clj +++ b/src/cider/nrepl/middleware/complete.clj @@ -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}] diff --git a/test/cljs/cider/nrepl/middleware/cljs_complete_test.clj b/test/cljs/cider/nrepl/middleware/cljs_complete_test.clj index b36947624..19f7a6a0a 100644 --- a/test/cljs/cider/nrepl/middleware/cljs_complete_test.clj +++ b/test/cljs/cider/nrepl/middleware/cljs_complete_test.clj @@ -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"