英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论