|
1 | 1 | (ns calva-backseat-driver.integrations.calva.features |
2 | 2 | (:require |
3 | 3 | ["vscode" :as vscode] |
| 4 | + [calva-backseat-driver.bracket-balance :as balance] |
4 | 5 | [calva-backseat-driver.integrations.calva.api :as calva] |
5 | 6 | [calva-backseat-driver.integrations.calva.editor :as editor] |
6 | 7 | [promesa.core :as p])) |
|
11 | 12 | (def ^:private empty-result-note |
12 | 13 | "Not expecting a empty string as a result? If it is the first time you are using a namespace, evaluate its ns-form in the `user` namespace first.") |
13 | 14 |
|
14 | | - |
15 | 15 | (def ^:private error-result-note |
16 | 16 | "* clj: Evaluating `*e` will give your information about the error. |
17 | 17 | * cljs: Evaluating `(.-stack *e), gives you a stack trace") |
18 | 18 |
|
| 19 | + |
19 | 20 | (defn evaluate-code+ |
20 | 21 | "Returns a promise that resolves to the result of evaluating Clojure/ClojureScript code. |
21 | 22 | Takes a string of code to evaluate and a session key (clj/cljs/cljc), js/undefined means current session." |
22 | 23 | [{:ex/keys [dispatch!] |
23 | 24 | :calva/keys [code repl-session-key ns]}] |
24 | | - (p/let [evaluate (get-in calva/calva-api [:repl :evaluateCode]) |
25 | | - result (-> (p/let [^js evaluation+ (if ns |
26 | | - (evaluate repl-session-key code ns) |
27 | | - (evaluate repl-session-key code))] |
28 | | - (dispatch! [[:app/ax.log :debug "[Server] Evaluating code:" code]]) |
29 | | - (cond-> {:result (.-result evaluation+) |
30 | | - :ns (.-ns evaluation+) |
31 | | - :stdout (.-output evaluation+) |
32 | | - :stderr (.-errorOutput evaluation+) |
33 | | - :session-key (.-replSessionKey evaluation+) |
34 | | - :note "Remember to check the output tool now and then to see what's happening in the application."} |
35 | | - (.-error evaluation+) |
36 | | - (merge {:error (.-error evaluation+) |
37 | | - :stacktrace (.-stacktrace evaluation+)}) |
38 | | - |
39 | | - (not ns) |
40 | | - (merge {:note no-ns-eval-note}) |
41 | | - |
42 | | - (= "" (.-result evaluation+)) |
43 | | - (merge {:note empty-result-note}))) |
44 | | - (p/catch (fn [err] |
45 | | - (dispatch! [[:app/ax.log :debug "[Server] Evaluation failed:" |
46 | | - err]]) |
47 | | - {:result "nil" |
48 | | - :stderr (pr-str err) |
49 | | - :note error-result-note})))] |
50 | | - (clj->js result))) |
| 25 | + (let [inferred (balance/infer-parens code) |
| 26 | + balancded-code (if (:success inferred) |
| 27 | + (:text inferred) |
| 28 | + code) |
| 29 | + balancing-ocurred? (not= code balancded-code)] |
| 30 | + (when balancing-ocurred? |
| 31 | + (dispatch! [[:app/ax.log :debug "[Server] Code was unbalanced:" code "balancded-code:" balancded-code]])) |
| 32 | + (p/let [evaluate (get-in calva/calva-api [:repl :evaluateCode]) |
| 33 | + result (-> (p/let [^js evaluation+ (if ns |
| 34 | + (evaluate repl-session-key balancded-code ns) |
| 35 | + (evaluate repl-session-key balancded-code))] |
| 36 | + (dispatch! [[:app/ax.log :debug "[Server] Evaluating code:" balancded-code]]) |
| 37 | + (cond-> {:result (.-result evaluation+) |
| 38 | + :ns (.-ns evaluation+) |
| 39 | + :stdout (.-output evaluation+) |
| 40 | + :stderr (.-errorOutput evaluation+) |
| 41 | + :session-key (.-replSessionKey evaluation+) |
| 42 | + :note "Remember to check the output tool now and then to see what's happening in the application."} |
| 43 | + |
| 44 | + balancing-ocurred? |
| 45 | + (merge |
| 46 | + {:balancing-note "The code provided for evaluation had unbalanced brackets. The code was automatically balanced before evaluation. Use the code in the `balanced-code` to correct your code on record." |
| 47 | + :balanced-code balancded-code}) |
| 48 | + |
| 49 | + (.-error evaluation+) |
| 50 | + (merge {:error (.-error evaluation+) |
| 51 | + :stacktrace (.-stacktrace evaluation+)}) |
| 52 | + |
| 53 | + (not ns) |
| 54 | + (merge {:note no-ns-eval-note}) |
| 55 | + |
| 56 | + (= "" (.-result evaluation+)) |
| 57 | + (merge {:note empty-result-note}))) |
| 58 | + (p/catch (fn [err] |
| 59 | + (dispatch! [[:app/ax.log :debug "[Server] Evaluation failed:" |
| 60 | + err]]) |
| 61 | + {:result "nil" |
| 62 | + :stderr (pr-str err) |
| 63 | + :note error-result-note})))] |
| 64 | + (clj->js result)))) |
51 | 65 |
|
52 | 66 | (defn get-clojuredocs+ [{:ex/keys [dispatch!] |
53 | 67 | :calva/keys [clojure-symbol]}] |
|
0 commit comments