Skip to content

Commit

Permalink
Integrate the new ClojureScript completions code
Browse files Browse the repository at this point in the history
This patch adds the necessary setup for ClojureScript completions with
clj-suitable. It also inlines the call to cljs-complete so that we avoid having
a wrapper.
  • Loading branch information
arichiardi committed Feb 7, 2020
1 parent 532983d commit 9769c93
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
* [#664](https://github.com/clojure-emacs/cider-nrepl/pull/664): Fix continue-all in conditional break.
* [#665](https://github.com/clojure-emacs/cider-nrepl/pull/665): Fix form location in map literals.

### Changes

* [#641](https://github.com/clojure-emacs/cider-nrepl/pull/641): Integrate the new suitable ClojureScript completion.

## 0.23.0 (2019-01-18)

### New Features
Expand Down
4 changes: 1 addition & 3 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:scm {:name "git" :url "https://github.com/clojure-emacs/cider-nrepl"}

:dependencies [[nrepl "0.6.0"]
^:inline-dep [cider/orchard "0.5.5"]
^:inline-dep [thunknyc/profile "0.5.2"]
^:inline-dep [mvxcvi/puget "1.2.0"]
^:inline-dep [fipp "0.6.22"] ; can be removed in unresolved-tree mode
^:inline-dep [compliment "0.3.10"]
^:inline-dep [cljs-tooling "0.3.1"]
^:inline-dep [org.rksm/suitable "0.2.14" :exclusions [org.clojure/clojurescript]]
^:inline-dep [org.rksm/suitable "0.3.5" :exclusions [org.clojure/clojurescript]]
^:inline-dep [cljfmt "0.6.6" :exclusions [org.clojure/clojurescript]]
^:inline-dep [org.clojure/tools.namespace "0.3.1"]
^:inline-dep [org.clojure/tools.trace "0.7.10"]
Expand Down
53 changes: 33 additions & 20 deletions src/cider/nrepl/middleware/complete.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,46 @@
(:require
[cider.nrepl.middleware.util.cljs :as cljs]
[cider.nrepl.middleware.util.error-handling :refer [with-safe-transport]]
[cljs-tooling.complete :as cljs-complete]
[compliment.core :as jvm-complete]
[compliment.utils :as jvm-complete-utils]
[compliment.core :as complete]
[compliment.utils :as complete-utils]
[orchard.misc :as misc]
[suitable.complete-for-nrepl :as suitable]))
[suitable.complete-for-nrepl :as suitable]
[suitable.compliment.sources.cljs :as suitable-sources]))

(defn- cljs-complete
[{:keys [enhanced-cljs-completion?] :as msg} cljs-env ns prefix extra-metadata]
(concat (cljs-complete/completions cljs-env prefix {:context-ns ns
:extra-metadata extra-metadata})
(when enhanced-cljs-completion? (suitable/complete-for-nrepl msg))))
(def clj-sources
"Source keywords for Clojure completions."
[:compliment.sources.special-forms/literals
:compliment.sources.class-members/static-members
:compliment.sources.ns-mappings/ns-mappings
:compliment.sources.resources/resources
:compliment.sources.keywords/keywords
:compliment.sources.local-bindings/local-bindings
:compliment.sources.class-members/members
:compliment.sources.namespaces-and-classes/namespaces-and-classes
:compliment.sources.special-forms/special-forms])

(def cljs-sources
"Source keywords for ClojureScript completions."
[::suitable-sources/cljs-source])

(defn complete
[{:keys [ns symbol context extra-metadata] :as msg}]
(let [ns (misc/as-sym ns)
prefix (str symbol)
extra-metadata (set (map keyword extra-metadata))]
[{:keys [ns symbol context extra-metadata enhanced-cljs-completion?] :as msg}]
(let [prefix (str symbol)
completion-opts {:ns (misc/as-sym ns)
:context context
:extra-metadata (set (map keyword extra-metadata))}]
(if-let [cljs-env (cljs/grab-cljs-env msg)]
(cljs-complete msg cljs-env ns prefix extra-metadata)
(jvm-complete/completions prefix {:ns ns
:context context
:extra-metadata extra-metadata}))))
(binding [suitable-sources/*compiler-env* cljs-env]
(concat (complete/completions prefix (merge completion-opts {:sources +cljs-sources+}))
(when enhanced-cljs-completion? (suitable/complete-for-nrepl msg))))
(complete/completions prefix (merge completion-opts {:sources +clj-sources+})))))

(defn completion-doc
[{:keys [ns symbol] :as msg}]
(when-not (cljs/grab-cljs-env msg)
(jvm-complete/documentation (str symbol) (misc/as-sym ns))))
(if-let [cljs-env (cljs/grab-cljs-env msg)]
(binding [suitable-sources/*compiler-env* cljs-env]
(complete/documentation (str symbol) (misc/as-sym ns) {:sources +cljs-sources+}))
(complete/documentation (str symbol) (misc/as-sym ns) {:sources +clj-sources+})))

(defn complete-reply [msg]
{:completions (complete msg)})
Expand All @@ -39,7 +52,7 @@

(defn flush-caches-reply
[msg]
(jvm-complete-utils/flush-caches)
(complete-utils/flush-caches)
{})

(defn handle-complete [handler msg]
Expand Down
2 changes: 1 addition & 1 deletion src/cider/nrepl/middleware/track_state.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[cider.nrepl.middleware.util :as util]
[cider.nrepl.middleware.util.cljs :as cljs]
[cider.nrepl.middleware.util.meta :as um]
[cljs-tooling.util.analysis :as cljs-ana]
[orchard.cljs.analysis :as cljs-ana]
[clojure.java.io :as io]
[clojure.tools.namespace.find :as ns-find]
[nrepl.misc :refer [response-for]]
Expand Down
22 changes: 14 additions & 8 deletions test/cljs/cider/nrepl/middleware/cljs_complete_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@

(deftest cljs-complete-test
(let [response (session/message {:op "complete"
:ns "cljs.user"
:symbol ""})]
(is (= #{"done"} (:status response)))
(is (sequential? (:completions response)))
(is (every? map? (:completions response))))

(let [response (session/message {:op "complete"
:ns "cljs.user"
:symbol "defpro"})
candidate (first (:completions response))]
(is (= "defprotocol" (:candidate candidate)))
Expand All @@ -24,7 +22,6 @@

(testing "function metadata"
(let [response (session/message {:op "complete"
:ns "cljs.user"
:symbol "assoc"
:extra-metadata ["arglists" "doc"]})
candidate (first (:completions response))]
Expand All @@ -33,7 +30,6 @@

(testing "macro metadata"
(let [response (session/message {:op "complete"
:ns "cljs.user"
:symbol "defprot"
:extra-metadata ["arglists" "doc"]})
candidate (first (:completions response))]
Expand Down Expand Up @@ -71,7 +67,17 @@
(is (empty? candidates)))))

(deftest cljs-complete-doc-test
(let [response (session/message {:op "complete-doc" :symbol "tru"})]
(is (= (:status response) #{"done"}))
(is (empty? (:completion-doc response))
"Can't handle CLJS yet.")))
(testing "no suitable documentation can be found"
(let [response (session/message {:op "complete-doc" :symbol "tru"})]
(is (= (:status response) #{"done"}))
(is (empty? (:completion-doc response)) "an unknown symbol should have empty doc.")))

(testing "suitable documentation for a symbol"
(let [response (session/message {:op "complete-doc" :symbol "map"})]
(is (= (:status response) #{"done"}))
(is (re-find #"\(\[f\] \[f coll\] \[f c1 c2\] \[f c1 c2 c3\] \[f c1 c2 c3 & colls\]\)" (:completion-doc response)) "should return the \"map\" docstring")))

(testing "suitable documentation for a macro symbol"
(let [response (session/message {:op "complete-doc" :symbol "when"})]
(is (= (:status response) #{"done"}))
(is (re-find #"\(\[test & body\]\)" (:completion-doc response)) "should return the \"when\" docstring"))))

0 comments on commit 9769c93

Please sign in to comment.