Skip to content

Commit

Permalink
Improving new execution hl system
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmonettas committed Feb 2, 2024
1 parent 866540c commit 9124f4e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 72 deletions.
25 changes: 10 additions & 15 deletions src-dbg/flow_storm/debugger/ui/flows/code.clj
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,13 @@
:print-level 3
:enable? true))))

(defn- calculate-execution-highlight-span [spans curr-coord]
(defn- calculate-execution-idx-range [spans curr-coord]
(let [[s1 s2 :as hl-coords-spans] (->> spans
(map-indexed (fn [i s] (assoc s :i i)))
(filter (fn [{:keys [coord]}] (= coord curr-coord))))]
(case (count hl-coords-spans)
1 (select-keys (get spans (:i s1)) [:idx-from :len])
2 (let [spans-range (subvec spans (:i s1) (inc (:i s2)))
last-span (last spans-range)
from (:idx-from s1)
len (- (+ (:idx-from last-span) (:len last-span))
from)]
{:idx-from from
:len len})
1 [(:idx-from s1) (+ (:idx-from s1) (:len s1))]
2 [(:idx-from s1) (+ (:idx-from s2) (:len s2))]
nil)))

(defn- build-style-spans
Expand All @@ -111,17 +105,18 @@
[coord-spans curr-coord]

(let [^StyleSpansBuilder spb (StyleSpansBuilder.)
{exec-idx-from :idx-from exec-len :len} (calculate-execution-highlight-span coord-spans curr-coord)]
(doseq [{:keys [idx-from len coord interesting?]} coord-spans]
[exec-from exec-to] (calculate-execution-idx-range coord-spans curr-coord)]
(doseq [{:keys [idx-from len coord interesting? tab?]} coord-spans]
(let [color-classes (cond-> ["code-token"]
(and coord (not interesting?))
(conj "possible")

(and exec-idx-from idx-from len exec-len
(<= exec-idx-from idx-from (+ idx-from len) (+ exec-idx-from exec-len)))
(and exec-from exec-to
(<= exec-from idx-from (+ idx-from len) exec-to)
(not tab?))
(conj "executing")

(and coord interesting?)
interesting?
(conj "interesting"))]

(.add spb color-classes len)))
Expand All @@ -144,7 +139,7 @@
(assoc tok :interesting? true)
tok)))
form-pprinter/coord-spans)
_ (def SPANS spans)

exec-idx (some (fn [{:keys [coord idx-from]}]
(when (= coord curr-coord)
idx-from))
Expand Down
95 changes: 38 additions & 57 deletions src-shared/flow_storm/form_pprinter.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns flow-storm.form-pprinter
(:require [clojure.pprint :as pp]
[flow-storm.utils :as utils]
[hansel.utils :as hansel-utils]))
[hansel.utils :as hansel-utils])
(:import [java.util ArrayDeque]))

(defn- seq-delims

Expand Down Expand Up @@ -188,68 +189,48 @@
:nl (println)
:text (print text)))))

(defn coord-spans
(defn- read-contiguous-spaces [^ArrayDeque ptokens-q]
(loop [len 0]
(if (.isEmpty ptokens-q)
len
(let [{:keys [kind]} (.peek ptokens-q)]
(if (= :sp kind)
(do
(.pop ptokens-q)
(recur (inc len)))
len)))))

"Given `ptokens` as generated by `pprint-tokens` return a collection of spans.
(defn- next-span [^ArrayDeque ptokens-q]
(when-not (.isEmpty ptokens-q)
(let [{:keys [coord kind len interesting? text]} (.pop ptokens-q)]
(cond

Spans are of two kinds :
- contiguous text that contains a :coord
- contiguous text that does NOT contains a :coord
(= kind :text)
{:coord coord, :len len, :interesting? interesting?, :text text}

All spans are in the form of {:idx-from INT :len INT} but the ones that spans over
:coord text will contain the :coord key.
(= kind :nl)
{:len (+ 1 (read-contiguous-spaces ptokens-q)) :tab? true}

If the print token is marked as `:interesting?` the resulting span will also be marked
with it.
"
(= kind :sp)
{:len 1}))))

[ptokens]
(defn coord-spans
"Given `ptokens` as generated by `pprint-tokens` return a collection of spans.
(let [;; create spans for all the tokens that has coords
coord-spans (->> (filterv :coord ptokens)
(map #(select-keys % [:idx-from :len :coord :interesting?])))
total-print-length (->> ptokens
(map (fn [{:keys [kind len]}]
(case kind
:text len
:sp 1
:nl 1)))
(reduce +))]
(if (seq coord-spans)
;; calculate and fill the holes with non coord spans
(let [[first-span :as spans] (->> coord-spans
(partition-all 2 1)
(mapcat (fn [[cs next-cs]]
(if-not next-cs
[cs]
(let [cs-idx-to (+ (:idx-from cs) (:len cs))]
(if (= cs-idx-to
(:idx-from next-cs))
[cs]
[cs {:idx-from cs-idx-to
:len (- (:idx-from next-cs) cs-idx-to)}])))))
(into []))
;; after filling the holes with non coord spans we need to also deal with
;; the beginning and the end in case our ptokens doesn't start or end with a coord
spans' (if-not (zero? (:idx-from first-span))
;; first make a span for the beginning if needed
(into [{:idx-from 0
:len (:idx-from first-span)}]
spans)
spans)
last-span (nth spans' (dec (count spans')))
last-span-end (+ (:idx-from last-span) (:len last-span))
final-spans (if (> total-print-length last-span-end)
;; then we need to add a tail
(conj spans' {:idx-from (inc last-span-end)
:len (- total-print-length last-span-end)})
spans')]
final-spans)

;; else, if there are no tokens with coords, make a big span
;; from 0 to the length of the whole text
[{:idx-from 0
:len total-print-length}])))
Spans are maps in the form of {:idx-from INT :len INT} with also some special marks.
- If the print-token contains a :coord it will be added to the span
- If the print-token is marked as :interesting? so it will be the span
- The spans generated for tabs (a new line followed by spaces) will be marked as :tab? true
"
[ptokens]
(let [ptokens-q (ArrayDeque. ptokens)]
(loop [idx-from 0
spans []]
(if-let [{:keys [len] :as span} (next-span ptokens-q)]
(recur (+ idx-from len)
(conj spans (assoc span :idx-from idx-from)))

spans))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Utilities for the repl ;;
Expand Down

0 comments on commit 9124f4e

Please sign in to comment.