I want to make emacs format the buffer using lsp formatter on save.
I tried to enable (format +onsave) in init.el and set +format-with-lsp to t, but only +format/buffer can invoke lsp format and format-on-save still use some third party formatters(eg. prettier for js).
I also tried (add-hook 'js2-mode-hook #'+format|enable-on-save) but doesn't work.
How can I correctly set lsp format on save?
If you see a function called +format|enable-on-save, it indicates you're on a very, very old version of Doom. I did away with the X|Y naming convention more than a year ago.
If you upgrade Doom to the latest version, all of that module's commands, +format/buffer, +format/region and +format/region-or-buffer will use LSP's formatter _if_ +format-with-lsp is non-nil _and_ the LSP server supports it _and_ you have the necessary dependencies (prettier, in this case) installed. Enabling :editor (format +onsave) will cause the buffer to be formatted when you save.
If it still isn't formatting with LSP, it's because the LSP server doesn't support it. Try M-x lsp-format-buffer. Does it work?
If you see a function called
+format|enable-on-save, it indicates you're on a _very_, _very_ old version of Doom. I did away with theX|Ynaming convention more than a year ago.If you upgrade Doom to the latest version, all of that module's commands,
+format/buffer,+format/regionand+format/region-or-bufferwill use LSP's formatter _if_+format-with-lspis non-nil _and_ the LSP server supports it _and_ you have the necessary dependencies (prettier, in this case) installed. Enabling:editor (format +onsave)will cause the buffer to be formatted when you save.If it still isn't formatting with LSP, it's because the LSP server doesn't support it. Try
M-x lsp-format-buffer. Does it work?
@hlissner Thanks! +format/buffer does use LSP formatter when I enable (format +onsave). Format-on-save still tried to use prettier yet. I looked at before-save-hook, it would trigger format-all-buffer, and it seems using prettier instead of LSP formatter.
Should I remove format-all-buffer from before-save-hook? cause I only want to let LSP format buffers.
I'm seeing a similar issue when trying to format some Go code upon saving. Instead of LSP being used, gofmt is (I assume this is where). M-x lsp-format-buffer works and I have set (go +lsp) in config.el. Am I missing anything?
Messages buffer when trying to save:
Reformatted with gofmt
Error: (error "Invalid rcs patch or internal error in +format--apply-rcs-patch")
Same issue for me with Ruby and Solargraph. +format/buffer uses LSP while format-on-save uses rufo.
I am seeing this as well with rufo and +format/buffer
I am also seeing this issue. It seems a workaround could be:
(add-hook 'before-save-hook #'+format/buffer nil t)
like here, in the file modules/editor/format/config.el
@systemctl603 I'll give that a try and see if that fixes it for me