NeoVim显示了当前工作空间之外的`std`和其他包的诊断信息。

huangapple go评论57阅读模式
英文:

NeoVim shows diagnostic for `std` and other crates outside my current workspace

问题

我使用 NeoVim 与 LSP Zero 插件 + Rust analyzer 进行 Rust 编程。

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

例如,如果我只是通过 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,但没有改变任何东西。

如何正确禁用当前工作区外的 crate 的诊断呢?

英文:

I use NeoVim with LSP Zero plugin + Rust analyzer for rust programming.

When I jump to a definition in some external crate (including std) Nvim's diagnostic starts to show me all the errors from that crate.

For example, if I just create new project via cargo init some_project, then open it via nvim some_project and jump to the definition of println macro, diagnostic starts to show me tons of this:

/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

Also I tried use CoC instead of native LSP, but it doesn't change anything.

What is correct way to disable diagnostic for crates outside my current workspace?

答案1

得分: 1

我似乎找到了解决此问题的方法。

在使用 Rust Analyzer 配置 LSP 时,我们需要指定自定义的 root_dir 函数:

local util = require('lspconfig.util')

local rustPorjectDir = nil -- 缓存当前项目目录
local rustRootPattern = util.root_pattern("Cargo.toml", "rust-project.json") -- 默认的 `root_dir` 实现

local function rust_root_dir(fname)
    local function get()
        rustPorjectDir = rustRootPattern(fname)
        return rustPorjectDir
    end

    return rustPorjectDir or get()
end
英文:

Looks like I found solution for this issue.

We need to specify custom root_dir function when configuring LSP with Rust Analyzer:

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

huangapple
  • 本文由 发表于 2023年2月23日 20:15:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定