From acb7a4ea8c7825d282913379c914873b438d09fe Mon Sep 17 00:00:00 2001 From: harivansh-afk Date: Thu, 20 Nov 2025 13:41:49 -0500 Subject: [PATCH] bg refac jobs --- lazy-lock.json | 17 +++-- lua/plugins/refactor.lua | 137 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 9 deletions(-) create mode 100644 lua/plugins/refactor.lua diff --git a/lazy-lock.json b/lazy-lock.json index 6ce2518..e8dd73b 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -1,9 +1,9 @@ { "Comment.nvim": { "branch": "master", "commit": "e30b7f2008e52442154b66f7c519bfd2f1e32acb" }, "arrow.nvim": { "branch": "master", "commit": "6e0f726f55f99332dd726a53effd6813786b6d49" }, - "avante.nvim": { "branch": "main", "commit": "f8a7cd1a606460ec0a2c4ec886bc102daccf912e" }, + "avante.nvim": { "branch": "main", "commit": "ca95e0386433da2077184719886fa658257261a3" }, "blink-cmp-copilot": { "branch": "main", "commit": "439cff78780c033aa23cf061d7315314b347e3c1" }, - "blink-ripgrep.nvim": { "branch": "main", "commit": "3f3f4424cb9bf7e5470b49d88f9edfc3abe57539" }, + "blink-ripgrep.nvim": { "branch": "main", "commit": "8adf3ff979ebe1ebbe6aa44fd317e4c9ae5bff91" }, "blink.cmp": { "branch": "main", "commit": "485c03400608cb6534bbf84da8c1c471fc4808c0" }, "colorful-menu.nvim": { "branch": "master", "commit": "932d589a55816063051ad9e599516a6e70a1c63f" }, "copilot.lua": { "branch": "master", "commit": "3123983d00ae6859f8bc987d14ebb524bb41b618" }, @@ -12,23 +12,22 @@ "dressing.nvim": { "branch": "master", "commit": "2d7c2db2507fa3c4956142ee607431ddb2828639" }, "flash.nvim": { "branch": "main", "commit": "fcea7ff883235d9024dc41e638f164a450c14ca2" }, "friendly-snippets": { "branch": "main", "commit": "572f5660cf05f8cd8834e096d7b4c921ba18e175" }, - "gitsigns.nvim": { "branch": "main", "commit": "20ad4419564d6e22b189f6738116b38871082332" }, + "gitsigns.nvim": { "branch": "main", "commit": "cdafc320f03f2572c40ab93a4eecb733d4016d07" }, "gruvbox": { "branch": "main", "commit": "5e0a460d8e0f7f669c158dedd5f9ae2bcac31437" }, "img-clip.nvim": { "branch": "main", "commit": "e7e29f0d07110405adecd576b602306a7edd507a" }, "lazy.nvim": { "branch": "main", "commit": "85c7ff3711b730b4030d03144f6db6375044ae82" }, "lualine.nvim": { "branch": "master", "commit": "3946f0122255bc377d14a59b27b609fb3ab25768" }, - "mason-lspconfig.nvim": { "branch": "main", "commit": "b1d9a914b02ba5660f1e272a03314b31d4576fe2" }, - "mason.nvim": { "branch": "main", "commit": "ad7146aa61dcaeb54fa900144d768f040090bff0" }, - "mini.animate": { "branch": "main", "commit": "0365de8b69331c25d0d0d7573407a7dc7719e578" }, + "mason-lspconfig.nvim": { "branch": "main", "commit": "7d527c76c43f46294de9c19d39c5a86317809b4b" }, + "mason.nvim": { "branch": "main", "commit": "57e5a8addb8c71fb063ee4acda466c7cf6ad2800" }, "neo-tree.nvim": { "branch": "v3.x", "commit": "f3df514fff2bdd4318127c40470984137f87b62e" }, - "neogit": { "branch": "master", "commit": "2cc5daffb838f718174d2c07f2ca0c39149bb2c1" }, + "neogit": { "branch": "master", "commit": "4ba620addbeeca2594c794b151e611c037973480" }, "nui.nvim": { "branch": "main", "commit": "de740991c12411b663994b2860f1a4fd0937c130" }, "nvim-autopairs": { "branch": "master", "commit": "7a2c97cccd60abc559344042fefb1d5a85b3e33b" }, - "nvim-lspconfig": { "branch": "master", "commit": "2010fc6ec03e2da552b4886fceb2f7bc0fc2e9c0" }, + "nvim-lspconfig": { "branch": "master", "commit": "b7c48a7111534b66bee077da8035ac7208a294ff" }, "nvim-treesitter": { "branch": "master", "commit": "42fc28ba918343ebfd5565147a42a26580579482" }, "nvim-web-devicons": { "branch": "master", "commit": "8dcb311b0c92d460fac00eac706abd43d94d68af" }, "plenary.nvim": { "branch": "master", "commit": "b9fd5226c2f76c951fc8ed5923d85e4de065e509" }, - "smear-cursor.nvim": { "branch": "main", "commit": "abfa5835920b1d76c0e24e1465a618ad914be90a" }, + "smear-cursor.nvim": { "branch": "main", "commit": "a00f5471aee7f5bcd20526374c70c2b87d5cd00f" }, "solarized.nvim": { "branch": "main", "commit": "c0dfe1cbfabd93b546baf5f1408f5df7e02e2050" }, "supermaven-nvim": { "branch": "main", "commit": "07d20fce48a5629686aefb0a7cd4b25e33947d50" }, "telescope.nvim": { "branch": "master", "commit": "d90956833d7c27e73c621a61f20b29fdb7122709" }, diff --git a/lua/plugins/refactor.lua b/lua/plugins/refactor.lua new file mode 100644 index 0000000..c6c9625 --- /dev/null +++ b/lua/plugins/refactor.lua @@ -0,0 +1,137 @@ +-- AI Refactor keybindings +-- Uses refactor.ts from ai-scripts repo to perform AI-powered refactoring +-- Write a comment at the end of the file with instructions, then press Space+ + +return { + { + "ai-refactor", + dir = vim.fn.expand("~/Documents/Github/ai-scripts"), + lazy = false, + config = function() + local refactor_script = vim.fn.expand("~/Documents/Github/ai-scripts/refactor.ts") + local active_jobs = {} + + -- Helper function to run refactor with a specific model + local function refactor_with_model(model) + return function() + local filepath = vim.fn.expand("%:p") + vim.cmd("write") + + vim.notify(string.format("Refactoring with model '%s'...", model), vim.log.levels.INFO) + + -- Run refactor script asynchronously in the background + -- Using nohup to detach from nvim - continues running if nvim closes + local log_file = vim.fn.expand("~/.ai/refactor-nvim.log") + local cmd = { + "sh", "-c", + string.format("nohup bun %s '%s' %s >> %s 2>&1 &", refactor_script, filepath, model, log_file) + } + + local job_id = vim.fn.jobstart(cmd, { + on_exit = function(_, exit_code) + active_jobs[job_id] = nil + if exit_code == 0 then + vim.schedule(function() + vim.cmd("checktime") -- Reload file if changed + vim.notify("Refactor complete!", vim.log.levels.INFO) + end) + else + vim.schedule(function() + vim.notify(string.format("Refactor failed with code %d", exit_code), vim.log.levels.ERROR) + end) + end + end, + on_stdout = function(_, data) + if data and #data > 0 then + vim.schedule(function() + for _, line in ipairs(data) do + if line ~= "" then + print(line) + end + end + end) + end + end, + on_stderr = function(_, data) + if data and #data > 0 then + vim.schedule(function() + for _, line in ipairs(data) do + if line ~= "" then + vim.notify(line, vim.log.levels.WARN) + end + end + end) + end + end, + }) + + if job_id > 0 then + active_jobs[job_id] = { + model = model, + file = vim.fn.fnamemodify(filepath, ":t"), + started = os.time() + } + end + end + end + + -- Command to list active refactor jobs + vim.api.nvim_create_user_command("RefactorJobs", function() + if vim.tbl_count(active_jobs) == 0 then + vim.notify("No active refactor jobs", vim.log.levels.INFO) + return + end + + local lines = { "Active refactor jobs:" } + for job_id, info in pairs(active_jobs) do + local elapsed = os.time() - info.started + table.insert(lines, string.format(" [%d] %s - %s (%ds)", job_id, info.model, info.file, elapsed)) + end + vim.notify(table.concat(lines, "\n"), vim.log.levels.INFO) + end, {}) + + -- GPT-5.1 models + vim.keymap.set("n", "ag", refactor_with_model("g"), { + desc = "Refactor with GPT-5.1 (medium)" + }) + vim.keymap.set("n", "aG", refactor_with_model("G"), { + desc = "Refactor with GPT-5.1 (high)" + }) + + -- Gemini-3 models + vim.keymap.set("n", "ai", refactor_with_model("i"), { + desc = "Refactor with Gemini-3 (medium)" + }) + vim.keymap.set("n", "aI", refactor_with_model("I"), { + desc = "Refactor with Gemini-3 (high)" + }) + + -- Claude Sonnet models + vim.keymap.set("n", "as", refactor_with_model("s"), { + desc = "Refactor with Sonnet-4.5 (medium)" + }) + vim.keymap.set("n", "aS", refactor_with_model("S"), { + desc = "Refactor with Sonnet-4.5 (high)" + }) + + -- Claude Opus models + vim.keymap.set("n", "ao", refactor_with_model("o"), { + desc = "Refactor with Opus-4 (medium)" + }) + vim.keymap.set("n", "aO", refactor_with_model("O"), { + desc = "Refactor with Opus-4 (high)" + }) + + -- xAI Grok models + vim.keymap.set("n", "ax", refactor_with_model("x"), { + desc = "Refactor with Grok-4 (medium)" + }) + vim.keymap.set("n", "aX", refactor_with_model("X"), { + desc = "Refactor with Grok-4 (high)" + }) + + print("AI Refactor keybindings loaded: a[gisoX] for different models") + print("Use :RefactorJobs to view active jobs, :messages to view output") + end, + }, +}