Compare commits

..

2 Commits

Author SHA1 Message Date
maye e53cf715f5 update python/rust lsp 2025-09-04 15:01:20 +08:00
maye 39a2d68c8c misc: update lsp config 2025-09-04 11:04:51 +08:00
10 changed files with 144 additions and 51 deletions

View File

@ -15,6 +15,24 @@ local M = {
return false return false
end, end,
}, },
lsp_servers = {
code = {
"lua_ls",
"gopls",
"clangd",
"bashls",
"yamlls",
"ts_ls",
"rust_analyzer",
"ruff",
},
format_lint = {
"goimports",
"gofumpt",
},
mason_ensure_installed = { "lua_ls", "bashls", "bacon", "rust-analyzer" },
treesitter_ensure_installed = { "go", "rust", "python" },
},
} }
return M return M

View File

@ -1,6 +1,12 @@
-- Get all LSP servers to configure -- Get all LSP servers to configure
local function get_all_servers() local function get_all_servers()
local servers = {} local servers = {}
local commons = require("commons")
-- Use configured LSP servers from commons
for _, server in ipairs(commons.lsp_servers.code) do
servers[server] = true
end
-- Auto-detect LSP servers installed by Mason using mason-lspconfig -- Auto-detect LSP servers installed by Mason using mason-lspconfig
local ok, mason_lspconfig = pcall(require, "mason-lspconfig") local ok, mason_lspconfig = pcall(require, "mason-lspconfig")
@ -11,29 +17,34 @@ local function get_all_servers()
end end
end end
-- If no servers detected, fall back to common servers that might be system-installed
if vim.tbl_isempty(servers) then
local fallback_servers = {
"lua_ls",
}
for _, server in pairs(fallback_servers) do
servers[server] = true
end
end
return servers return servers
end end
-- Configure a single LSP server -- Configure a single LSP server
local function configure_server(server, lspconfig, capabilities) local function configure_server(server, lspconfig, capabilities)
local ok, settings = pcall(require, "lsp." .. server) -- Only attempt to load .lua files from lsp folder
if ok then local lsp_config_path = "lsp." .. server
if lspconfig then local lua_file_path = vim.fn.stdpath("config") .. "/lua/lsp/" .. server .. ".lua"
settings.capabilities = capabilities
lspconfig[server].setup(settings) -- Check if the .lua file exists
if vim.fn.filereadable(lua_file_path) == 1 then
local ok, settings = pcall(require, lsp_config_path)
if ok then
if lspconfig then
settings.capabilities = capabilities
lspconfig[server].setup(settings)
else
vim.lsp.config(server, settings)
vim.lsp.enable(server)
end
else else
vim.lsp.config(server, settings) -- Fallback to default config if .lua file exists but fails to load
vim.lsp.enable(server) if lspconfig then
lspconfig[server].setup({ capabilities = capabilities })
else
vim.lsp.config(server, {})
vim.lsp.enable(server)
end
end end
else else
if lspconfig then if lspconfig then

View File

@ -1,8 +1,4 @@
return { return {
-- Command to start the language server
-- This should match the executable name installed by Mason or system package manager
cmd = { "pyright" },
-- File types that should automatically attach this LSP server -- File types that should automatically attach this LSP server
filetypes = { "python" }, filetypes = { "python" },
@ -22,27 +18,20 @@ return {
-- Server-specific settings (varies by language server) -- Server-specific settings (varies by language server)
-- Check your language server's documentation for available options -- Check your language server's documentation for available options
settings = { settings = {
python = { basedpyright = {
analysis = { analysis = {
autoImportCompletions = true,
ignorePatterns = { "*.pyi" },
diagnosticSeverityOverrides = {
reportAny = false,
reportExplicitAny = false,
},
typeCheckingMode = "standard",
autoSearchPaths = true, autoSearchPaths = true,
useLibraryCodeForTypes = true, useLibraryCodeForTypes = true,
diagnosticMode = "workspace",
disableOrganizeImports = true,
}, },
}, },
}, },
-- Optional: Additional capabilities (usually handled globally)
-- capabilities = require("blink.cmp").get_lsp_capabilities(),
-- Optional: Custom initialization options
-- init_options = {
-- hostInfo = "neovim",
-- },
-- Optional: Custom handlers for LSP methods
-- handlers = {
-- ["textDocument/hover"] = vim.lsp.with(
-- vim.lsp.handlers.hover,
-- { border = "rounded" }
-- ),
-- },
} }

33
lua/lsp/clangd.lua Normal file
View File

@ -0,0 +1,33 @@
---@type vim.lsp.Config
return {
keys = {
{ "<leader>ch", "<cmd>ClangdSwitchSourceHeader<cr>", desc = "Switch Source/Header (C/C++)" },
},
root_dir = function(name)
return require("lspconfig.util").root_pattern(
"Makefile",
"configure.ac",
"configure.in",
"config.h.in",
"meson.build",
"meson_options.txt",
"build.ninja"
)(fname) or require("lspconfig.util").root_pattern("compile_commands.json", "compile_flags.txt")(fname) or require(
"lspconfig.util"
).find_git_ancestor(name)
end,
cmd = {
"clangd",
"--background-index",
"--clang-tidy",
"--header-insertion=iwyu",
"--completion-arg-placeholders",
"--function-arg-placeholders",
"--fallback-style=llvm",
},
init_options = {
usePlaceholders = true,
completeUnimported = true,
clangdFileStatus = true,
},
}

View File

@ -1,6 +1,5 @@
---@type vim.lsp.Config ---@type vim.lsp.Config
return { return {
cmd = { "gopls" },
filetypes = { "go", "gomod", "gosum" }, filetypes = { "go", "gomod", "gosum" },
root_markers = { root_markers = {
"go.mod", "go.mod",
@ -8,8 +7,21 @@ return {
}, },
settings = { settings = {
gopls = { gopls = {
gofumpt = true,
codelenses = {
gc_details = false,
generate = true,
regenerate_cgo = true,
run_govulncheck = true,
test = true,
tidy = true,
upgrade_dependency = true,
vendor = true,
},
analyses = { analyses = {
nliness = true,
unusedparams = true, unusedparams = true,
unusedwrite = true,
}, },
hints = { hints = {
assignVariableTypes = true, assignVariableTypes = true,
@ -20,8 +32,10 @@ return {
parameterNames = true, parameterNames = true,
rangeVariableTypes = true, rangeVariableTypes = true,
}, },
usePlaceholders = true,
completeUnimported = true,
staticcheck = true, staticcheck = true,
gofumpt = true, directoryFilters = { "-.git", "-.vscode", "-.idea", "-.vscode-test", "-node_modules" },
semanticTokens = true, semanticTokens = true,
}, },
}, },

9
lua/lsp/ruff.lua Normal file
View File

@ -0,0 +1,9 @@
return {
cmd_env = { RUFF_TRACE = "messages" },
init_options = {
settings = {
logLevel = "error",
},
},
keys = { { "<leader>co", function() end, desc = "Organize Imports" } },
}

24
lua/lsp/yamlls.lua Normal file
View File

@ -0,0 +1,24 @@
return {
capabilities = {
textDocument = {
foldingRange = {
dynamicRegistration = false,
lineFoldingOnly = true,
},
},
},
settings = {
redhat = { telemetry = { enabled = false } },
yaml = {
keyOrdering = false,
format = {
enable = true,
},
validate = true,
schemaStore = {
enable = false,
url = "",
},
},
},
}

View File

@ -19,9 +19,8 @@ return {
}, },
formatters_by_ft = { formatters_by_ft = {
lua = { "stylua" }, lua = { "stylua" },
python = { "black" },
rust = { "rustfmt" }, rust = { "rustfmt" },
javascript = { "prettier", stop_after_first = true }, javascript = { "biome" },
sh = { "shfmt" }, sh = { "shfmt" },
xml = { "xmlformat" }, xml = { "xmlformat" },
}, },

View File

@ -4,14 +4,7 @@ return {
build = ":MasonUpdate", build = ":MasonUpdate",
opts_extend = { "ensure_installed" }, opts_extend = { "ensure_installed" },
opts = { opts = {
ensure_installed = { ensure_installed = {},
-- Formatters and tools
"stylua",
-- LSP servers (will be auto-detected by mason-lspconfig)
"lua-language-server",
"gopls",
"pyright"
}
}, },
event = "VeryLazy", event = "VeryLazy",
config = function(_, opts) config = function(_, opts)

View File

@ -18,6 +18,9 @@ return {
words = { enabled = false }, words = { enabled = false },
}, },
keys = { keys = {
{ "<leader>,", function() Snacks.picker.buffers() end, desc = "Buffers" },
{ "<leader>:", function() Snacks.picker.command_history() end, desc = "Command History" },
{ "<leader>n", function() Snacks.picker.notifications() end, desc = "Notification History" },
{ "<leader>e", function() Snacks.explorer() end, desc = "Snack File Explorer" }, { "<leader>e", function() Snacks.explorer() end, desc = "Snack File Explorer" },
{ "<leader><space>", function() Snacks.picker.smart() end, desc = "Smart Find Files" }, { "<leader><space>", function() Snacks.picker.smart() end, desc = "Smart Find Files" },
{ "<leader>fg", function() Snacks.picker.git_files() end, desc = "Find Git Files" }, { "<leader>fg", function() Snacks.picker.git_files() end, desc = "Find Git Files" },