Skip to content

Commit 0245f6b

Browse files
author
ndr
committed
Refactor post-annotation cursor manipulation
1 parent 7b83260 commit 0245f6b

File tree

4 files changed

+56
-41
lines changed

4 files changed

+56
-41
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.nrepl-port
22
target
33
profiles.clj
4-
scratch.cljs
54
test
65
.lein-repl-history

src/lt/plugins/typedclojure.cljs

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
;;;; aliasing and qualification ;;;;
1515

16-
(def typed-alias "
17-
(if-let [[a typedns] (first (filter #(= (find-ns 'clojure.core.typed)
16+
(def typed-alias
17+
'(if-let [[a typedns] (first (filter #(= (find-ns 'clojure.core.typed)
1818
(val %))
1919
(ns-aliases *ns*)))]
20-
(str a \"/\")
21-
(str \"clojure.core.typed/\"))")
20+
(str a "/")
21+
(str "clojure.core.typed/")))
2222

2323
(defn aliased<- [f]
2424
(str typed-alias " " (pr-str f)))
@@ -39,48 +39,32 @@
3939

4040
;;; raise wrappers
4141

42-
(defn raise* [e s & {:keys [res] :or {res :replace}}]
42+
(defn raise* [e fun-string & {:keys [res] :or {res :replace}}]
4343
(object/raise e
4444
:eval.custom
45-
s
45+
fun-string
4646
{:result-type res :verbatim true}))
4747

48-
(defn raise-ann [e s & [opts]]
48+
(defn raise-ann [e fun-string & [opts]]
4949
(object/raise e
5050
:eval.custom
51-
s
51+
fun-string
5252
(merge {:result-type :return
5353
:handler e
5454
:trigger ::annotate!}
5555
opts)))
5656

5757
;;; return handling
5858

59-
(defn post-ann [e {:keys [offset] :as meta*}]
60-
(let [cursor (ed/->cursor e)]
61-
(if offset
62-
(cursor/move-relative e
63-
cursor
64-
offset)
65-
(ed/set-selection e
66-
(ed/adjust-loc cursor -1)
67-
(ed/adjust-loc cursor -4)))))
68-
6959
(behavior ::annotate!
7060
:triggers #{::annotate!}
7161
:reaction (fn [this {:keys [result meta]}]
7262
(let [returned (reader/read-string result)]
7363
(ed/replace-selection this returned)
74-
(post-ann this meta))))
75-
64+
(cursor/select-relative (merge meta {:editor this})))))
7665

7766
;;; core.typed/ann annotator
7867

79-
(defn ->newline-above! [e c]
80-
(cmd/exec! :typedclojure.pseudoparedit.top)
81-
(ed/insert-at-cursor e "\n")
82-
(ed/move-cursor e c))
83-
8468
(defn ->ann-var [token]
8569
(str "(str \"(\""
8670
(aliased<- "ann")
@@ -104,9 +88,9 @@
10488
(:whitespace token)) (notifos/set-msg! "core.typed/ann can only annotate vars")
10589
(:orphan token) (raise-ann e
10690
(->ann-var nil)
107-
{:offset {:ch -5}})
91+
{:sel [-4 -4]})
10892
:else (do
109-
(->newline-above! e c)
93+
(cursor/->newline-above! e c)
11094
(raise-ann e (->ann-var (:string token)))))))})
11195

11296
;;; core.typed/ann-form annotator
@@ -137,7 +121,7 @@
137121
(raise-ann e (->ann-form (ed/selection e))))
138122
(:orphan token) (raise-ann e
139123
(->ann-form nil)
140-
{:offset {:ch -5}})
124+
{:sel [-4 -4]})
141125
(:selection token) (raise-ann e (->ann-form (:string token)))
142126
:else (let [[start end] (token/bounds e (:at token))]
143127
(ed/set-selection e start end)
@@ -156,24 +140,32 @@
156140
")"))
157141

158142
(cmd/command {:command :typedclojure.pred
159-
:desc "Typed Clojure: add type predicate"
143+
:desc "Typed Clojure: add predicate"
160144
:exec (fn []
161145
(let [e (pool/last-active)
162146
c (ed/->cursor e)
163147
token (if (ed/selection? e)
164148
{:string (ed/selection e) :selection true}
165-
(token/->token e c))]
149+
(token/->token e c))
150+
post-opts {:rel :start
151+
:sel [12 9]}]
166152
(cond
167153
(or (:boundary token)
168154
(:whitespace token)) (do
169155
(ed/move-cursor e (:at token))
170156
(cmd/exec! :paredit.select.parent)
171-
(raise* e (->pred (ed/selection e))))
172-
(:orphan token) (raise* e (->pred))
173-
(:selection token) (raise* e (->pred (:string token)))
157+
(raise-ann e
158+
(->pred (ed/selection e))
159+
post-opts))
160+
(:orphan token) (raise-ann e (->pred) post-opts)
161+
(:selection token) (raise-ann e
162+
(->pred (:string token))
163+
post-opts)
174164
:else (let [[start end] (token/bounds e (:at token))]
175165
(ed/set-selection e start end)
176-
(raise* e (->pred (:string token)))))))})
166+
(raise-ann e
167+
(->pred (:string token))
168+
post-opts)))))})
177169

178170
;;;; checking commands ;;;;
179171

@@ -204,10 +196,7 @@
204196
(cmd/command {:command :typedclojure.check.ns
205197
:desc "Typed Clojure: check namespace"
206198
:exec (fn []
207-
(object/raise (pool/last-active)
208-
:eval.custom
209-
ns-checker
210-
{:result-type :statusbar :verbatim true}))})
199+
(raise* (pool/last-active) ns-checker :res :statusbar))})
211200

212201
;;; core.typed/check-form-info
213202

src/lt/plugins/typedclojure/cursor.cljs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,41 @@
66
[lt.objs.editor :as ed]
77
[lt.plugins.paredit :as par]))
88

9+
10+
(defn post-ann-boundary [editor bound]
11+
(let [loc (ed/adjust-loc (ed/->cursor editor) -1)
12+
[start end] (par/form-boundary editor loc nil)]
13+
(if (= :start bound)
14+
start
15+
end)))
16+
917
(defn move-relative
1018
"Given an editor, a cursor, and a location offset {:line n :ch m}, move
1119
the cursor to a new location."
1220
[e cursor {:keys [line ch] :or [line 0 ch 0] :as offset}]
1321
(let [new-pos (merge-with + cursor offset)]
1422
(ed/move-cursor e new-pos)))
1523

24+
(defn select-relative*
25+
"Given an editor, a location, and a vector of integers [start end], sets
26+
the selection from column (location + start) to column (location + end)"
27+
[e loc [start end]]
28+
(ed/set-selection e
29+
(ed/adjust-loc loc start)
30+
(ed/adjust-loc loc end)))
31+
32+
(defn select-relative [{:keys [editor sel rel] :or {sel [0 -3] rel :end}}]
33+
(let [loc (post-ann-boundary editor rel)]
34+
(select-relative* editor
35+
loc
36+
sel)))
37+
38+
(defn ->newline-above! [e loc]
39+
(cmd/exec! :typedclojure.pseudoparedit.top)
40+
(ed/insert-at-cursor e "\n")
41+
(ed/move-cursor e loc))
42+
43+
1644
;;; To meet the requirements of our factoring functions, we supplement
1745
;;; LT and LT-Paredit with a new command that behaves roughly as
1846
;;; Emacs' beginning-of-defun. This will be removed when functionally

src/lt/plugins/typedclojure/token.cljs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
(ns lt.plugins.typedclojure.token
22
"Token and form detection addenda."
33
(:require [lt.objs.editor :as ed]
4-
[clojure.string :as string]
5-
[lt.objs.editor.pool :as p]))
4+
[clojure.string :as string]))
65

76
(defn ->token [e loc]
87
(let [opening? #(or (= "(" %) (= "[" %) (= "{" %))

0 commit comments

Comments
 (0)