6

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.

vecin
  • 503
  • 5
  • 15

1 Answers1

10

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.

Sage
  • 316
  • 2
  • 7
  • 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) – vecin Dec 02 '21 at 04:40
  • 1
    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. – Sage Dec 02 '21 at 22:35
  • 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! – baggiponte Oct 17 '22 at 09:41
  • pylance (the other lsp from microsoft) offers all these things, but it's not open source... – niid Dec 16 '22 at 15:11