From 8e694fdca8ca4e793d5bdc20184cf9008e952973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl-Einar=20Bergstr=C3=B8m?= Date: Wed, 28 Oct 2026 13:18:45 +0100 Subject: [PATCH] feat: a link brings a hidden lazysecrets back, unless nvim is busy (#6) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit state(pid) and reveal(pid), for lazysecrets to call through $NVIM when a link is handed to it: the hidden window comes back when nvim is in normal or terminal mode; while you're typing, in a command or a picker, or mid-operator, the link opens elsewhere. Reviewed-on: https://git.kebcloud.no/keb/lazysecrets.nvim/pulls/6 Co-authored-by: Karl-Einar Bergstrøm --- README.md | 3 +++ lua/lazysecrets/init.lua | 33 +++++++++++++++++++++++++++++++++ tests/smoke.lua | 22 ++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/README.md b/README.md index 6d0bc90..311660f 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,9 @@ Brings [lazysecrets](https://github.com/karleinarb/lazysecrets) to Neovim. ## Features - Prioritizes showing the secrets in the repo you are in - The lazysecrets session stays alive for the whole nvim session +- A `lazysecrets://` link opened from the browser or chat brings the hidden + session back, unless you're in the middle of something (typing, a + command, a picker): then it opens elsewhere (lazysecrets 0.5 or later) - `e` on a file in lazysecrets will open it in your current nvim session - Follows the `:colorscheme` you have set in nvim diff --git a/lua/lazysecrets/init.lua b/lua/lazysecrets/init.lua index a2969c2..21b2beb 100644 --- a/lua/lazysecrets/init.lua +++ b/lua/lazysecrets/init.lua @@ -167,6 +167,39 @@ local function last_line(buf) end end +-- For lazysecrets itself, from outside (nvim --server), when a link is +-- handed to it: whether its window, with the pid, is "shown", "hidden" +-- and can be brought back, or "busy": nvim is in the middle of something +-- (typing, a command, a picker, a pending operator), which a window +-- popping up would break; the link then opens elsewhere. +---@param pid integer +---@return "shown"|"hidden"|"busy"|"none" +function M.state(pid) + if not running() or vim.fn.jobpid(term.job) ~= pid then + return "none" + end + if term.win and vim.api.nvim_win_is_valid(term.win) then + return "shown" + end + local mode = vim.api.nvim_get_mode() + if mode.blocking or not vim.tbl_contains({ "n", "nt", "t" }, mode.mode) then + return "busy" + end + return "hidden" +end + +-- Shows the window of the lazysecrets with the pid, if it's hidden and +-- nvim isn't busy (see state). Returns whether it's in view. +---@param pid integer +---@return boolean +function M.reveal(pid) + local state = M.state(pid) + if state == "hidden" then + show() + end + return state == "shown" or state == "hidden" +end + ---@param opts? { args?: string[], win?: table, cwd?: string } function M.open(opts) opts = opts or {} diff --git a/tests/smoke.lua b/tests/smoke.lua index 92aa167..71ce79b 100644 --- a/tests/smoke.lua +++ b/tests/smoke.lua @@ -42,5 +42,27 @@ vim.cmd.wincmd("p") m.open({ cwd = other .. "/sub" }) assert(job() ~= first, "opening in another repo reused the first one") +-- state and reveal, as lazysecrets asks them from outside for a link. +local pid = vim.fn.jobpid(job()) +assert(m.state(pid) == "shown", "state of the open window: " .. m.state(pid)) +assert(m.state(pid + 1) == "none", "state of another process") +vim.cmd.wincmd("p") +vim.wait(100, function() + return m.state(pid) == "hidden" +end) +assert(m.state(pid) == "hidden", "leaving the window didn't hide it: " .. m.state(pid)) + +local get_mode = vim.api.nvim_get_mode +vim.api.nvim_get_mode = function() + return { mode = "c", blocking = false } -- typing a command +end +assert(m.state(pid) == "busy" and not m.reveal(pid), "a link would interrupt a command") +assert(vim.api.nvim_win_get_config(0).relative == "", "reveal() showed the window while busy") +vim.api.nvim_get_mode = get_mode + +assert(m.reveal(pid), "reveal() of the hidden window") +assert(vim.api.nvim_win_get_config(0).relative == "editor", "reveal() didn't bring the window back") +assert(vim.fn.jobpid(job()) == pid, "reveal() brought back another lazysecrets") + vim.fn.delete(tmp, "rf") print("ok")