From 7dff61113b1795867e035fb8f740221eb5cb77d9 Mon Sep 17 00:00:00 2001 From: a770 Date: Tue, 10 Jun 2025 22:14:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=BF=90=E8=A1=8C=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=9A=84code=20runner=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/README.md | 2 +- lua/core/keymap.lua | 79 ++++++++++++++++++--------------- lua/lazy/index.lua | 2 + lua/lazy/plugins/blinkcmp.lua | 6 +-- lua/lazy/plugins/coderunner.lua | 79 +++++++++++++++++++++++++++++++++ 5 files changed, 128 insertions(+), 40 deletions(-) create mode 100644 lua/lazy/plugins/coderunner.lua diff --git a/.github/README.md b/.github/README.md index 4de0ab7..614446f 100644 --- a/.github/README.md +++ b/.github/README.md @@ -4,7 +4,7 @@ 需要提前安装`npm`等等环境,如果是arch用户可以使用`paru`或者`yay`安装,示例如下 ```bash -paru -S python-neovim python-pip npm deno +paru -S npm deno imagemagick ueberzugpp ``` ![效果](效果.jpg) diff --git a/lua/core/keymap.lua b/lua/core/keymap.lua index 1c47cb4..254a34e 100644 --- a/lua/core/keymap.lua +++ b/lua/core/keymap.lua @@ -1,4 +1,3 @@ - -- === -- === map function -- === @@ -156,39 +155,45 @@ mapcmd("sr", "lua search_and_replace_current_file()") -- 替换当前目录及子目录下所有文件内容 function search_and_replace() - -- 获取用户输入的查找内容,使用 input() 函数动态输入替换内容 - local search_text = vim.fn.input("Search for: ") + -- 获取用户输入的查找内容,使用 input() 函数动态输入替换内容 + local search_text = vim.fn.input("Search for: ") - -- 获取用户输入的替换内容 - local replace_text = vim.fn.input("Replace with: ") + -- 获取用户输入的替换内容 + local replace_text = vim.fn.input("Replace with: ") - -- 执行替换命令 - if search_text ~= "" and replace_text ~= "" then - local cmd = 'execute "!grep -rl \\"' .. search_text .. '\\" ./ | xargs sed -i \\"s/' .. search_text .. '/' .. replace_text .. '/g\\""' - vim.cmd(cmd) - print("Replaced all occurrences of '" .. search_text .. "' with '" .. replace_text .. "'") - else - print("Search or replace text cannot be empty.") - end + -- 执行替换命令 + if search_text ~= "" and replace_text ~= "" then + local cmd = 'execute "!grep -rl \\"' + .. search_text + .. '\\" ./ | xargs sed -i \\"s/' + .. search_text + .. "/" + .. replace_text + .. '/g\\""' + vim.cmd(cmd) + print("Replaced all occurrences of '" .. search_text .. "' with '" .. replace_text .. "'") + else + print("Search or replace text cannot be empty.") + end end -- 替换当前文件内容 function search_and_replace_current_file() - -- 获取用户输入的查找内容 - local search_text = vim.fn.input("Search for in current file: ") + -- 获取用户输入的查找内容 + 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.") - end + -- 执行替换命令 + 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 -- === @@ -204,15 +209,17 @@ mapkey("", "", "/<++>:nohlsearchc4l") -- 拼写检查 mapcmd("sc", "set spell!") --- 运行代码 -vim.cmd([[ - au filetype dart noremap r :wall:Telescope flutter commands - au filetype python noremap r :wall:set splitbelow:sp:term uv run % - au filetype go noremap r :wall:set splitbelow:sp:term go run % - au filetype markdown noremap r :PeekClose:PeekOpen - au filetype rust noremap r :wall:set splitbelow:sp:term cargo run -]]) +-- === +-- === 运行代码(该功能已经迁移到plugins/coderunner.lua) +-- === +-- vim.cmd([[ +-- au filetype dart noremap r :wall:Telescope flutter commands +-- au filetype python noremap r :wall:set splitbelow:sp:term uv run % +-- au filetype go noremap r :wall:set splitbelow:sp:term go run % +-- au filetype markdown noremap r :PeekClose:PeekOpen +-- au filetype rust noremap r :wall:set splitbelow:sp:term cargo run +-- ]]) -- === -- === map function external environment @@ -227,10 +234,10 @@ function map:key(mode, lhs, rhs) vim.api.nvim_set_keymap(mode, lhs, rhs, { noremap = true }) end function map:cmd(key, cmd) - vim.api.nvim_set_keymap('n', key, ':' .. cmd .. '', { noremap = true }) + vim.api.nvim_set_keymap("n", key, ":" .. cmd .. "", { noremap = true }) end function map:lua(key, txt) - vim.api.nvim_set_keymap('n', key, ':lua ' .. txt .. '', { noremap = true }) + vim.api.nvim_set_keymap("n", key, ":lua " .. txt .. "", { noremap = true }) end return map diff --git a/lua/lazy/index.lua b/lua/lazy/index.lua index 85ae040..a8960f0 100644 --- a/lua/lazy/index.lua +++ b/lua/lazy/index.lua @@ -17,6 +17,8 @@ vim.opt.rtp:prepend(vim.env.LAZY or lazypath) -- 启动Lazy插件管理快捷键 vim.keymap.set("n", "l", ":Lazy", { noremap = true }) require("lazy").setup({ + -- 运行代码 + require("lazy.plugins.coderunner"), -- 自动补全插件 require("lazy.plugins.blinkcmp"), -- lsp配置,全局的错误和警告提示,修复建议,重命名变量,格式化代码等等 diff --git a/lua/lazy/plugins/blinkcmp.lua b/lua/lazy/plugins/blinkcmp.lua index d02d71a..fd0b18a 100644 --- a/lua/lazy/plugins/blinkcmp.lua +++ b/lua/lazy/plugins/blinkcmp.lua @@ -46,10 +46,10 @@ return { completion = { -- 示例:使用'prefix'对于'foo_|_bar'单词将匹配'foo_'(光标前面的部分),使用'full'将匹配'foo__bar'(整个单词) keyword = { range = "full" }, - -- 选择补全项目时显示文档(0.5秒延迟) - documentation = { auto_show = true, auto_show_delay_ms = 500 }, + -- 选择补全项目时显示文档(0秒延迟) + documentation = { auto_show = true, auto_show_delay_ms = 0 }, -- 不预选第一个项目,选中后自动插入该项目文本 - list = { selection = { preselect = false, auto_insert = true } }, + list = { selection = { preselect = false, auto_insert = false } }, -- 针对菜单的外观配置 -- menu = { -- min_width = 15, diff --git a/lua/lazy/plugins/coderunner.lua b/lua/lazy/plugins/coderunner.lua new file mode 100644 index 0000000..1831c0b --- /dev/null +++ b/lua/lazy/plugins/coderunner.lua @@ -0,0 +1,79 @@ +-- === +-- === 运行代码 +-- === + +-- 定义一个映射表,key 是文件类型(或包含多个类型的 table),value 是要绑定的命令 +-- 可以通过`echo &filetype`或者`lua= vim.bo.filetype`或者`lua print(vim.bo.filetype)`来获取当前打开文件的文件类型 +local keymaps = { + [{ + "rust", + "python", + }] = { cmd = ":wall:RunCode", desc = "Save and Run Code" }, + markdown = { cmd = ":PeekClose:PeekOpen", desc = "Reload Markdown Preview" }, + dart = { cmd = ":wall:Telescope flutter commands", desc = "Save and Open Flutter Commands" }, + go = { cmd = ":wall:set splitbelow:sp:term go run %", desc = "Save and Run Go file" }, +} +-- 创建自动命令组 +local ft_group = vim.api.nvim_create_augroup("FileTypeKeymaps", { clear = true }) +-- 遍历上面的表,为每个条目创建自动命令 +for filetype, mapping in pairs(keymaps) do + vim.api.nvim_create_autocmd("FileType", { + pattern = filetype, + group = ft_group, + callback = function(args) + vim.keymap.set("n", "r", mapping.cmd, { + noremap = true, + silent = true, + buffer = args.buf, + desc = mapping.desc, + }) + end, + }) +end + +return { + "CRAG666/code_runner.nvim", + dependencies = {}, + config = function() + require("code_runner").setup({ + -- project = { + -- ["~/sixsixsix"] = { + -- name = "sixsixsix", + -- description = "六爻网页排盘", + -- command = "cargo run --release" + -- }, + -- }, + filetype = { + python = "uv run $fileName", + rust = { + "cargo run", + -- "cd $dir &&", + -- "rustc $fileName &&", + -- "$dir/$fileNameWithoutExt", + }, + -- typescript = "deno run", + -- java = { + -- "cd $dir &&", + -- "javac $fileName &&", + -- "java $fileNameWithoutExt", + -- }, + -- c = function(...) + -- c_base = { + -- "cd $dir &&", + -- "gcc $fileName -o", + -- "/tmp/$fileNameWithoutExt", + -- } + -- local c_exec = { + -- "&& /tmp/$fileNameWithoutExt &&", + -- "rm /tmp/$fileNameWithoutExt", + -- } + -- vim.ui.input({ prompt = "Add more args:" }, function(input) + -- c_base[4] = input + -- vim.print(vim.tbl_extend("force", c_base, c_exec)) + -- require("code_runner.commands").run_from_fn(vim.list_extend(c_base, c_exec)) + -- end) + -- end, + }, + }) + end, +}