From 643684344764a3223034cd5309a8a86b1040474b Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 10 Jun 2024 12:48:24 +0200 Subject: [PATCH 1/8] wip --- .../nextjournal/clojure_mode/commands.cljc | 50 ++++++++++--------- .../nextjournal/clojure_mode/scratch.cljs | 12 +++++ test/nextjournal/clojure_mode_tests.cljc | 3 +- 3 files changed, 41 insertions(+), 24 deletions(-) create mode 100644 src-shared/nextjournal/clojure_mode/scratch.cljs diff --git a/src-shared/nextjournal/clojure_mode/commands.cljc b/src-shared/nextjournal/clojure_mode/commands.cljc index 3a67d09..7fd2f5d 100644 --- a/src-shared/nextjournal/clojure_mode/commands.cljc +++ b/src-shared/nextjournal/clojure_mode/commands.cljc @@ -139,37 +139,41 @@ (defn slurp [direction] (fn [^js state] (u/update-ranges state - (j/fn [^:js {:as range :keys [from to empty]}] + (j/fn [^:js {:keys [from empty]}] (when empty (when-let [parent (n/closest (n/tree state from) - (every-pred n/coll? + (every-pred (some-fn n/coll? + n/string?) #(not (case direction 1 (some-> % n/with-prefix n/right n/end-edge?) - -1 (some-> % n/with-prefix n/left n/start-edge?)))))] - (when-let [target (case direction 1 (first (remove n/line-comment? (n/rights (n/with-prefix parent)))) - -1 (first (remove n/line-comment? (n/lefts (n/with-prefix parent)))))] - {:cursor/mapped from - :changes (case direction - 1 - (let [edge (n/down-last parent)] - [{:from (-> target n/end) - :insert (n/name edge)} - (-> edge - n/from-to - (j/assoc! :insert " "))]) - -1 - (let [^string edge (n/left-edge-with-prefix state parent) - start (n/start (n/with-prefix parent))] - [{:from start - :to (+ start (count edge)) - :insert " "} - {:from (n/start target) - :insert edge}]))}))))))) + -1 (some-> % n/with-prefix n/left n/start-edge?)))))] + (let [str? (n/string? parent)] + (when-let [target (case direction 1 (first (remove n/line-comment? (n/rights (n/with-prefix parent)))) + -1 (first (remove n/line-comment? (n/lefts (n/with-prefix parent)))))] + (js/console.log :target target) + {:cursor/mapped from + :changes (case direction + 1 + (let [edge (n/down-last parent)] + [{:from (-> target n/end) + :insert (n/name edge)} + (-> edge + n/from-to + (cond-> + (not str?) (j/assoc! :insert " ")))]) + -1 + (let [^string edge (n/left-edge-with-prefix state parent) + start (n/start (n/with-prefix parent))] + [{:from start + :to (+ start (count edge)) + :insert " "} + {:from (n/start target) + :insert edge}]))})))))))) (defn barf [direction] (fn [^js state] - (->> (j/fn [^:js {:as range :keys [from to empty]}] + (->> (j/fn [^:js {:keys [from empty]}] (when empty (when-let [parent (-> (n/tree state from) (n/closest n/coll?))] diff --git a/src-shared/nextjournal/clojure_mode/scratch.cljs b/src-shared/nextjournal/clojure_mode/scratch.cljs new file mode 100644 index 0000000..7e48c2c --- /dev/null +++ b/src-shared/nextjournal/clojure_mode/scratch.cljs @@ -0,0 +1,12 @@ +(ns nextjournal.clojure-mode.scratch + (:require [nextjournal.clojure-mode.commands :as commands] + [nextjournal.clojure-mode :as cm-clojure] + [nextjournal.clojure-mode.extensions.eval-region :as eval-region] + [nextjournal.clojure-mode.test-utils :as test-utils])) + +(def extensions + (.concat cm-clojure/default-extensions (eval-region/extension #js {}))) + +(def apply-f (partial test-utils/apply-f extensions)) + +(js/console.log (apply-f (commands/slurp 1) "\"|\" 1")) diff --git a/test/nextjournal/clojure_mode_tests.cljc b/test/nextjournal/clojure_mode_tests.cljc index aade04c..ac732fe 100644 --- a/test/nextjournal/clojure_mode_tests.cljc +++ b/test/nextjournal/clojure_mode_tests.cljc @@ -272,7 +272,8 @@ "('is-d|ata) :x" 1 "('is-d|ata :x)" "('xy|z 1) 2" 1 "('xy|z 1 2)" "'ab|c 1" 1 "'ab|c 1" - )) + + "\"x|\" 1" 1 "\"x1\"")) #?(:squint nil :cljs From d914bed165704d5ccaa598ef048ab421a3b8eae9 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 10 Jun 2024 15:07:33 +0200 Subject: [PATCH 2/8] Fix #54: slurping within string doesn't work --- src-shared/nextjournal/clojure_mode/commands.cljc | 4 +++- src-shared/nextjournal/clojure_mode/scratch.cljs | 12 ------------ test/nextjournal/clojure_mode_tests.cljc | 4 ++-- 3 files changed, 5 insertions(+), 15 deletions(-) delete mode 100644 src-shared/nextjournal/clojure_mode/scratch.cljs diff --git a/src-shared/nextjournal/clojure_mode/commands.cljc b/src-shared/nextjournal/clojure_mode/commands.cljc index 7fd2f5d..e07bb6a 100644 --- a/src-shared/nextjournal/clojure_mode/commands.cljc +++ b/src-shared/nextjournal/clojure_mode/commands.cljc @@ -136,6 +136,9 @@ (def log js/console.log) +(defn ->str [x] + (js/JSON.stringify (str x))) + (defn slurp [direction] (fn [^js state] (u/update-ranges state @@ -151,7 +154,6 @@ (let [str? (n/string? parent)] (when-let [target (case direction 1 (first (remove n/line-comment? (n/rights (n/with-prefix parent)))) -1 (first (remove n/line-comment? (n/lefts (n/with-prefix parent)))))] - (js/console.log :target target) {:cursor/mapped from :changes (case direction 1 diff --git a/src-shared/nextjournal/clojure_mode/scratch.cljs b/src-shared/nextjournal/clojure_mode/scratch.cljs deleted file mode 100644 index 7e48c2c..0000000 --- a/src-shared/nextjournal/clojure_mode/scratch.cljs +++ /dev/null @@ -1,12 +0,0 @@ -(ns nextjournal.clojure-mode.scratch - (:require [nextjournal.clojure-mode.commands :as commands] - [nextjournal.clojure-mode :as cm-clojure] - [nextjournal.clojure-mode.extensions.eval-region :as eval-region] - [nextjournal.clojure-mode.test-utils :as test-utils])) - -(def extensions - (.concat cm-clojure/default-extensions (eval-region/extension #js {}))) - -(def apply-f (partial test-utils/apply-f extensions)) - -(js/console.log (apply-f (commands/slurp 1) "\"|\" 1")) diff --git a/test/nextjournal/clojure_mode_tests.cljc b/test/nextjournal/clojure_mode_tests.cljc index ac732fe..7113f1f 100644 --- a/test/nextjournal/clojure_mode_tests.cljc +++ b/test/nextjournal/clojure_mode_tests.cljc @@ -249,7 +249,7 @@ "(" "<(a) b>" )) - (deftest slurp + (deftest slurp-test (are [input dir expected] (= (apply-f (commands/slurp dir) input) expected) "(|) a" 1 "(|a)" @@ -273,7 +273,7 @@ "('xy|z 1) 2" 1 "('xy|z 1 2)" "'ab|c 1" 1 "'ab|c 1" - "\"x|\" 1" 1 "\"x1\"")) + "\"x|\" 1" 1 "\"x| 1\"")) #?(:squint nil :cljs From 91954ddd7669632e5b3d9afb99e596d9bf637431 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 10 Jun 2024 15:07:49 +0200 Subject: [PATCH 3/8] scratch file --- test/nextjournal/scratch.cljs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 test/nextjournal/scratch.cljs diff --git a/test/nextjournal/scratch.cljs b/test/nextjournal/scratch.cljs new file mode 100644 index 0000000..def770a --- /dev/null +++ b/test/nextjournal/scratch.cljs @@ -0,0 +1,14 @@ +(ns nextjournal.clojure-mode.scratch + (:require [nextjournal.clojure-mode.commands :as commands] + [nextjournal.clojure-mode :as cm-clojure] + [nextjournal.clojure-mode.extensions.eval-region :as eval-region] + [nextjournal.clojure-mode.test-utils :as test-utils])) + +(def extensions + (.concat cm-clojure/default-extensions (eval-region/extension #js {}))) + +(def apply-f (partial test-utils/apply-f extensions)) + +(js/console.log "\"|\" 1") +(js/console.log (apply-f (commands/slurp 1) "\"|\" 1")) + From c5790669a755a2ccb8788504a06420fd4e2d0572 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 10 Jun 2024 15:11:06 +0200 Subject: [PATCH 4/8] slurp backwards --- src-shared/nextjournal/clojure_mode/commands.cljc | 6 +++--- test/nextjournal/clojure_mode_tests.cljc | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src-shared/nextjournal/clojure_mode/commands.cljc b/src-shared/nextjournal/clojure_mode/commands.cljc index e07bb6a..dc41f85 100644 --- a/src-shared/nextjournal/clojure_mode/commands.cljc +++ b/src-shared/nextjournal/clojure_mode/commands.cljc @@ -167,9 +167,9 @@ -1 (let [^string edge (n/left-edge-with-prefix state parent) start (n/start (n/with-prefix parent))] - [{:from start - :to (+ start (count edge)) - :insert " "} + [(cond-> {:from start + :to (+ start (count edge))} + (not str?) (j/assoc! :insert " ")) {:from (n/start target) :insert edge}]))})))))))) diff --git a/test/nextjournal/clojure_mode_tests.cljc b/test/nextjournal/clojure_mode_tests.cljc index 7113f1f..0909b3c 100644 --- a/test/nextjournal/clojure_mode_tests.cljc +++ b/test/nextjournal/clojure_mode_tests.cljc @@ -273,7 +273,8 @@ "('xy|z 1) 2" 1 "('xy|z 1 2)" "'ab|c 1" 1 "'ab|c 1" - "\"x|\" 1" 1 "\"x| 1\"")) + "\"x|\" 1" 1 "\"x| 1\"" + "1 \"x|\"" -1 "\"1 x|\"")) #?(:squint nil :cljs From db329b3c512f69ddf9c32023c1ed7af9956c6b7d Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 10 Jun 2024 15:12:04 +0200 Subject: [PATCH 5/8] changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0826d18..5d1e72f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.3.3 + +- Fix [#54](https://github.com/nextjournal/clojure-mode/issues/54): support slurping from within string literal + ## 0.3.2 - Bump squint to 0.7.105 From 4e5f8dd945190d900190642c953a83810741d0df Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 10 Jun 2024 17:39:45 +0200 Subject: [PATCH 6/8] wipper --- src-shared/nextjournal/clojure_mode/commands.cljc | 6 ++++-- test/nextjournal/clojure_mode_tests.cljc | 3 +++ test/nextjournal/scratch.cljs | 6 +++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src-shared/nextjournal/clojure_mode/commands.cljc b/src-shared/nextjournal/clojure_mode/commands.cljc index dc41f85..08cc439 100644 --- a/src-shared/nextjournal/clojure_mode/commands.cljc +++ b/src-shared/nextjournal/clojure_mode/commands.cljc @@ -152,6 +152,7 @@ 1 (some-> % n/with-prefix n/right n/end-edge?) -1 (some-> % n/with-prefix n/left n/start-edge?)))))] (let [str? (n/string? parent)] + (prn :parent (n/string state parent) :str str?) (when-let [target (case direction 1 (first (remove n/line-comment? (n/rights (n/with-prefix parent)))) -1 (first (remove n/line-comment? (n/lefts (n/with-prefix parent)))))] {:cursor/mapped from @@ -163,12 +164,13 @@ (-> edge n/from-to (cond-> - (not str?) (j/assoc! :insert " ")))]) + true #_(not str?) (j/assoc! :insert " ")))]) -1 (let [^string edge (n/left-edge-with-prefix state parent) start (n/start (n/with-prefix parent))] [(cond-> {:from start - :to (+ start (count edge))} + :to (+ start (count edge)) + #_#_:insert " "} (not str?) (j/assoc! :insert " ")) {:from (n/start target) :insert edge}]))})))))))) diff --git a/test/nextjournal/clojure_mode_tests.cljc b/test/nextjournal/clojure_mode_tests.cljc index 0909b3c..35fe066 100644 --- a/test/nextjournal/clojure_mode_tests.cljc +++ b/test/nextjournal/clojure_mode_tests.cljc @@ -8,11 +8,14 @@ [nextjournal.clojure-mode.commands :as commands] [nextjournal.clojure-mode.extensions.formatting :as format] [nextjournal.clojure-mode.extensions.eval-region :as eval-region] + [nextjournal.scratch] #?@(:squint [] :cljs [[nextjournal.livedoc :as livedoc]]) #?(:squint ["assert" :as assert])) #?(:squint (:require-macros [nextjournal.clojure-mode-tests.macros :refer [deftest are testing is]]))) +#_(js/process.exit 0) + (def extensions (.concat cm-clojure/default-extensions (eval-region/extension #js {})) ;; optionally test with live grammar diff --git a/test/nextjournal/scratch.cljs b/test/nextjournal/scratch.cljs index def770a..52c0e8c 100644 --- a/test/nextjournal/scratch.cljs +++ b/test/nextjournal/scratch.cljs @@ -1,4 +1,4 @@ -(ns nextjournal.clojure-mode.scratch +(ns nextjournal.scratch (:require [nextjournal.clojure-mode.commands :as commands] [nextjournal.clojure-mode :as cm-clojure] [nextjournal.clojure-mode.extensions.eval-region :as eval-region] @@ -9,6 +9,6 @@ (def apply-f (partial test-utils/apply-f extensions)) -(js/console.log "\"|\" 1") -(js/console.log (apply-f (commands/slurp 1) "\"|\" 1")) +(js/console.log "a ;; hello\n(|)") +(js/console.log (apply-f (commands/slurp -1) "a ;; hello\n(|)")) From 71553eec284d304f1a50815f64afedc79c144260 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 10 Jun 2024 20:44:25 +0200 Subject: [PATCH 7/8] wip --- src-shared/nextjournal/clojure_mode/commands.cljc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src-shared/nextjournal/clojure_mode/commands.cljc b/src-shared/nextjournal/clojure_mode/commands.cljc index 08cc439..9f547c9 100644 --- a/src-shared/nextjournal/clojure_mode/commands.cljc +++ b/src-shared/nextjournal/clojure_mode/commands.cljc @@ -152,7 +152,6 @@ 1 (some-> % n/with-prefix n/right n/end-edge?) -1 (some-> % n/with-prefix n/left n/start-edge?)))))] (let [str? (n/string? parent)] - (prn :parent (n/string state parent) :str str?) (when-let [target (case direction 1 (first (remove n/line-comment? (n/rights (n/with-prefix parent)))) -1 (first (remove n/line-comment? (n/lefts (n/with-prefix parent)))))] {:cursor/mapped from @@ -164,10 +163,11 @@ (-> edge n/from-to (cond-> - true #_(not str?) (j/assoc! :insert " ")))]) + (not str?) (j/assoc! :insert " ")))]) -1 (let [^string edge (n/left-edge-with-prefix state parent) start (n/start (n/with-prefix parent))] + (prn :parent (n/string state parent) :str str?) [(cond-> {:from start :to (+ start (count edge)) #_#_:insert " "} From b4105f44791b1078231fe92b6166d574166cf9b8 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 17 Jun 2024 12:01:25 +0200 Subject: [PATCH 8/8] Fix slurp string --- package.json | 2 +- .../nextjournal/clojure_mode/commands.cljc | 20 +++++++++---------- test/nextjournal/scratch.cljs | 11 +++++----- yarn.lock | 8 ++++---- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index a9a112b..8cf7e13 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "@lezer/highlight": "^1.0.0", "@lezer/lr": "^1.0.0", "@nextjournal/lezer-clojure": "1.0.0", - "squint-cljs": "0.7.105", + "squint-cljs": "0.7.111", "w3c-keyname": "^2.2.4" }, "comments": { diff --git a/src-shared/nextjournal/clojure_mode/commands.cljc b/src-shared/nextjournal/clojure_mode/commands.cljc index 9f547c9..b90ead8 100644 --- a/src-shared/nextjournal/clojure_mode/commands.cljc +++ b/src-shared/nextjournal/clojure_mode/commands.cljc @@ -112,7 +112,7 @@ (defn nav [dir] (fn [state] (u/update-ranges state - (j/fn [^:js {:as range :keys [from to empty]}] + (j/fn [^:js {:keys [from to empty]}] (if empty {:cursor (nav-position state from dir)} {:cursor (j/get (u/from-to from to) (case dir -1 :from 1 :to))}))))) @@ -120,7 +120,7 @@ (defn nav-select [dir] (fn [^js state] (u/update-ranges state - (j/fn [^:js {:as range :keys [from to empty]}] + (j/fn [^:js {:keys [from to empty]}] (if empty {:range (n/balanced-range state from (nav-position state from dir))} {:range (j/let [^:js {:keys [from to]} (u/from-to from to)] @@ -158,8 +158,8 @@ :changes (case direction 1 (let [edge (n/down-last parent)] - [{:from (-> target n/end) - :insert (n/name edge)} + #js [#js {:from (-> target n/end) + :insert (n/name edge)} (-> edge n/from-to (cond-> @@ -167,13 +167,11 @@ -1 (let [^string edge (n/left-edge-with-prefix state parent) start (n/start (n/with-prefix parent))] - (prn :parent (n/string state parent) :str str?) - [(cond-> {:from start - :to (+ start (count edge)) - #_#_:insert " "} - (not str?) (j/assoc! :insert " ")) - {:from (n/start target) - :insert edge}]))})))))))) + #js [(cond-> #js {:from start + :to (+ start (count edge))} + (not str?) (j/assoc! :insert " ")) + #js {:from (n/start target) + :insert edge}]))})))))))) (defn barf [direction] (fn [^js state] diff --git a/test/nextjournal/scratch.cljs b/test/nextjournal/scratch.cljs index 52c0e8c..85156e2 100644 --- a/test/nextjournal/scratch.cljs +++ b/test/nextjournal/scratch.cljs @@ -4,11 +4,12 @@ [nextjournal.clojure-mode.extensions.eval-region :as eval-region] [nextjournal.clojure-mode.test-utils :as test-utils])) -(def extensions - (.concat cm-clojure/default-extensions (eval-region/extension #js {}))) +(comment + (def extensions + (.concat cm-clojure/default-extensions (eval-region/extension #js {}))) -(def apply-f (partial test-utils/apply-f extensions)) + (def apply-f (partial test-utils/apply-f extensions)) -(js/console.log "a ;; hello\n(|)") -(js/console.log (apply-f (commands/slurp -1) "a ;; hello\n(|)")) + (js/console.log "a ;; hello\n(|)") + (js/console.log (apply-f (commands/slurp -1) "a ;; hello\n(|)"))) diff --git a/yarn.lock b/yarn.lock index 959858c..edfa917 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1176,10 +1176,10 @@ source-map@^0.5.6: resolved "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz" integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w= -squint-cljs@0.7.105: - version "0.7.105" - resolved "https://registry.yarnpkg.com/squint-cljs/-/squint-cljs-0.7.105.tgz#848a588aaf5d19593b2d8b24bad026382435a4ff" - integrity sha512-YtnPewo1ZM5p9kaC6j/rNw4F/FzsLaAS61Vhikw6z6dRJvns/Y/3rwGcb8BCoD6Abx23/frlk0B4/xhedgCbUw== +squint-cljs@0.7.111: + version "0.7.111" + resolved "https://registry.yarnpkg.com/squint-cljs/-/squint-cljs-0.7.111.tgz#1441933ed7162d5590570875394c12b181db9427" + integrity sha512-BZSq3OxqdRN1xpIYBOWDI03tppaOLxtmapUDsS32ViKg2anu6fo44qh3qGAvXcapDwc1mkIS6A2y2GA+SgB2Rw== dependencies: chokidar "^3.5.3"