kkinnear / zprint

Executables, uberjar, and library to beautifully format Clojure and Clojurescript source code and s-expressions.
MIT License
555 stars 47 forks source link

Flagging a data structure for custom printing in REPL #102

Open Engelberg opened 5 years ago

Engelberg commented 5 years ago

I have data structures in my libraries that benefit from custom printing. I currently override Clojure's print-method and pprint handler and toString method so most people get a pleasing experience at the REPL. But with the rise of Cider's multiple pretty-printing options, including zprint, it's getting harder to figure out how to make the data structures print well in every printer.

I see that zprint has a way to force printing of a specific record using .toString. But I can't think of a way to use that usefully from Cider's REPL print handling. What would be ideal, I think, would be if I could mark a given record in some way so that zprint will use the .toString method of that record. Or maybe zprint could check to see if the print-method has been overridden for a record, and if so, use that instead. Is there a better solution? Would you be receptive to a pull request relating to this behavior?

kkinnear commented 5 years ago

Sure, I'd be delighted with a solution to this problem. It's nice to see someone using zprint for structures in addition to code! As to ways to get zprint to use the .toString method (without giving it an options map) -- checking if the print-method has been overridden sounds like a fine idea, not least because it would pick up what people have already done. If you want to make that happen, that would be great.

Not that it is hard to find, but fzprint-record is probably where you want to start. The only wrinkle which might not be obvious is that the thing that comes in to print, zloc, can usually either be a location in a parsed zipper or a structure, and there are z... routines which do the "right" thing in either case. In this case, though, it can't be a location in a zipper -- fzprint-record is only called with structures. So you don't have to be careful about that.

While we are here, let me offer you a huge thanks for Instaparse! While I don't use it in zprint, I have used it in some Clojure code that I have managed to squeeze into my day job. It has been several years since I used Instaparse, but I found the entire experience quite a pleasure -- and that was after trying some other parsing approaches and libraries which were distinctly the opposite. Thanks for all of the effort you put into making that a stellar package, both code and documentation! I have no particular love of, nor expertise in parsing, and it was still a great experience.