fix: lsp config and format all lua file

main
maye 2025-08-25 16:43:09 +08:00
parent 1089d6af1e
commit fd9b13e5e7
15 changed files with 102 additions and 218 deletions

View File

@ -1,4 +1,4 @@
require('configs.options')
require('configs.mappings')
require('configs.lazy')
require('configs.autocmds')
require("configs.options")
require("configs.mappings")
require("configs.lazy")
require("configs.autocmds")

View File

@ -10,7 +10,7 @@ local M = {
"pyright",
"yamlls",
"ts_ls",
"rust-analyzer",
"rust_analyzer",
},
tools = {
is_version_gte_0_11 = function()

View File

@ -1,28 +1,34 @@
-- Mason package name to LSP server name mapping
local mason_to_lsp = {
["lua-language-server"] = "lua_ls",
["typescript-language-server"] = "ts_ls",
["rust-analyzer"] = "rust_analyzer",
["gopls"] = "gopls",
["pyright"] = "pyright",
["clangd"] = "clangd",
["bash-language-server"] = "bashls",
["yaml-language-server"] = "yamlls",
}
-- Get all LSP servers to configure
local function get_all_servers()
local servers = {}
-- Get installed LSP servers from Mason
local function get_mason_installed_servers()
local mason_registry = require("mason-registry")
local installed_servers = {}
for _, pkg in ipairs(mason_registry.get_installed_packages()) do
local lsp_name = mason_to_lsp[pkg.name]
if lsp_name then
table.insert(installed_servers, lsp_name)
-- Use only servers from commons - single source of truth
for _, server in pairs(require("commons").servers) do
servers[server] = true
end
return servers
end
-- Configure a single LSP server
local function configure_server(server, lspconfig, capabilities)
local ok, settings = pcall(require, "lsp." .. server)
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
if lspconfig then
lspconfig[server].setup({ capabilities = capabilities })
else
vim.lsp.config(server, {})
vim.lsp.enable(server)
end
end
return installed_servers
end
if require("commons").tools.is_version_gte_0_11() then
@ -31,31 +37,12 @@ if require("commons").tools.is_version_gte_0_11() then
capabilities = require("blink.cmp").get_lsp_capabilities(),
root_markers = { ".git" },
})
-- Get installed servers from Mason
local installed_servers = get_mason_installed_servers()
-- Configure and enable installed servers
for _, server in ipairs(installed_servers) do
local ok, settings = pcall(require, "lsp." .. server)
if ok then
vim.lsp.config(server, settings)
else
-- Basic configuration for servers without custom config
vim.lsp.config(server, {})
end
vim.lsp.enable(server)
-- Configure all servers
for server in pairs(get_all_servers()) do
configure_server(server)
end
-- Also configure servers from commons.servers that have custom configs
for _, server in pairs(require("commons").servers) do
local ok, settings = pcall(require, "lsp." .. server)
if ok then
vim.lsp.config(server, settings)
vim.lsp.enable(server)
end
end
vim.diagnostic.config({
update_in_insert = true,
severity_sort = true,
@ -66,28 +53,9 @@ else
-- Legacy nvim-lspconfig setup for Neovim < 0.11
local lspconfig = require("lspconfig")
local capabilities = require("blink.cmp").get_lsp_capabilities()
-- Get installed servers from Mason
local installed_servers = get_mason_installed_servers()
-- Configure installed servers
for _, server in ipairs(installed_servers) do
local ok, settings = pcall(require, "lsp." .. server)
if ok then
settings.capabilities = capabilities
lspconfig[server].setup(settings)
else
-- Basic configuration for servers without custom config
lspconfig[server].setup({ capabilities = capabilities })
end
end
-- Also configure servers from commons.servers
for _, server in pairs(require("commons").servers) do
local ok, settings = pcall(require, "lsp." .. server)
if ok then
settings.capabilities = capabilities
lspconfig[server].setup(settings)
end
-- Configure all servers
for server in pairs(get_all_servers()) do
configure_server(server, lspconfig, capabilities)
end
end

View File

@ -1,84 +0,0 @@
---@type vim.lsp.Config
--[[
Example LSP server configuration for Neovim 0.11+ native LSP API
This file demonstrates the structure and common patterns for configuring
LSP servers in the new vim.lsp.Config format. Copy this file and modify
it for your specific language server.
Key components:
- cmd: Command and arguments to start the server
- filetypes: File types that should trigger this LSP server
- root_markers: Files/directories that define project root (nested arrays = equal priority)
- settings: Server-specific configuration (schema varies by server)
For server-specific settings schemas, check the server's documentation:
- Language servers often provide JSON schemas for their settings
- Many examples can be found at: https://github.com/neovim/nvim-lspconfig
This configuration works with both Neovim 0.11+ (native vim.lsp.config)
and older versions (via nvim-lspconfig) thanks to the compatibility layer
in lua/functions/lsp.lua
--]]
return {
-- Command to start the language server
-- This should match the executable name installed by Mason or system package manager
cmd = { "language-server-executable" },
-- File types that should automatically attach this LSP server
filetypes = { "javascript", "typescript", "javascriptreact", "typescriptreact" },
-- Root directory markers - LSP will search for these files/directories
-- to determine the project root. Nested arrays indicate equal priority.
-- The first match wins within each priority level.
root_markers = {
{ "package.json", "tsconfig.json", "jsconfig.json" }, -- High priority
{ ".git", ".hg" } -- Fallback markers
},
-- Server-specific settings (varies by language server)
-- Check your language server's documentation for available options
settings = {
-- Example: TypeScript server settings
typescript = {
inlayHints = {
includeInlayParameterNameHints = "all",
includeInlayParameterNameHintsWhenArgumentMatchesName = false,
includeInlayFunctionParameterTypeHints = true,
includeInlayVariableTypeHints = true,
includeInlayPropertyDeclarationTypeHints = true,
includeInlayFunctionLikeReturnTypeHints = true,
includeInlayEnumMemberValueHints = true,
},
suggest = {
includeCompletionsForModuleExports = true,
},
preferences = {
includePackageJsonAutoImports = "auto",
},
},
javascript = {
-- JavaScript-specific settings
suggest = {
includeCompletionsForModuleExports = 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" }
-- ),
-- },
}

View File

@ -1,25 +1,25 @@
---@type vim.lsp.Config
return {
cmd = { "lua-language-server" },
filetypes = { "lua" },
root_markers = { ".luarc.json", ".luarc.jsonc", ".git" },
settings = {
Lua = {
diagnostics = {
globals = {'vim'},
},
runtime = {
version = 'LuaJIT',
},
workspace = {
checkThirdParty = false,
library = {
vim.env.VIMRUNTIME,
},
},
telemetry = {
enable = false,
},
}
}
cmd = { "lua-language-server" },
filetypes = { "lua" },
root_markers = { ".luarc.json", ".luarc.jsonc", ".git" },
settings = {
Lua = {
diagnostics = {
globals = { "vim" },
},
runtime = {
version = "LuaJIT",
},
workspace = {
checkThirdParty = false,
library = {
vim.env.VIMRUNTIME,
},
},
telemetry = {
enable = false,
},
},
},
}

View File

@ -10,8 +10,13 @@ return {
-- to determine the project root. Nested arrays indicate equal priority.
-- The first match wins within each priority level.
root_markers = {
{ "pyprojtect.toml", "setup.py", "setup.cfg", "requirements.txt", "pyrightconfig.json" }, -- High priority
{ ".git", ".hg" }, -- Fallback markers
"pyproject.toml",
"setup.py",
"setup.cfg",
"requirements.txt",
"pyrightconfig.json",
".git",
".hg",
},
-- Server-specific settings (varies by language server)
@ -41,4 +46,3 @@ return {
-- ),
-- },
}

View File

@ -14,7 +14,8 @@ return {
procMacro = {
enable = true,
},
checkOnSave = {
checkOnSave = true,
check = {
command = "clippy",
extraArgs = { "--no-deps" },
},

View File

@ -2,20 +2,20 @@ return {
"akinsho/bufferline.nvim",
version = "*",
dependencies = "nvim-tree/nvim-web-devicons",
event = "VeryLazy",
config = function(_,opts)
require("bufferline").setup({
options = {
close_command = "bdelete! %d",
right_mouse_command = "bdelete! %d",
offsets = {
{
filetype = "snacks_layout_box",
text = "Snacks File Picker",
separator = true,
}
}
}
})
end,
event = "VeryLazy",
config = function(_, opts)
require("bufferline").setup({
options = {
close_command = "bdelete! %d",
right_mouse_command = "bdelete! %d",
offsets = {
{
filetype = "snacks_layout_box",
text = "Snacks File Picker",
separator = true,
},
},
},
})
end,
}

View File

@ -2,7 +2,7 @@ return {
"ellisonleao/gruvbox.nvim",
priority = 1000,
lazy = false,
event = "VimEnter",
event = "VimEnter",
config = function()
require("gruvbox").setup({
italic = {
@ -12,8 +12,7 @@ return {
emphasis = false,
},
})
vim.o.background = "dark"
vim.o.background = "dark"
vim.cmd.colorscheme("gruvbox")
end,
}

View File

@ -2,9 +2,7 @@ return {
"neovim/nvim-lspconfig",
-- event = "InsertEnter",
-- cond = not require("commons").tools.is_version_gte_0_11(),
event = { "BufReadPre", "BufNewFile" },
event = { "BufReadPre", "BufNewFile" },
dependencies = { { "saghen/blink.cmp" }, { "williamboman/mason.nvim" } },
config = function()
require("functions.lsp")
end,
config = function() require("functions.lsp") end,
}

View File

@ -25,7 +25,7 @@ return {
-- Config
local config = {
options = {
refresh = { statusline = 100 },
refresh = { statusline = 100 },
component_separators = "",
section_separators = "",
theme = "auto",

View File

@ -4,7 +4,7 @@ return {
build = ":MasonUpdate",
opts_extend = { "ensure_installed" },
opts = { ensure_installed = { "stylua", "lua-language-server", "gopls", "pyright" } },
event = "VeryLazy",
event = "VeryLazy",
config = function(_, opts)
require("mason").setup(opts)
local mr = require("mason-registry")

View File

@ -1,6 +1,6 @@
return {
"norcalli/nvim-colorizer.lua",
ft = { "css", "scss", "html" },
ft = { "css", "scss", "html" },
config = function(_)
require("colorizer").setup()
vim.defer_fn(function() require("colorizer").attach_to_buffer(0) end, 0)

View File

@ -1,7 +1,7 @@
return {
"nvim-treesitter/nvim-treesitter",
build = function() require("nvim-treesitter.install").update({ with_sync = true })() end,
event = { "BufReadPost" },
event = { "BufReadPost" },
config = function()
local configs = require("nvim-treesitter.configs")
configs.setup({
@ -25,7 +25,7 @@ return {
end
end,
},
playground = { enable = false },
playground = { enable = false },
incremental_selection = { enable = true },
indent = { enable = true },
})

View File

@ -2,15 +2,13 @@ local M = {}
M.lazy_file_events = { "BufReadPost", "BufNewFile", "BufWritePre" }
function M.setup()
M.lazy_file()
end
function M.setup() M.lazy_file() end
function M.lazy_file()
local Event = require("lazy.core.handler.event")
local Event = require("lazy.core.handler.event")
Event.mappings.LazyFile = {id = "LazyFile", event = M.lazy_file_events}
Event.mappings["User LazyFile"] = Event.mappings.LazyFile
Event.mappings.LazyFile = { id = "LazyFile", event = M.lazy_file_events }
Event.mappings["User LazyFile"] = Event.mappings.LazyFile
end
return M