From 1d824b92c0a8715ff95e41609214ed6225b1abdf Mon Sep 17 00:00:00 2001 From: Yuan Chiu Date: Mon, 5 May 2025 22:19:11 +0800 Subject: [PATCH] =?UTF-8?q?nvim:=20=E8=AA=BF=E6=95=B4=20lsp=5Fservers=20?= =?UTF-8?q?=E7=B5=90=E6=A7=8B=E5=B0=87=E6=B8=85=E5=96=AE=E6=8B=89=E5=87=BA?= =?UTF-8?q?=E4=BE=86=EF=BC=8C=E4=B8=A6=E5=A2=9E=E5=8A=A0=E5=88=A4=E6=96=B7?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E6=9C=89go=E5=9F=B7=E8=A1=8C=E7=92=B0?= =?UTF-8?q?=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lsp/exact_config/lsp_servers.lua | 29 +++++++++++++++++++ .../exact_lua/lsp/exact_config/lspconfig.lua | 17 ++--------- .../nvim/exact_lua/lsp/exact_config/mason.lua | 16 ++-------- 3 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 dot_config/nvim/exact_lua/lsp/exact_config/lsp_servers.lua diff --git a/dot_config/nvim/exact_lua/lsp/exact_config/lsp_servers.lua b/dot_config/nvim/exact_lua/lsp/exact_config/lsp_servers.lua new file mode 100644 index 0000000..bc63985 --- /dev/null +++ b/dot_config/nvim/exact_lua/lsp/exact_config/lsp_servers.lua @@ -0,0 +1,29 @@ +-- 統一管理要啟用的語言伺服器 +local M = {} + +-- 依主機功能篩選條件(可選) +local has_go = vim.fn.executable("go") == 1 + +-- 程式語言所需要的與語言伺服器名稱對照表 +-- +M.lsp_servers = { + -- "lua_ls", "rust_analyzer" -- 官方範例 + + "phpactor", -- PHP + has_go and "sqls" or nil, -- SQL + "pyright", -- Python + has_go and "gopls" or nil, -- Go + "html", -- HTML + "cssls", -- CSS + "volar", -- Vue3 + -- "ts_ls", -- TypeScript,目前暫時不需要,先註解掉 + "marksman", -- Markdown + "yamlls", -- YAML +} + +-- 過濾 nil 值 +M.lsp_servers = vim.tbl_filter(function(item) + return item ~= nil +end, M.lsp_servers) + +return M diff --git a/dot_config/nvim/exact_lua/lsp/exact_config/lspconfig.lua b/dot_config/nvim/exact_lua/lsp/exact_config/lspconfig.lua index ec7f3f3..5c04e33 100644 --- a/dot_config/nvim/exact_lua/lsp/exact_config/lspconfig.lua +++ b/dot_config/nvim/exact_lua/lsp/exact_config/lspconfig.lua @@ -1,3 +1,5 @@ +local servers = require("lsp.config.lsp_servers").lsp_servers + local lspconfig = require("lspconfig") local capabilities = require("cmp_nvim_lsp").default_capabilities() @@ -9,21 +11,6 @@ local on_attach = function(_, bufnr) map("n", "K", vim.lsp.buf.hover) end --- 程式語言所需要的與語言伺服器名稱對照表 --- -local servers = { - "phpactor", -- PHP - "sqls", -- SQL - "pyright", -- Python - "gopls", -- Go - "html", -- HTML - "cssls", -- CSS - "volar", -- Vue3 - -- "ts_ls", -- TypeScript,目前暫時不需要,先註解掉 - "marksman", -- Markdown - "yamlls", -- YAML -} - -- 載入所有的對應的 LSP server 設定 -- -- 相關設定請看 diff --git a/dot_config/nvim/exact_lua/lsp/exact_config/mason.lua b/dot_config/nvim/exact_lua/lsp/exact_config/mason.lua index b507995..d3085a6 100644 --- a/dot_config/nvim/exact_lua/lsp/exact_config/mason.lua +++ b/dot_config/nvim/exact_lua/lsp/exact_config/mason.lua @@ -1,21 +1,11 @@ +local lsp = require("lsp.config.lsp_servers") + require("mason").setup() require("mason-lspconfig").setup { -- A list of servers to automatically install if they're not already installed. Example: { "rust_analyzer@nightly", "lua_ls" } -- This setting has no relation with the `automatic_installation` setting. ---@type string[] - ensure_installed = { - -- "lua_ls", "rust_analyzer" -- 官方範例 - "phpactor", - "sqls", - "pyright", - "gopls", - "html", - "cssls", - "volar", - -- "ts_ls", - "marksman", - "yamlls", - }, + ensure_installed = lsp.lsp_servers, -- Whether servers that are set up (via lspconfig) should be automatically installed if they're not already installed. -- This setting has no relation with the `ensure_installed` setting.