-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsession.cljc
483 lines (432 loc) · 15.1 KB
/
session.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
(ns paravim.session
(:require [paravim.buffers :as buffers]
[paravim.scroll :as scroll]
[paravim.constants :as constants]
[paravim.minimap :as minimap]
[odoyle.rules :as o]
[clojure.string :as str]
[clojure.core.async :as async]
[clojure.spec.alpha :as s]
[libvim-clj.constants :as vim-const]))
(defonce *initial-session (atom nil))
(defonce *session (atom nil))
(defonce *reload? (atom false))
(defn get-constants [session]
(first (o/query-all session ::get-constants)))
(defn get-vim [session]
(first (o/query-all session ::get-vim)))
(defn get-font [session]
(first (o/query-all session ::get-font)))
(defn get-window [session]
(first (o/query-all session ::get-window)))
(defn get-current-tab [session]
(first (o/query-all session ::get-current-tab)))
(defn get-tab [session tab-id]
(some (fn [tab]
(when (= tab-id (:id tab))
tab))
(o/query-all session ::get-tab)))
(defn get-mouse [session]
(first (o/query-all session ::get-mouse)))
(defn get-text-box [session tab-id]
(some (fn [text-box]
(when (= tab-id (:id text-box))
text-box))
(o/query-all session ::get-text-box)))
(defn get-bounding-box [session box-id]
(some (fn [bounding-box]
(when (= box-id (:id bounding-box))
bounding-box))
(o/query-all session ::get-bounding-box)))
(defn get-buffer [session buffer-id]
(some (fn [buffer]
(when (= buffer-id (:id buffer))
(dissoc buffer :id)))
(o/query-all session ::get-buffer)))
(defn get-current-buffer [session]
(first (o/query-all session ::get-current-buffer)))
(defn get-minimap [session buffer-id]
(some (fn [minimap]
(when (= buffer-id (:id minimap))
minimap))
(o/query-all session ::get-minimap)))
(defn get-globals [session]
(first (o/query-all session ::get-globals)))
(defn insert
([session id attr->value]
(o/insert session id
(reduce-kv
(fn [m k v]
(assoc m (keyword "paravim.session" (name k)) v))
{}
attr->value)))
([session id attr value]
(o/insert session id (keyword "paravim.session" (name attr)) value)))
(def queries
(o/ruleset
{::get-constants
[:what
[::constant ::base-rect-entity base-rect-entity]
[::constant ::base-rects-entity base-rects-entity]
[::constant ::font-width font-width]
[::constant ::font-height font-height]
[::constant ::base-font-entity base-font-entity]
[::constant ::base-text-entity base-text-entity]
[::constant ::roboto-font-entity roboto-font-entity]
[::constant ::roboto-text-entity roboto-text-entity]
[::constant ::toolbar-text-entities toolbar-text-entities]
[::constant ::highlight-text-entities highlight-text-entities]]
::get-vim
[:what
[::vim ::mode mode]
[::vim ::ascii ascii]
[::vim ::control? control?]
[::vim ::show-search? show-search?]
[::vim ::visual-range visual-range]
[::vim ::highlights highlights]
[::vim ::message message]
[::vim ::command-start command-start]
[::vim ::command-text command-text]
[::vim ::command-completion command-completion]
[::vim ::command-text-entity command-text-entity]
[::vim ::command-cursor-entity command-cursor-entity]]
::get-font
[:what
[::font ::size size]
[::font ::multiplier multiplier]]
::get-window
[:what
[::window ::width width]
[::window ::height height]]
::get-current-tab
[:what
[::tab ::current id]]
::get-tab
[:what
[id ::buffer-id buffer-id]]
::get-mouse
[:what
[::mouse ::x x]
[::mouse ::y y]
[::mouse ::target target]
[::mouse ::cursor cursor]]
::get-text-box
[:what
[id ::left left]
[id ::right right]
[id ::top top]
[id ::bottom bottom]]
::get-bounding-box
[:what
[id ::x1 x1]
[id ::y1 y1]
[id ::x2 x2]
[id ::y2 y2]
[id ::align align]]
::get-buffer
[:what
[id ::tab-id tab-id]
[id ::text-entity text-entity]
[id ::parinfer-text-entity parinfer-text-entity]
[id ::rects-entity rects-entity]
[id ::parsed-code parsed-code]
[id ::needs-parinfer? needs-parinfer?]
[id ::needs-parinfer-init? needs-parinfer-init?]
[id ::needs-clojure-refresh? needs-clojure-refresh?]
[id ::camera camera]
[id ::camera-x camera-x]
[id ::camera-y camera-y]
[id ::camera-target-x camera-target-x]
[id ::camera-target-y camera-target-y]
[id ::scroll-speed-x scroll-speed-x]
[id ::scroll-speed-y scroll-speed-y]
[id ::path path]
[id ::file-name file-name]
[id ::lines lines]
[id ::clojure? clojure?]
[id ::cursor-line cursor-line]
[id ::cursor-column cursor-column]
[id ::show-minimap? show-minimap?]]
::get-current-buffer
[:what
[::tab ::current tab-id]
[tab-id ::buffer-id buffer-id]]
::get-minimap
[:what
[id ::minimap/show? show?]
[id ::minimap/rects-entity rects-entity]
[id ::minimap/text-entity text-entity]]
::get-globals
[:what
[::global :paravim.start/command-chan command-chan]
[::global :paravim.start/single-command-chan single-command-chan]]}))
(def rules
(o/ruleset
{::init-font-size
[:what
[::constant ::font-height font-height]
[::font ::size size {:then false}]
[::font ::multiplier multiplier]
:when
(== 0 size)
:then
(-> o/*session*
(o/insert ::font ::size (* multiplier font-height))
o/reset!)]
::update-font-multiplier
[:what
[::constant ::font-height font-height]
[::font ::size size]
:when
(pos? size)
:then
(-> o/*session*
(o/insert ::font ::multiplier (/ size font-height))
o/reset!)]
::mouse-hovers-over-text
[:what
[::window ::width window-width]
[::window ::height window-height]
[::mouse ::x mouse-x]
[::mouse ::y mouse-y]
[::tab ::current tab-id]
[::font ::multiplier font-multiplier]
[tab-id ::left left]
[tab-id ::right right]
[tab-id ::top top]
[tab-id ::bottom bottom]
:when
(<= left mouse-x (- window-width right))
(<= (top window-height font-multiplier)
mouse-y
(bottom window-height font-multiplier))
:then
(-> o/*session*
(o/insert ::mouse {::target :text
::cursor :ibeam})
o/reset!)]
::mouse-hovers-over-bounding-box
[:what
[::window ::width window-width]
[::mouse ::x mouse-x]
[::mouse ::y mouse-y]
[::font ::multiplier font-multiplier]
[id ::x1 x1]
[id ::y1 y1]
[id ::x2 x2]
[id ::y2 y2]
[id ::align align]
:when
(let [x1 (cond->> (* x1 font-multiplier)
(= :right align)
(- window-width))
y1 (* y1 font-multiplier)
x2 (cond->> (* x2 font-multiplier)
(= :right align)
(- window-width))
y2 (* y2 font-multiplier)]
(and (<= x1 mouse-x x2)
(<= y1 mouse-y y2)))
:then
(-> o/*session*
(o/insert ::mouse {::target id
::cursor :hand})
o/reset!)]
::update-buffer-when-font-changes-or-window-resizes
[:what
[::global :paravim.start/single-command-chan single-command-chan]
[::vim ::mode mode {:then false}]
[::vim ::visual-range visual-range {:then false}]
[::vim ::show-search? show-search? {:then false}]
[::vim ::highlights highlights {:then false}]
[::font ::multiplier multiplier]
[::window ::width window-width]
[::window ::height window-height]
[id ::tab-id tab-id {:then false}]
:when
;; ignore ascii buffers
(number? id)
:then
(let [constants (get-constants o/*session*)
buffer (get-buffer o/*session* id)
text-box (get-text-box o/*session* tab-id)
window {:width window-width :height window-height}
new-buffer (-> buffer
(paravim.buffers/update-cursor mode multiplier text-box constants window)
(paravim.buffers/update-highlight constants)
(paravim.buffers/update-selection constants visual-range)
(paravim.buffers/update-search-highlights constants show-search? highlights))]
(-> o/*session*
(insert id new-buffer)
o/reset!)
(async/put! single-command-chan [:resize-window]))]
::move-camera-to-target
[:what
[::time ::delta delta-time]
[id ::camera-x camera-x {:then false}]
[id ::camera-y camera-y {:then false}]
[id ::camera-target-x camera-target-x {:then false}]
[id ::camera-target-y camera-target-y {:then false}]
[id ::scroll-speed-x scroll-speed-x {:then false}]
[id ::scroll-speed-y scroll-speed-y {:then false}]
:when
(or (not (== camera-x camera-target-x))
(not (== camera-y camera-target-y)))
:then
(let [new-buffer (scroll/animate-camera
camera-x camera-y
camera-target-x camera-target-y
scroll-speed-x scroll-speed-y
delta-time)]
(-> o/*session*
(insert id new-buffer)
o/reset!))]
::rubber-band-effect
[:what
[::font ::multiplier multiplier]
[::window ::width window-width]
[::window ::height window-height]
[id ::tab-id tab-id]
[id ::text-entity text-entity]
[id ::show-minimap? show-minimap?]
[id ::camera-target-x camera-target-x]
[id ::camera-target-y camera-target-y]
[id ::scroll-speed-x scroll-speed-x]
[id ::scroll-speed-y scroll-speed-y]
:then
(let [constants (get-constants o/*session*)
text-box (get-text-box o/*session* tab-id)
window {:width window-width :height window-height}
new-buffer (scroll/rubber-band-camera
text-entity show-minimap?
camera-target-x camera-target-y
scroll-speed-x scroll-speed-y
multiplier text-box constants window)]
(-> o/*session*
(insert id new-buffer)
o/reset!))]
::minimap
[:what
[::font ::multiplier multiplier]
[::window ::width window-width]
[::window ::height window-height]
[id ::tab-id tab-id]
[id ::text-entity text-entity]
[id ::lines lines]
[id ::camera-x camera-x]
[id ::camera-y camera-y]
[id ::show-minimap? show-minimap? {:then false}]
:when
;; ignore ascii buffers
(number? id)
:then
(let [text-box (get-text-box o/*session* tab-id)
constants (get-constants o/*session*)
new-minimap (paravim.minimap/->minimap
text-entity lines camera-x camera-y
constants multiplier
window-width window-height text-box)
show? (::minimap/show? new-minimap)]
(-> o/*session*
(o/insert id new-minimap)
(cond-> (not= show? show-minimap?)
(o/insert id ::show-minimap? show?))
o/reset!))]}))
(defn reload! [callback]
(reset! *initial-session
(-> (reduce o/add-rule (o/->session) (concat queries rules))
(o/insert ::vim {::mode 'NORMAL
::ascii nil
::control? false
::show-search? false
::visual-range nil
::highlights []
::message nil
::command-start nil
::command-text nil
::command-completion nil
::command-text-entity nil
::command-cursor-entity nil})
(o/insert ::font {::size 0 ;; initialized in the font rule
::multiplier constants/default-font-multiplier})
(o/insert ::tab ::current ::constants/files)
callback))
;; reload the session if it's been created already
(when @*session
(reset! *reload? true)))
;; create initial session
(reload! identity)
;; specs
(s/def ::base-rect-entity map?)
(s/def ::base-rects-entity map?)
(s/def ::font-width number?)
(s/def ::font-height number?)
(s/def ::base-font-entity map?)
(s/def ::base-text-entity map?)
(s/def ::roboto-font-entity map?)
(s/def ::roboto-text-entity map?)
(s/def ::toolbar-text-entities map?)
(s/def ::highlight-text-entities map?)
(s/def ::start-line integer?)
(s/def ::start-column integer?)
(s/def ::end-line integer?)
(s/def ::end-column integer?)
(s/def ::mode (-> vim-const/modes vals set))
(s/def ::ascii (s/nilable keyword?))
(s/def ::control? boolean?)
(s/def ::show-search? boolean?)
(s/def ::visual-range (s/nilable (s/keys :req-un [::start-line ::start-column ::end-line ::end-column])))
(s/def ::highlights (s/coll-of (s/keys :req-un [::start-line ::start-column ::end-line ::end-column])))
(s/def ::message (s/nilable string?))
(s/def ::command-start (s/nilable string?))
(s/def ::command-text (s/nilable string?))
(s/def ::command-completion (s/nilable string?))
(s/def ::command-text-entity (s/nilable map?))
(s/def ::command-cursor-entity (s/nilable map?))
(s/def ::size number?)
(s/def ::multiplier number?)
(s/def ::width number?)
(s/def ::height number?)
(s/def ::current constants/tab?)
(s/def ::x number?)
(s/def ::y number?)
(s/def ::target (s/nilable keyword?))
(s/def ::cursor (s/nilable keyword?))
(s/def ::left number?)
(s/def ::right number?)
(s/def ::top fn?)
(s/def ::bottom fn?)
(s/def ::x1 number?)
(s/def ::y1 number?)
(s/def ::x2 number?)
(s/def ::y2 number?)
(s/def ::align #{:left :right})
(s/def ::tab-id constants/tab?)
(s/def ::text-entity map?)
(s/def ::parinfer-text-entity (s/nilable map?))
(s/def ::rects-entity (s/nilable map?))
(s/def ::parsed-code (s/nilable vector?))
(s/def ::needs-parinfer? boolean?)
(s/def ::needs-parinfer-init? boolean?)
(s/def ::needs-clojure-refresh? boolean?)
(s/def ::camera map?)
(s/def ::camera-x number?)
(s/def ::camera-y number?)
(s/def ::camera-target-x number?)
(s/def ::camera-target-y number?)
(s/def ::scroll-speed-x number?)
(s/def ::scroll-speed-y number?)
(s/def ::path (s/nilable string?))
(s/def ::file-name (s/nilable string?))
(s/def ::lines (s/coll-of string?))
(s/def ::clojure? boolean?)
(s/def ::cursor-line integer?)
(s/def ::cursor-column integer?)
(s/def ::show-minimap? boolean?)
(s/def ::buffer-id (s/nilable integer?))
(s/def ::minimap/show? boolean?)
(s/def ::minimap/rects-entity map?)
(s/def ::minimap/text-entity map?)
(s/def :paravim.start/command-chan any?)
(s/def :paravim.start/single-command-chan any?)
(s/def ::delta number?)