Skip to content

Commit

Permalink
move options on form into metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
timothypratley committed Sep 10, 2024
1 parent f487ccd commit 485b576
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{:deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.scicloj/kindly {:mvn/version "4-beta7"}}
org.scicloj/kindly {:local/root "../kindly"
#_:mvn/version #_"4-beta8"}}
:aliases {:dev {:extra-deps {org.scicloj/clay {:mvn/version "2-alpha87"}
scicloj/tablecloth {:mvn/version "7.021"}}
:extra-paths ["test" "notebooks"]}
Expand Down
3 changes: 1 addition & 2 deletions src/scicloj/kindly_advice/v1/api.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns scicloj.kindly-advice.v1.api
(:require [scicloj.kindly-advice.v1.advisors :as advisors]
[scicloj.kindly-advice.v1.completion :as completion]
[scicloj.kindly.v4.api :as kindly]))
[scicloj.kindly-advice.v1.completion :as completion]))

(def *advisors
(atom advisors/default-advisors))
Expand Down
26 changes: 7 additions & 19 deletions src/scicloj/kindly_advice/v1/completion.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,17 @@
(:kindly/options m))
(when (var? x) (meta-options @x))))


(defn deep-merge
"Recursively merges maps.
See https://dnaeon.github.io/recursively-merging-maps-in-clojure/. "
[& maps]
(letfn [(m [& xs]
(if (some #(and (map? %) (not (record? %))) xs)
(apply merge-with m xs)
(last xs)))]
(reduce m maps)))

(defn complete-options [{:keys [form value]
(defn complete-options [{:keys [form]
:as context}]
(let [options (deep-merge (meta-options form)
(meta-options value))]
;; Kindly options found on ns cause *options* to be mutated
(let [form-options (meta-options form)]
;; Kindly options found on ns form cause options to be mutated
(when (and (sequential? form)
(-> form first (= 'ns)))
(set! kindly/*options* (deep-merge kindly/*options* options (meta-options *ns*))))
;; Toolmakers should ensure *options* is reset when reading namespaces by (binding [kindly/*options* kindly/*options*] ...)
(-> form first (= 'ns))
form-options)
(kindly/merge-options! form-options))
(update context :kindly/options
(fn [context-options]
(deep-merge context-options kindly/*options* options)))))
(kindly/deep-merge context-options (kindly/get-options))))))

(defn complete [context]
(-> context
Expand Down
5 changes: 5 additions & 0 deletions test/scicloj/kindly_advice/v1/completion_test.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns scicloj.kindly-advice.v1.completion-test
(:require [clojure.test :refer [deftest is testing]]
[scicloj.kindly.v4.kind :as kind]
[scicloj.kindly.v4.api :as kindly]
[scicloj.kindly-advice.v1.completion :as kac]))

(def table3 (kind/table {}))
Expand Down Expand Up @@ -31,3 +32,7 @@
(is (= :kind/table (kac/meta-kind #'table3)))
(is (= :kind/table (kac/meta-kind #'table4)))
(is (= :kind/table (kac/meta-kind #'table5)))))

(deftest options-test
(kac/complete-options {:form ^{:kindly/options {:foo "bar"}} '(ns test.ns)})
(is (= {:foo "bar"} (kindly/get-options))))

0 comments on commit 485b576

Please sign in to comment.