From cbf457999028cb81a076e68e5d080a03417522a6 Mon Sep 17 00:00:00 2001 From: newbieQQ Date: Thu, 11 Jun 2026 11:53:18 +0800 Subject: [PATCH] refactor: fix medium-severity issues (lazy leak, formatoptions, dupe map, dead opts, split theme, cmp keys, capabilities injection, typos, style) --- init.lua | 36 +++++++------- lua/G.lua | 1 - lua/keymap.lua | 4 -- lua/lsp/basedpyright.lua | 1 - lua/lsp/bash.lua | 1 - lua/lsp/c.lua | 1 - lua/lsp/go.lua | 1 - lua/lsp/json.lua | 4 +- lua/lsp/lua.lua | 2 - lua/lsp/yaml.lua | 1 - lua/options.lua | 12 +---- lua/plugs.lua | 4 +- lua/plugs/edit-plugs.lua | 31 +++++------- lua/plugs/nvim-cmp.lua | 10 ++-- lua/plugs/nvim-lspconfig.lua | 2 + lua/plugs/nvimtree.lua | 5 +- lua/plugs/symbols-outline.lua | 2 +- lua/plugs/theme.lua | 36 ++++++-------- lua/plugs/treesitter.lua | 9 ---- lua/plugs/winbar.lua | 2 +- snippets/lua.json | 90 ++++++++++++++++++++++++++++++++++- 21 files changed, 149 insertions(+), 106 deletions(-) diff --git a/init.lua b/init.lua index 45c6006..96411f5 100644 --- a/init.lua +++ b/init.lua @@ -1,31 +1,29 @@ G = require('G') --- Clone lazy.nvim (only needed for native Neovim, not VSCode) -if not G.g.vscode then - local lazypath = G.fn.stdpath("data") .. "/lazy/lazy.nvim" - if not G.loop.fs_stat(lazypath) then - G.fn.system({ - "git", - "clone", - "--filter=blob:none", - "https://github.com/folke/lazy.nvim.git", - "--branch=stable", - lazypath, - }) - end - G.opt.rtp:prepend(lazypath) +-- 安装/定位 lazy.nvim(VSCode 和原生 Neovim 都需要) +local lazypath = G.fn.stdpath('data') .. '/lazy/lazy.nvim' +if not G.loop.fs_stat(lazypath) then + G.fn.system({ + 'git', + 'clone', + '--filter=blob:none', + 'https://github.com/folke/lazy.nvim.git', + '--branch=stable', + lazypath, + }) end +G.opt.rtp:prepend(lazypath) -require("keymap") +require('keymap') if G.g.vscode then require('vscode') else - require("options") - local clone_prefix = G.use_ssh and "git@github.com:%s.git" or "https://github.com/%s.git" - require("lazy").setup( + require('options') + local clone_prefix = G.use_ssh and 'git@github.com:%s.git' or 'https://github.com/%s.git' + require('lazy').setup( require('plugs'), { - lockfile = G.fn.stdpath("data") .. "/lazy/lazy-lock.json", + lockfile = G.fn.stdpath('data') .. '/lazy/lazy-lock.json', git = { url_format = clone_prefix, } diff --git a/lua/G.lua b/lua/G.lua index 0590f0a..17909a7 100644 --- a/lua/G.lua +++ b/lua/G.lua @@ -22,7 +22,6 @@ function G.map(maps) else print("太多变量了") end - --G.api.nvim_set_keymap(map[1], map[2], map[3], map[4]) end end diff --git a/lua/keymap.lua b/lua/keymap.lua index 9b04fd1..b5496b7 100644 --- a/lua/keymap.lua +++ b/lua/keymap.lua @@ -20,20 +20,16 @@ G.map({ { 'n', '', ':tabn', opt }, { 'n', '', ':tabp', opt }, - { 'n', '', 'j', opt }, { 'n', '', 'h', opt }, { 'n', '', 'k', opt }, { 'n', '', 'l', opt }, - { 'n', '', ':q', opt }, { 'n', '', ':w !sudo tee %', opt }, { 'n', '', ':q!', opt }, { 'v', '', '"+y', opt }, - - { 'n', 'y', 'ggyG', opt }, { 'n', 'p', 'ggpG', opt }, { 'n', 'v', 'ggVG', opt }, diff --git a/lua/lsp/basedpyright.lua b/lua/lsp/basedpyright.lua index ebe6bad..873421c 100644 --- a/lua/lsp/basedpyright.lua +++ b/lua/lsp/basedpyright.lua @@ -1,5 +1,4 @@ return { - capabilities = require("cmp_nvim_lsp").default_capabilities(), settings = { basedpyright = { analysis = { diff --git a/lua/lsp/bash.lua b/lua/lsp/bash.lua index e6ef83f..978ef55 100644 --- a/lua/lsp/bash.lua +++ b/lua/lsp/bash.lua @@ -1,5 +1,4 @@ return{ - capabilities = require('cmp_nvim_lsp').default_capabilities(), on_attach = function() G.api.nvim_create_user_command('R', G.run_cmd('sh %'), {}) end diff --git a/lua/lsp/c.lua b/lua/lsp/c.lua index 9ebefd4..3c006ed 100644 --- a/lua/lsp/c.lua +++ b/lua/lsp/c.lua @@ -1,5 +1,4 @@ return { - capabilities = require('cmp_nvim_lsp').default_capabilities(), on_attach = function() G.api.nvim_create_user_command('R', G.run_cmd('g++ "%" -o "%<" && ./"%<" && rm "%<"'), {}) end diff --git a/lua/lsp/go.lua b/lua/lsp/go.lua index 07b3d3a..e5d19ca 100644 --- a/lua/lsp/go.lua +++ b/lua/lsp/go.lua @@ -1,5 +1,4 @@ return { - capabilities = require('cmp_nvim_lsp').default_capabilities(), on_attach = function() G.api.nvim_create_user_command('R', G.run_cmd('go run %'), {}) G.api.nvim_create_user_command('Rgin', G.run_cmd('go run ./main.go'), {}) diff --git a/lua/lsp/json.lua b/lua/lsp/json.lua index 1ee1daf..a564707 100644 --- a/lua/lsp/json.lua +++ b/lua/lsp/json.lua @@ -1,3 +1 @@ -return { - capabilities = require('cmp_nvim_lsp').default_capabilities(), -} +return {} diff --git a/lua/lsp/lua.lua b/lua/lsp/lua.lua index fdd0fde..288722d 100644 --- a/lua/lsp/lua.lua +++ b/lua/lsp/lua.lua @@ -5,8 +5,6 @@ table.insert(runtime_path, "lua/?/init.lua") return { - capabilities = require('cmp_nvim_lsp').default_capabilities(), - settings = { Lua = { diff --git a/lua/lsp/yaml.lua b/lua/lsp/yaml.lua index 7990cc8..2bd9f59 100644 --- a/lua/lsp/yaml.lua +++ b/lua/lsp/yaml.lua @@ -1,5 +1,4 @@ return { - capabilities = require('cmp_nvim_lsp').default_capabilities(), settings = { yaml = { schemas = { diff --git a/lua/options.lua b/lua/options.lua index b20cf55..57456b7 100644 --- a/lua/options.lua +++ b/lua/options.lua @@ -1,4 +1,3 @@ --- G.opt.ttimeout = true G.opt.ttimeoutlen = 100 @@ -39,15 +38,8 @@ G.opt.wrap = false G.cmd("filetype plugin indent on") -- 取消换行注释 -G.au({ "BufEnter" }, { - pattern = { "*" }, - callback = function() - -- vim.opt.formatoptions = vim.opt.formatoptions - { "c", "r", "o" } - G.opt.formatoptions = G.opt.formatoptions - - "o" -- O and o, don't continue comments - + "r" -- But do continue when pressing enter. - end, -}) +G.opt.formatoptions = G.opt.formatoptions - "o" + "r" + G.au({ "InsertEnter" }, { pattern = { "*" }, diff --git a/lua/plugs.lua b/lua/plugs.lua index 7467c69..b0b2319 100644 --- a/lua/plugs.lua +++ b/lua/plugs.lua @@ -7,6 +7,6 @@ return { require('plugs.winbar'), require('plugs.nvimtree'), require('plugs.theme'), - require("plugs.edit-plugs"), - require("plugs.dev"), + require('plugs.edit-plugs'), + require('plugs.dev'), } diff --git a/lua/plugs/edit-plugs.lua b/lua/plugs/edit-plugs.lua index 0fd3ac2..a209b64 100644 --- a/lua/plugs/edit-plugs.lua +++ b/lua/plugs/edit-plugs.lua @@ -13,31 +13,27 @@ return { { 'terryma/vim-multiple-cursors', }, + --格式整理 { - --格式整理 - { - 'junegunn/vim-easy-align', - config = function() - G.map({ - { "v", "ga", ":EasyAlign", { noremap = true } }, - { "v", "=", ":EasyAlign", { noremap = true } }, - }) - end - }, - }, - { - -- 注释插件 - { - 'tpope/vim-commentary', - } + 'junegunn/vim-easy-align', + config = function() + G.map({ + { "v", "ga", ":EasyAlign", { noremap = true } }, + { "v", "=", ":EasyAlign", { noremap = true } }, + }) + end }, + -- 注释插件(neovim 0.12 内置 gc,用 treesitter,先注释测试) + -- { + -- 'tpope/vim-commentary', + -- }, { 'github/copilot.vim', -- github copilot + event = "InsertEnter", }, { 'windwp/nvim-autopairs', event = "InsertEnter", - opts = {}, -- this is equalent to setup({}) function config = function() require('nvim-autopairs').setup({ disable_filetype = { "vim" }, @@ -69,7 +65,6 @@ return { "f", "F", "t", "T", "" }, - lazy = true, config = function() require("hop").setup { keys = 'asdfghjkl;' } local hop = require('hop') diff --git a/lua/plugs/nvim-cmp.lua b/lua/plugs/nvim-cmp.lua index a756d85..05f3d7b 100644 --- a/lua/plugs/nvim-cmp.lua +++ b/lua/plugs/nvim-cmp.lua @@ -18,7 +18,7 @@ return { }, config = function() local has_words_before = function() - unpack = unpack or table.unpack + local unpack = 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 @@ -43,17 +43,17 @@ return { mapping = cmp.mapping.preset.insert({ [""] = cmp.mapping({ - i = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true }), + i = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = false }), c = function(fallback) if cmp.visible() then - cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true }) + cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = false }) else fallback() end end, }), - [""] = cmp.mapping(function(fallback) + [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_next_item() elseif vim.fn["vsnip#available"](1) == 1 then @@ -65,7 +65,7 @@ return { end end, { "i", "s" }), - [""] = cmp.mapping(function() + [""] = cmp.mapping(function() if cmp.visible() then cmp.select_prev_item() elseif vim.fn["vsnip#jumpable"](-1) == 1 then diff --git a/lua/plugs/nvim-lspconfig.lua b/lua/plugs/nvim-lspconfig.lua index 2c46d6a..ddfb22d 100644 --- a/lua/plugs/nvim-lspconfig.lua +++ b/lua/plugs/nvim-lspconfig.lua @@ -19,7 +19,9 @@ return { jsonls = require('lsp.json'), } + local capabilities = require('cmp_nvim_lsp').default_capabilities() for server, config in pairs(servers) do + config.capabilities = capabilities vim.lsp.config(server, config) vim.lsp.enable(server) end diff --git a/lua/plugs/nvimtree.lua b/lua/plugs/nvimtree.lua index c2f2607..94c0729 100644 --- a/lua/plugs/nvimtree.lua +++ b/lua/plugs/nvimtree.lua @@ -1,7 +1,7 @@ return { "nvim-tree/nvim-tree.lua", dependencies = { - 'kyazdani42/nvim-web-devicons' + 'nvim-tree/nvim-web-devicons' }, keys = { "" @@ -33,8 +33,5 @@ return { }) end } - G.map({ - { "n", "", ":NvimTreeToggle", { noremap = true } }, - }) end } diff --git a/lua/plugs/symbols-outline.lua b/lua/plugs/symbols-outline.lua index a4ce924..3d481a9 100644 --- a/lua/plugs/symbols-outline.lua +++ b/lua/plugs/symbols-outline.lua @@ -20,7 +20,7 @@ return { wrap = false, keymaps = { close = { "", "q" }, - goto_location = "", + goto_location = "", focus_location = "h", hover_symbol = "", toggle_preview = "K", diff --git a/lua/plugs/theme.lua b/lua/plugs/theme.lua index badc411..3e1428f 100644 --- a/lua/plugs/theme.lua +++ b/lua/plugs/theme.lua @@ -7,8 +7,7 @@ return { end }, { - -- line插件 - 'kdheepak/tabline.nvim', + -- 状态栏 'nvim-lualine/lualine.nvim', config = function() require('lualine').setup { @@ -17,10 +16,6 @@ return { theme = 'auto', component_separators = { left = '', right = '' }, section_separators = { left = '', right = '' }, - disabled_filetypes = { - statusline = {}, - winbar = {}, - }, ignore_focus = {}, always_divide_middle = true, globalstatus = false, @@ -50,27 +45,26 @@ return { lualine_y = {}, lualine_z = {} }, - tabline = {}, - winbar = {}, - inactive_winbar = {}, - extensions = {} } + end, + }, + { + -- 标签栏 + 'kdheepak/tabline.nvim', + config = function() require('tabline').setup { - -- Defaults configuration options enable = true, options = { - -- If lualine is installed tabline will use separators configured in lualine by default. - -- These options can be used to override those settings. section_separators = { ' ', ' ' }, component_separators = { '', '' }, - max_bufferline_percent = 66, -- set to nil by default, and it uses vim.o.columns * 2/3 - show_tabs_always = false, -- this shows tabs only when there are more than one tab or if the first tab is named - show_devicons = true, -- this shows devicons in buffer section - show_bufnr = false, -- this appends [bufnr] to buffer section, - show_filename_only = true, -- shows base filename only instead of relative path in filename - modified_icon = "+ ", -- change the default modified icon - modified_italic = false, -- set to true by default; this determines whether the filename turns italic if modified - show_tabs_only = false, -- this shows only tabs instead of tabs + buffers + max_bufferline_percent = 66, + show_tabs_always = false, + show_devicons = true, + show_bufnr = false, + show_filename_only = true, + modified_icon = "+ ", + modified_italic = false, + show_tabs_only = false, }, } G.cmd [[ diff --git a/lua/plugs/treesitter.lua b/lua/plugs/treesitter.lua index e0056bb..4da2bfd 100644 --- a/lua/plugs/treesitter.lua +++ b/lua/plugs/treesitter.lua @@ -4,15 +4,6 @@ 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" diff --git a/lua/plugs/winbar.lua b/lua/plugs/winbar.lua index 5798e0f..3403186 100644 --- a/lua/plugs/winbar.lua +++ b/lua/plugs/winbar.lua @@ -14,7 +14,7 @@ return { }, icons = { - seperator = '>', + separator = '>', editor_state = '●', lock_icon = '', }, diff --git a/snippets/lua.json b/snippets/lua.json index 0db3279..21c64a7 100644 --- a/snippets/lua.json +++ b/snippets/lua.json @@ -1,3 +1,91 @@ { - + "plug": { + "prefix": "plug", + "body": [ + "{", + " '${1:owner/repo}',", + " config = function()", + " require('${2}').setup({$3})", + " end,", + "}," + ] + }, + "plugkey": { + "prefix": "plugkey", + "body": [ + "{", + " '${1:owner/repo}',", + " keys = { '${2}' },", + " config = function()", + " require('${3}').setup({$4})", + " end,", + "}," + ] + }, + "plugftp": { + "prefix": "plugftp", + "body": [ + "{", + " '${1:owner/repo}',", + " ft = { '${2}' },", + " config = function()", + " require('${3}').setup({$4})", + " end,", + "}," + ] + }, + "map": { + "prefix": "map", + "body": "{ '${1:n}', '${2:}', '${3:cmd}', { noremap = true } }," + }, + "mapl": { + "prefix": "mapl", + "body": [ + "{ '${1:n}', '${2:}',", + " function()", + " ${3}", + " end,", + " { noremap = true }", + "}," + ] + }, + "func": { + "prefix": "func", + "body": [ + "function ${1:name}(${2:args})", + " ${3}", + "end" + ] + }, + "localf": { + "prefix": "localf", + "body": [ + "local ${1:name} = function(${2:args})", + " ${3}", + "end" + ] + }, + "Gmap": { + "prefix": "Gmap", + "body": [ + "G.map({", + " { '${1:n}', '${2:}', '${3:cmd}', { noremap = true } },", + "})" + ] + }, + "req": { + "prefix": "req", + "body": "local ${1:name} = require('${1}')" + }, + "au": { + "prefix": "au", + "body": [ + "G.au({ '${1:BufEnter}' }, {", + " pattern = { '${2:*}' },", + " callback = function()", + " ${3}", + " end,", + "})" + ] + } }