Skip to content

Instantly share code, notes, and snippets.

@saikyun
Created November 7, 2021 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saikyun/7f6d099498aca7977a484d605f061b1d to your computer and use it in GitHub Desktop.
Save saikyun/7f6d099498aca7977a484d605f061b1d to your computer and use it in GitHub Desktop.
(import freja/new_gap_buffer :as gb)
(import freja/state)
# will contain the parsers for each line
(def cache @[])
(def gap-buffer (get-in state/editor-state [:left-state :editor :gb]))
(var buf "")
(gb/add-hook
gap-buffer
:hooks/new-line
:parse
(fn [gb k line-i current-line]
(set buf (string buf current-line))
(let [parser (or (-?> (with-dyns [:out stdout]
(print "lul")
(def v (get cache (dec line-i)))
# (pp v)
# (tracev v)
v)
(parser/clone))
(parser/new))]
#(with-dyns [:out stdout]
# (print "lul: " current-line))
(if-let [err (parser/error parser)]
(print err)
(do
(parser/consume parser current-line)
(pp (parser/state parser))
(def vs (seq [v :iterate (parser/produce parser)]
v))
# (pp vs)
(array/push cache parser))))))
# to recalculate all cache, we can clear lines
(array/clear (gap-buffer :lines))
# and say that it was changed
(put gap-buffer :changed true)
(gb/add-hook
gap-buffer
:hooks/invalidate-cache
:store-lines
# line-i is the line index that is invalidated
# that means all following lines will be recalculated,
# so we remove those
(fn [gb k line-i]
(print "removing from: " line-i " to " (length cache))
(when (< line-i (length cache))
(array/remove cache line-i (length cache)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment