I come from the following problem:
- I'm using Neovim (0.8.3) with AstroNvim as plugin/everything-manager.
- I've successfully set up multiple LSP servers from within that, which use the right servers, interpreters, settings etc via the user configuration file. So, I'd like to not circumvent loading the configuration.
- I want to use that to format files from my shell command line non-interactively.
What I've thought should work is:
nvim -c ':e qa_throttle.py' -c ':lua vim.lsp.buf.format()' -c ':wq'
:lua vim.lsp.buf.format() manually after opening the file using
nvim qa_throttle.py just works.)
It doesn't; omitting the
q to see the errors, I get told that for the buffer there's no LSP set up; "[LSP] Format request failed; no matching language server".
My first guess was that LSP server bring up and connection takes a moment, so I added a
-c :sleep before the
-c ':lua… option - didn't help at all.
So, my second guess was that the
-c-specified commands just happen to run on some invisible or command buffer or something, and not on the actual file-containing buffer. OK, so, maybe I can script the selection of that buffer?
nvim -c ':argdo lua vim.lsp.buf.format()' -c ':wq' qa_throttle.py has the same problem.
Let's prove that if I run the command on all buffers, at least one commands "hits" the right buffer and formats it: replacing
:luado should run on all buffers:
nvim -c ':e qa_throttle.py' -c ':luado vim.lsp.buf.format()' -c ':wq'
This time, execution took way longer, I got not a single but a whole battery of the aforementioned failure messages. But the buffer I want to format remains unchanged.
So, I'm a bit blocked here.
How do I execute (esp. lua) commands in Neovim on the right document buffer from a command line to achieve document formatting?