clj-commons / rewrite-clj

Rewrite Clojure code and edn
https://cljdoc.org/d/rewrite-clj
MIT License
591 stars 55 forks source link

Error on assert for syntax quoted elements during slurp or barf operations #317

Open openvest opened 1 month ago

openvest commented 1 month ago

Version version 1.1.48

Symptom Cannot slurp or barf, forward or backward in a syntax quoted element | indicating the cursor, see:

[:left '[foo| bar]  :right]

Related is trying to slurp forward from:

@(def | x) 42

Actual behavior assertion error is thrown

Diagnosis assertion error is inside QuoteNode creation

Action I know that there has been discussion in a separate thread about asserts; but here it looks to be harmful to the cause.

BTW: Thanks for the great library. It is adding paredit functionality to repl-balance (an update of rebel-readine)

lread commented 1 month ago

Thanks for the issue @openvest, I'm happy to see that folks are starting to use the paredit API more.

Here's my repro of your issue:

(require '[rewrite-clj.paredit :as par]
         '[rewrite-clj.zip :as z])

;; navigate to foo and slurp forward
(def zloc (-> "[:left '[foo bar] :right]"
              z/of-string
              z/down
              z/right
              z/down
              z/down))

(z/string zloc)
;; => "foo"

(par/slurp-forward zloc)
;; => Execution error (AssertionError) at rewrite-clj.node.protocols/assert-sexpr-count (protocols.cljc:177).
;;    Assert failed: can only contain 1 non-whitespace form.
;;    (= (count (without-whitespace nodes)) c)

Very good, I can reproduce!

Let's retry without a quoted node to see what happens:

;; repeat with non-quoted
;; navigate to foo
(def zloc (-> "[:left [foo bar] :right]"
              z/of-string
              z/down
              z/right
              z/down))

(z/string zloc)
;; => "foo"

(-> zloc 
    par/slurp-forward
    z/root-string)
;; => "[:left [foo bar :right]]"

Good, no probs without quoted node.

I had launched into reviewing the paredit API with various fixes, but got distracted by other projects. This one is new to me, so thanks for sharing!