diff --git a/public/css/style.css b/public/css/style.css index 6a9a133..c3e7d40 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -278,6 +278,8 @@ a { line-height: var(--cell-height); color: var(--sheet-error); margin-left: 10px; + overflow: hidden; + /* This is quite useless, but cleaner than overflowing */ } .code-header-btn { diff --git a/src/bean/code.cljs b/src/bean/code.cljs index b9c3ed0..1b541fa 100644 --- a/src/bean/code.cljs +++ b/src/bean/code.cljs @@ -2,7 +2,7 @@ (:require [bean.grid :as grid])) (defn reevaluate [{:keys [code-in-editor] :as sheet}] - (grid/eval-code sheet code-in-editor)) + (grid/eval-code sheet code-in-editor false)) (defn set-code [sheet code] (assoc sheet :code-in-editor code)) diff --git a/src/bean/code_errors.cljs b/src/bean/code_errors.cljs index 9b42335..20ba1a6 100644 --- a/src/bean/code_errors.cljs +++ b/src/bean/code_errors.cljs @@ -1,4 +1,7 @@ (ns bean.code-errors) +(defn get-error [sheet] + (:code-error sheet)) + (defn named-ref-error [named error] (str "name: " named ". " error)) diff --git a/src/bean/grid.cljs b/src/bean/grid.cljs index a3d0e4c..d93b097 100644 --- a/src/bean/grid.cljs +++ b/src/bean/grid.cljs @@ -138,12 +138,15 @@ :depgraph (deps/make-depgraph parsed-grid)})) (defn- escalate-bindings-errors [sheet] - (reduce (fn [sheet [named {:keys [error]}]] - (if error - (reduced (assoc sheet :code-error (code-errors/named-ref-error named error))) - sheet)) - (dissoc sheet :code-error) - (:bindings sheet))) + (if (code-errors/get-error sheet) + ;; This likely already set a parse error, return as is + sheet + (reduce (fn [sheet [named {:keys [error]}]] + (if error + (reduced (assoc sheet :code-error (code-errors/named-ref-error named error))) + sheet)) + (dissoc sheet :code-error) + (:bindings sheet)))) (declare eval-dep) @@ -222,9 +225,10 @@ (defn eval-code ;; Suppressing errors so we let the grid evaluate before showing any errors in the code ([sheet] (eval-code sheet (:code sheet) true)) - ([sheet code & suppress-errors] - (let [res (let [code-ast (parser/parse-statement code)] - (if-let [parse-error (parser/error code-ast)] + ([sheet code suppress-errors] + (let [res (let [code-ast (parser/parse-statement code) + parse-error (parser/error code-ast)] + (if (string? parse-error) (assoc sheet :code-error parse-error) (-> (reduce (fn [sheet [_ [_ named] expr]] (eval-named named diff --git a/src/bean/ui/code.cljs b/src/bean/ui/code.cljs index 87b25b8..480eced 100644 --- a/src/bean/ui/code.cljs +++ b/src/bean/ui/code.cljs @@ -1,11 +1,12 @@ (ns bean.ui.code (:require [bean.code :as code] - [bean.ui.util :refer [cs]])) + [bean.ui.util :refer [cs]] + [bean.code-errors :as code-errors])) (defn set-eval-state [sheet] (assoc-in sheet [:ui :code-evaluation-state] - (if (:errors sheet) + (if (code-errors/get-error sheet) :error :evaluated))) diff --git a/test/bean/code_test.cljs b/test/bean/code_test.cljs new file mode 100644 index 0000000..dd17156 --- /dev/null +++ b/test/bean/code_test.cljs @@ -0,0 +1,26 @@ +(ns bean.code-test + (:require [clojure.test :refer [deftest testing is]] + [bean.code :as code] + [bean.grid :as grid])) + +(deftest code-errors-test + (testing "When the code has a parse error, it is escalated on reevaluation" + (is (= (-> (grid/new-sheet [[""]] "") + (code/set-code "foo: 99fail") + code/reevaluate + :code-error) + "Parse Error: idx 7. [{:tag :string, :expecting \"*\"} {:tag :regexp, :expecting #\"^\\s+\"} {:tag :string, :expecting \"+\"} {:tag :string, :expecting \"\\n\"}]"))) + + (testing "When the code has an error in a statement, it is escalated on reevaluation" + (is (= (-> (grid/new-sheet [[""]] "") + (code/set-code "foo: B3") + code/reevaluate + :code-error) + "name: foo. Invalid address [2 1]"))) + + (testing "When the code has an error in a statement, it is escalated on initial + evaluation after the grid is evaluated" + (is (= (-> (grid/new-sheet [["1"]] "foo: A1+\"bar\"") + grid/eval-sheet + :code-error) + "name: foo. + only works for Integers")))) diff --git a/test/bean/grid_test.cljs b/test/bean/grid_test.cljs index 0b5c73e..439c13f 100644 --- a/test/bean/grid_test.cljs +++ b/test/bean/grid_test.cljs @@ -300,7 +300,7 @@ (is (= {[:cell [0 1]] #{[:named "addaone"]}} (as-> (new-sheet [["1" "2"]] "addaone:4+A1") sheet (eval-sheet sheet) - (eval-code sheet "addaone:4+B1") + (eval-code sheet "addaone:4+B1" false) (get-in sheet [:depgraph]))))) (testing "Depgraph is updated when a named reference's dependents change"