emacs-format-all-the-code icon indicating copy to clipboard operation
emacs-format-all-the-code copied to clipboard

stuck on formatting the buffer after I save it

Open c02y opened this issue 4 years ago • 6 comments
trafficstars

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?

c02y avatar May 20 '21 18:05 c02y

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!

lassik avatar May 20 '21 18:05 lassik

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-process for that formatter such that the process is associated with the *format-all-formatter* buffer (you have to separate its stdout from its stderr, the simpler start-process cannot do that, so you need to use the complex make-process)
  • Add a handler function with set-process-sentinel to 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-region to send the source code to the process, followed by process-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-formatters is nil, it's okay to kill the formatter process to save memory, though this may not be that important.

lassik avatar May 20 '21 18:05 lassik

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.

lassik avatar May 20 '21 18:05 lassik

Thank you, I haven't expected it to be so complicated.

Maybe check out this: https://emacs.stackexchange.com/q/30585/794

c02y avatar May 20 '21 19:05 c02y

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.

lassik avatar May 20 '21 19:05 lassik

I just moved from Atom to doom, which uses this package and I also noticed the lag, esp w/ prettier.

claytonrcarter avatar Jun 10 '22 10:06 claytonrcarter