Skip to content

Commit 2fa1d0d

Browse files
authored
Fix #54: support slurping from within string (#55)
1 parent 3df5e83 commit 2fa1d0d

File tree

6 files changed

+62
-32
lines changed

6 files changed

+62
-32
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.3.3
4+
5+
- Fix [#54](https://github.com/nextjournal/clojure-mode/issues/54): support slurping from within string literal
6+
37
## 0.3.2
48

59
- Bump squint to 0.7.105

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"@lezer/highlight": "^1.0.0",
2424
"@lezer/lr": "^1.0.0",
2525
"@nextjournal/lezer-clojure": "1.0.0",
26-
"squint-cljs": "0.7.105",
26+
"squint-cljs": "0.7.111",
2727
"w3c-keyname": "^2.2.4"
2828
},
2929
"comments": {

src-shared/nextjournal/clojure_mode/commands.cljc

+31-25
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@
112112
(defn nav [dir]
113113
(fn [state]
114114
(u/update-ranges state
115-
(j/fn [^:js {:as range :keys [from to empty]}]
115+
(j/fn [^:js {:keys [from to empty]}]
116116
(if empty
117117
{:cursor (nav-position state from dir)}
118118
{:cursor (j/get (u/from-to from to) (case dir -1 :from 1 :to))})))))
119119

120120
(defn nav-select [dir]
121121
(fn [^js state]
122122
(u/update-ranges state
123-
(j/fn [^:js {:as range :keys [from to empty]}]
123+
(j/fn [^:js {:keys [from to empty]}]
124124
(if empty
125125
{:range (n/balanced-range state from (nav-position state from dir))}
126126
{:range (j/let [^:js {:keys [from to]} (u/from-to from to)]
@@ -136,40 +136,46 @@
136136

137137
(def log js/console.log)
138138

139+
(defn ->str [x]
140+
(js/JSON.stringify (str x)))
141+
139142
(defn slurp [direction]
140143
(fn [^js state]
141144
(u/update-ranges state
142-
(j/fn [^:js {:as range :keys [from to empty]}]
145+
(j/fn [^:js {:keys [from empty]}]
143146
(when empty
144147
(when-let [parent (n/closest (n/tree state from)
145-
(every-pred n/coll?
148+
(every-pred (some-fn n/coll?
149+
n/string?)
146150
#(not
147151
(case direction
148152
1 (some-> % n/with-prefix n/right n/end-edge?)
149-
-1 (some-> % n/with-prefix n/left n/start-edge?)))))]
150-
(when-let [target (case direction 1 (first (remove n/line-comment? (n/rights (n/with-prefix parent))))
151-
-1 (first (remove n/line-comment? (n/lefts (n/with-prefix parent)))))]
152-
{:cursor/mapped from
153-
:changes (case direction
154-
1
155-
(let [edge (n/down-last parent)]
156-
[{:from (-> target n/end)
157-
:insert (n/name edge)}
158-
(-> edge
159-
n/from-to
160-
(j/assoc! :insert " "))])
161-
-1
162-
(let [^string edge (n/left-edge-with-prefix state parent)
163-
start (n/start (n/with-prefix parent))]
164-
[{:from start
165-
:to (+ start (count edge))
166-
:insert " "}
167-
{:from (n/start target)
168-
:insert edge}]))})))))))
153+
-1 (some-> % n/with-prefix n/left n/start-edge?)))))]
154+
(let [str? (n/string? parent)]
155+
(when-let [target (case direction 1 (first (remove n/line-comment? (n/rights (n/with-prefix parent))))
156+
-1 (first (remove n/line-comment? (n/lefts (n/with-prefix parent)))))]
157+
{:cursor/mapped from
158+
:changes (case direction
159+
1
160+
(let [edge (n/down-last parent)]
161+
#js [#js {:from (-> target n/end)
162+
:insert (n/name edge)}
163+
(-> edge
164+
n/from-to
165+
(cond->
166+
(not str?) (j/assoc! :insert " ")))])
167+
-1
168+
(let [^string edge (n/left-edge-with-prefix state parent)
169+
start (n/start (n/with-prefix parent))]
170+
#js [(cond-> #js {:from start
171+
:to (+ start (count edge))}
172+
(not str?) (j/assoc! :insert " "))
173+
#js {:from (n/start target)
174+
:insert edge}]))}))))))))
169175

170176
(defn barf [direction]
171177
(fn [^js state]
172-
(->> (j/fn [^:js {:as range :keys [from to empty]}]
178+
(->> (j/fn [^:js {:keys [from empty]}]
173179
(when empty
174180
(when-let [parent (-> (n/tree state from)
175181
(n/closest n/coll?))]

test/nextjournal/clojure_mode_tests.cljc

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
[nextjournal.clojure-mode.commands :as commands]
99
[nextjournal.clojure-mode.extensions.formatting :as format]
1010
[nextjournal.clojure-mode.extensions.eval-region :as eval-region]
11+
[nextjournal.scratch]
1112
#?@(:squint []
1213
:cljs [[nextjournal.livedoc :as livedoc]])
1314
#?(:squint ["assert" :as assert]))
1415
#?(:squint (:require-macros [nextjournal.clojure-mode-tests.macros :refer [deftest are testing is]])))
1516

17+
#_(js/process.exit 0)
18+
1619
(def extensions
1720
(.concat cm-clojure/default-extensions (eval-region/extension #js {}))
1821
;; optionally test with live grammar
@@ -249,7 +252,7 @@
249252
"(<a) b>" "<(a) b>"
250253
))
251254

252-
(deftest slurp
255+
(deftest slurp-test
253256
(are [input dir expected]
254257
(= (apply-f (commands/slurp dir) input) expected)
255258
"(|) a" 1 "(|a)"
@@ -272,7 +275,9 @@
272275
"('is-d|ata) :x" 1 "('is-d|ata :x)"
273276
"('xy|z 1) 2" 1 "('xy|z 1 2)"
274277
"'ab|c 1" 1 "'ab|c 1"
275-
))
278+
279+
"\"x|\" 1" 1 "\"x| 1\""
280+
"1 \"x|\"" -1 "\"1 x|\""))
276281

277282
#?(:squint nil
278283
:cljs

test/nextjournal/scratch.cljs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(ns nextjournal.scratch
2+
(:require [nextjournal.clojure-mode.commands :as commands]
3+
[nextjournal.clojure-mode :as cm-clojure]
4+
[nextjournal.clojure-mode.extensions.eval-region :as eval-region]
5+
[nextjournal.clojure-mode.test-utils :as test-utils]))
6+
7+
(comment
8+
(def extensions
9+
(.concat cm-clojure/default-extensions (eval-region/extension #js {})))
10+
11+
(def apply-f (partial test-utils/apply-f extensions))
12+
13+
(js/console.log "a ;; hello\n(|)")
14+
(js/console.log (apply-f (commands/slurp -1) "a ;; hello\n(|)")))
15+

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -1176,10 +1176,10 @@ source-map@^0.5.6:
11761176
resolved "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz"
11771177
integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=
11781178

1179-
1180-
version "0.7.105"
1181-
resolved "https://registry.yarnpkg.com/squint-cljs/-/squint-cljs-0.7.105.tgz#848a588aaf5d19593b2d8b24bad026382435a4ff"
1182-
integrity sha512-YtnPewo1ZM5p9kaC6j/rNw4F/FzsLaAS61Vhikw6z6dRJvns/Y/3rwGcb8BCoD6Abx23/frlk0B4/xhedgCbUw==
1179+
1180+
version "0.7.111"
1181+
resolved "https://registry.yarnpkg.com/squint-cljs/-/squint-cljs-0.7.111.tgz#1441933ed7162d5590570875394c12b181db9427"
1182+
integrity sha512-BZSq3OxqdRN1xpIYBOWDI03tppaOLxtmapUDsS32ViKg2anu6fo44qh3qGAvXcapDwc1mkIS6A2y2GA+SgB2Rw==
11831183
dependencies:
11841184
chokidar "^3.5.3"
11851185

0 commit comments

Comments
 (0)