diff --git a/lua/core/keymap.lua b/lua/core/keymap.lua index 4f9bbd9..2adebe9 100644 --- a/lua/core/keymap.lua +++ b/lua/core/keymap.lua @@ -2,8 +2,8 @@ -- === map function -- === -local function mapkey(mode, lhs, rhs) - vim.keymap.set(mode, lhs, rhs, { silent = true, nowait = true }) +local function mapkey(mode, lhs, rhs, opts) + vim.keymap.set(mode, lhs, rhs, vim.tbl_extend("force", { silent = true, nowait = true }, opts or {})) end local function mapcmd(key, cmd) @@ -90,6 +90,8 @@ maplua({ "x", "o", "n" }, "", smart_select("select_parent", 1), "扩大 Tree -- 缩小范围 (退格键):不断向下寻找子节点 maplua({ "x", "o" }, "", smart_select("select_child", -1), "缩小 Treesitter/LSP 范围") + + -- === -- === Cursor Movement -- === @@ -181,13 +183,9 @@ mapkey("x", "", ">gv") -- === 批量替换 -- === --- 设置快捷键,替换所有文件内容 -mapcmd("sa", "lua search_and_replace()") --- 设置快捷键,替换当前文件内容 -mapcmd("sr", "lua search_and_replace_current_file()") - -- 替换当前目录及子目录下所有文件内容 -function search_and_replace() +local function search_and_replace() + return function () -- 获取用户输入的查找内容,使用 input() 函数动态输入替换内容 local search_text = vim.fn.input("Search for: ") @@ -208,27 +206,33 @@ function search_and_replace() else print("Search or replace text cannot be empty.") end + end end -- 替换当前文件内容 -function search_and_replace_current_file() - -- 获取用户输入的查找内容 - local search_text = vim.fn.input("Search for in current file: ") +local function search_and_replace_current_file() + return function() + -- 获取用户输入的查找内容 + local search_text = vim.fn.input("Search for in current file: ") - -- 获取用户输入的替换内容 - local replace_text = vim.fn.input("Replace with: ") + -- 获取用户输入的替换内容 + local replace_text = vim.fn.input("Replace with: ") - -- 执行替换命令 - if search_text ~= "" and replace_text ~= "" then - -- 使用 sed 替换当前文件中的匹配内容,并正确转义引号 - local cmd = string.format("!sed -i 's/%s/%s/g' %%", search_text, replace_text) - vim.cmd(cmd) - print("Replaced all occurrences of '" .. search_text .. "' with '" .. replace_text .. "' in current file.") - else - print("Search or replace text cannot be empty.") + -- 执行替换命令 + if search_text ~= "" and replace_text ~= "" then + -- 使用 sed 替换当前文件中的匹配内容,并正确转义引号 + local cmd = string.format("!sed -i 's/%s/%s/g' %%", search_text, replace_text) + vim.cmd(cmd) + print("Replaced all occurrences of '" .. search_text .. "' with '" .. replace_text .. "' in current file.") + else + print("Search or replace text cannot be empty.") + end end end +maplua("n","sa",search_and_replace(), "替换当前目录及子目录下所有文件内容") +maplua("n","sr",search_and_replace_current_file(), "替换当前文件内容") + -- === -- === 临时“存档”文件当前的版本,并与后续的修改进行 diff 对比 -- === @@ -264,6 +268,12 @@ mapkey({ "n", "x", "o" }, "", "/<++>:nohlsearchc4l" -- 拼写检查 mapcmd("sc", "set spell!") +-- 注释快捷键 +mapkey("n", "cn", "gcc", { remap = true }) +mapkey("x", "cn", "gc", { remap = true }) +mapkey("n", "cu", "gcc", { remap = true }) +mapkey("x", "cu", "gc", { remap = true }) + -- === -- === 运行代码(该功能已经迁移到plugins/coderunner.lua) -- === diff --git a/lua/pack/configs/bufferline.lua b/lua/pack/configs/bufferline.lua index 6a18a14..b6e56fc 100644 --- a/lua/pack/configs/bufferline.lua +++ b/lua/pack/configs/bufferline.lua @@ -4,46 +4,45 @@ map:cmd('tu', 'enew') -- 关闭当前缓冲区,neovim自带的,完整命令bdelete map:cmd('tq', 'bd') --- 在缓冲区之间移动 -map:cmd('tn', 'BufferLineCyclePrev') -map:cmd('ti', 'BufferLineCycleNext') - --- 移动缓冲区的位置 -map:cmd('tmn', 'BufferLineMovePrev') -map:cmd('tmi', 'BufferLineMoveNext') - --- 关闭缓冲区 -map:cmd('tN', 'BufferLineCloseLeft') -map:cmd('tI', 'BufferLineCloseRight') -map:cmd('tQ', 'BufferLineCloseOthers') - -- === 顶部标签栏 (Bufferline) === if vim.g.vscode then return end local P = { - name = "bufferline.nvim", - module = "bufferline", - deps = { "nvim-web-devicons" }, -- 确保图标库先加载 + name = "bufferline.nvim", + module = "bufferline", + deps = { "nvim-web-devicons" }, -- 确保图标库先加载 } -- 懒加载触发器:打开或新建文件时触发 vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, { - once = true, - callback = function() - PackUtils.load(P, function(plugin) - plugin.setup({ - options = { - modified_icon = "", - buffer_close_icon = "×", - -- show_buffer_close_icons = false, - max_name_length = 14, - max_prefix_length = 13, - tab_size = 10, - indicator = { - style = "none", - }, - }, - }) - end) - end + once = true, + callback = function() + PackUtils.load(P, function(plugin) + -- 在缓冲区之间移动 + map:cmd('tn', 'BufferLineCyclePrev') + map:cmd('ti', 'BufferLineCycleNext') + + -- 移动缓冲区的位置 + map:cmd('tmn', 'BufferLineMovePrev') + map:cmd('tmi', 'BufferLineMoveNext') + + -- 关闭缓冲区 + map:cmd('tN', 'BufferLineCloseLeft') + map:cmd('tI', 'BufferLineCloseRight') + map:cmd('tQ', 'BufferLineCloseOthers') + plugin.setup({ + options = { + modified_icon = "", + buffer_close_icon = "×", + -- show_buffer_close_icons = false, + max_name_length = 14, + max_prefix_length = 13, + tab_size = 10, + indicator = { + style = "none", + }, + }, + }) + end) + end }) diff --git a/lua/pack/configs/kommentary.lua b/lua/pack/configs/kommentary.lua deleted file mode 100644 index 66176f0..0000000 --- a/lua/pack/configs/kommentary.lua +++ /dev/null @@ -1,26 +0,0 @@ --- 注释快捷键 -local map = require("core.keymap") -map:key("n", "cn", "kommentary_line_increase") -map:key("x", "cn", "kommentary_visual_increase") -- 选择模式下注释/反注释后退出该模式 -map:key("n", "cu", "kommentary_line_decrease") -map:key("x", "cu", "kommentary_visual_decrease") - -local P = { - name = "b3nj5m1n/kommentary", -- 仓库名 - module = "kommentary.config", -- require模块名 -} - --- 懒加载触发器 -vim.api.nvim_create_autocmd({ - "UIEnter", -}, { - callback = function() - vim.schedule(function() - PackUtils.load(P, function(plugin) - plugin.configure_language("default", { - prefer_single_line_comments = true, - }) - end) - end) - end -}) diff --git a/lua/pack/configs/lspconfig.lua b/lua/pack/configs/lspconfig.lua index 6f8ea51..c8f3dfd 100644 --- a/lua/pack/configs/lspconfig.lua +++ b/lua/pack/configs/lspconfig.lua @@ -7,115 +7,115 @@ local is_arm = arch:match("arm") or arch:match("aarch64") local servers = { "lua_ls", "rust_analyzer", "pylsp" } if not is_arm then - vim.list_extend(servers, { "marksman", "ts_ls", "svelte", "cssls", "html" }) + vim.list_extend(servers, { "marksman", "ts_ls", "svelte", "cssls", "html" }) end -- 2. 插件配置清单 local P = { - name = "nvim-lspconfig", - module = "lspconfig", - deps = { "mason.nvim", "mason-lspconfig.nvim", "inlay-hints.nvim" }, + name = "nvim-lspconfig", + module = "lspconfig", + deps = { "mason.nvim", "mason-lspconfig.nvim", "inlay-hints.nvim" }, } -- 3. 懒加载触发器:当打开文件时触发 vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, { - callback = function() - PackUtils.load(P, function() - -- === A. 基础依赖初始化 (Mason) === - require("mason").setup() - require("mason-lspconfig").setup({ - ensure_installed = servers, - }) - require("inlay-hints").setup() + callback = function() + PackUtils.load(P, function() + -- === A. 基础依赖初始化 (Mason) === + require("mason").setup() + require("mason-lspconfig").setup({ + ensure_installed = servers, + }) + require("inlay-hints").setup() - -- === B. 全局诊断设置 === - vim.diagnostic.config({ - signs = { - text = { - [vim.diagnostic.severity.ERROR] = "✘", - [vim.diagnostic.severity.WARN] = "▲", - [vim.diagnostic.severity.HINT] = "⚑", - [vim.diagnostic.severity.INFO] = "»", - }, - }, - }) + -- === B. 全局诊断设置 === + vim.diagnostic.config({ + signs = { + text = { + [vim.diagnostic.severity.ERROR] = "✘", + [vim.diagnostic.severity.WARN] = "▲", + [vim.diagnostic.severity.HINT] = "⚑", + [vim.diagnostic.severity.INFO] = "»", + }, + }, + }) - -- === C. 全局快捷键映射 === - -- local opts = { noremap = true, silent = true } - vim.keymap.set("n", "h", vim.lsp.buf.hover, opts) -- h显示提示文档 - vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts) -- gd跳转到定义 - vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts) -- gD跳转到声明(例如c语言中的头文件中的原型、一个变量的extern声明) - vim.keymap.set("n", "go", vim.lsp.buf.type_definition, opts) -- go跳转到变量类型定义的位置(例如一些自定义类型) - vim.keymap.set("n", "rn", vim.lsp.buf.rename, opts) -- rn变量重命名 - vim.keymap.set("n", "aw", vim.lsp.buf.code_action, opts) -- aw可以在出现警告或错误的地方打开建议的修复方法 - vim.keymap.set("n", "d", vim.diagnostic.open_float, opts) -- d浮动窗口显示所在行警告或错误信息 - vim.keymap.set("n", "-", vim.diagnostic.goto_prev, opts) -- -跳转到上一处警告或错误的地方 - vim.keymap.set("n", "=", vim.diagnostic.goto_next, opts) -- +跳转到下一处警告或错误的地方 - -- vim.keymap.set("n", "gr", vim.lsp.buf.references, opts) -- gr跳转到引用了对应变量或函数的位置,改用snacks - -- vim.keymap.set({ 'n', 'x' }, 'f', function() vim.lsp.buf.format({ async = true }) end, opts) -- f进行代码格式化 + -- === C. 全局快捷键映射 === + -- local opts = { noremap = true, silent = true } + vim.keymap.set("n", "h", vim.lsp.buf.hover, opts) -- h显示提示文档 + vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts) -- gd跳转到定义 + vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts) -- gD跳转到声明(例如c语言中的头文件中的原型、一个变量的extern声明) + vim.keymap.set("n", "go", vim.lsp.buf.type_definition, opts) -- go跳转到变量类型定义的位置(例如一些自定义类型) + vim.keymap.set("n", "rn", vim.lsp.buf.rename, opts) -- rn变量重命名 + vim.keymap.set("n", "aw", vim.lsp.buf.code_action, opts) -- aw可以在出现警告或错误的地方打开建议的修复方法 + vim.keymap.set("n", "d", vim.diagnostic.open_float, opts) -- d浮动窗口显示所在行警告或错误信息 + vim.keymap.set("n", "-", vim.diagnostic.goto_prev, opts) -- -跳转到上一处警告或错误的地方 + vim.keymap.set("n", "=", vim.diagnostic.goto_next, opts) -- +跳转到下一处警告或错误的地方 + -- vim.keymap.set("n", "gr", vim.lsp.buf.references, opts) -- gr跳转到引用了对应变量或函数的位置,改用snacks + -- vim.keymap.set({ 'n', 'x' }, 'f', function() vim.lsp.buf.format({ async = true }) end, opts) -- f进行代码格式化 - -- === D. 特定 LSP 配置 (使用 Neovim 0.11+ vim.lsp.config 语法) === + -- === D. 特定 LSP 配置 (使用 Neovim 0.11+ vim.lsp.config 语法) === - -- Python (pylsp) + uv 虚拟环境自适应 - vim.lsp.config("pylsp", { - on_init = function(client) - -- 1. 安全获取 root_dir - local root_dir = client.config.root_dir - -- 2. 只有当 root_dir 不为 nil 时才进行后续探测 - if root_dir then - local venv_python = root_dir .. "/.venv/bin/python" - -- 3. 检查虚拟环境文件是否真的存在且可读 - if vim.fn.filereadable(venv_python) == 1 then - client.config.settings.pylsp.plugins.jedi.environment = venv_python - -- 只有修改了设置才发送通知 - client.notify("workspace/didChangeConfiguration", { - settings = client.config.settings - }) - end - end - -- 无论是否找到虚拟环境,都返回 true 让 LSP 继续启动 - return true - end, - settings = { - pylsp = { - plugins = { - jedi = { environment = nil } - } - } - }, - }) + -- Python (pylsp) + uv 虚拟环境自适应 + vim.lsp.config("pylsp", { + on_init = function(client) + -- 1. 安全获取 root_dir + local root_dir = client.config.root_dir + -- 2. 只有当 root_dir 不为 nil 时才进行后续探测 + if root_dir then + local venv_python = root_dir .. "/.venv/bin/python" + -- 3. 检查虚拟环境文件是否真的存在且可读 + if vim.fn.filereadable(venv_python) == 1 then + client.config.settings.pylsp.plugins.jedi.environment = venv_python + -- 只有修改了设置才发送通知 + client.notify("workspace/didChangeConfiguration", { + settings = client.config.settings + }) + end + end + -- 无论是否找到虚拟环境,都返回 true 让 LSP 继续启动 + return true + end, + settings = { + pylsp = { + plugins = { + jedi = { environment = nil } + } + } + }, + }) - -- Lua (lua_ls) - vim.lsp.config("lua_ls", { - settings = { - ["Lua"] = { - hint = { enable = true }, - diagnostics = { globals = { "vim", "require", "opts", "PackUtils", "jit" } }, - }, - }, - }) + -- Lua (lua_ls) + vim.lsp.config("lua_ls", { + settings = { + ["Lua"] = { + hint = { enable = true }, + diagnostics = { globals = { "vim", "require", "opts", "PackUtils", "jit" } }, + }, + }, + }) - -- Go (gopls) - vim.lsp.config("gopls", { - settings = { - ["gopls"] = { - hints = { - rangeVariableTypes = true, - parameterNames = true, - constantValues = true, - assignVariableTypes = true, - compositeLiteralFields = true, - compositeLiteralTypes = true, - functionTypeParameters = true, - }, - }, - }, - }) + -- Go (gopls) + vim.lsp.config("gopls", { + settings = { + ["gopls"] = { + hints = { + rangeVariableTypes = true, + parameterNames = true, + constantValues = true, + assignVariableTypes = true, + compositeLiteralFields = true, + compositeLiteralTypes = true, + functionTypeParameters = true, + }, + }, + }, + }) - -- 遍历列表并正式启用服务器 - for _, server in ipairs(servers) do - vim.lsp.enable(server) - end - end) - end + -- 遍历列表并正式启用服务器 + for _, server in ipairs(servers) do + vim.lsp.enable(server) + end + end) + end }) diff --git a/lua/pack/plugins.lua b/lua/pack/plugins.lua index f0c967c..829330b 100644 --- a/lua/pack/plugins.lua +++ b/lua/pack/plugins.lua @@ -23,8 +23,6 @@ local specs = { "https://github.com/kevinhwang91/promise-async", -- conform.lua 格式化工具formatter "https://github.com/stevearc/conform.nvim", - -- kommentary.lua 注释插件 - "https://github.com/b3nj5m1n/kommentary", -- noice.lua 取代消息、命令行和弹出菜单的 UI "https://github.com/folke/noice.nvim", "https://github.com/MunifTanjim/nui.nvim", diff --git a/nvim-pack-lock.json b/nvim-pack-lock.json index 9fb3540..cf2753f 100644 --- a/nvim-pack-lock.json +++ b/nvim-pack-lock.json @@ -28,10 +28,6 @@ "rev": "11be32be3761c6263df2311afb6baa0de0863967", "src": "https://github.com/MysticalDevil/inlay-hints.nvim" }, - "kommentary": { - "rev": "d5a111a3bc4109a8f913a5863c9092b3b3801482", - "src": "https://github.com/b3nj5m1n/kommentary" - }, "mason-lspconfig.nvim": { "rev": "25f609e7fca78af7cede4f9fa3af8a94b1c4950b", "src": "https://github.com/williamboman/mason-lspconfig.nvim"