From fd9b13e5e712f985f3adf8a109e4dc0db6f9bda3 Mon Sep 17 00:00:00 2001 From: maye Date: Mon, 25 Aug 2025 16:43:09 +0800 Subject: [PATCH] fix: lsp config and format all lua file --- init.lua | 8 +- lua/commons/init.lua | 2 +- lua/functions/lsp.lua | 106 ++++++------------ lua/lsp/example.lua | 84 -------------- lua/lsp/lua_ls.lua | 44 ++++---- lua/lsp/pyright.lua | 10 +- .../{rust-analyzer.lua => rust_analyzer.lua} | 3 +- lua/plugins/bufferline.lua | 32 +++--- lua/plugins/colorscheme.lua | 5 +- lua/plugins/lspconfig.lua | 6 +- lua/plugins/lualine.lua | 2 +- lua/plugins/mason.lua | 2 +- lua/plugins/nvim-colorizer.lua | 2 +- lua/plugins/treesitter.lua | 4 +- lua/utils/plugins.lua | 10 +- 15 files changed, 102 insertions(+), 218 deletions(-) delete mode 100644 lua/lsp/example.lua rename lua/lsp/{rust-analyzer.lua => rust_analyzer.lua} (93%) diff --git a/init.lua b/init.lua index d5ad347..11a5612 100644 --- a/init.lua +++ b/init.lua @@ -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") diff --git a/lua/commons/init.lua b/lua/commons/init.lua index 585cfd2..2ea5d42 100644 --- a/lua/commons/init.lua +++ b/lua/commons/init.lua @@ -10,7 +10,7 @@ local M = { "pyright", "yamlls", "ts_ls", - "rust-analyzer", + "rust_analyzer", }, tools = { is_version_gte_0_11 = function() diff --git a/lua/functions/lsp.lua b/lua/functions/lsp.lua index fccab5c..4e1c61b 100644 --- a/lua/functions/lsp.lua +++ b/lua/functions/lsp.lua @@ -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 diff --git a/lua/lsp/example.lua b/lua/lsp/example.lua deleted file mode 100644 index e8d2cfc..0000000 --- a/lua/lsp/example.lua +++ /dev/null @@ -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" } - -- ), - -- }, -} \ No newline at end of file diff --git a/lua/lsp/lua_ls.lua b/lua/lsp/lua_ls.lua index 91746c9..3452b18 100644 --- a/lua/lsp/lua_ls.lua +++ b/lua/lsp/lua_ls.lua @@ -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, + }, + }, + }, } diff --git a/lua/lsp/pyright.lua b/lua/lsp/pyright.lua index 616d86e..4625225 100644 --- a/lua/lsp/pyright.lua +++ b/lua/lsp/pyright.lua @@ -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 { -- ), -- }, } - diff --git a/lua/lsp/rust-analyzer.lua b/lua/lsp/rust_analyzer.lua similarity index 93% rename from lua/lsp/rust-analyzer.lua rename to lua/lsp/rust_analyzer.lua index 4ad3e97..07f6ce6 100644 --- a/lua/lsp/rust-analyzer.lua +++ b/lua/lsp/rust_analyzer.lua @@ -14,7 +14,8 @@ return { procMacro = { enable = true, }, - checkOnSave = { + checkOnSave = true, + check = { command = "clippy", extraArgs = { "--no-deps" }, }, diff --git a/lua/plugins/bufferline.lua b/lua/plugins/bufferline.lua index 45cb5ce..c705590 100644 --- a/lua/plugins/bufferline.lua +++ b/lua/plugins/bufferline.lua @@ -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, } diff --git a/lua/plugins/colorscheme.lua b/lua/plugins/colorscheme.lua index 19d6f76..5d9996b 100644 --- a/lua/plugins/colorscheme.lua +++ b/lua/plugins/colorscheme.lua @@ -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, } - diff --git a/lua/plugins/lspconfig.lua b/lua/plugins/lspconfig.lua index 8c7ceec..991da74 100644 --- a/lua/plugins/lspconfig.lua +++ b/lua/plugins/lspconfig.lua @@ -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, } diff --git a/lua/plugins/lualine.lua b/lua/plugins/lualine.lua index 5cadba4..4640df3 100644 --- a/lua/plugins/lualine.lua +++ b/lua/plugins/lualine.lua @@ -25,7 +25,7 @@ return { -- Config local config = { options = { - refresh = { statusline = 100 }, + refresh = { statusline = 100 }, component_separators = "", section_separators = "", theme = "auto", diff --git a/lua/plugins/mason.lua b/lua/plugins/mason.lua index 0097194..bce1167 100644 --- a/lua/plugins/mason.lua +++ b/lua/plugins/mason.lua @@ -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") diff --git a/lua/plugins/nvim-colorizer.lua b/lua/plugins/nvim-colorizer.lua index cf94e83..f5bb1a5 100644 --- a/lua/plugins/nvim-colorizer.lua +++ b/lua/plugins/nvim-colorizer.lua @@ -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) diff --git a/lua/plugins/treesitter.lua b/lua/plugins/treesitter.lua index 0cd909e..db69b30 100644 --- a/lua/plugins/treesitter.lua +++ b/lua/plugins/treesitter.lua @@ -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 }, }) diff --git a/lua/utils/plugins.lua b/lua/utils/plugins.lua index 77f868d..437789f 100644 --- a/lua/utils/plugins.lua +++ b/lua/utils/plugins.lua @@ -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