 (behavior ::elm-format-expression
           :triggers #{:elm.format-expression}
           :reaction (fn [ed]
-                      (let [pos (editor/->cursor ed)
-                            cwd (-> @ed :info :path u/project-path)]
-                        (when-let [{:keys [from to]} (sel/get-top-level-expr ed pos)]
-                          (let [[ok? res] (format-input cwd (editor/range ed from to))]
-                            (if ok?
-                              (editor/replace ed from to (strip-module res))
-                              (handle-format-error res))
-                            (editor/move-cursor ed pos))))))
+                      (when (verify-installed)
+                        (let [pos (editor/->cursor ed)
+                              cwd (-> @ed :info :path u/project-path)]
+                          (when-let [{:keys [from to]} (sel/get-top-level-expr ed pos)]
+                            (let [[ok? res] (format-input cwd (editor/range ed from to))]
+                              (if ok?
+                                (editor/replace ed from to (strip-module res))
+                                (handle-format-error res))
+                              (editor/move-cursor ed pos)))))))
 
 (behavior ::elm-format-buffer
           :triggers #{:elm.format-buffer}
           :reaction (fn [ed]
-                      (let [pos (editor/->cursor ed)
-                            [ok? res] (format-input (-> @ed :info :path u/project-path)
-                                                    (editor/->val ed))]
-                        (if ok?
-                          (do
-                            (editor/set-val-and-keep-cursor ed res)
-                            (editor/center-cursor ed))
-                          (handle-format-error res)))))
+                      (when (verify-installed)
+                        (let [pos (editor/->cursor ed)
+                              [ok? res] (format-input (-> @ed :info :path u/project-path)
+                                                      (editor/->val ed))]
+                          (if ok?
+                            (do
+                              (editor/set-val-and-keep-cursor ed res)
+                              (editor/center-cursor ed))
+                            (handle-format-error res))))))
 
 
 

(behavior ::elm-format
          :triggers #{:elm.format}
          :reaction (fn [ed]
                      (when-let [path (-> @ed :info :path)]
                        (let [pos (editor/->cursor ed)
                              [ok? res] (format-path (u/project-path path) path)]
                          (if ok?
                            (pool/reload ed)
                            (handle-format-error res))
                          (editor/move-cursor ed pos)
                          (editor/center-cursor ed)))))

(defn- strip-module [expr]
  (->> (s/split-lines expr)

            [lt.objs.command :as cmd]
            [lt.objs.editor.pool :as pool]
            [lt.objs.editor :as editor]
            [lt.objs.popup :as popup]
            [lt.objs.platform :as platform]
            [clojure.string :as s])
  (:require-macros [lt.macros :refer [behavior]]))



(defn handle-no-elm-format []
  (notifos/done-working)
  (console/log (.-PATH (.-env js/process)))
  (popup/popup! {:header "We couldn't find elm-format in your path"
                 :body (str "In order to formatting related commands, elm-format must be installed and on your system PATH."
                            "(Check the console for PATH as seen from Light Table")
                 :buttons [{:label "Install elm-format"
                            :action (fn []
                                      (platform/open "https://github.com/avh4/elm-format#installation-"))}
                           {:label "ok"}]}))


(defn verify-installed []
  (if (.which clients/shelljs "elm-format")
    true
    (do
      (handle-no-elm-format)
      nil)))



