【问题标题】:NeoVim shows diagnostic for `std` and other crates outside my current workspaceNeoVim 显示对 `std` 和我当前工作区外的其他板条箱的诊断
【发布时间】:2023-02-26 19:22:56
【问题描述】:

我将 NeoVim 与 LSP Zero 插件 + Rust analyzer 一起用于 Rust 编程。

当我跳转到某个外部板条箱(包括std)中的定义时,Nvim 的诊断开始向我显示该板条箱中的所有错误。

例如,如果我只是通过 cargo init some_project 创建新项目,然后通过 nvim some_project 打开它并跳转到 println 宏的定义,诊断开始向我展示大量这样的东西:

/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/lib.rs|100 col 1-32 error| `#![feature]` may not be used on the stable release channel
/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/lib.rs|100 col 12-30 error| `#![feature]` may not be used on the stable release channel
/home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/lib.rs|101 col 12-30 error| `#![feature]` may not be used on the stable release channel

我也尝试使用 CoC 而不是本地 LSP,但它没有改变任何东西。

什么是禁用当前工作区外的板条箱诊断的正确方法?

【问题讨论】:

    标签: rust neovim language-server-protocol rust-analyzer


    【解决方案1】:

    看起来我找到了解决这个问题的方法。

    使用Rust Analyzer配置LSP时需要指定自定义root_dir函数:

    local util = require('lspconfig.util')
    
    local rustPorjectDir = nil -- caches current project dir
    local rustRootPattern = util.root_pattern("Cargo.toml", "rust-project.json") -- default `root_dir` implementation
    
    local function rust_root_dir(fname)
        local function get()
            rustPorjectDir = rustRootPattern(fname)
            return rustPorjectDir
        end
    
        return rustPorjectDir or get()
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-19
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 1970-01-01
      • 2013-03-18
      • 2021-07-20
      相关资源
      最近更新 更多