Skip to content

Commit

Permalink
when reading an ns form, mutate kindly options
Browse files Browse the repository at this point in the history
  • Loading branch information
timothypratley committed Sep 8, 2024
1 parent 20cfbb6 commit 66b54c1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
14 changes: 13 additions & 1 deletion src/scicloj/kindly_advice/v1/api.cljc
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
(ns scicloj.kindly-advice.v1.api
(:require [scicloj.kindly-advice.v1.advisors :as advisors]
[scicloj.kindly-advice.v1.completion :as completion]))
[scicloj.kindly-advice.v1.completion :as completion]
[scicloj.kindly.v4.api :as kindly]))

(def *advisors
(atom advisors/default-advisors))

(defn f [{:keys [form]}]
(if (and (sequential? form)
(-> form first (= 'ns)))
(set! kindly/*options* (completion/deep-merge kindly/*options*)
(completion/meta-options form))))

(defn advise
"Adds advice to a context such as `{:form [:div]}`.
Advice recommends a kind `{:form [:div], :value [:div], :kind :kindly/hiccup}`.
Expand All @@ -14,6 +21,11 @@
(advise context @*advisors))
([context advisors]
(-> context

;; if ns form, mutate *options*?
;; replace or merge?
f

completion/complete
(#(reduce advisors/update-context
%
Expand Down
13 changes: 9 additions & 4 deletions src/scicloj/kindly_advice/v1/completion.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
(or
(when-let [m (meta x)]
(:kindly/options m))
(when (var? x) (meta-kind @x))))
(when (var? x) (meta-options @x))))

(defn deep-merge
"Recursively merges maps together. If all the maps supplied have nested maps
Expand All @@ -81,11 +81,16 @@

(defn complete-options [{:keys [form value]
:as context}]
(let [meta-options (or (meta-options form)
(meta-options value))]
(let [options (or (meta-options form)
(meta-options value))]
;; Kindly options found on ns 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*] ...)
(update context :kindly/options
(fn [options]
(deep-merge options kindly/*options* meta-options)))))
(deep-merge options kindly/*options* options)))))

(defn complete [context]
(-> context
Expand Down

0 comments on commit 66b54c1

Please sign in to comment.