mirror of
https://github.com/cap153/nvim.git
synced 2025-12-31 14:55:07 +08:00
添加运行代码的code runner插件
This commit is contained in:
parent
0351e788f3
commit
7dff61113b
5 changed files with 128 additions and 40 deletions
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
-- ===
|
||||
-- === map function
|
||||
-- ===
|
||||
|
|
@ -156,39 +155,45 @@ mapcmd("<leader>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("", "<LEADER><LEADER>", "<Esc>/<++><CR>:nohlsearch<CR>c4l")
|
|||
-- 拼写检查
|
||||
mapcmd("<LEADER>sc", "set spell!")
|
||||
|
||||
-- 运行代码
|
||||
vim.cmd([[
|
||||
au filetype dart noremap r :wall<cr>:Telescope flutter commands<cr>
|
||||
au filetype python noremap r :wall<cr>:set splitbelow<cr>:sp<cr>:term uv run %<cr>
|
||||
au filetype go noremap r :wall<cr>:set splitbelow<cr>:sp<cr>:term go run %<cr>
|
||||
au filetype markdown noremap r :PeekClose<cr>:PeekOpen<cr>
|
||||
au filetype rust noremap r :wall<cr>:set splitbelow<cr>:sp<cr>:term cargo run<cr>
|
||||
]])
|
||||
-- ===
|
||||
-- === 运行代码(该功能已经迁移到plugins/coderunner.lua)
|
||||
-- ===
|
||||
|
||||
-- vim.cmd([[
|
||||
-- au filetype dart noremap r :wall<cr>:Telescope flutter commands<cr>
|
||||
-- au filetype python noremap r :wall<cr>:set splitbelow<cr>:sp<cr>:term uv run %<cr>
|
||||
-- au filetype go noremap r :wall<cr>:set splitbelow<cr>:sp<cr>:term go run %<cr>
|
||||
-- au filetype markdown noremap r :PeekClose<cr>:PeekOpen<cr>
|
||||
-- au filetype rust noremap r :wall<cr>:set splitbelow<cr>:sp<cr>:term cargo run<cr>
|
||||
-- ]])
|
||||
|
||||
-- ===
|
||||
-- === 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 .. '<cr>', { noremap = true })
|
||||
vim.api.nvim_set_keymap("n", key, ":" .. cmd .. "<cr>", { noremap = true })
|
||||
end
|
||||
function map:lua(key, txt)
|
||||
vim.api.nvim_set_keymap('n', key, ':lua ' .. txt .. '<cr>', { noremap = true })
|
||||
vim.api.nvim_set_keymap("n", key, ":lua " .. txt .. "<cr>", { noremap = true })
|
||||
end
|
||||
|
||||
return map
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ vim.opt.rtp:prepend(vim.env.LAZY or lazypath)
|
|||
-- 启动Lazy插件管理快捷键
|
||||
vim.keymap.set("n", "<leader>l", ":Lazy<CR>", { noremap = true })
|
||||
require("lazy").setup({
|
||||
-- 运行代码
|
||||
require("lazy.plugins.coderunner"),
|
||||
-- 自动补全插件
|
||||
require("lazy.plugins.blinkcmp"),
|
||||
-- lsp配置,全局的错误和警告提示,修复建议,重命名变量,格式化代码等等
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
79
lua/lazy/plugins/coderunner.lua
Normal file
79
lua/lazy/plugins/coderunner.lua
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
-- ===
|
||||
-- === 运行代码
|
||||
-- ===
|
||||
|
||||
-- 定义一个映射表,key 是文件类型(或包含多个类型的 table),value 是要绑定的命令
|
||||
-- 可以通过`echo &filetype`或者`lua= vim.bo.filetype`或者`lua print(vim.bo.filetype)`来获取当前打开文件的文件类型
|
||||
local keymaps = {
|
||||
[{
|
||||
"rust",
|
||||
"python",
|
||||
}] = { cmd = ":wall<cr>:RunCode<cr>", desc = "Save and Run Code" },
|
||||
markdown = { cmd = ":PeekClose<cr>:PeekOpen<cr>", desc = "Reload Markdown Preview" },
|
||||
dart = { cmd = ":wall<cr>:Telescope flutter commands<cr>", desc = "Save and Open Flutter Commands" },
|
||||
go = { cmd = ":wall<cr>:set splitbelow<cr>:sp<cr>:term go run %<cr>", 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,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue