lassik / emacs-format-all-the-code

Auto-format source code in many languages with one command
https://melpa.org/#/format-all
MIT License
612 stars 106 forks source link

Minimal LSP support #47

Open rassie opened 5 years ago

rassie commented 5 years ago

It seems that unification efforts (e.g. https://github.com/lassik/emacs-format-all-the-code/issues/4) stalled a bit. I'd like to propose (and kindly ask to be implemented, since my elisp is basically non-existent) a minimal solution for LSP integration inside format-all-the-code which would make my developer life a bit more consistent.

I'd imagine it'd work this way:

  1. If lsp-mode is not enabled for the current buffer, fallback to the current behaviour.
  2. Otherwise, check for textDocument/formatting capability of the current language server (or maybe servers, since that's apparently a supported workflow). If it's not available, fallback to current behaviour.
  3. Otherwise, use textDocument/formatting (probably via lsp-format-buffer) instead of the predefined tool.

Does this sound sensible and would you consider this for format-all-the-code?

lassik commented 5 years ago

Thanks for bringing LSP to our attention. Sounds sensible to support it in format-all, but I know almost nothing about it so I don't know what the best approach is. lsp-format-buffer could be the right thing. I'm a bit swamped with other projects right now...

rassie commented 5 years ago

That's only fair, more reason for me to dive into elisp :)

However, I've looked into code and noticed that doom-emacs, which I'm a user of, does quite a bit of wrapping around format-all. Might be a better idea to check for LSP in the doom-emacs auto-formatter and leave format-all as it is.

@hlissner: what do you think?

lassik commented 5 years ago

Either approach is fine with me. However, format-all is designed to work with bare Emacs for all users.

How does LSP work from a user's standpoint? Is there a separate server (command line program) for each language (or group of languages), and Emacs commands to start them in the background and connect them to Emacs?

rassie commented 5 years ago

Yes, exactly, there is protocol (https://microsoft.github.io/language-server-protocol/), so that a single editing mode can communicate with a range of so-called "language servers". One of the commands is textDocument/formatting which asks the language server to format code (whole buffer, snippet, etc., depending on language server's capabilities).