0

I have neovim 0.8.0 installed. I have neovim/nvim-lspconfig plugin installed. I have installed pyright language server on my system (pip install pyright) I have configured neovim to use the pyright language server.

So now when I open a python file I get some warnings produced by pyright. :LspInfo command confirms that "1 client(s) attached to this buffer" and this client is pyright.

So far so good. Now I start editing some code. For example I write the following function call:

x = get_value_of_x("aaaaaaaaaa", "bbbbbbb", "ccccccc",
        "dddddddd")

As you can see this code is not properly formatted. According to pep8 rules it should be formatted like this:

x = get_value_of_x("aaaaaaaaaa", "bbbbbbb", "ccccccc",
                   "dddddddd")

I expected that neovim integrated with language server will automatically indent the code, but somehow it does not happen.

I tried to trigger format explicitly by executing the following command:

:lua vim.lsp.buf.format()

but get following error message: [LSP] Format request failed, no matching language servers.

I tried to install different language server ("jedi-language-server") instead of "pyright" - and the result is approximately the same: both servers function properly (for example "go to definition" functionality works fine) but not auto-formatting is available.

No log messages generated during these formatting attempts in ~/.cache/nvim/lsp.log

How can I configure nvim / lsp servers to properly indent python code?

2 Answers 2

-1

There are many ways.

Here is one using Coc.Nvim (neoclide/coc.nvim)

Follow the instructions to install Coc.Nvim

Basically (if you are using Plug)

  1. Add the following line to your .vimrc
Plug 'neoclide/coc.nvim' , {'branch': 'release'}
  1. Resource your .vimrc using the vim command:

:so %

  1. Run the vim command:

:PlugInstall

  1. Restart Neovim

When installed install Pyright support by entering the vim command:

:CocInstall coc-pyright

Add the following line to your .vimrc file to add Format support:

  " Add `:Format` command to format current buffer.
  command! -nargs=0 Format :call CocAction('format')

  " Use CTRL-S for selections ranges.
  " Requires 'textDocument/selectionRange' support of language server.
  nmap <silent> <C-s> <Plug>(coc-range-select)
  xmap <silent> <C-s> <Plug>(coc-range-select)

The :Format command format the entire file.

For that to work you'll need to have Black installed. This can be achieved using the following shell command:

pip install black

If Coc.Nvim fails to find Black you can configure it explicitly by typing the vim command:

:CocConfig

The coc-settings.json file will open.

Make sure the following lines (On a Windows machine) are part of the config.

Remark: Coc.Nvim support also range formatting if the corresponding language server support it. It seems that PyRight is not supporting range formatting yet.

{
    "python.formatting.provider": "black",
    "python.formatting.blackPath": "C:\\Python36_x64\\Scripts\\black.exe",
}
6
  • 1
    Thanks for the answer! I have installed Coc.Nvim and coc-pyright extension. How can I use :Format command now? I have even installed black - but still the code is not properly indented.
    – lesnik
    May 3 at 19:51
  • The vim command :Format should reformat your code but maybe you have a provlem with the installation of black. Can you tell us what is your OS? To verify black is in the path can you run the shell command black? Could you also try to save your file and run the vim command :!black % that should reformat it. May 4 at 3:25
  • 1
    Operating system is ubuntu linux. Vim command :!black % works - it does re-format the whole file. But :Format does not: "E492: Not an editor command: Format". And frankly speaking the re-formatting of the whole file with black is not what I need. There could be several valid ways to format a function call and black re-formats even portions of code which are already correctly formatted :(
    – lesnik
    May 4 at 7:38
  • Is the answer essentially "pyright and other python LSPs do not include formatting; use black for that"?
    – D. Ben Knoble
    May 4 at 14:01
  • Coc.Nvim bring support for file formatting (using black) it also bring range formatting if the corresponding LSP is supporting it. It seems that PyRight doesn't support it yet. May 4 at 17:32
-1

For range formatting you may have a look at black-macchiato.

Install black-macchiato by running the shell command:

pip install black-macchiato

To format a range of select lines run the vim command:

:'<,'>!python -m macchiato

Or install the pluging smbl64/vim-black-macchiato: vim-black-macchiato

1
  • 1
    If you're going for this, maybe it is more comfortable to run it as part of formatprg: :set formatprg=python3\ -m\ macchiato and then type gqip to format that paragraph. Jun 3 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.