diff options
| author | Peter Kling <peter.kling@uni-hamburg.de> | 2024-10-01 16:20:13 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-01 07:20:13 -0700 |
| commit | 814f02ed039ec9fce89949aa4306796b4e14daa5 (patch) | |
| tree | 7eee9406d91c0d56bff5b8ad0e20548e830f6b55 /lua/lspconfig/configs/lua_ls.lua | |
| parent | docs: update configs.md (diff) | |
| download | nvim-lspconfig-814f02ed039ec9fce89949aa4306796b4e14daa5.tar nvim-lspconfig-814f02ed039ec9fce89949aa4306796b4e14daa5.tar.gz nvim-lspconfig-814f02ed039ec9fce89949aa4306796b4e14daa5.tar.bz2 nvim-lspconfig-814f02ed039ec9fce89949aa4306796b4e14daa5.tar.lz nvim-lspconfig-814f02ed039ec9fce89949aa4306796b4e14daa5.tar.xz nvim-lspconfig-814f02ed039ec9fce89949aa4306796b4e14daa5.tar.zst nvim-lspconfig-814f02ed039ec9fce89949aa4306796b4e14daa5.zip | |
fix(lua-language-server): root directory pattern #3322
Problem:
Looking at the code for root dir detection for `lua_ls`:
https://github.com/neovim/nvim-lspconfig/blob/9bda20fb967075355f253911bc066a8b5a03c77e/lua/lspconfig/server_configurations/lua_ls.lua#L17-L27
I was surprised that finding the git ancestor has lower priority than
the `lua/` subdirectories (which, btw, is not mentioned in docs).
Consider the following directory structure:
HOME/
├─ workspaces/
│ ├─ lua/
│ ├─ python/
│ ├─ work/
│ │ ├─ SomeProjectWithLuaFiles/
│ │ │ ├─ .git/
The `lua/` and `python/` directories contain some miscellaneous
language-specific projects I sometimes work on. The project directory
under work/ also contains some lua files. I expected the `.git` directory
in the project to ensure that the project root dir is correctly detected
to be `…/SomeProjectWithLuaFiles`. But since the [search for the `lua/`
subdirectory](https://github.com/neovim/nvim-lspconfig/blob/9bda20fb967075355f253911bc066a8b5a03c77e/lua/lspconfig/server_configurations/lua_ls.lua#L22)
is done first, my LSP detects `HOME/workspaces/` as the root directory.
Solution:
Search for the git ancestor before looking for the `lua/` subdirectory.
Return the longer root path if both a `.git/` and a `lua/` ancestor are found.
Fixes #3165
Diffstat (limited to 'lua/lspconfig/configs/lua_ls.lua')
| -rw-r--r-- | lua/lspconfig/configs/lua_ls.lua | 11 |
1 files changed, 3 insertions, 8 deletions
diff --git a/lua/lspconfig/configs/lua_ls.lua b/lua/lspconfig/configs/lua_ls.lua index 5c5e1df1..6012194a 100644 --- a/lua/lspconfig/configs/lua_ls.lua +++ b/lua/lspconfig/configs/lua_ls.lua @@ -19,11 +19,9 @@ return { if root and root ~= vim.env.HOME then return root end - root = util.root_pattern 'lua/'(fname) - if root then - return root - end - return util.find_git_ancestor(fname) + local root_lua = util.root_pattern 'lua/'(fname) or '' + local root_git = util.find_git_ancestor(fname) or '' + return #root_lua >= #root_git and root_lua or root_git end, single_file_support = true, log_level = vim.lsp.protocol.MessageType.Warning, @@ -83,8 +81,5 @@ See `lua-language-server`'s [documentation](https://luals.github.io/wiki/setting * [Lua.workspace.library](https://luals.github.io/wiki/settings/#workspacelibrary) ]], - default_config = { - root_dir = [[root_pattern(".luarc.json", ".luarc.jsonc", ".luacheckrc", ".stylua.toml", "stylua.toml", "selene.toml", "selene.yml", ".git")]], - }, }, } |
