【问题标题】:Neovim built-in LSP shows No code actions available for Python filesNeovim 内置 LSP 显示 No code actions available for Python files
【发布时间】:2023-01-29 08:41:08
【问题描述】:

当我打开一个 python 文件时,诊断似乎工作正常。然后我导航到有诊断错误的行,按快捷方式调用代码操作(在我的例子中为“<space>ca”),我收到一条消息“没有可用的代码操作”。我尝试针对不同的错误运行它,例如以下错误:

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

到目前为止,我已经尝试了两个 LSP 服务器:pyright 和 pylsp,它们都给了我相同的“没有可用的代码操作”

我见过类似的问题,但 JavaScript 询问了here,它建议安装一个插件,但这对我不起作用。

【问题讨论】:

    标签: neovim language-server-protocol pyright python-language-server nvim-lspconfig


    【解决方案1】:

    不幸的是,Pyright 和 Pyls 都不提供任何诊断解决代码操作,例如 jdtls for java ...
    我建议在 github 上查看他们各自的存储库以获取更多信息和开发:
    pyls, pyright

    要更深入地了解您的语言服务器的功能,请在 vim 中运行以下命令:

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

    它将在当前缓冲区中输出您所连接的语言服务器的功能。
    例如,这是没有特殊配置的 Pyright 的输出:

    {
      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
      }
    }
    

    目前 Pyright 仅支持组织进口代码操作。
    请记住,有些 lsp 根本不提供代码操作,但通常它们会提供基本需求,例如转到定义/声明、悬停信息、文档、签名帮助、重命名和参考。

    【讨论】:

    • 在检查了 pylsp 和 pyright 这两个链接之后,确实没有提到可用的代码操作。但是,pyright 没有提到 linting,而且 linting 消息显示在我的 python 文件中,linting 来自哪里?有没有办法检查正在使用的 linter? (例如 flake8)
    • 您可能已经在 lspconfig 配置中设置了诊断,现在在 nvim 0.6 中,它与个别语言特定的配置是分开的。要检查您使用的是什么 linter,请运行 :compiler 命令以获取可用的 linter 和格式化程序列表。您可以使用格式化程序插件,如 formatter.nvim、NeoFormat 或 null-ls 来为支持的语言选择显式格式化程序/linter。
    • 这令人印象深刻,每次我深入研究 pyright 时,我都发现它与 PyCharm 提供的功能相比相形见绌……即使它得到了 Microsoft 的支持!
    • pylance(微软的另一个 lsp)提供了所有这些东西,但它不是开源的......
    猜你喜欢
    • 2011-02-19
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多