From 3b11323f806d2b9f2537f8c0de3f5500f0c9c0c4 Mon Sep 17 00:00:00 2001 From: maye Date: Wed, 27 Aug 2025 17:43:55 +0800 Subject: [PATCH] refactor: * --- lazy-lock.json | 1 + lua/commons/init.lua | 10 ---------- lua/functions/lsp.lua | 20 +++++++++++++++++--- lua/plugins/conform.lua | 1 + lua/plugins/lspconfig.lua | 6 +++++- lua/plugins/mason-lspconfig.lua | 9 +++++++++ lua/plugins/mason.lua | 11 ++++++++++- 7 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 lua/plugins/mason-lspconfig.lua diff --git a/lazy-lock.json b/lazy-lock.json index 70796e7..f82f6bb 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -9,6 +9,7 @@ "gruvbox.nvim": { "branch": "main", "commit": "12c2624287dc827edb5d72b2bc4c9619e692a554" }, "lazy.nvim": { "branch": "main", "commit": "6c3bda4aca61a13a9c63f1c1d1b16b9d3be90d7a" }, "lualine.nvim": { "branch": "master", "commit": "b8c23159c0161f4b89196f74ee3a6d02cdc3a955" }, + "mason-lspconfig.nvim": { "branch": "main", "commit": "1ec4da522fa49dcecee8d190efda273464dd2192" }, "mason.nvim": { "branch": "main", "commit": "7dc4facca9702f95353d5a1f87daf23d78e31c2a" }, "nvim-autopairs": { "branch": "master", "commit": "23320e75953ac82e559c610bec5a90d9c6dfa743" }, "nvim-colorizer.lua": { "branch": "master", "commit": "a065833f35a3a7cc3ef137ac88b5381da2ba302e" }, diff --git a/lua/commons/init.lua b/lua/commons/init.lua index 2ea5d42..c6d0b54 100644 --- a/lua/commons/init.lua +++ b/lua/commons/init.lua @@ -2,16 +2,6 @@ local M = { constants = { big_file_size_limit = 50 * 1024, -- 50KB }, - servers = { - "lua_ls", - "gopls", - "clangd", - "bashls", - "pyright", - "yamlls", - "ts_ls", - "rust_analyzer", - }, tools = { is_version_gte_0_11 = function() local version = vim.version() diff --git a/lua/functions/lsp.lua b/lua/functions/lsp.lua index 4e1c61b..6f3850f 100644 --- a/lua/functions/lsp.lua +++ b/lua/functions/lsp.lua @@ -2,9 +2,23 @@ local function get_all_servers() local servers = {} - -- Use only servers from commons - single source of truth - for _, server in pairs(require("commons").servers) do - servers[server] = true + -- Auto-detect LSP servers installed by Mason using mason-lspconfig + local ok, mason_lspconfig = pcall(require, "mason-lspconfig") + if ok then + local installed_servers = mason_lspconfig.get_installed_servers() + for _, server in ipairs(installed_servers) do + servers[server] = true + 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 diff --git a/lua/plugins/conform.lua b/lua/plugins/conform.lua index e91eb68..603dc91 100644 --- a/lua/plugins/conform.lua +++ b/lua/plugins/conform.lua @@ -23,6 +23,7 @@ return { rust = { "rustfmt" }, javascript = { "prettier", stop_after_first = true }, sh = { "shfmt" }, + xml = { "xmlformat" }, }, format_on_save = { timeout_ms = 500, lsp_format = "fallback" }, }, diff --git a/lua/plugins/lspconfig.lua b/lua/plugins/lspconfig.lua index 991da74..8c5cf31 100644 --- a/lua/plugins/lspconfig.lua +++ b/lua/plugins/lspconfig.lua @@ -3,6 +3,10 @@ return { -- event = "InsertEnter", -- cond = not require("commons").tools.is_version_gte_0_11(), event = { "BufReadPre", "BufNewFile" }, - dependencies = { { "saghen/blink.cmp" }, { "williamboman/mason.nvim" } }, + dependencies = { + { "saghen/blink.cmp" }, + { "williamboman/mason.nvim" }, + { "williamboman/mason-lspconfig.nvim" } + }, config = function() require("functions.lsp") end, } diff --git a/lua/plugins/mason-lspconfig.lua b/lua/plugins/mason-lspconfig.lua new file mode 100644 index 0000000..e8ac016 --- /dev/null +++ b/lua/plugins/mason-lspconfig.lua @@ -0,0 +1,9 @@ +return { + "williamboman/mason-lspconfig.nvim", + dependencies = { "williamboman/mason.nvim" }, + event = { "BufReadPre", "BufNewFile" }, + opts = { + -- Automatically install LSP servers detected by mason-lspconfig + automatic_installation = true, + }, +} \ No newline at end of file diff --git a/lua/plugins/mason.lua b/lua/plugins/mason.lua index bce1167..90f68b6 100644 --- a/lua/plugins/mason.lua +++ b/lua/plugins/mason.lua @@ -3,7 +3,16 @@ return { cmd = "Mason", build = ":MasonUpdate", opts_extend = { "ensure_installed" }, - opts = { ensure_installed = { "stylua", "lua-language-server", "gopls", "pyright" } }, + opts = { + ensure_installed = { + -- Formatters and tools + "stylua", + -- LSP servers (will be auto-detected by mason-lspconfig) + "lua-language-server", + "gopls", + "pyright" + } + }, event = "VeryLazy", config = function(_, opts) require("mason").setup(opts)