-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathutil.cljc
170 lines (151 loc) · 6.38 KB
/
util.cljc
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
(ns nextjournal.clojure-mode.util
(:require #?@(:squint [] :cljs [[applied-science.js-interop :as j]])
["@codemirror/state" :refer [EditorSelection
StateEffect
Transaction]]
[nextjournal.clojure-mode.selections :as sel])
#?(:squint (:require-macros [applied-science.js-interop :as j])))
#?(:squint (def node-js? (some? js/globalThis.process))
:cljs (goog-define node-js? false))
(defn user-event-annotation [event-name]
(.. Transaction -userEvent (of event-name)))
(defn get-user-event-annotation [tr]
(.annotation ^Transaction tr (.-userEvent Transaction)))
(defn guard [x f] (when (f x) x))
(defn ^js from-to [p1 p2]
(if (> p1 p2) #js{:from p2 :to p1} #js{:from p1 :to p2}))
(defn dispatch-some
"If passed a transaction, dispatch to view and return true to stop processing commands."
[^js view tr]
(if tr
(do (.dispatch view tr)
true)
false))
(defn insertion
"Returns a `change` that inserts string `s` at position `from` and moves cursor to end of insertion."
([from s] (insertion from from s))
([from to ^string s]
{:changes {:insert s
:from from
:to to}
:cursor (+ from (count s))}))
(defn deletion
([from] (deletion (max 0 (dec from)) from))
([from to]
(let [from (if (= from to)
(max 0 (dec from))
from)]
{:cursor from
:changes {:from from :to to}})))
(defn line-content-at [state from]
(-> state
(j/call-in [:doc :lineAt] from)
(j/get :text)))
(defn map-cursor [^js original-range ^js state update-map]
{:pre [(map? update-map)]}
(let [{:keys [cursor/mapped
cursor
from-to
range
changes]} (guard update-map map?)
change-desc (when changes (.changes state (-> changes
clj->js)))]
(cond-> #js{:range (or range
(cond mapped (sel/cursor (.mapPos change-desc mapped))
cursor (sel/cursor cursor)
from-to (sel/range (from-to 0) (from-to 1)))
original-range)}
change-desc (j/!set :changes change-desc))))
(defn update-ranges
"Applies `f` to each range in `state` (see `changeByRange`)"
([state f]
(update-ranges state nil f))
([^js state tr-specs f ]
(->> (fn [range]
(or (when-some [result (f range)]
(map-cursor range state result))
#js{:range range}))
(.changeByRange state)
(#(j/extend! % tr-specs))
(.update state))))
(defn dispatch-changes [^js state dispatch ^js changes]
(when-not (.-empty changes)
(dispatch (.update state #js{:changes changes}))))
(defn update-lines
[^js state f & [{:keys [from to spec]
:or {from 0}}]]
(let [iterator (.. state -doc iter)]
(loop [result (.next iterator)
changes #js[]
from-pos from
line-num 1]
(j/let [^:js {:keys [done lineBreak ^string value]} result]
(if (or done
(> from to))
(.update state (j/extend! #js{:changes (.changes state changes)} spec))
(recur (.next iterator)
(if-let [change (and (not lineBreak) (f from-pos value line-num))]
(j/push! changes change)
changes)
(+ from-pos (count value))
(cond-> line-num lineBreak inc)))))))
(defn update-selected-lines
"`f` will be called for each selected line with args [line, changes-array, range]
and should *mutate* changes-array"
[^js state f]
(let [at-line (atom -1)
doc (.-doc state)]
(->> (j/fn [^:js {:as range :keys [from to anchor head]}]
(j/let [changes #js[]]
(loop [^js line (.lineAt doc from)]
(j/let [^:js {line-number :number line-to :to} line]
(when (> (.-number line) @at-line)
(reset! at-line line-number)
(f line changes range))
(if-let [next-line (and (> to line-to)
(guard (.lineAt doc (inc line-to))
#(> (.-number ^js %) line-number)))]
(recur next-line)
(let [^js change-set (.changes state changes)]
#js{:changes changes
:range (.range EditorSelection
(.mapPos change-set anchor 1)
(.mapPos change-set head 1))}))))))
(.changeByRange state))))
(j/defn iter-changed-lines
"`f` will be called for each changed line with args [line, changes-array]
and should *mutate* changes-array. Selections will be mapped through the resulting changeset."
[^:js {:as tr
:keys [^js changes ^js effects selection]
{:as ^js state :keys [^js doc]} :state} f]
(let [at-line (atom -1)
next-changes #js[]
_ (.iterChanges
changes
(fn [_from-a _to-a from-b to-b _inserted]
(j/let [^:js {:as line line-number :number line-to :to} (.lineAt doc from-b)]
(loop [line line]
(when (> line-number @at-line)
(reset! at-line line-number)
(f line next-changes))
(when-not (<= to-b line-to)
(let [next-line (.lineAt doc (inc line-to))]
(when (and next-line (> (.-number next-line) (.-number line)))
(recur next-line))))))))
next-changeset (.changes state next-changes)]
(if (seq next-changes)
(-> (j/select-keys tr [:annotations
:scrollIntoView
:reconfigure])
(j/assoc! :changes (.compose changes next-changeset))
(cond->
selection
(j/assoc! :selection (.. state -selection (map next-changeset)))
effects
(j/assoc! :effects (.mapEffects StateEffect effects next-changeset))))
tr)))
(j/defn something-selected? [^:js {{:keys [ranges]} :selection}]
(not (every? #(.-empty ^js %) ranges)))
(j/defn range-str [state ^:js {:as _selection :keys [from to]}]
(str (j/call-in state [:doc :slice] from to)))
#_(prn (js/call-in #js {:a {:b {:c 3}}} [:a :b :c] inc))