           (editor/move-cursor ed p))))))
 
 
+(defn cycle-if* [ed]
+  (let [pos (editor/->cursor ed)
+        form (u/get-top-level-form ed pos)
+        zloc (some-> form
+                     :form-str
+                     z/of-string
+                     (z/find-last-by-pos (u/->zipper-pos-start pos form)))]
+
+    (when-let [res (some-> zloc cycle-if z/root-string)]
+      (editor/replace ed (:start form) (:end form) res)
+      (editor/move-cursor ed pos)
+      (u/format-keep-pos ed))))
+
 
 
 (behavior ::cycle-if!
           :triggers #{:refactor.cycle-if!}
           :reaction (fn [ed]
-                      (replace-cmd ed cycle-if :fmt true)))
+                      (cycle-if* ed)))
 
 (behavior ::cycle-col!
           :triggers #{:refactor.cycle-col!}

(ns lt.plugins.cljrefactor.form-refactor
  (:require [lt.plugins.cljrefactor.parser :as p]
            [lt.plugins.cljrefactor.util :as u]
            [lt.object :as object]
            [lt.objs.editor.pool :as pool]
            [lt.objs.editor :as editor]
            [lt.objs.command :as cmd]
            [lt.plugins.paredit :as pe]
            [lt.plugins.clojure :as cp]
            [clojure.zip :as z]
            [clojure.string :as s])
  (:require-macros [lt.macros :refer [behavior]]))


(defn top [zipnode]
  (loop [n zipnode]
    (if-not (z/up n)
      n
      (recur (z/up n)))))

(defn if-node? [loc]
  (when (and (seq loc) (list? (z/node loc)))
    (-> loc z/down z/node  ((fn [s] (some #{s} ['if 'if-not]))))))


(defn swap-if-nodes [loc]
  (let [move (-> loc z/down z/right z/right z/node)]
    (-> loc z/down z/right z/right z/remove top (z/append-child move))))

(defn cycle-if [form-str]
  (let [root (p/str->seq-zip form-str)
        if-node (if-node? root)]
    (when if-node
      (-> root
          swap-if-nodes
          z/down
          (z/replace (if (= if-node 'if) 'if-not 'if))
          p/zip->str))))


(defn cycle-col [form-str]

       (= (.substr form-str 0 2) "#{") (wrap-in "(" ")" 2)
       :else nil))))



(defn replace-cmd [ed replace-fn & {:keys [fmt]}]
