是否可以通过工作区配置文件来覆盖或增强 Neovim 的 LSP 配置?

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

Is it possible to override or augment neovim's LSP configuration via a workspace config file?

问题

我正在使用Neovim进行C++开发(目前是AstroNvim,之前使用过LazyVim),我非常喜欢它。通常情况下,我对nvim-lspconfig和mason的默认设置没有任何问题 - 通过mason从官方LLVM发布中下载的标准clangd服务器运行得很好。

但是在进行ESP32开发时,官方LLVM发布不识别xtensa*-*-elf目标三元组。这会阻止它正确解析源文件以及compile_commands.json中的其他内容,因此我需要使用Espressif自己的clang发布,并且总是需要调整我的配置来强制执行类似以下的操作:

-- nvim-lspconfig.lua
return {
  {
    "neovim/nvim-lspconfig",
    opts = {
      servers = {
        clangd = {},
        -- 其他语言服务器
      },
      setup = {
        clangs = function(_, opts)
          opts.cmd = "~/.espressif/tools/esp-clang/bin/clangd"
        end
      },
    },
  },
}

随着时间的推移,这变得非常烦人,特别是如果我在项目完成之前需要不断切换。

因此,问题是,是否有一种方式可以使用一些特定于项目的dotfile覆盖全局Neovim LSP配置,当我在项目的工作区内启动编辑器时,它会被自动识别和解析?类似于.editorconfig,但用于lua样式的配置,可以“帮助”懒惰的插件管理器配置具有特定于项目的设置的语言服务器。如果通过内置功能或某个插件提供了这样的机制,请还提供一些关于如何覆盖lspconfig设置的提示,特别是设置clangd可执行文件路径(如果与我上面示例中的方式不同)。谢谢!

英文:

I am using Neovim for C++ development (currently AstroNvim, and previously used LazyVim), and like it a lot. Usually I do not have any issues with the defaults for nvim-lspconfig and mason - the standard clangd server downloaded by mason from the official LLVM release is working fine.

But when it comes to ESP32 development, the official LLVM release does not recognize the xtensa*-*-elf target triplet. This prevents it from properly parsing the source files and whatever else is in compile_commands.json, so I need to use Espressif's own clang release, and always have to tweak my configuration to force something like:

-- nvim-lspconfig.lua
return {
  {
    "neovim/nvim-lspconfig",
    opts = {
      servers = {
        clangd = {},
        -- whatever other language servers you want
      },
      setup = {
        clangs = function(_, opts)
          opts.cmd = "~/.espressif/tools/esp-clang/bin/clangd"
        end
      },
    },
  },
}

Over time this gets really annoying, especially if I need to switch back and forth before a project is complete.

So the question is, is there a way to override the global Neovim LSP configuration using some project-specific dotfile, which is recognized and parsed automatically when I start the editor inside the project's workspace? Something like .editorconfig, but for lua-style configuration, which can "help" the lazy plugin manager configure the language servers with project-specific settings. If such mechanism exists via built-in functionality, or provided by some plugin, please also provide some hint as to how lspconfig settings can be overridden, specifically setting the clangd executable path (if it differs from the way I showed in the sample above). Thanks!

答案1

得分: 0

是的,使用 nvim-config-local 插件应该是可能的。
https://github.com/klen/nvim-config-local

将以下内容添加到 plugins 文件夹:

return {
  "klen/nvim-config-local",
  config = function()
    require("config-local").setup({
      -- 默认选项(可选)
    
      -- 要加载的配置文件模式(支持 Lua)
      config_files = { ".clangd.lua", ".nvim.lua", ".nvimrc", ".exrc" },
    
      -- 插件保存文件数据的位置
      hashfile = vim.fn.stdpath("data") .. "/config-local",
    
      autocommands_create = true, -- 创建自动命令(VimEnter、DirectoryChanged)
      commands_create = true, -- 创建命令(ConfigLocalSource、ConfigLocalEdit、ConfigLocalTrust、ConfigLocalIgnore)
      silent = false, -- 禁用插件消息(配置已加载/已忽略)
      lookup_parents = false, -- 在父目录中查找配置文件
    })
  end,
}

然后,在项目根目录中创建一个名为 .clangd.nvim 的文件,内容如下:

require("lspconfig").clangd.setup({
  cmd = {
    "~/.espressif/tools/esp-clang/bin/clangd",
    "--pretty",
    "--header-insertion=iwyu",
    -- "--background-index",
    -- "--suggest-missing-includes",
    "--query-driver=/path/to/toolchain/bin/",
    "-j=40",
    "--pch-storage=memory",
    "--clang-tidy",
    "--compile-commands-dir=.",
  },
  filetypes = { "c", "cpp", "objc", "objcpp", "cuda", "proto" },
})

已在 LazyVim 上测试过(但尚未测试过您特定的 ESP32 clangd):

$ nvim --version
NVIM v0.9.1
Build type: Release
LuaJIT 2.1.0-beta3

$ cat /etc/issue
Linux Mint 21.1 Vera \n \l
英文:

Yes, it should be possible with the nvim-config-local plugin.
https://github.com/klen/nvim-config-local

Add this to the plugins folder:

return {
  "klen/nvim-config-local",
  config = function()
    require("config-local").setup({
      -- Default options (optional)

      -- Config file patterns to load (lua supported)
      config_files = { ".clangd.lua", ".nvim.lua", ".nvimrc", ".exrc" },

      -- Where the plugin keeps files data
      hashfile = vim.fn.stdpath("data") .. "/config-local",

      autocommands_create = true, -- Create autocommands (VimEnter, DirectoryChanged)
      commands_create = true, -- Create commands (ConfigLocalSource, ConfigLocalEdit, ConfigLocalTrust, ConfigLocalIgnore)
      silent = false, -- Disable plugin messages (Config loaded/ignored)
      lookup_parents = false, -- Lookup config files in parent directories
    })
  end,
}

Then in your project root directory, create a file .clangd.nvim with this content:

require("lspconfig").clangd.setup({
	cmd = {
		"~/.espressif/tools/esp-clang/bin/clangd",
		"--pretty",
		"--header-insertion=iwyu",
		-- "--background-index",
		-- "--suggest-missing-includes",
		"--query-driver=/path/to/toolchain/bin/",
		"-j=40",
		"--pch-storage=memory",
		"--clang-tidy",
		"--compile-commands-dir=.",
	},
	filetypes = { "c", "cpp", "objc", "objcpp", "cuda", "proto" },
})

Tested with LazyVim (but not with your specific ESP32 clangd though):

$ nvim --version
NVIM v0.9.1
Build type: Release
LuaJIT 2.1.0-beta3

$ cat /etc/issue
Linux Mint 21.1 Vera \n \l

huangapple
  • 本文由 发表于 2023年7月6日 12:57:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625619.html
匿名

发表评论

匿名网友

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

确定