Implementation of "structural editor for sexps" / par-edit in OCaml / Reason / ReScript?

Related to Exists Scratch/snap/blockly for ocaml/js?
ParEdit: EmacsWiki: Par Edit
Paredit, a Visual Guide - Calva User Guide

Squinting quite a bit, ParEdit is a “structural editor” for sexps where we are always in a valid AST state: i.e. matched “”'s, matched ()'s matched 's matched {}'s.

This is commonly used for editing EmacsLisp / Clojure. This has been implemented for both Emacs & VSCode / Clojure. Paredit, a Visual Guide - Calva User Guide

Question: Is there anything like ParEdit implemented in OCaml, Rescript, or Reason ? Preferably via Incr_dom / Bonsai, but open to other solutions too.


Maybe gopcaml-mode?

1 Like

Thanks for the mention @luke36 !

To clarify, gopcaml-mode is a structural-editor for OCaml code written in OCaml.

For your usecase @zeroexcuses, I guess you want a structural editor for s-expressions but written in OCaml, which may be slightly different, although looking at the source-code of gopcaml-mode will likely help, as the implementation of a par-edit style operations will probably be easiest using the same zipper-based architecture that Gopcaml-mode uses.

1 Like

@Gopiandcode : Interesting. How much does gopcaml-mode depend on emacs? I.e. how much work would be involved in porting this to work in the browser via some colored <pre> tags ?

The emacs integration is not a major part of the code — the majority of the code is written to handle the breadth of the OCaml AST, so it probably wouldn’t be too challenging to port to the browser.

That being said, for your usecase of S-exps, it would probably be easier to write a zipper for s-expressions instead of porting gopcaml-mode.

Just a minor remark: Paredit is a Emacs minor mode for editing s-exps that started in 2005: paredit.el - paredit - paredit -- parenthetical editing in Emacs and has been ported/reimplemented to other editors/environments. One of these efforts is Calva’s Paredit implementation.
Nowadays there is also Parinfer / Parinfer GitHub

I do not know of any implementation in something OCaml.
There is a JS implementation you could use: paredit.js Paredit.js is also what Calva used initially before rewriting Paredit in Clojurescript. There is also a (dated) VS Code extension that uses paredit.js for other Lisps Strict Paredit.

If you want/need to write your own, just be warned that depending what language you are par-editing, you also need to parse special s-exps, which may not be handled by a s-exp parser library. The first problem will be square brackets (like with Chez Scheme, Racket and Clojure), then the identifiers (e.g. in Chez almost everything that can’t be parsed as a number is a valid identifier, including { and \||\| - which yields the identifier |\ and |name with spaces!|) and then there is stuff like Scheme byte vectors #vu8(r g b), Chez Scheme’s vectors #(1 2 3), fixnum vectors like #vfx(1 2 3) or #3(1 2 3), …