purcell/emacs-reformatter

use replace-buffer-contents if possible?

wbolster opened this issue · 5 comments

it seems reformatter tries to minimize the impact of changing the buffer:

                       ;; This replacement method minimises
                       ;; disruption to marker positions and the
                       ;; undo list
                       (narrow-to-region beg end)
                       (insert-file-contents out-file nil nil nil t)

emacs 26+ introduced a new replace-buffer-contents function that is supposed to cover the ‘reformat buffer’ use case with minimal impact:

One potential case where this behavior is useful is external code formatting programs

https://www.gnu.org/software/emacs/manual/html_node/elisp/Replacing.html

perhaps reformatter could use this function if available?

(not sure it would be useful for formatting only a region. maybe narrowing works?)

Yes and no. See discussion in #3. Tl;dr - yes, but replace-buffer-contents is horribly broken in Emacs 26.1, so no. But I'll leave this issue open for now because in the long term I might do this.

For information, bug related with replace-buffer-contents seems to be fixed with emacs-26.2: emacs-mirror/emacs@0d3c358

Done, thanks folks! And in general I'm going to move some of the code inside the macro out into functions over time so that reformatter-based packages don't need to be recompiled in order to pick up fixes like this. That was probably a slight mis-step on my part.

P.S. I tested this out locally and it seems solid, but perhaps give it a spin and let me know if you see any issues. Note that you might have to recompile your reformatters as mentioned above. :-)

thanks, @purcell!