cider icon indicating copy to clipboard operation
cider copied to clipboard

Paredit and (maybe) indentation breaks in REPL when prior output contains unbalanced parentheses

Open dsedivec opened this issue 4 years ago • 6 comments

Expected behavior

REPL indent works when output contains unbalanced parentheses

Actual behavior

REPL indent raises an error when output contains unbalanced parentheses:

(wrong-type-argument wholenump -3)

Steps to reproduce the problem

  1. Open a Clojure REPL
  2. Eval (print ")))))")
  3. M-x indent-sexp RET

This seems contrived, but that's just for reproduction. TL;DR I think this error may be very common:

I actually ran into this in a shadow-cljs REPL, where the warnings have excerpts from my code. When a warning is, say, at the end of one of a defn form, the warning excerpt contains a bunch of close parentheses. Per CIDER's suggestion, I have Paredit turned on in my REPLs. After a CLJS warning with ...)) is printed, typing any open parenthesis yields an error because ( is bound to paredit-open-round, which calls indent-sexp.

ielm exhibits this same problem, BTW. I think SLY gets around this by assigning comment syntax to output, but I couldn't fully figure out how that works.

Environment & Version information

CIDER version information

;; CIDER 1.2.0snapshot, nREPL 0.9.0-beta5
;; Clojure 1.10.3, Java 17.0.1

Emacs version

Emacs 27.2 NeXTStep port from MacPorts (but also reproducible in my daily-driver master build, currently master from yesterday)

Operating system

macOS 11.6.1

Thank you hard-working CIDER developers, it's really awesome and so are you :)

dsedivec avatar Dec 18 '21 04:12 dsedivec

That's a long-standing bug, that I never had much time to think about. I normally work around it, but clearly such problematic output (after I notice it messes up the REPL). Perhaps we can borrow something from SLY's solution.

bbatsov avatar Dec 18 '21 15:12 bbatsov

I think SLY gets around this by assigning comment syntax to output, but I couldn't fully figure out how that works.

This is because forward-sexp and backward-sexp functions correctly ignoring comments. E.g. in emacs -Q in the *scratch* buffer with the following code, you can correctly use C-M-f and C-M-b to move over the unbalanced parenthesis in the comment:

( ;; )
 )

So yes, assigning a comment face/attribute to the output will fix this issue.

andreyorst avatar Feb 03 '22 06:02 andreyorst

PR welcome!

bbatsov avatar Feb 03 '22 06:02 bbatsov

AFAIU one would need to modify cider-repl--emit-output and cider-repl-emit-result to add properties to region beginning and region end. I'm unfamiliar with Emacs markers so I'm not sure how to properly determine the output region.

andreyorst avatar Feb 03 '22 18:02 andreyorst

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

stale[bot] avatar Jun 13 '22 02:06 stale[bot]

@bbatsov can you look into it and tell me if my assumption is correct? I don't mind to work on this, just need an indication that the place I was looking at is a correct place to solve this

andreyorst avatar Jun 13 '22 13:06 andreyorst