Neovim built-in LSP shows No code actions available for Python files
Asked Answered
O

1

9

When I open a python file, diagnostics seem to be working fine. Then I navigate to a line with a diagnostic error, press the shortcut to invoke code actions ('<space>ca' in my case) and I get a message 'No code actions available'. I have tried running it for different errors, like the following ones:

b =1  #E225 missing whitespace around operator
from jinja2 import StrictUndefined  #'jinja2.StricUndefined' imported but unused
import jjj # import-error: Unable to import 'jjj'

I have tried two LSP servers so far: pyright and pylsp, both gave me the same 'No code actions available'

I've seen a similar question but JavaScript asked here and it suggest installing a plugin but that didn't work for me.

Ology answered 29/11, 2021 at 23:13 Comment(0)
V
14

Both Pyright and Pyls don't provide any diagnostic solving code actions like jdtls for java unfortunately...
I would recommend checking out their individual repositories on github for further information and development:
pyls, pyright

For more insight on what your language server is capable of, run the following command in vim:

:lua print(vim.inspect(vim.lsp.buf_get_clients()[1].resolved_capabilities))

It will output the capabilities of the language server you are attached to in the current buffer.
For example this is the output for Pyright with no special configurations:

{
  call_hierarchy = true,
  code_action = {
    codeActionKinds = { "quickfix", "source.organizeImports" },
    workDoneProgress = true
  },
  code_lens = false,
  code_lens_resolve = false,
  completion = true,
  declaration = false,
  document_formatting = false,
  document_highlight = {
    workDoneProgress = true
  },
  document_range_formatting = false,
  document_symbol = {
    workDoneProgress = true
  },
  execute_command = true,
  find_references = {
    workDoneProgress = true
  },
  goto_definition = {
    workDoneProgress = true
  },
  hover = {
    workDoneProgress = true
  },
  implementation = false,
  rename = true,
  signature_help = true,
  signature_help_trigger_characters = { "(", ",", ")" },
  text_document_did_change = 2,
  text_document_open_close = true,
  text_document_save = true,
  text_document_save_include_text = false,
  text_document_will_save = false,
  text_document_will_save_wait_until = false,
  type_definition = false,
  workspace_folder_properties = {
    changeNotifications = false,
    supported = false
  },
  workspace_symbol = {
    workDoneProgress = true
  }
}

Currently Pyright only supports the organize imports code action.
Keep in mind some lsp's don't provide code actions at all, but generally they do provide the basic needs such as go-to definition/declaration, hover info, documentation, signature help, renaming and references.

Variety answered 2/12, 2021 at 0:48 Comment(4)
After checking both links, pylsp and pyright, there is no mention, indeed, of code actions being available. However, pyright does not mention linting neither and linting messages are showing up in my python files, where is the linting coming from? Is there a way to check what linter is being used? (e.g. flake8)Ology
You might have set diagnostics in the lspconfig configurations, which now in nvim 0.6 are separate from the individual language sepcific configurations. To check what linter you are using, run :compiler command for a list of linters and formatters available. You could use formatter plugins like formatter.nvim, NeoFormat or null-ls to choose explicit formatters/linters for supported languages.Variety
that's impressive, every time I dig into pyright I found out it pales in comparison with what PyCharm offers... even though it's supported by Microsoft!Newfeld
pylance (the other lsp from microsoft) offers all these things, but it's not open source...Acorn

© 2022 - 2024 — McMap. All rights reserved.