|
7 | 7 | [elin.function.nrepl.cider :as e.f.n.cider]
|
8 | 8 | [elin.function.sexpr :as e.f.sexpr]
|
9 | 9 | [elin.protocol.host :as e.p.host]
|
| 10 | + [elin.schema :as e.schema] |
10 | 11 | [elin.schema.handler :as e.s.handler]
|
11 | 12 | [elin.schema.nrepl :as e.s.nrepl]
|
12 | 13 | [malli.core :as m]
|
|
15 | 16 | (def ?NreplAndCljKondo
|
16 | 17 | (m.util/select-keys e.s.handler/?Components [:component/nrepl :component/clj-kondo]))
|
17 | 18 |
|
18 |
| -(m/=> local-lookup [:=> [:cat ?NreplAndCljKondo string? string?] e.s.nrepl/?Lookup]) |
| 19 | +(m/=> local-lookup [:=> [:cat ?NreplAndCljKondo string? string?] (e.schema/error-or e.s.nrepl/?Lookup)]) |
19 | 20 | (defn- local-lookup
|
20 | 21 | [{:as elin :component/keys [host clj-kondo]} ns-str sym-str]
|
21 | 22 | (e/let [{cur-lnum :lnum cur-col :col} (async/<!! (e.p.host/get-cursor-position! host))
|
|
44 | 45 | clj-kondo protocol-ns protocol-name (:name info-response))]
|
45 | 46 | (assoc info-response :protocol-implementations impls)))
|
46 | 47 |
|
47 |
| -(m/=> lookup [:=> [:cat ?NreplAndCljKondo string? string?] e.s.nrepl/?Lookup]) |
48 |
| -(defn lookup |
| 48 | +(def ^:private ?LookupOrder |
| 49 | + [:sequential |
| 50 | + [:enum :nrepl :clj-kondo :local]]) |
| 51 | + |
| 52 | +(def ^:private ?LookupConfig |
| 53 | + [:map |
| 54 | + [:order ?LookupOrder]]) |
| 55 | + |
| 56 | +(defn- nrepl-lookup |
49 | 57 | [{:as elin :component/keys [nrepl clj-kondo]}
|
50 | 58 | ns-str
|
51 | 59 | sym-str]
|
52 | 60 | (try
|
53 |
| - (let [res (e.f.n.cider/info!! nrepl ns-str sym-str) |
54 |
| - error? (e/error? res) |
55 |
| - protocol-var-str (when-not error? |
56 |
| - (:protocol res)) |
57 |
| - proto-def (when (and (not error?) |
58 |
| - (not protocol-var-str) |
59 |
| - (:ns res) |
60 |
| - (:name res)) |
61 |
| - (e.f.clj-kondo/protocol-definition clj-kondo (:ns res) (:name res)))] |
| 61 | + (e/let [res (e.f.n.cider/info!! nrepl ns-str sym-str) |
| 62 | + protocol-var-str (:protocol res) |
| 63 | + proto-def (when (and (not protocol-var-str) |
| 64 | + (:ns res) |
| 65 | + (:name res)) |
| 66 | + (e.f.clj-kondo/protocol-definition clj-kondo (:ns res) (:name res)))] |
62 | 67 | (cond
|
63 | 68 | protocol-var-str
|
64 | 69 | (protocol-lookup elin protocol-var-str res)
|
|
68 | 73 | (format "%s/%s" (:protocol-ns proto-def) (:protocol-name proto-def))
|
69 | 74 | res)
|
70 | 75 |
|
71 |
| - (and (not error?) |
72 |
| - (or |
73 |
| - ;; clojure |
74 |
| - (contains? res :name) |
75 |
| - ;; java |
76 |
| - (contains? res :class))) |
| 76 | + (or |
| 77 | + ;; clojure |
| 78 | + (contains? res :name) |
| 79 | + ;; java |
| 80 | + (contains? res :class)) |
77 | 81 | res
|
78 | 82 |
|
79 | 83 | :else
|
80 |
| - (let [res (e.f.clj-kondo/lookup clj-kondo ns-str sym-str)] |
81 |
| - (if-not (e/error? res) |
82 |
| - res |
83 |
| - (local-lookup elin ns-str sym-str))))) |
84 |
| - |
| 84 | + (e/not-found))) |
85 | 85 | (catch Exception e
|
86 | 86 | (e/fault {:message (pr-str e)}))))
|
87 | 87 |
|
| 88 | +(m/=> lookup [:=> [:cat ?NreplAndCljKondo string? string? ?LookupConfig] (e.schema/error-or e.s.nrepl/?Lookup)]) |
| 89 | +(defn lookup |
| 90 | + [{:as elin :component/keys [clj-kondo]} |
| 91 | + ns-str |
| 92 | + sym-str |
| 93 | + config] |
| 94 | + (loop [[next-order & rest-orders] (:order config)] |
| 95 | + (if-not next-order |
| 96 | + (e/not-found) |
| 97 | + (let [resp (condp = next-order |
| 98 | + :nrepl |
| 99 | + (nrepl-lookup elin ns-str sym-str) |
| 100 | + |
| 101 | + :clj-kondo |
| 102 | + (e.f.clj-kondo/lookup clj-kondo ns-str sym-str) |
| 103 | + |
| 104 | + :local |
| 105 | + (local-lookup elin ns-str sym-str) |
| 106 | + |
| 107 | + (e/unsupported {:message (str "Unsupported order: " next-order)}))] |
| 108 | + (if-not (e/error? resp) |
| 109 | + resp |
| 110 | + (recur rest-orders)))))) |
| 111 | + |
88 | 112 | (defn clojuredocs-lookup
|
89 | 113 | "Returns a result of looking up the help of the function under the cursor in clojuredocs."
|
90 |
| - [{:as elin :component/keys [host nrepl]} export-edn-url] |
| 114 | + [{:as elin :component/keys [host nrepl]} export-edn-url config] |
91 | 115 | (e/let [{:keys [lnum col]} (async/<!! (e.p.host/get-cursor-position! host))
|
92 | 116 | {:keys [code]} (e.f.sexpr/get-expr elin lnum col)
|
93 | 117 | [ns-str name-str] (e/error-or
|
94 | 118 | (e/let [ns-str (e.f.sexpr/get-namespace elin)
|
95 |
| - resp (lookup elin ns-str code)] |
| 119 | + resp (lookup elin ns-str code config)] |
96 | 120 | [(:ns resp) (:name resp)])
|
97 | 121 | (str/split code #"/" 2))]
|
98 | 122 | (or (e/error-or (e.f.n.cider/clojuredocs-lookup!! nrepl ns-str name-str export-edn-url))
|
|
0 commit comments