-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsnitch.cljs
More file actions
65 lines (56 loc) · 2.88 KB
/
snitch.cljs
File metadata and controls
65 lines (56 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(ns calva-power-tools.tool.snitch
(:require
["vscode" :as vscode]
[calva-power-tools.calva :as calva]
[calva-power-tools.extension.db :as db]
[calva-power-tools.extension.life-cycle-helpers :as lc-helpers]
[calva-power-tools.util :as util]
[promesa.core :as p]
[clojure.string :as string]))
(defn- instrument-form [form]
(let [snitched (.replace form
#"\b(defn-?|defmethod|fn|let)(?=\s)"
(fn [s]
(case (string/trim s)
"defn-" "defn*"
"defn" "defn*"
"defmethod" "defmethod*"
"let" "*let"
"fn" "*fn"
s)))]
(calva/execute-calva-command! "calva.runCustomREPLCommand"
#js {:snippet snitched})))
(defn- instrument-top-level-form []
(instrument-form (some-> (calva/currentTopLevelForm)
second)))
(defn- instrument-current-form []
(instrument-form (some-> (calva/currentForm)
second)))
(defn- get-snitched-defn-results []
(calva/execute-calva-command! "calva.runCustomREPLCommand"
#js {:snippet "${top-level-defined-symbol|replace|$|<}"}))
(defn- reconstruct-last-defn-call-to-clipboard []
(-> (calva/execute-calva-command! "calva.runCustomREPLCommand"
#js {:snippet "${top-level-defined-symbol|replace|$|>}"})
(.then (fn [_]
(p/let [ns (calva/getNamespace)
evaluation+ (calva/evaluateCode+ js/undefined "*1" ns)
last-call (.-result evaluation+)]
(vscode/env.clipboard.writeText last-call)
(vscode/window.showInformationMessage "The snitched call to this function is saved to the clipboard."))))))
(defn- load-dependency []
(-> (util/load-dependency {:deps/mvn-name "org.clojars.abhinav/snitch"})
(.then (fn [_]
(calva/execute-calva-command!
"calva.runCustomREPLCommand"
#js {:snippet "(clojure.core/require '[snitch.core :refer [defn* defmethod* *fn *let]])"
:repl "clj"})))))
(defn- register-command! [command f]
(lc-helpers/register-command! db/!app-db command f))
(defn activate! []
;; Register commands that call Calva's custom REPL command
(register-command! "cpt.snitch.loadSnitchDependency" #'load-dependency)
(register-command! "cpt.snitch.instrumentTopLevelForm" #'instrument-top-level-form)
(register-command! "cpt.snitch.instrumentCurrentForm" #'instrument-current-form)
(register-command! "cpt.snitch.getSnitchedDefnResults" #'get-snitched-defn-results)
(register-command! "cpt.snitch.reconstructLastDefnCallToClipboard" #'reconstruct-last-defn-call-to-clipboard))