Move files out of their respective fs structure into program dirs in root and symlink dotfils using HM.

This commit is contained in:
Martin Larsson 2025-01-16 23:49:38 +00:00
parent 10bab010b7
commit fb2adb4547
72 changed files with 9 additions and 0 deletions

View file

@ -0,0 +1,62 @@
local utils = require("utils")
-- https://clangd.llvm.org/extensions.html#switch-between-sourceheader
local function switch_source_header(bufnr)
bufnr = utils.validate_bufnr(bufnr)
local clangd_client = vim.lsp.get_clients({ bufnr = bufnr, name = "clangd" })[1]
local params = { uri = vim.uri_from_bufnr(bufnr) }
if clangd_client then
clangd_client.request("textDocument/switchSourceHeader", params, function(err, result)
if err then
error(tostring(err))
end
if not result then
print "Corresponding file cannot be determined"
return
end
vim.api.nvim_command("drop " .. vim.uri_to_fname(result))
end, bufnr)
else
print "method textDocument/switchSourceHeader is not supported by any servers active on the current buffer"
end
end
return {
cmd = {
"clangd",
"--background-index", -- Enables background indexing
"--clang-tidy", -- Enables clang-tidy diagnostics
"--completion-style=bundled", -- Simpler completions for faster performance
"--rename-file-limit=0", -- No limit on renaming files
"--header-insertion=iwyu", -- Suggest missing includes based on IWYU
"--inlay-hints", -- Enable inlay hints for parameter and type information
"--limit-results=70", -- Limit autocompletion and symbol results
"--suggest-missing-includes", -- Still show missing includes suggestions
"--pch-storage=disk", -- Stores precompiled headers on disk (fixes the issue where system ran out of memory when indexing large projects, not a huge performance hit on fast m2 ssds)
"--log=error", -- Log only errors
},
filetypes = { "c", "cpp", "objc", "objcpp", "cuda", "proto" },
root_markers = {
".clangd",
".clang-tidy",
".clang-format",
"compile_commands.json",
"compile_flags.txt",
"configure.ac",
},
on_attach = function(_, bufnr)
local lsp_maps = {
{
"<leader>ko",
function() switch_source_header(0) end,
},
}
local keymaps = { n = {} }
for i, _ in ipairs(lsp_maps) do
local binding, cmd = unpack(lsp_maps[i])
keymaps.n[binding] = { cmd = cmd, opts = { buffer = bufnr } }
end
utils.add_keymaps(keymaps)
end,
}

View file

@ -0,0 +1,14 @@
return {
cmd = { "cmake-language-server" },
filetypes = { "cmake" },
root_markers = {
"CMakeLists.txt",
"CMakePresets.json",
"CTestConfig.cmake",
"build",
"cmake",
},
init_options = {
buildDirectory = "build",
},
}

View file

@ -0,0 +1,56 @@
local mod_cache = nil
return {
cmd = { "gopls" },
filetypes = { "go", "gomod", "gowork", "gotmpl" },
settings = {
gopls = {
["ui.inlayhints.hints"] = {
compositeLiteralFields = true,
constantValues = true,
parameterNames = true,
},
analyses = {
unusedparams = true,
},
staticcheck = true,
lintTool = "golangci-lint",
},
},
root_dir = function(callback)
local path = vim.fn.expand("%:p")
if not path or path == "" then
callback(nil)
return
end
-- Asynchronously fetch GOMODCACHE if not already set
if not mod_cache then
vim.system({ "go", "env", "GOMODCACHE" }, { text = true }, function(result)
if result and result.code == 0 and result.stdout then
mod_cache = vim.trim(result.stdout)
else
vim.notify("[gopls] Unable to fetch GOMODCACHE", vim.log.levels.WARN)
mod_cache = nil
end
end)
end
-- Check if the file is in the module cache
if mod_cache and path:sub(1, #mod_cache) == mod_cache then
local clients = vim.lsp.get_clients({ name = "gopls" })
if #clients > 0 then
callback(clients[#clients].config.root_dir)
return
end
end
-- Fallback: Find project root markers
local go_mod_root = vim.fs.find({ "go.work", "go.mod", ".git" }, { upward = true, path = path })[1]
if go_mod_root then
callback(vim.fs.dirname(go_mod_root))
else
callback(nil)
end
end,
}

View file

@ -0,0 +1,39 @@
return {
cmd = { "lua-language-server" },
filetypes = { "lua" },
root_markers = {
".luarc.json",
".luarc.jsonc",
".luacheckrc",
".stylua.toml",
"stylua.toml",
"selene.toml",
"selene.yml",
".git"
},
on_init = function(client)
local path = vim.tbl_get(client, "workspace_folders", 1, "name")
if not path then
return
end
-- override the lua-language-server settings for Neovim config
client.settings = vim.tbl_deep_extend("force", client.settings, {
Lua = {
runtime = {
version = "LuaJIT"
},
-- Make the server aware of Neovim runtime files
workspace = {
checkThirdParty = false,
library = {
vim.env.VIMRUNTIME
-- Depending on the usage, you might want to add additional paths here.
-- "${3rd}/luv/library"
-- "${3rd}/busted/library",
}
}
}
})
end
}

View file

@ -0,0 +1,21 @@
return {
cmd = { "pyright-langserver", "--stdio" },
filetypes = { "python", "py" },
root_markers = {
"pyproject.toml",
"setup.py",
"setup.cfg",
"requirements.txt",
"Pipfile",
"pyrightconfig.json",
},
settings = {
python = {
analysis = {
autoSearchPaths = true,
useLibraryCodeForTypes = true,
diagnosticMode = "openFilesOnly",
},
},
},
}

106
nvim/lua/lsp/setup.lua Normal file
View file

@ -0,0 +1,106 @@
local utils = require("utils")
local inlay_hints_handler = require("inlay_hints_handler")
local format_handler = require("format_handler")
local function chain_on_attach(...)
local funcs = { ... }
return function(client, bufnr)
for _, func in ipairs(funcs) do
func(client, bufnr)
end
end
end
local function global_on_attach(client, bufnr)
inlay_hints_handler.add_buffer(bufnr)
if client.server_capabilities.documentFormattingProvider then
vim.api.nvim_buf_create_user_command(bufnr, "Format", vim.lsp.buf.format, { nargs = 0 })
vim.api.nvim_create_autocmd("BufWritePre", {
buffer = bufnr,
callback = function()
format_handler.format()
end,
})
end
utils.add_keymaps({
n = {
["gd"] = {
cmd = function()
vim.lsp.buf.definition()
end,
opts = {
noremap = true,
silent = true,
buffer = bufnr
}
},
["gD"] = {
cmd = function()
vim.lsp.buf.declaration()
end,
opts = {
noremap = true,
silent = true,
buffer = bufnr
}
},
}
})
end
local global_capabilities = require("blink.cmp").get_lsp_capabilities()
global_capabilities.offsetEncoding = { "utf-16" }
vim.diagnostic.config({
underline = true, -- Underline diagnostic errors
virtual_text = false, -- Disable inline text messages
signs = true, -- Show icons in the sign column
update_in_insert = true, -- Update diagnostics during insert mode
})
vim.lsp.config("*", {
capabilities = global_capabilities,
handlers = {
["textDocument/publishDiagnostics"] = vim.lsp.diagnostic.on_publish_diagnostics,
},
root_markers = { ".git" },
})
-- Find all files in lua/lsp/servers and require them
-- We use them to ensure that the servers are installed and configured
local errors = {}
local dir_path = "lsp/servers"
utils.foreach(utils.get_file_names_in_dir(dir_path, "*.lua", true), function(server_name)
local server_path = dir_path .. "/" .. server_name
local result, conf = utils.xpcallmsg(
function() return require(server_path) end,
"Failed to require " .. server_path,
errors
)
if not result or type(conf) ~= "table" or vim.tbl_isempty(conf) or conf.cmd == nil then
error("Invalid configuration for " .. server_name)
return
end
conf.on_attach = (function()
if conf.on_attach then
return chain_on_attach(global_on_attach, conf.on_attach)
end
return global_on_attach
end)()
-- These still throw errors when wrapped by xpcall.
-- Wanted it to just handle incorrect input and let the runtime continue
-- as it would if the require was successful when wrapped. That would be great
-- for WIP LSP configuration, instead we have the ugly if statements above.
vim.lsp.config(server_name, conf)
vim.lsp.enable(server_name)
end)
if #errors > 0 then
error(table.concat(errors, "\n"))
end