rupert / pyls-black

Black plugin for the Python Language Server
MIT License
72 stars 8 forks source link

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

Open RustemB opened 3 years ago

RustemB commented 3 years ago

How can I configure LanguageClient to use black?

farvour commented 3 years ago

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.

JudgeGregg commented 3 years ago

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.