Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Commit d817039

Browse files
committed
Add rudimentary custom watches
1 parent 7719969 commit d817039

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

javascript.behaviors

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
:lt.plugins.js/js-exception :lt.plugins.js/on-eval
44
:lt.plugins.js/on-eval.one
55
:lt.plugins.js/watch-src
6+
:lt.plugins.js/watch-custom-src
67
:lt.plugins.js/js-watch
78
(:lt.object/add-tag :watchable)]
89
:files [(:lt.objs.files/file-types [{:name "Javascript" :exts [:js] :mime "text/javascript" :tags [:editor.javascript]}
@@ -18,4 +19,4 @@
1819
:lt.plugins.js.node/changelive!
1920
:lt.plugins.js.node/connect-retry]
2021
:nodejs.lang [:lt.plugins.js.node/connect!]
21-
}}
22+
}}

src/lt/plugins/js.cljs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,42 @@
8484
opts-str (.stringify js/JSON opts)]
8585
(str "lttools.watch(" src ", " opts-str ")" semi)))
8686

87+
(defn fill-placeholders [src exp meta]
88+
(-> exp
89+
(string/replace "__SELECTION*__", (str "'" src "'"))
90+
(string/replace "__SELECTION__" src)
91+
(string/replace "__ID__" (:id meta))))
92+
93+
(defn custom-src->watch [src exp meta]
94+
(let [hassemi (= (last src) ";")
95+
subsrc (if hassemi (butlast src) src)
96+
end (if hassemi ";" "")
97+
sym (name (gensym "jswatch_temp"))]
98+
(str "(function() {"
99+
"var " sym " = (" subsrc ");"
100+
(src->watch meta (fill-placeholders sym exp meta)) ";"
101+
"return " sym ";"
102+
"}())" end)))
103+
104+
(defn clean-code [src]
105+
(string/replace src (js/RegExp. "\n*#!.*\n" "gm") "\n"))
106+
107+
87108
(behavior ::watch-src
88109
:triggers #{:watch.src+}
89110
:reaction (fn [editor cur meta src]
90111
(src->watch meta src)))
91112

92-
(defn clean-code [src]
93-
(string/replace src (js/RegExp. "\n*#!.*\n" "gm") "\n"))
113+
(behavior ::watch-custom-src
114+
:triggers #{:watch.custom.src+}
115+
:reaction (fn [editor cur meta {:keys [exp]} src]
116+
(let [type (-> (parse exp) (.-body) (aget 0) (.-type))])
117+
(if (= "ExpressionStatement" type)
118+
(custom-src->watch src exp meta)
119+
(do
120+
(notifos/set-msg! "Custom expression is not a syntactic statement" {:class "error"})
121+
(src->watch meta src)))))
122+
94123

95124
(behavior ::on-eval
96125
:triggers #{:eval}

0 commit comments

Comments
 (0)