-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
paredit does not handle "decorated" nodes #317
Comments
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! |
Ah. Ok. Interesting. The paredit slurp (and other paredit fns) code assumes that if a node can have children, we can directly add a child node. But in the case of a quote node (and others I'm sure), this is not correct. A quote node is, more abstractly, a node that can have a single child but also optionally have whitespace. In the case above, the single child is the vector. And it is the vector we want to be slurping into, but paredit is trying to slurp into the quote node. Exploring in the REPL: We can add whitespace to a quote node: (-> "'[foo bar]"
z/of-string
(z/insert-child (n/spaces 1))
((juxt z/tag z/string z/root-string)))
;; => [:quote "' [foo bar]" "' [foo bar]"] But we cannot add a non-whitespace node: (-> "'[foo bar]"
z/of-string
(z/insert-child :boop))
;; => 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) But we can add a non-whitespace element to the quoted vector (-> "'[foo bar]"
z/of-string
z/down
(z/insert-child :boop)
((juxt z/tag z/string z/root-string)))
;; => [:vector "[:boop foo bar]" "'[:boop foo bar]"] I'll see how I can adapt paredit to handle these types of nodes. |
I'm thinking through this. Some interesting cases:Should slurp
These should all slurp
When slurping into the current node these should all slurp
Barfing should barf
Other NodesThis, of course, is not limited to quote We have:
To think about (I think these all fall under decorators too, but the decoration includes user specified content?):
And
Other paredit operationsThis issue is not limited to slurping and barfing. We'll have to evaluate each paredit fn in the context of decorator nodes.
|
Known LimitationsRewrite-clj enforces, in some cases, its idea of valid clojure. Discard (uneval) nodeA slurp of But what about Soo... for now, no-op any paredit ops on uneval nodes that would result in invalid nodes. Metadata nodeA slurp But... if slurping from a metadata map, things stop making sense to rewrite-clj. So... for now, no-op on paredit ops on a metadata map that would result in invalid nodes. ??I'll add more as I discover them. |
I may revisit my algorithm for updating the location in the zipper after a paredit operation. Rewrite-cljs used an internal When fixing paredit issue, I decided to track navigation so that it could later be restored. I felt searching through the entire zipper for a location after a paredit operation was a bit much and tracking/restoring was relatively straightforward. But... while looking at this issue, I'm finding that tracking navigation is adding considerable complexity to the paredit code. I may (and may not, not sure yet) revert to finding the location in the zipper after an op. The difference from |
Version
version 1.1.48
Symptom
Cannot slurp or barf, forward or backward in a syntax quoted element
|
indicating the cursor, see:Related is trying to slurp forward from:
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)
The text was updated successfully, but these errors were encountered: