jsonian icon indicating copy to clipboard operation
jsonian copied to clipboard

Pretty print the buffer when opening long single line JSON file

Open taku0 opened this issue 1 year ago • 2 comments

When opening long single line JSON file, ask the user if they want to pretty print the buffer. This makes opening and editing the file faster since, as you know, Emacs cannot handle long line well.

Opening 30 MiB file (made with jq -c . test-assets/large-json-file.json) took 4 seconds with pretty printing rather than 13 seconds without pretty printing. Our implementation is faster than json-pretty-print-buffer, which took 10 seconds.

Why we are faster than json-pretty-print-buffer:

json-pretty-print-buffer is generic while our jsonian--pretty-print-long-line assumes the buffer is single line. json-pretty-print-buffer parse the buffer, pretty print it in a temporary buffer, and replace the buffer content with replace-region-contents. It keeps existing markers and properties intact but slow. We alternatively scan the buffer and insert line breaks and indentations. We also utilize jsonian--huge-edit to optimizes the undo list; Emacs normally records one undo entry for each basic modifications such as inserting a line break or indenting a line. When applying json-pretty-print-buffer to a large file, this floods the undo list and it will be truncated by Emacs. Even without truncation, it makes undo slower. jsonian--huge-edit creates a few undo entries that delete whole region and replace it with a new content. It also takes care of existing markers.

This is ported from my JSON Par mode.

taku0 avatar Jun 12 '23 08:06 taku0