Skip to content

Commit

Permalink
add hide-code detection
Browse files Browse the repository at this point in the history
downstream tools can benefit from having this logic
  • Loading branch information
timothypratley committed Nov 23, 2024
1 parent e0a9123 commit 736b814
Showing 1 changed file with 51 additions and 5 deletions.
56 changes: 51 additions & 5 deletions src/scicloj/kindly_advice/v1/completion.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,48 @@
:meta-kind (or (meta-kind form)
(meta-kind value))))

(defn hide-code? [{:as note :keys [code form value kind]} options]
(let [m (merge options (meta form) (meta value))
hide-code (select-keys m [:hide-code
:kind/hide-code
:kindly/hide-code
:kindly/hide-code?])]
(or (= kind :kind/hidden)
(nil? code)
(some val hide-code)
(and kind
(empty? hide-code)
(some-> options :kinds-that-hide-code kind)))))

(def default-hide-value-syms
'#{ns comment
def defonce defn defmacro
defrecord defprotocol deftype
extend-protocol extend
require})

(defn hide-value? [{:as note :keys [form value kind]}
{:as options :keys [hide-nils hide-vars hide-value-syms]}]
(let [m (merge options (meta form) (meta value))
hide-value (select-keys m [:hide-value
:kind/hide-value
:kindly/hide-value
:kindly/hide-value?])]
(or (= kind :kind/hidden)
(some val hide-value)
(and hide-nils (nil? value))
(and hide-vars (var? value))
(and (sequential? form)
(some->> form first (get (or hide-value-syms default-hide-value-syms))))
(and kind
(empty? hide-value)
(some-> options :kinds-that-hide-values kind)))))

(defn with-hide-options [context options]
(cond-> options
(hide-code? context options) (assoc :hide-code true)
(hide-value? context options) (assoc :hide-value true)))

(defn meta-options [x]
(or
(when-let [m (meta x)]
Expand All @@ -60,18 +102,22 @@
(defn complete-options [{:keys [form value]
:as context}]
(let [form-options (meta-options form)]
;; Kindly options found on ns form cause options to be mutated
;; Kindly options found on ns form cause options to be mutated,
;; note that options on the value are already on the ns.
(when (and (sequential? form)
(-> form first (= 'ns))
form-options)
(kindly/merge-options! form-options))

(update context :kindly/options
(fn [context-options]
(kindly/deep-merge context-options
(kindly/get-options)
(meta-options value)
form-options)))))
;; context options come from configuration
(->> (kindly/deep-merge context-options
;; options from the ns
(kindly/get-options)
form-options
(meta-options value))
(with-hide-options context))))))

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

0 comments on commit 736b814

Please sign in to comment.