Closed dlukes closed 4 years ago
Have you considered setting up a function for it and calling that in the autocmd
instead?
function! MaybeFormat() abort
if !has_key(g:LanguageClient_serverCommands, &filetype)
return
endif
call LanguageClient#textDocument_formatting_sync()
endfunction
autocmd BufWritePre * call MaybeFormat()
This still has the problem that if the language server does not have formatting capabilities you will probably face an error (or maybe even end up in the loop again, not sure).
Thanks very much for the alternative suggestion! I'm currently using/evaluating the builtin LSP in Neovim nightly, so I don't have a pressing need for this anymore, but it's definitely useful to have it here :)
I'm trying to figure out the best way to set up format on save for all filetypes which have a language server. At first, I thought I could just do this, and at worst, I'd maybe have to silence warnings/errors if no language server is running:
As it turns out, this does work for filetypes which have a language server, but hangs indefinitely on save for filetypes which don't, waiting forever in this loop in
LanguageClient_runSync
(presumably because the Rust bridge isn't there to respond?):https://github.com/autozimu/LanguageClient-neovim/blob/fb02afed15363c8879277b413b89e6ca51589e6a/autoload/LanguageClient.vim#L1024-L1026
So I searched the issue tracker and found out about
LanguageClient#isAlive
, which I thought I could use at runtime to determine whetherLanguageClient#textDocument_formatting_sync
is safe to run (presumably, ifLanguageClient#isAlive
, then the call won't hang). However, as per the suggestion in the linked comment, I would need to useLanguageClient_runSync
to runLanguageClient#isAlive
synchronously, which means we get stuck in the aforementioned while loop once again.So I ended up devising with the following solution, which seems to be working so far:
In other words: install a
FileType
autocommand, which sets up a per-buffer autocommand to run LSP formatting for each buffer belonging to one of the supported filetypes. To make matters worse, the whole thing has to be wrapped in anexecute
, because we need determine the list of supported filetypes based on the keys ofg:LanguageClient_serverCommands
.This feels somewhat baroque and complicated, so I was wondering whether I've missed a more obvious, cleaner way, that someone better acquainted with the internals of the project might recommend over this? :)