pyls-black
pyls-black copied to clipboard
How to add to vim/nvim LanguageClient (or lsp-nvim)?
How can I configure LanguageClient to use black?
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.
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.