emacs-format-all-the-code
emacs-format-all-the-code copied to clipboard
stuck on formatting the buffer after I save it
When I save a buffer, the buffer will be automatically formatted by this package, but it takes around 1s to finish the formatting process, I have to wait for it then I am able to do other actions such as move the focus to other windows.
Is it able to put the formatting process in the background or delay the formatting process for a few seconds, just don't block the whole Emacs?
Yes, it should be possible to start the formatter as a background process, and only send it the code on save. I'm planning to implement this but haven't got around to it. No changes to the command line invocations of the formatters are needed. In case anybody wants to try this, PRs are welcome!
It'll go roughly like this:
- Create a
*format-all-formatter*buffer in the background, separate from any files being edited. - Whenever a new formatter is selected,
make-processfor that formatter such that the process is associated with the*format-all-formatter*buffer (you have to separate its stdout from its stderr, the simplerstart-processcannot do that, so you need to use the complexmake-process) - Add a handler function with
set-process-sentinelto replace the text in the source file with the formatted code which the formatter has written into the*format-all-formatter*buffer once it has exited. Remember to check for abnormal exit (nonzero exit code). - In format-all-buffer, use
process-send-regionto send the source code to the process, followed byprocess-send-eof. - Once the formatter has exited, start it again so it's ready for the next call to
format-all-buffer. - Whenever the user switches to a different formatter (or same formatter with different command line arguments), restart the formatter process.
- Whenever the user switches to a buffer where
format-all-formattersis nil, it's okay to kill the formatter process to save memory, though this may not be that important.
If the user has set up a chain with more than one formatter, things are going to be annoying :) Maybe disable formatter preloading in that case.
Thank you, I haven't expected it to be so complicated.
Maybe check out this: https://emacs.stackexchange.com/q/30585/794
Yes. I've written another package live-preview which runs a background process.
format-all's current use case is so simple that we get away with a synchronous process, though that means really slow formatters noticeably freeze Emacs
apheleia runs a background formatter on save. I think a more promising approach is to keep the formatter warmed up during editing, feed it the code on save, and synchronously wait for it to complete. Formatting code doesn't take long, starting up some formatters does. But apheleia works right now, so it has that bragging right and I don't.
I just moved from Atom to doom, which uses this package and I also noticed the lag, esp w/ prettier.