pyls-black icon indicating copy to clipboard operation
pyls-black copied to clipboard

How to add to vim/nvim LanguageClient (or lsp-nvim)?

Open RustemB opened this issue 4 years ago • 2 comments

How can I configure LanguageClient to use black?

RustemB avatar Jul 30 '20 11:07 RustemB

If you're using a relatively newer (v0.5+?) version of neovim, I discovered the built-in lua LanguageClient supports invoking the hook:

lua vim.lsp.buf.formatting()

So just bind this to your favorite key or setup an autocmd BufWritePre for it.

autocmd BufWritePre *.py lua vim.lsp.buf.formatting()

Just ensure you have the pyls-black option installed with your python-language-server pip package and the LS will handle the formatting for you.

farvour avatar Mar 17 '21 08:03 farvour

Thank you, it does work indeed. However I had to use vim.lsp.buf.formatting_sync(), else due to the async nature of formating, the modifications appear after the file has been written to disk, hence I had to save the file twice.

JudgeGregg avatar Jun 19 '21 16:06 JudgeGregg