Skip to content

Commit

Permalink
Refactor lspconfig's root_dir function
Browse files Browse the repository at this point in the history
  • Loading branch information
MysticalDevil committed Oct 12, 2023
1 parent 3138aaf commit 9039f4a
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 30 deletions.
27 changes: 18 additions & 9 deletions lua/lsp/config/clangd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,24 @@ opts.init_options = {
completeUnimported = true,
semanticHighlighting = true,
}
opts.root_dir = require("lspconfig.util").root_pattern(
".clangd",
".clang-tidy",
".clang-format",
"compile_commands.json",
"compile_flags.txt",
"configure.ac",
".git"
)
opts.root_dir = function(fname)
return require("lspconfig.util").root_pattern(
"configure.ac",
"Makefile",
"configure.in",
"config.h.in",
"meson.build",
"meson_options.txt",
"build.ninja"
)(fname) or require("lspconfig.util").root_pattern(
".clangd",
".clang-tidy",
".clang-format",
"compile_commands.json",
"compile_flags.txt"
)(fname) or require("lspconfig.util").find_git_ancestor(fname)
end

opts.on_attach = function(client, bufnr)
util.disable_format(client)
util.key_attach(bufnr)
Expand Down
5 changes: 4 additions & 1 deletion lua/lsp/config/clojure_lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ local util = require("lsp.util")

local opts = util.default_configs()
opts.filetypes = { "clojure", "edn" }
opts.root_dir = lsp_util.root_pattern("project.clj", "deps.edn", "build.boot", "shadow-cljs.edn", ".git", "bb.edn")
opts.root_dir = function(fname)
return lsp_util.root_pattern("project.clj", "deps.edn", "build.boot", "shadow-cljs.edn", "bb.edn")(fname)
or lsp_util.find_git_ancestor(fname)
end

return util.set_on_setup(opts)
4 changes: 3 additions & 1 deletion lua/lsp/config/csharp_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ opts.filetypes = { "cs" }
opts.init_options = {
AutomaticWorkspaceInit = true,
}
opts.root_dir = lsp_util.root_pattern("*.sln", "*.csproj", "*.fsproj", ".git")
opts.root_dir = function(fname)
return lsp_util.root_pattern("*.sln", "*.csproj", "*.fsproj")(fname) or lsp_util.find_git_ancestor(fname)
end

return util.set_on_setup(opts)
4 changes: 3 additions & 1 deletion lua/lsp/config/denols.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ local opts = util.default_configs()

opts.filetypes =
{ "javascript", "javascriptreact", "javascript.jsx", "typescript", "typescriptreact", "typescript.tsx" }
opts.root_dir = lsp_util.root_pattern("deno.json", "deno.jsonc")
opts.root_dir = function(fname)
return lsp_util.root_pattern("deno.json", "deno.jsonc")(fname)
end
opts.setting = {
deno = {
enable = true,
Expand Down
4 changes: 3 additions & 1 deletion lua/lsp/config/fennel_language_server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ local util = require("lsp.util")
local opts = util.default_configs()

opts.filetypes = { "fennel" }
opts.root_dir = lsp_util.root_pattern("fnl")
opts.root_dir = function(fname)
return lsp_util.root_pattern("fnl")(fname)
end
opts.settings = {
fennel = {
workspace = {
Expand Down
5 changes: 4 additions & 1 deletion lua/lsp/config/gopls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ opts.settings = {
opts.init_options = {
usePlaceholders = true,
}
opts.root_dir = require("lspconfig.util").root_pattern("go.work", "go.mod", ".git")
opts.root_dir = function(fname)
return require("lspconfig.util").root_pattern("go.work", "go.mod")(fname)
or require("lspconfig.util").find_git_ancestor(fname)
end
opts.single_file_support = true

return util.set_on_setup(opts)
4 changes: 3 additions & 1 deletion lua/lsp/config/kotlin_language_server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ local util = require("lsp.util")
local opts = util.default_configs()

opts.filetypes = { "kotlin" }
opts.root_dir = lsp_util.root_pattern("settings.gradle", "settings.gradle.kts")
opts.root_dir = function(fname)
return lsp_util.root_pattern("settings.gradle", "settings.gradle.kts")(fname)
end

return util.set_on_setup(opts)
21 changes: 11 additions & 10 deletions lua/lsp/config/lua_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ opts.settings = {
},
},
}
opts.root_dir = require("lspconfig.util").root_pattern(
".luarc.json",
".luarc.jsonc",
".luacheckrc",
".stylua.toml",
"stylua.toml",
"selene.toml",
"selene.yml",
".git"
)
opts.root_dir = function(fname)
return require("lspconfig.util").root_pattern(
".luarc.json",
".luarc.jsonc",
".luacheckrc",
".stylua.toml",
"stylua.toml",
"selene.toml",
"selene.yml"
)(fname) or require("lspconfig.util").find_git_ancestor(fname)
end
opts.single_file_support = true

return util.set_on_setup(opts, "lua")
4 changes: 3 additions & 1 deletion lua/lsp/config/ruby_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ opts.settings = {}
opts.init_options = {
formatter = "auto",
}
opts.root_dir = require("lspconfig.util").root_pattern("Gemfile", ".git")
opts.root_dir = function(fname)
return require("lspconfig.util").root_pattern("Gemfile")(fname) or require("lspconfig.util").find_git_ancestor(fname)
end
opts.single_file_support = true

return util.set_on_setup(opts)
4 changes: 3 additions & 1 deletion lua/lsp/config/rust_analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ opts.settings = {
},
},
}
opts.root_dir = require("lspconfig.util").root_pattern("Cargo.toml", "rust-project.json")
opts.root_dir = function(fname)
return require("lspconfig.util").root_pattern("Cargo.toml", "rust-project.json")(fname)
end

return util.set_on_setup(opts, "rust")
4 changes: 3 additions & 1 deletion lua/lsp/config/taplo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ local util = require("lsp.util")
local opts = util.default_configs()

opts.filetypes = { "toml" }
opts.root_dir = require("lspconfig.util").root_pattern("*.toml", ".git")
opts.root_dir = function(fname)
return require("lspconfig.util").root_pattern("*.toml")(fname) or require("lspconfig.util").find_git_ancestor(fname)
end
opts.single_file_support = true

return util.set_on_setup(opts)
4 changes: 3 additions & 1 deletion lua/lsp/config/v_analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ local util = require("lsp.util")
local opts = util.default_configs()

opts.filetypes = { "v", "vsh", "vv", "vlang" }
opts.root_dir = lsp_util.root_pattern("v.mod", ".git")
opts.root_dir = function(fname)
return lsp_util.root_pattern("v.mod")(fname) or lsp_util.find_git_ancestor(fname)
end

return util.set_on_setup(opts)
5 changes: 4 additions & 1 deletion lua/lsp/config/zls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ opts.settings = {
inlay_hints_hide_redundant_param_names_last_token = false,
},
}
opts.root_dir = require("lspconfig.util").root_pattern("zls.json", ".git")
opts.root_dir = function(fname)
return require("lspconfig.util").root_pattern("zls.json")(fname) or require("lspconfig.util").find_git_ancestor(fname)
end

opts.single_file_support = true

return util.set_on_setup(opts)

0 comments on commit 9039f4a

Please sign in to comment.