From f1ed06ae9f6ac35519fdac5e02d07301efc698e6 Mon Sep 17 00:00:00 2001 From: newbieQQ Date: Thu, 11 Jun 2026 10:12:48 +0800 Subject: [PATCH] refactor: split nvim-lspconfig into 6 modules, change rename to --- lua/plugs.lua | 11 +- lua/plugs/mason.lua | 29 ++++ lua/plugs/nvim-cmp.lua | 107 ++++++++++++ lua/plugs/nvim-lspconfig.lua | 316 +--------------------------------- lua/plugs/symbols-outline.lua | 74 ++++++++ lua/plugs/treesitter.lua | 53 ++++++ lua/plugs/winbar.lua | 40 +++++ 7 files changed, 311 insertions(+), 319 deletions(-) create mode 100644 lua/plugs/mason.lua create mode 100644 lua/plugs/nvim-cmp.lua create mode 100644 lua/plugs/symbols-outline.lua create mode 100644 lua/plugs/treesitter.lua create mode 100644 lua/plugs/winbar.lua diff --git a/lua/plugs.lua b/lua/plugs.lua index dda8cb2..7467c69 100644 --- a/lua/plugs.lua +++ b/lua/plugs.lua @@ -1,9 +1,12 @@ return { - require('plugs.nvim-lspconfig'), - require('plugs.nvimtree'), -- nvimtree - require('plugs.theme'), -- theme + require('plugs.mason'), + require('plugs.symbols-outline'), + require('plugs.nvim-cmp'), + require('plugs.treesitter'), + require('plugs.winbar'), + require('plugs.nvimtree'), + require('plugs.theme'), require("plugs.edit-plugs"), require("plugs.dev"), - } diff --git a/lua/plugs/mason.lua b/lua/plugs/mason.lua new file mode 100644 index 0000000..019c3e1 --- /dev/null +++ b/lua/plugs/mason.lua @@ -0,0 +1,29 @@ +return { + { + -- lsp 下载器 + "williamboman/mason-lspconfig.nvim", + dependencies = { + "williamboman/mason.nvim", + }, + config = function() + require "mason".setup { + ui = { + icons = { + package_installed = "✓", + package_pending = "➜", + package_uninstalled = "✗" + } + } + } + require("mason-lspconfig").setup({ + ensure_installed = { + "bashls", + -- "basedpyright", + "lua_ls", + "jsonls", + "yamlls", + } + }) + end + }, +} diff --git a/lua/plugs/nvim-cmp.lua b/lua/plugs/nvim-cmp.lua new file mode 100644 index 0000000..a756d85 --- /dev/null +++ b/lua/plugs/nvim-cmp.lua @@ -0,0 +1,107 @@ +return { + { + -- lsp补全 + "hrsh7th/nvim-cmp", + dependencies = { + 'hrsh7th/cmp-nvim-lsp', + 'hrsh7th/cmp-buffer', + 'hrsh7th/cmp-path', + 'hrsh7th/cmp-cmdline', + { + 'hrsh7th/vim-vsnip', + config = function() + G.g.vsnip_snippet_dir = G.fn.stdpath("config") .. "/snippets" + end + }, + 'hrsh7th/cmp-vsnip', + 'onsails/lspkind-nvim', + }, + config = function() + local has_words_before = function() + unpack = unpack or table.unpack + local line, col = unpack(G.api.nvim_win_get_cursor(0)) + return col ~= 0 and G.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match('%s') == nil + end + + local feedkey = function(key, mode) + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true) + end + local cmp = require('cmp') + local cmp_opt = { + snippet = { + expand = function(args) + vim.fn["vsnip#anonymous"](args.body) + end, + }, + sources = cmp.config.sources({ + { name = 'nvim_lsp' }, + { name = 'vsnip' }, + }, + { name = 'buffer' }, + { name = 'path' } + ), + mapping = cmp.mapping.preset.insert({ + + [""] = cmp.mapping({ + i = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true }), + c = function(fallback) + if cmp.visible() then + cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true }) + else + fallback() + end + end, + }), + + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif vim.fn["vsnip#available"](1) == 1 then + feedkey("(vsnip-expand-or-jump)", "") + elseif has_words_before() then + cmp.complete() + else + fallback() + end + end, { "i", "s" }), + + [""] = cmp.mapping(function() + if cmp.visible() then + cmp.select_prev_item() + elseif vim.fn["vsnip#jumpable"](-1) == 1 then + feedkey("(vsnip-jump-prev)", "") + end + end, { "i", "s" }), + + }), + + window = { + completion = { + winhighlight = "Normal:Pmenu,FloatBorder:Pmenu,Search:None", + col_offset = -3, + side_padding = 0, + border = "rounded", + scrollbar = false, + }, + documentation = { + winhighlight = "Normal:Pmenu,FloatBorder:Pmenu,Search:None", + border = "rounded", + scrollbar = false, + }, + }, + + formatting = { + fields = { "kind", "abbr", "menu" }, + format = function(entry, vim_item) + local kind = require("lspkind").cmp_format({ mode = "symbol_text", maxwidth = 50, })(entry, vim_item) + local strings = vim.split(kind.kind, "%s", { trimempty = true }) + kind.kind = " " .. (strings[1] or "") .. " " + kind.menu = " (" .. (strings[2] or "") .. ")" + return kind + end, + }, + } + require('cmp').setup(cmp_opt) + end, + }, +} diff --git a/lua/plugs/nvim-lspconfig.lua b/lua/plugs/nvim-lspconfig.lua index 8df7e24..2c46d6a 100644 --- a/lua/plugs/nvim-lspconfig.lua +++ b/lua/plugs/nvim-lspconfig.lua @@ -9,7 +9,6 @@ return { config = function() require 'neodev'.setup {} - -- 定义需要启用的服务器列表及其对应的配置 local servers = { lua_ls = require('lsp.lua'), clangd = require('lsp.c'), @@ -20,332 +19,19 @@ return { jsonls = require('lsp.json'), } - -- 使用 Neovim 0.11+ 的新 API 进行配置和启用 for server, config in pairs(servers) do vim.lsp.config(server, config) vim.lsp.enable(server) end - -- require 'lspconfig'.lua_ls.setup(require('lsp.lua')) - -- require 'lspconfig'.clangd.setup(require('lsp.c')) - -- require 'lspconfig'.bashls.setup(require('lsp.bash')) - -- require 'lspconfig'.basedpyright.setup(require('lsp.basedpyright')) - -- require 'lspconfig'.yamlls.setup(require('lsp.yaml')) - -- require 'lspconfig'.gopls.setup(require('lsp.go')) - -- require 'lspconfig'.jsonls.setup(require('lsp.json')) - G.map({ - - { 'n', 'rn', 'lua vim.lsp.buf.rename()' }, - + { 'n', '', 'lua vim.lsp.buf.rename()' }, { 'n', 'gd', 'lua vim.lsp.buf.definition()' }, { 'n', 'gh', 'lua vim.lsp.buf.hover()' }, { 'n', 'gD', 'lua vim.lsp.buf.declaration()' }, { 'n', 'gi', 'lua vim.lsp.buf.implementation()' }, { 'n', 'gr', 'lua vim.lsp.buf.references()' }, { 'n', '', 'lua vim.lsp.buf.format()' }, - - }) - end - - }, - { - -- lsp - "williamboman/mason-lspconfig.nvim", - dependencies = { - "williamboman/mason.nvim", -- lsp 下载器 - }, - config = function() - require "mason".setup { - ui = { - icons = { - package_installed = "✓", - package_pending = "➜", - package_uninstalled = "✗" - } - } - } - require("mason-lspconfig").setup({ - ensure_installed = { - "bashls", - -- "basedpyright", - "lua_ls", - "jsonls", - "yamlls", - } - }) - end - }, - { -- - 'simrat39/symbols-outline.nvim', - config = function() - local opts = { - highlight_hovered_item = true, - show_guides = true, - auto_preview = false, - position = 'right', - relative_width = true, - width = 25, - auto_close = false, - show_numbers = false, - show_relative_numbers = false, - show_symbol_details = true, - preview_bg_highlight = 'Pmenu', - autofold_depth = nil, - auto_unfold_hover = true, - fold_markers = { '', '' }, - wrap = false, - keymaps = { -- These keymaps can be a string or a table for multiple keys - close = { "", "q" }, - goto_location = "", - focus_location = "h", - hover_symbol = "", - toggle_preview = "K", - rename_symbol = "r", - code_actions = "a", - fold = "o", - unfold = "l", - fold_all = "W", - unfold_all = "E", - fold_reset = "R", - }, - lsp_blacklist = {}, - symbol_blacklist = {}, - symbols = { - File = { icon = "", hl = "@text.uri" }, - Module = { icon = "", hl = "@namespace" }, - Namespace = { icon = "", hl = "@namespace" }, - Package = { icon = "", hl = "@namespace" }, - Class = { icon = "𝓒", hl = "@type" }, - Method = { icon = "ƒ", hl = "@method" }, - Property = { icon = "", hl = "@method" }, - Field = { icon = "", hl = "@field" }, - Constructor = { icon = "", hl = "@constructor" }, - Enum = { icon = "ℰ", hl = "@type" }, - Interface = { icon = "ﰮ", hl = "@type" }, - Function = { icon = "", hl = "@function" }, - Variable = { icon = "", hl = "@constant" }, - Constant = { icon = "", hl = "@constant" }, - String = { icon = "𝓐", hl = "@string" }, - Number = { icon = "#", hl = "@number" }, - Boolean = { icon = "⊨", hl = "@boolean" }, - Array = { icon = "", hl = "@constant" }, - Object = { icon = "⦿", hl = "@type" }, - Key = { icon = "🔐", hl = "@type" }, - Null = { icon = "NULL", hl = "@type" }, - EnumMember = { icon = "", hl = "@field" }, - Struct = { icon = "𝓢", hl = "@type" }, - Event = { icon = "🗲", hl = "@type" }, - Operator = { icon = "+", hl = "@operator" }, - TypeParameter = { icon = "𝙏", hl = "@parameter" }, - Component = { icon = "", hl = "@function" }, - Fragment = { icon = "", hl = "@constant" }, - }, - } - require("symbols-outline").setup(opts) - G.map({ - { "n", "", "SymbolsOutline", { noremap = true } }, - }) - end - }, - { - -- lsp补全 - { - "hrsh7th/nvim-cmp", - dependencies = { - 'hrsh7th/cmp-nvim-lsp', -- { name = 'nvim_lua' } - 'hrsh7th/cmp-buffer', -- { name = 'buffer' }, - 'hrsh7th/cmp-path', -- { name = 'path' } - 'hrsh7th/cmp-cmdline', -- { name = 'cmdline' } - { - 'hrsh7th/vim-vsnip', - config = function() - G.g.vsnip_snippet_dir = G.fn.stdpath("config") .. "/snippets" - end - }, - 'hrsh7th/cmp-vsnip', - 'onsails/lspkind-nvim', - }, - config = function() - local has_words_before = function() - unpack = unpack or table.unpack - local line, col = unpack(G.api.nvim_win_get_cursor(0)) - return col ~= 0 and G.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match('%s') == nil - end - - local feedkey = function(key, mode) - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true) - end - local cmp = require('cmp') - local cmp_opt = { - snippet = { - expand = function(args) - vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users. - end, - }, - sources = cmp.config.sources({ - { name = 'nvim_lsp' }, - { name = 'vsnip' }, - }, - { name = 'buffer' }, - { name = 'path' } - ), - mapping = cmp.mapping.preset.insert({ - - [""] = cmp.mapping({ - i = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true }), - c = function(fallback) - if cmp.visible() then - cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true }) - else - fallback() - end - end, - }), - - [""] = cmp.mapping(function(fallback) - if cmp.visible() then - cmp.select_next_item() - elseif vim.fn["vsnip#available"](1) == 1 then - feedkey("(vsnip-expand-or-jump)", "") - elseif has_words_before() then - cmp.complete() - else - fallback() - end - end, { "i", "s" }), - - [""] = cmp.mapping(function() - if cmp.visible() then - cmp.select_prev_item() - elseif vim.fn["vsnip#jumpable"](-1) == 1 then - feedkey("(vsnip-jump-prev)", "") - end - end, { "i", "s" }), - - }), - - window = { - completion = { - winhighlight = "Normal:Pmenu,FloatBorder:Pmenu,Search:None", - col_offset = -3, - side_padding = 0, - border = "rounded", - scrollbar = false, - }, - documentation = { - winhighlight = "Normal:Pmenu,FloatBorder:Pmenu,Search:None", - border = "rounded", - scrollbar = false, - }, - }, - - formatting = { - fields = { "kind", "abbr", "menu" }, - format = function(entry, vim_item) - local kind = require("lspkind").cmp_format({ mode = "symbol_text", maxwidth = 50, })(entry, vim_item) - local strings = vim.split(kind.kind, "%s", { trimempty = true }) - kind.kind = " " .. (strings[1] or "") .. " " - kind.menu = " (" .. (strings[2] or "") .. ")" - return kind - end, - }, - } - require('cmp').setup(cmp_opt) - end, - }, - - { - -- 语法高亮 -- - 'nvim-treesitter/nvim-treesitter', - config = function() - local treesitter_opt = { - ensure_installed = { - -- "c", - -- "cpp", - -- "python", - -- "java", - -- "lua", - -- "bash", - -- "vimdoc", - }, - indent = { enable = true }, - ignore_install = { - "txt" - }, - sync_install = false, - auto_install = true, - highlight = { - enable = true, - disable = function(_, buf) - local max_filesize = 100 * 1024 -- 100 KB - local ok, stats = pcall(G.loop.fs_stat, G.api.nvim_buf_get_name(buf)) - if ok and stats and stats.size > max_filesize then - return true - end - end, - additional_vim_regex_highlighting = false, - }, - parsers = { - vue = { - install_info = { - url = "https://github.com/ikatyang/tree-sitter-vue", - files = { "src/parser.c" }, - branch = "main", - }, - }, - }, - } - require 'nvim-treesitter'.setup(treesitter_opt) - require 'nvim-treesitter.install'.prefer_git = true - if G.use_ssh then - local parsers = require 'nvim-treesitter.parsers'.get_parser_configs() - for _, p in pairs(parsers) do - p.install_info.url = p.install_info.url:gsub("https://github.com/", "git@github.com:") - end - end - end - }, - }, - { - 'fgheng/winbar.nvim', - config = function() - require('winbar').setup({ - enabled = true, -- 是否启动winbar - - -- show_file_path = true, -- 是否显示文件路径 - show_symbols = true, -- 是否显示函数标签 - - -- 颜色配置,为空,将使用默认配色 - colors = { - path = '#aaffff', -- 路径的颜色,比如#ababab - file_name = '#bbbbff', -- 文件名称的颜色,比如#acacac - symbols = '#aaffaa', -- 函数颜色 - }, - - -- 图标配置 - icons = { - seperator = '>', -- 路径分割符号 - editor_state = '●', - lock_icon = '', - }, - - -- 关闭winbar的窗口 - exclude_filetype = { - 'help', - 'startify', - 'dashboard', - 'packer', - 'neogitstatus', - 'NvimTree', - 'Trouble', - 'alpha', - 'lir', - 'Outline', - 'spectre_panel', - 'toggleterm', - 'qf', - } }) end }, diff --git a/lua/plugs/symbols-outline.lua b/lua/plugs/symbols-outline.lua new file mode 100644 index 0000000..a4ce924 --- /dev/null +++ b/lua/plugs/symbols-outline.lua @@ -0,0 +1,74 @@ +return { + { -- + 'simrat39/symbols-outline.nvim', + config = function() + local opts = { + highlight_hovered_item = true, + show_guides = true, + auto_preview = false, + position = 'right', + relative_width = true, + width = 25, + auto_close = false, + show_numbers = false, + show_relative_numbers = false, + show_symbol_details = true, + preview_bg_highlight = 'Pmenu', + autofold_depth = nil, + auto_unfold_hover = true, + fold_markers = { '', '' }, + wrap = false, + keymaps = { + close = { "", "q" }, + goto_location = "", + focus_location = "h", + hover_symbol = "", + toggle_preview = "K", + rename_symbol = "r", + code_actions = "a", + fold = "o", + unfold = "l", + fold_all = "W", + unfold_all = "E", + fold_reset = "R", + }, + lsp_blacklist = {}, + symbol_blacklist = {}, + symbols = { + File = { icon = "", hl = "@text.uri" }, + Module = { icon = "", hl = "@namespace" }, + Namespace = { icon = "", hl = "@namespace" }, + Package = { icon = "", hl = "@namespace" }, + Class = { icon = "𝓒", hl = "@type" }, + Method = { icon = "ƒ", hl = "@method" }, + Property = { icon = "", hl = "@method" }, + Field = { icon = "", hl = "@field" }, + Constructor = { icon = "", hl = "@constructor" }, + Enum = { icon = "ℰ", hl = "@type" }, + Interface = { icon = "ﰮ", hl = "@type" }, + Function = { icon = "", hl = "@function" }, + Variable = { icon = "", hl = "@constant" }, + Constant = { icon = "", hl = "@constant" }, + String = { icon = "𝓐", hl = "@string" }, + Number = { icon = "#", hl = "@number" }, + Boolean = { icon = "⊨", hl = "@boolean" }, + Array = { icon = "", hl = "@constant" }, + Object = { icon = "⦿", hl = "@type" }, + Key = { icon = "🔐", hl = "@type" }, + Null = { icon = "NULL", hl = "@type" }, + EnumMember = { icon = "", hl = "@field" }, + Struct = { icon = "𝓢", hl = "@type" }, + Event = { icon = "🗲", hl = "@type" }, + Operator = { icon = "+", hl = "@operator" }, + TypeParameter = { icon = "𝙏", hl = "@parameter" }, + Component = { icon = "", hl = "@function" }, + Fragment = { icon = "", hl = "@constant" }, + }, + } + require("symbols-outline").setup(opts) + G.map({ + { "n", "", "SymbolsOutline", { noremap = true } }, + }) + end + }, +} diff --git a/lua/plugs/treesitter.lua b/lua/plugs/treesitter.lua new file mode 100644 index 0000000..e0056bb --- /dev/null +++ b/lua/plugs/treesitter.lua @@ -0,0 +1,53 @@ +return { + { + -- 语法高亮 -- + 'nvim-treesitter/nvim-treesitter', + config = function() + local treesitter_opt = { + ensure_installed = { + -- "c", + -- "cpp", + -- "python", + -- "java", + -- "lua", + -- "bash", + -- "vimdoc", + }, + indent = { enable = true }, + ignore_install = { + "txt" + }, + sync_install = false, + auto_install = true, + highlight = { + enable = true, + disable = function(_, buf) + local max_filesize = 100 * 1024 -- 100 KB + local ok, stats = pcall(G.loop.fs_stat, G.api.nvim_buf_get_name(buf)) + if ok and stats and stats.size > max_filesize then + return true + end + end, + additional_vim_regex_highlighting = false, + }, + parsers = { + vue = { + install_info = { + url = "https://github.com/ikatyang/tree-sitter-vue", + files = { "src/parser.c" }, + branch = "main", + }, + }, + }, + } + require 'nvim-treesitter'.setup(treesitter_opt) + require 'nvim-treesitter.install'.prefer_git = true + if G.use_ssh then + local parsers = require 'nvim-treesitter.parsers'.get_parser_configs() + for _, p in pairs(parsers) do + p.install_info.url = p.install_info.url:gsub("https://github.com/", "git@github.com:") + end + end + end + }, +} diff --git a/lua/plugs/winbar.lua b/lua/plugs/winbar.lua new file mode 100644 index 0000000..5798e0f --- /dev/null +++ b/lua/plugs/winbar.lua @@ -0,0 +1,40 @@ +return { + { + 'fgheng/winbar.nvim', + config = function() + require('winbar').setup({ + enabled = true, + + show_symbols = true, + + colors = { + path = '#aaffff', + file_name = '#bbbbff', + symbols = '#aaffaa', + }, + + icons = { + seperator = '>', + editor_state = '●', + lock_icon = '', + }, + + exclude_filetype = { + 'help', + 'startify', + 'dashboard', + 'packer', + 'neogitstatus', + 'NvimTree', + 'Trouble', + 'alpha', + 'lir', + 'Outline', + 'spectre_panel', + 'toggleterm', + 'qf', + } + }) + end + }, +}