|
| 1 | +(ns lt.plugins.clojure.collapsible-exception |
| 2 | + (:require [lt.util.dom :as dom] |
| 3 | + [lt.object :as object] |
| 4 | + [lt.objs.editor :as ed] |
| 5 | + [lt.objs.notifos :as notifos] |
| 6 | + [crate.binding :refer [bound]]) |
| 7 | + (:require-macros [lt.macros :refer [defui behavior]])) |
| 8 | + |
| 9 | +(def ^:private ^:const NOT_FOUND -1) |
| 10 | + |
| 11 | +(defn truncate |
| 12 | + "truncate a string at newline or at 100 characters long" |
| 13 | + [text] |
| 14 | + (when-not (empty? text) |
| 15 | + (if (= NOT_FOUND (.indexOf text "\n")) |
| 16 | + (subs text 0 100); take 100 characters |
| 17 | + (first (clojure.string/split-lines text))))) |
| 18 | + |
| 19 | +(defn ->collapse-class [this summary] |
| 20 | + (str "inline-exception result-mark" |
| 21 | + (when (:open this) " open"))) |
| 22 | + |
| 23 | +(defui collapsible-exception-UI [this info] |
| 24 | + (let [stacktrace (:result info) |
| 25 | + summary (str (:summary info) " ...")] |
| 26 | + [:span {:class (bound this #(->collapse-class % summary)) |
| 27 | + :style "background: #73404c; color: #ffa6a6; |
| 28 | + max-width:initial; max-height:initial"} |
| 29 | + [:span.truncated summary] |
| 30 | + [:span.full stacktrace]]) |
| 31 | + :mousewheel (fn [e] (dom/stop-propagation e)) |
| 32 | + :click (fn [e] (dom/prevent e) |
| 33 | + (object/raise this :click)) |
| 34 | + :contextmenu (fn [e] (dom/prevent e) |
| 35 | + (object/raise this :menu! e)) |
| 36 | + :dblclick (fn [e] (dom/prevent e) |
| 37 | + (object/raise this :double-click))) |
| 38 | + |
| 39 | +(object/object* ::collapsible-exception |
| 40 | + :triggers #{:click :double-click :clear!} |
| 41 | + :tags #{:inline :collapsible.exception} |
| 42 | + :init |
| 43 | + (fn [this info] |
| 44 | + (when-let [ed (ed/->cm-ed (:ed info))] |
| 45 | + (let [content (collapsible-exception-UI this info)] |
| 46 | + (object/merge! this (assoc info |
| 47 | + :widget (ed/line-widget (ed/->cm-ed (:ed info)) (:line (:loc info)) |
| 48 | + content, {:coverGutter false}))) |
| 49 | + content)))) |
| 50 | + |
| 51 | +(behavior ::expandable-exceptions |
| 52 | + :triggers #{:editor.exception.collapsible} |
| 53 | + :reaction |
| 54 | + (fn [this summary stack loc] |
| 55 | + (let [ed (:ed @this) |
| 56 | + line (ed/line-handle ed (:line loc)) |
| 57 | + ex-obj (object/create ::collapsible-exception |
| 58 | + {:ed this, :result stack, |
| 59 | + :summary summary |
| 60 | + :loc loc, :line line})] |
| 61 | + (when-let [prev (get (@this :widgets) [line :inline])] |
| 62 | + (when (:open @prev) (object/merge! ex-obj {:open true})) |
| 63 | + (object/raise prev :clear!)) |
| 64 | + (when (:start-line loc) |
| 65 | + (doseq [widget (map #(get (@this :widgets) [(ed/line-handle ed %) :inline]) |
| 66 | + (range (:start-line loc) (:line loc))) |
| 67 | + :when widget] |
| 68 | + (object/raise widget :clear!))) |
| 69 | + (object/update! this [:widgets] assoc [line :inline] ex-obj)))) |
| 70 | + |
| 71 | + |
| 72 | +(behavior ::clj-expandable-exception |
| 73 | + :triggers #{:editor.eval.clj.exception} |
| 74 | + :reaction (fn [obj res passed?] |
| 75 | + (when-not passed? |
| 76 | + (notifos/done-working "")) |
| 77 | + (let [meta (:meta res) |
| 78 | + loc {:line (dec (:end-line meta)) :ch (:end-column meta 0) |
| 79 | + :start-line (dec (:line meta 1))}] |
| 80 | + (notifos/set-msg! (:result res) {:class "error"}) |
| 81 | + (object/raise obj :editor.exception.collapsible (:result res) (:stack res) loc)))) |
| 82 | + |
| 83 | +(behavior ::cljs-expandable-exception |
| 84 | + :triggers #{:editor.eval.cljs.exception} |
| 85 | + :reaction (fn [obj res passed?] |
| 86 | + (when-not passed? |
| 87 | + (notifos/done-working "")) |
| 88 | + (let [meta (:meta res) |
| 89 | + loc {:line (dec (:end-line meta)) :ch (:end-column meta) |
| 90 | + :start-line (dec (:line meta))} |
| 91 | + msg (or (:stack res) (truncate (:ex res))) |
| 92 | + stack (cond |
| 93 | + (:stack res) (:stack res) |
| 94 | + (and (:ex res) (.-stack (:ex res))) (.-stack (:ex res)) |
| 95 | + (and (:ex res) (:verbatim meta)) (:ex res) |
| 96 | + (and (:ex res) (not (:verbatim meta))) (pr-str (:ex res)) |
| 97 | + (not (nil? msg)) (or (:stack res) (:ex res)); untruncated stacktrace |
| 98 | + :else "Unknown error")] |
| 99 | + (notifos/set-msg! msg {:class "error"}) |
| 100 | + (object/raise obj :editor.exception.collapsible msg stack loc)))) |
0 commit comments