Skip to content

Commit a0435a0

Browse files
author
Case Nelson
committed
try to move cursor back to original position
1 parent b14d5af commit a0435a0

File tree

6 files changed

+138
-57
lines changed

6 files changed

+138
-57
lines changed

Diff for: rplugin/node/clj-refactor.js

+46-40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/clj_refactor/edit.cljs

+36
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
(ns clj-refactor.edit
22
(:require
3+
[clojure.string :as string]
4+
[clojure.zip :as cz]
35
[rewrite-clj.node :as n]
46
[rewrite-clj.node.forms :as nf]
7+
[rewrite-clj.node.protocols :as np]
58
[rewrite-clj.paredit :as p]
69
[rewrite-clj.zip :as z]
710
[rewrite-clj.zip.utils :as zu]
@@ -20,6 +23,9 @@
2023
(take-while p?)
2124
last))
2225

26+
(defn to-root [loc]
27+
(exec-to loc z/up #(not (top? %))))
28+
2329
(defn parent-let? [zloc]
2430
(= 'let (-> zloc z/up z/leftmost z/sexpr)))
2531

@@ -112,3 +118,33 @@
112118
z/rightmost
113119
z/down
114120
z/down)))
121+
122+
(defn remove-all-after
123+
[zloc]
124+
(loop [zloc (zu/remove-right-while zloc (constantly true))]
125+
(if-let [uploc (z/up zloc)]
126+
(recur (zu/remove-right-while uploc (constantly true)))
127+
zloc)))
128+
129+
(defn read-position
130+
[old-pos zloc]
131+
(-> zloc
132+
(zdbg "read-position")
133+
(remove-all-after)
134+
(z/root-string)
135+
(z/of-string)
136+
(z/rightmost)
137+
(z/find-next-depth-first (comp z/end? z/next))
138+
(z/node)
139+
(meta)
140+
((juxt :row :col))))
141+
142+
(defn mark-position
143+
[zloc marker]
144+
(z/replace zloc (assoc (z/node zloc) ::marker marker)))
145+
146+
(defn find-mark
147+
[zloc marker]
148+
(if-let [mloc (z/find (to-root zloc) z/next #(= marker (get (z/node %) ::marker)))]
149+
mloc
150+
zloc))

Diff for: src/clj_refactor/main.cljs

+23-4
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,35 @@
3535

3636
(declare run-transform)
3737

38+
(defn swap-position!
39+
[zloc cursor-ref]
40+
(swap! cursor-ref edit/read-position zloc)
41+
zloc)
42+
3843
(defn zip-it
3944
"Finds the loc at row col of the file and runs the transformer-fn."
4045
[transformer lines row col args]
4146
(try
42-
(let [sexpr (string/join "\n" lines)
47+
(let [new-cursor (atom [row col])
48+
sexpr (string/join "\n" lines)
4349
pos {:row row :col col :end-row row :end-col col}
4450
new-sexpr (-> sexpr
4551
(z/of-string)
4652
(z/find-last-by-pos pos #(not= (z/tag %) :whitespace))
53+
(edit/mark-position :new-cursor)
4754
(transformer args)
55+
(edit/find-mark :new-cursor)
56+
(swap-position! new-cursor)
57+
(edit/zdbg (pr-str @new-cursor))
4858
;; TODO should check if anything has changed
4959
;; - should return nil if transformer returned nil
5060
(z/root-string)
5161
(parinfer/parenMode)
5262
(aget "text"))]
53-
(split-lines new-sexpr))
63+
(let [[row col] @new-cursor]
64+
{:row row
65+
:col col
66+
:new-lines (split-lines new-sexpr)}))
5467
(catch :default e
5568
(jdbg "zip-it" e (.-stack e))
5669
(throw e))))
@@ -62,8 +75,14 @@
6275
(fn [err buf]
6376
(.getLineSlice buf 0 -1 true true
6477
(fn [err lines]
65-
(when-let [new-lines (clj->js (zip-it transformer (js->clj lines) row col (concat args static-args)))]
66-
(.setLineSlice buf 0 -1 true true new-lines))))))
78+
(when-let [{:keys [row col new-lines]} (zip-it transformer (js->clj lines) row col (concat args static-args))]
79+
(jdbg "saving" row col)
80+
(try
81+
(.setLineSlice buf 0 -1 true true (clj->js new-lines)
82+
(fn [err]
83+
(.command nvim (str "call cursor("row "," col")"))))
84+
(catch :default e
85+
(jdbg "save" e (.-stack e)))))))))
6786
(catch :default e
6887
(jdbg "run-transform" e))))
6988

Diff for: src/clj_refactor/transform.cljs

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
(let [node (z/sexpr zloc)]
8989
(if (symbol? node)
9090
(-> zloc
91-
(edit/exec-to z/up #(not (edit/top? %))) ; Go to top level form
91+
(edit/to-root)
9292
(z/insert-left (list 'declare node)) ; add declare
9393
(z/insert-left (n/newline-node "\n\n"))) ; add new line after location
9494
zloc)))
@@ -262,7 +262,7 @@
262262
arg
263263
(symbol (str "arg" (inc i)))))]
264264
(-> example-loc
265-
(edit/exec-to z/up #(not (edit/top? %))) ; Go to top level form
265+
(edit/to-root)
266266
(z/insert-left `(~'defn ~fn-name [~@args])) ; add declare
267267
(z/insert-left (n/newline-node "\n\n"))))) ; add new line after location
268268

@@ -274,7 +274,7 @@
274274
used-syms (map symbol used-locals)]
275275
(-> expr-loc
276276
(z/replace `(~fn-sym ~@used-syms))
277-
(edit/exec-to z/up #(not (edit/top? %))) ; Go to top level form
277+
(edit/to-root)
278278
(z/insert-left `(~'defn ~fn-sym [~@used-syms] ~expr))
279279
(z/insert-left (n/newline-node "\n\n"))
280280
(z/left)

Diff for: test/clj_refactor/edit_test.cljs

+20-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
(:require
33
[rewrite-clj.paredit :as p]
44
[rewrite-clj.zip :as z]
5+
[rewrite-clj.zip.whitespace :as zw]
56
[cljs.nodejs :as nodejs]
67
[cljs.test :refer-macros [deftest is testing run-tests are]]
78
[clojure.string :as str]
89
[clj-refactor.edit :as e]
9-
[clj-refactor.test-helper :refer [apply-zip apply-zip-to]]))
10+
[clj-refactor.test-helper :refer [str-zip-to zip-to apply-zip apply-zip-to]]))
1011

1112
(deftest test-remove-right
1213
(are [i j] (= i j)
@@ -61,3 +62,21 @@
6162
'(x [a c b] y z) (apply-zip '(x [a b c] y z) 'b e/transpose-with-right)
6263

6364
'(x y [a b c] z) (apply-zip '(x [a b c] y z) '[a b c] e/transpose-with-right)))
65+
66+
(deftest test-marking
67+
(let [zloc (str-zip-to "(a (b c))\n(x (y z))" 'z identity)]
68+
(is (= 'z
69+
(-> zloc
70+
(e/mark-position ::marked)
71+
(z/up)
72+
(z/up)
73+
(z/insert-left '(x y))
74+
(z/left)
75+
(e/find-mark ::marked)
76+
(z/sexpr))))))
77+
78+
(deftest test-read-position
79+
(is (= [1 5]
80+
(str-zip-to "(a (b c))\n(x (y z))" 'b (partial e/read-position [55 44]))))
81+
(is (= [2 2]
82+
(str-zip-to "(a (b c))\n(x (y z))" 'x (partial e/read-position [55 44])))))

Diff for: test/clj_refactor/test_helper.cljs

+10-9
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,32 @@
33
[cljs.reader :as r]
44
[rewrite-clj.zip :as z]))
55

6-
(defn zip-to
7-
[form goto f]
8-
(-> form
9-
(str)
6+
(defn str-zip-to
7+
[form-str goto f]
8+
(-> form-str
109
(z/of-string)
1110
(z/find z/next #(= (z/sexpr %) goto))
1211
(f)))
1312

14-
(defn apply-goto
13+
(defn zip-to
1514
[form goto f]
16-
(zip-to form goto f))
15+
(-> form
16+
(str)
17+
(str-zip-to goto f)))
1718

1819
(defn apply-zip-to
1920
[form goto f]
20-
(z/sexpr (apply-goto form goto f)))
21+
(z/sexpr (zip-to form goto f)))
2122

2223
(defn apply-zip
2324
[form goto f]
24-
(-> (apply-goto form goto f)
25+
(-> (zip-to form goto f)
2526
(z/root-string)
2627
(r/read-string)))
2728

2829
(defn apply-zip-root
2930
[form goto f]
3031
(r/read-string
3132
(str "(do "
32-
(z/root-string (apply-goto form goto f))
33+
(z/root-string (zip-to form goto f))
3334
")")))

0 commit comments

Comments
 (0)