From 75c475fc3cd7d5cd1197620c6e19ebe965930739 Mon Sep 17 00:00:00 2001 From: G Date: Thu, 14 May 2026 17:12:06 -0700 Subject: [PATCH] fix pause in opencode window --- lua/lazyvim/plugins/extras/ai/opencode.lua | 464 +++++++++++---------- 1 file changed, 237 insertions(+), 227 deletions(-) diff --git a/lua/lazyvim/plugins/extras/ai/opencode.lua b/lua/lazyvim/plugins/extras/ai/opencode.lua index 4b808c4d..77f066b6 100644 --- a/lua/lazyvim/plugins/extras/ai/opencode.lua +++ b/lua/lazyvim/plugins/extras/ai/opencode.lua @@ -28,242 +28,218 @@ return { opts.spec = opts.spec or {} table.insert(opts.spec, { "a", group = "OpenCode" }) table.insert(opts.spec, { "ag", group = "git" }) - table.insert(opts.spec, { "ax", group = "diagnostics/quickfix" }) - if LazyVim.has("grapple.nvim") then - table.insert(opts.spec, { "af", group = "grapple" }) - end table.insert(opts.spec, { "ap", group = "prompt" }) end, }, - { - "folke/snacks.nvim", - }, - { "Nickvandyke/opencode.nvim", version = "*", - cmd = "OpenCode", - keys = function() - local keys = { - { "gO", false }, - { - "aa", - function() - require("opencode").toggle() - focus_opencode() - end, - mode = { "n", "t" }, - desc = "Toggle OpenCode", - }, - { - "aq", - function() - require("opencode").stop() - end, - mode = { "n", "t" }, - desc = "Stop/Close OpenCode", - }, - { - "as", - function() - require("opencode").select() - end, - mode = { "n", "x" }, - desc = "Select action", - }, - { - "ai", - function() - safe_ask("", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Ask (empty)", - }, - { - "aI", - function() - safe_ask("@this: ", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Ask with context", - }, - { - "ab", - function() - safe_ask("@buffer ", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Ask about buffer", - }, - { - "agd", - function() - safe_ask("@diff ", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Git diff", - }, - { - "ax", - group = "diagnostics/quickfix", - }, - { - "axx", - function() - safe_ask("@quickfix ", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Ask with quickfix", - }, - { - "axd", - function() - safe_ask("@diagnostics ", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Ask with diagnostics", - }, - { - "av", - function() - safe_ask("@visible ", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Ask with visible text", - }, - { - "aB", - function() - safe_ask("@buffers ", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Ask with all buffers", - }, - { - "am", - function() - safe_ask("@marks ", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Ask with marks", - }, - { - "ape", - function() - safe_ask("@this explain: ", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Explain", - }, - { - "apf", - function() - safe_ask("@this fix: ", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Fix", - }, - { - "apd", - function() - safe_ask("@this diagnose: ", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Diagnose", - }, - { - "apr", - function() - safe_ask("@this review: ", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Review", - }, - { - "apt", - function() - safe_ask("@this test: ", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Test", - }, - { - "apo", - function() - safe_ask("@this optimize: ", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Optimize", - }, - { - "an", - function() - require("opencode").command("session.new") - end, - mode = { "n" }, - desc = "New session", - }, - { - "ac", - function() - require("opencode").command("session.close") - end, - mode = { "n" }, - desc = "Close session", - }, - { - "go", - function() - return require("opencode").operator("@this ") - end, - expr = true, - mode = { "n", "x" }, - desc = "Add range to OpenCode", - }, - { - "goo", - function() - return require("opencode").operator("@this ") .. "_" - end, - expr = true, - mode = { "n" }, - desc = "Add line to OpenCode", - }, - } + dependencies = { + "nvim-lua/plenary.nvim", + }, + cmd = { "OpenCode", "OpenCodeToggle" }, + keys = { + { "gO", false }, - if LazyVim.has("grapple.nvim") then - table.insert(keys, { - "af", - function() - safe_ask("@grapple ", { submit = true, focus = false }) - end, - mode = { "n", "x" }, - desc = "Ask with grapple", - }) - end - - return keys - end, - config = function() - vim.g.opencode_opts = {} - - -- Fix for marksman crashing on opencode buffers - vim.api.nvim_create_autocmd({ "BufAdd", "BufWinEnter", "TermOpen" }, { - callback = function(event) - if vim.api.nvim_buf_get_name(event.buf):match("opencode") then - vim.bo[event.buf].filetype = "opencode" - vim.b[event.buf].lsp_ignore = true - -- Detach any clients that might have already attached - local clients = vim.lsp.get_clients({ bufnr = event.buf }) - for _, client in ipairs(clients) do - vim.lsp.buf_detach_client(event.buf, client.id) - end - end + { + "aa", + function() + require("opencode").toggle() + focus_opencode() end, - }) + mode = { "n", "t" }, + desc = "Toggle OpenCode", + }, + + { + "aq", + function() + require("opencode").stop() + end, + mode = { "n", "t" }, + desc = "Stop/Close OpenCode", + }, + + { + "as", + function() + require("opencode").select() + end, + mode = { "n", "x" }, + desc = "Select action", + }, + + { + "ai", + function() + safe_ask("", { submit = true, focus = false }) + end, + mode = { "n", "x" }, + desc = "Ask (empty)", + }, + + { + "aI", + function() + safe_ask("@this: ", { submit = true, focus = false }) + end, + mode = { "n", "x" }, + desc = "Ask with context", + }, + + { + "ab", + function() + safe_ask("@buffer ", { submit = true, focus = false }) + end, + mode = { "n", "x" }, + desc = "Ask about buffer", + }, + + { + "agd", + function() + safe_ask("@diff ", { submit = true, focus = false }) + end, + mode = { "n", "x" }, + desc = "Git diff", + }, + + { + "ax", + function() + safe_ask("@quickfix ", { submit = true, focus = false }) + end, + mode = { "n", "x" }, + desc = "Ask with quickfix", + }, + + { + "av", + function() + safe_ask("@visible ", { submit = true, focus = false }) + end, + mode = { "n", "x" }, + desc = "Ask with visible text", + }, + + { + "aB", + function() + safe_ask("@buffers ", { submit = true, focus = false }) + end, + mode = { "n", "x" }, + desc = "Ask with all buffers", + }, + + { + "am", + function() + safe_ask("@marks ", { submit = true, focus = false }) + end, + mode = { "n", "x" }, + desc = "Ask with marks", + }, + + { + "ape", + function() + safe_ask("@this explain: ", { submit = true, focus = false }) + end, + mode = { "n", "x" }, + desc = "Explain", + }, + + { + "apf", + function() + safe_ask("@this fix: ", { submit = true, focus = false }) + end, + mode = { "n", "x" }, + desc = "Fix", + }, + + { + "apd", + function() + safe_ask("@this diagnose: ", { submit = true, focus = false }) + end, + mode = { "n", "x" }, + desc = "Diagnose", + }, + + { + "apr", + function() + safe_ask("@this review: ", { submit = true, focus = false }) + end, + mode = { "n", "x" }, + desc = "Review", + }, + + { + "apt", + function() + safe_ask("@this test: ", { submit = true, focus = false }) + end, + mode = { "n", "x" }, + desc = "Test", + }, + + { + "apo", + function() + safe_ask("@this optimize: ", { submit = true, focus = false }) + end, + mode = { "n", "x" }, + desc = "Optimize", + }, + + { + "an", + function() + require("opencode").command("session.new") + end, + mode = { "n" }, + desc = "New session", + }, + + { + "ac", + function() + require("opencode").command("session.close") + end, + mode = { "n" }, + desc = "Close session", + }, + + { + "go", + function() + return require("opencode").operator("@this ") + end, + expr = true, + mode = { "n", "x" }, + desc = "Add range to OpenCode", + }, + + { + "goo", + function() + return require("opencode").operator("@this ") .. "_" + end, + expr = true, + mode = { "n" }, + desc = "Add line to OpenCode", + }, + }, + opts = function() + -- 1. Setup autogroups/autocmds for terminal optimizations + local opencode_augroup = vim.api.nvim_create_augroup("opencode_integrated", { clear = true }) vim.api.nvim_create_autocmd({ "BufEnter" }, { + group = opencode_augroup, pattern = "*:opencode --port*", callback = function() vim.cmd("startinsert") @@ -271,11 +247,15 @@ return { }) vim.api.nvim_create_autocmd({ "TermOpen" }, { - group = vim.api.nvim_create_augroup("opencode_integrated", { clear = true }), + -- group = vim.api.nvim_create_augroup("opencode_integrated", { clear = true }), + group = opencode_augroup, pattern = "*:opencode --port*", callback = function(event) vim.bo[event.buf].buflisted = false - vim.bo[event.buf].filetype = "opencode" + + -- Override space layout globally inside this exact buffer window + vim.keymap.set("t", "", "", { buffer = event.buf, nowait = true, noremap = true }) + vim.b[event.buf].which_key_ignore = true vim.keymap.set({ "t", "n" }, "", "h", { buffer = event.buf, desc = "Go to Left Window" }) vim.keymap.set( @@ -319,6 +299,36 @@ return { end end, }) + -- Essential: Return an empty table container to stop lazy.nvim fallback calls + return {} + end, + }, + + -- 2. Modify which-key v3 auto-triggers to fully drop terminal mode input tracking + { + "folke/which-key.nvim", + optional = true, + opts = function(_, opts) + opts.triggers = opts.triggers or {} + + -- Check if default auto triggers are present + local has_auto = false + for _, trigger in ipairs(opts.triggers) do + -- Standard v3 layout tables wrap the auto hook directive as an array string value + if type(trigger) == "table" and trigger == "" then + has_auto = true + -- Strip terminal mode ("t") and command mode ("c") from automatic hooking loops + trigger.mode = "nixso" + elseif trigger == "" then + -- Fallback mutation path if declared as a raw flat string pattern + has_auto = true + end + end + + -- If LazyVim default configs haven't initialized it, enforce a clean non-terminal rule + if not has_auto then + table.insert(opts.triggers, { "", mode = "nixso" }) + end end, }, }