feat: nvim config tracks the live tree
Port the live ~/.config/nvim working tree (bcherb2/nvim master plus its uncommitted git.lua and minimap.lua edits): adds claudecode, eslint, lsp, typescript, meow-yarn and refactoring-fix specs, drops the files the live repo deleted, and takes live for everything else. Kept from public: lazy.lua with luarocks disabled, the ai_assist cond guard (nvim-ai-assist is not anonymously clonable, so a fresh box only gets it from the private clone list), and the scrubbed endpoint comment in place of live's LAN address.
This commit is contained in:
1 parent
9baaf78522
commit
0aaaf51421
28 files changed
+1001
-611
No files matched your search
@@ -23,6 +23,22 @@ local function image_debug(message)
|
||||
vim.fn.writefile({ line }, path, "a")
|
||||
end
|
||||
|
||||
-- Rendered mermaid/PlantUML rows all map to the block's CLOSING fence. If
|
||||
-- `lnum` is the closing fence of such a block, return the opening-fence line,
|
||||
-- else nil.
|
||||
local DIAGRAM_LANGS = { mermaid = true, plantuml = true, puml = true }
|
||||
local function diagram_open_fence(buf, lnum)
|
||||
local function get(l) return vim.api.nvim_buf_get_lines(buf, l - 1, l, false)[1] or "" end
|
||||
if not get(lnum):match("^%s*[`~][`~][`~]+%s*$") then return nil end
|
||||
for l = lnum - 1, math.max(1, lnum - 5000), -1 do
|
||||
local fence, info = get(l):match("^%s*([`~][`~][`~]+)%s*(%S*)")
|
||||
if fence then
|
||||
local lang = (info:match("^{?%.?([%w_-]+)") or ""):lower()
|
||||
return DIAGRAM_LANGS[lang] and l or nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
{
|
||||
"delphinus/md-render.nvim",
|
||||
@@ -32,6 +48,10 @@ return {
|
||||
{ "nvim-tree/nvim-web-devicons", version = "*" },
|
||||
},
|
||||
init = function()
|
||||
-- Rendered width: follows the window, capped at this many columns. A
|
||||
-- per-call `max_width` would pin it instead and overflow narrow windows.
|
||||
vim.g.md_render_max_width = 100
|
||||
|
||||
-- Ghostty does not normally forward TERM_PROGRAM /
|
||||
-- GHOSTTY_RESOURCES_DIR into the remote shell. Neovim 0.12 therefore
|
||||
-- cannot prove Kitty graphics support from the remote side on its own.
|
||||
@@ -40,7 +60,9 @@ return {
|
||||
--
|
||||
-- Keep the terminal identity as Ghostty. md-render uses that identity to
|
||||
-- avoid Ghostty's flaky stored-image re-placement path during redraws.
|
||||
if use_direct_kitty_bytes() and not vim.env.TERM_PROGRAM then
|
||||
-- herdr >= 0.9.5 sets TERM_PROGRAM=herdr, which md-render doesn't know,
|
||||
-- so supports_kitty() went false and mermaid fell back to code blocks.
|
||||
if use_direct_kitty_bytes() and (not vim.env.TERM_PROGRAM or vim.env.TERM_PROGRAM == "herdr") then
|
||||
vim.env.TERM_PROGRAM = "ghostty"
|
||||
end
|
||||
|
||||
@@ -57,8 +79,21 @@ return {
|
||||
-- drops to source on the right line and performs the edit. (Pure-motion
|
||||
-- keys h/j/k/w/G/… are deliberately NOT mapped, so you can still navigate
|
||||
-- the render freely.) Yank keys stay in the read-only render buffer so
|
||||
-- copying text does not dismiss rendered diagrams. Visual-entry keys hop
|
||||
-- to source so the resulting selection can be changed safely.
|
||||
-- copying text does not dismiss rendered diagrams, but yy and linewise /
|
||||
-- multi-line Visual y/Y copy the markdown SOURCE behind those rows (see
|
||||
-- MdRenderSourceYank below). Visual-entry keys hop to source so the
|
||||
-- resulting selection can be changed safely. Redo,
|
||||
-- case, increment, filter and format keys (<C-r> U ~ <C-a> <C-x> & !
|
||||
-- gu gU g~ g? gq gw gJ gp gP) hop too; on the render buffer they would
|
||||
-- only fail with E21.
|
||||
--
|
||||
-- Diagram guard: every row of a rendered mermaid/PlantUML image maps to
|
||||
-- the block's closing fence, so a destructive key there (dd, x, J, …)
|
||||
-- would delete/alter that fence and the unbalanced fence would swallow
|
||||
-- the rest of the document. For those keys (DIAGRAM_UNSAFE) we hop to
|
||||
-- the OPENING fence instead and don't replay, so you can edit the
|
||||
-- diagram source deliberately. p/gp (paste below the fence), u/<C-r>/U
|
||||
-- and v/V are still replayed.
|
||||
-- NOTE: the plugin sets the render buffer's filetype under
|
||||
-- `eventignore=all` (preview.lua, so markdown ftplugins/treesitter don't
|
||||
-- clobber the pre-rendered content), so a `FileType md-render` autocmd
|
||||
@@ -68,7 +103,46 @@ return {
|
||||
local HOP_KEYS = {
|
||||
"d", "D", "c", "C", "x", "X", "s", "S", "r", "R",
|
||||
"p", "P", "J", ">", "<", "=", "u", "v", "V", ".",
|
||||
"<C-r>", "U", "~", "<C-a>", "<C-x>", "&", "!",
|
||||
"gu", "gU", "g~", "g?", "gq", "gw", "gJ", "gp", "gP",
|
||||
}
|
||||
local DIAGRAM_UNSAFE = {}
|
||||
for _, key in ipairs({
|
||||
"d", "D", "c", "C", "x", "X", "s", "S", "r", "R", "J", ">", "<", "=",
|
||||
".", "~", "<C-a>", "<C-x>", "&", "!",
|
||||
"gu", "gU", "g~", "g?", "gq", "gw", "gJ", "P", "gP",
|
||||
}) do
|
||||
DIAGRAM_UNSAFE[key] = true
|
||||
end
|
||||
|
||||
-- Auto mode only returns to render on InsertLeave, so a normal-mode hop
|
||||
-- (dd, x, u, p, V…d, .) used to strand you in raw source ("images
|
||||
-- disappear after editing"). After a hop, go back to render the next
|
||||
-- time Neovim is idle in Normal mode (CursorHold; LazyVim 'updatetime'
|
||||
-- = 200ms). Disable with: vim.g.md_render_hop_return = false
|
||||
local function arm_return_to_render(src_buf)
|
||||
if vim.g.md_render_hop_return == false then return end
|
||||
local group = vim.api.nvim_create_augroup("MdRenderHopReturn" .. src_buf, { clear = true })
|
||||
local function disarm() pcall(vim.api.nvim_del_augroup_by_id, group) end
|
||||
vim.api.nvim_create_autocmd("CursorHold", {
|
||||
group = group,
|
||||
buffer = src_buf,
|
||||
callback = function()
|
||||
if vim.api.nvim_get_mode().mode ~= "n" then return end -- e.g. still in Visual
|
||||
disarm()
|
||||
local preview = require("md-render").preview
|
||||
if type(preview._schedule_auto_transition) == "function" then
|
||||
preview._schedule_auto_transition(src_buf, "render") -- no-op unless auto mode is on
|
||||
elseif vim.b[src_buf].md_render_auto and vim.api.nvim_get_current_buf() == src_buf then
|
||||
preview.toggle()
|
||||
end
|
||||
end,
|
||||
})
|
||||
-- Insert-entering hops (c/s/C/S/R) return via md-render's own
|
||||
-- InsertLeave; leaving the buffer cancels.
|
||||
vim.api.nvim_create_autocmd({ "InsertEnter", "BufLeave" }, { group = group, buffer = src_buf, callback = disarm })
|
||||
end
|
||||
|
||||
local hop_group = vim.api.nvim_create_augroup("MdRenderEditHop", { clear = true })
|
||||
vim.api.nvim_create_autocmd({ "BufEnter", "BufWinEnter" }, {
|
||||
group = hop_group,
|
||||
@@ -93,10 +167,23 @@ return {
|
||||
-- before any follow-up keystroke. That keeps multi-key operators
|
||||
-- in order — `d` becomes operator-pending and your next key (e.g.
|
||||
-- the second `d`, or `w`, `ip`) completes it natively in source.
|
||||
-- (The plugin schedules its insert-entry hop because those are
|
||||
-- single keys that immediately switch to Insert mode; operators
|
||||
-- need the ordering guarantee a sync feed provides.)
|
||||
-- (md-render's own insert-entry hop scheduled its feed and lost
|
||||
-- keys typed in the same burst; patch_auto_insert_hop in
|
||||
-- `config` makes it feed the same way as this.)
|
||||
require("md-render").preview.toggle()
|
||||
local src_buf = vim.api.nvim_get_current_buf()
|
||||
if vim.bo[src_buf].filetype == "md-render" then return end -- toggle refused
|
||||
|
||||
if DIAGRAM_UNSAFE[key] then
|
||||
local fence = diagram_open_fence(src_buf, vim.api.nvim_win_get_cursor(0)[1])
|
||||
if fence then
|
||||
vim.api.nvim_win_set_cursor(0, { fence, 0 })
|
||||
vim.notify(("md-render: '%s' not applied to a rendered diagram; jumped to its source fence. "
|
||||
.. "Edit here (<leader>mr, or leaving Insert mode, re-renders)."):format(key), vim.log.levels.INFO)
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
if key == "v" or key == "V" then
|
||||
-- Enter Visual mode before the user's next queued key arrives.
|
||||
-- feedkeys() would put v/V behind that key, turning `Vd` into
|
||||
@@ -104,8 +191,13 @@ return {
|
||||
local visual_count = count > 0 and tostring(count) or ""
|
||||
vim.cmd("normal! " .. visual_count .. key)
|
||||
else
|
||||
vim.api.nvim_feedkeys(prefix .. key, "n", false)
|
||||
-- vim.keycode: <C-r>/<C-a>/<C-x> must be fed as keys, not text.
|
||||
-- "i" inserts at the FRONT of the typeahead: when the rest of
|
||||
-- the command is already queued (fast typing, `gUU`, `dw`), an
|
||||
-- appended replay would run after it (`gUU` became `U` + `gU`).
|
||||
vim.api.nvim_feedkeys(vim.keycode(prefix .. key), "ni", false)
|
||||
end
|
||||
arm_return_to_render(src_buf)
|
||||
end, {
|
||||
buffer = ev.buf,
|
||||
noremap = true,
|
||||
@@ -115,29 +207,114 @@ return {
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
-- Yanking in the render buffer used to copy the decorated text (icons,
|
||||
-- padding, reflowed paragraphs), which pastes as garbage into markdown.
|
||||
-- yy and Visual y/Y (except a charwise selection within one rendered
|
||||
-- line, which keeps the native yank) copy the SOURCE lines behind the
|
||||
-- rendered rows instead, linewise; the view stays rendered. A rendered
|
||||
-- diagram yanks its whole fenced block. These yanks are set with
|
||||
-- setreg(), so they don't fire TextYankPost / enter yanky's ring.
|
||||
local yank_ns = vim.api.nvim_create_namespace("md_render_source_yank")
|
||||
|
||||
local function render_ctx()
|
||||
local ok, state = pcall(vim.api.nvim_win_get_var, 0, "md_render_state")
|
||||
if not ok or type(state) ~= "table" or state.mode ~= "render" then return nil end
|
||||
local session = require("md-render").preview._toggle_sessions[state.source_buf]
|
||||
if not session or session.buf ~= vim.api.nvim_get_current_buf() then return nil end
|
||||
local map = session.content and session.content.source_line_map
|
||||
if type(map) ~= "table" or #map == 0 or not vim.api.nvim_buf_is_valid(state.source_buf) then return nil end
|
||||
return map, state.source_buf
|
||||
end
|
||||
|
||||
local function source_lines_for(map, src_buf, r1, r2)
|
||||
r1 = math.max(1, math.min(r1, #map))
|
||||
r2 = math.max(r1, math.min(r2, #map))
|
||||
local total = vim.api.nvim_buf_line_count(src_buf)
|
||||
local s1 = math.max(1, math.min(map[r1], total))
|
||||
s1 = diagram_open_fence(src_buf, s1) or s1
|
||||
-- The last row's block ends right before the next row that maps to a
|
||||
-- later source line (covers joined paragraph lines and fences).
|
||||
local s2 = total
|
||||
for r = r2 + 1, #map do
|
||||
if map[r] > map[r2] then
|
||||
s2 = map[r] - 1
|
||||
break
|
||||
end
|
||||
end
|
||||
s2 = math.max(s1, math.min(s2, total))
|
||||
local lines = vim.api.nvim_buf_get_lines(src_buf, s1 - 1, s2, false)
|
||||
while #lines > 1 and lines[#lines]:match("^%s*$") do table.remove(lines) end
|
||||
return lines
|
||||
end
|
||||
|
||||
local function yank_source(buf, r1, r2, reg)
|
||||
local map, src_buf = render_ctx()
|
||||
if not map then return false end
|
||||
vim.fn.setreg(reg, source_lines_for(map, src_buf, r1, r2), "l")
|
||||
pcall(vim.hl.range, buf, yank_ns, "IncSearch", { r1 - 1, 0 }, { r2 - 1, 0 }, { regtype = "V", timeout = 150 })
|
||||
return true
|
||||
end
|
||||
|
||||
local yank_group = vim.api.nvim_create_augroup("MdRenderSourceYank", { clear = true })
|
||||
vim.api.nvim_create_autocmd({ "BufEnter", "BufWinEnter" }, {
|
||||
group = yank_group,
|
||||
desc = "md-render: yank in render view copies markdown source",
|
||||
callback = function(ev)
|
||||
if vim.bo[ev.buf].filetype ~= "md-render" then return end
|
||||
if vim.b[ev.buf].md_render_yank_installed then return end
|
||||
vim.b[ev.buf].md_render_yank_installed = true
|
||||
|
||||
vim.keymap.set("n", "yy", function()
|
||||
local r, count, reg = vim.fn.line("."), vim.v.count1, vim.v.register
|
||||
if not yank_source(ev.buf, r, r + count - 1, reg) then
|
||||
vim.api.nvim_feedkeys('"' .. reg .. count .. "yy", "n", false)
|
||||
end
|
||||
end, { buffer = ev.buf, desc = "md-render: yank markdown source line(s)" })
|
||||
|
||||
local function visual_yank(always_source)
|
||||
return function()
|
||||
if not render_ctx() then return "y" end
|
||||
if not always_source and vim.fn.mode() == "v" and vim.fn.line("v") == vim.fn.line(".") then
|
||||
return "y" -- charwise within one rendered line: native yank
|
||||
end
|
||||
local a, b, reg = vim.fn.line("v"), vim.fn.line("."), vim.v.register
|
||||
local buf = ev.buf
|
||||
vim.schedule(function() yank_source(buf, math.min(a, b), math.max(a, b), reg) end)
|
||||
return "<Esc>"
|
||||
end
|
||||
end
|
||||
vim.keymap.set("x", "y", visual_yank(false), {
|
||||
buffer = ev.buf, expr = true, desc = "md-render: yank markdown source of selection",
|
||||
})
|
||||
vim.keymap.set("x", "Y", visual_yank(true), {
|
||||
buffer = ev.buf, expr = true, desc = "md-render: yank markdown source of selected rows",
|
||||
})
|
||||
end,
|
||||
})
|
||||
end,
|
||||
keys = {
|
||||
{
|
||||
"<leader>mp",
|
||||
function() require("md-render").preview.show({ max_width = 140 }) end,
|
||||
function() require("md-render").preview.show() end,
|
||||
ft = "markdown",
|
||||
desc = "Markdown preview (wide float toggle)",
|
||||
desc = "Markdown preview (float toggle)",
|
||||
},
|
||||
{
|
||||
"<leader>mt",
|
||||
function() require("md-render").preview.show_tab({ max_width = 140 }) end,
|
||||
function() require("md-render").preview.show_tab() end,
|
||||
ft = "markdown",
|
||||
desc = "Markdown preview (wide tab toggle)",
|
||||
desc = "Markdown preview (tab toggle)",
|
||||
},
|
||||
{
|
||||
"<leader>ms",
|
||||
function() require("md-render").preview.split({ max_width = 140 }) end,
|
||||
function() require("md-render").preview.split() end,
|
||||
ft = "markdown",
|
||||
desc = "Markdown source/render split",
|
||||
},
|
||||
{
|
||||
"<leader>mr",
|
||||
function() require("md-render").preview.toggle({ max_width = 140 }) end,
|
||||
function() require("md-render").preview.toggle() end,
|
||||
ft = "markdown",
|
||||
desc = "Markdown render toggle in-place",
|
||||
},
|
||||
@@ -329,6 +506,116 @@ return {
|
||||
|
||||
patch_offscreen_mermaid()
|
||||
|
||||
-- md-render bug (v3.11.1, content_builder.lua ~2616/~2693): when a
|
||||
-- mermaid/PlantUML block becomes an image, the builder pops the
|
||||
-- already-emitted code lines from self.lines but NOT from
|
||||
-- self.source_line_map, so every rendered line below the diagram maps
|
||||
-- #removed lines too early (render-mode dd/x/p/o edit the wrong line).
|
||||
-- All map writes go through add_line, so re-align there. No-op once
|
||||
-- fixed upstream (the map is then never longer than the lines).
|
||||
local function patch_source_line_map()
|
||||
local ok, mod = pcall(require, "md-render.content_builder")
|
||||
local CB = ok and type(mod) == "table" and mod.ContentBuilder or nil
|
||||
if not CB or CB._thembones_source_map_patch then return end
|
||||
if type(CB.add_line) ~= "function" or type(CB.result) ~= "function" then return end
|
||||
CB._thembones_source_map_patch = true
|
||||
local original_add_line, original_result = CB.add_line, CB.result
|
||||
|
||||
CB.add_line = function(self, ...)
|
||||
original_add_line(self, ...)
|
||||
local map, n = self.source_line_map, #self.lines
|
||||
if type(map) == "table" and #map > n then
|
||||
local newest = map[#map] -- entry just appended for line n
|
||||
for i = #map, n, -1 do map[i] = nil end -- drop the stale tail
|
||||
map[n] = newest
|
||||
end
|
||||
end
|
||||
|
||||
CB.result = function(self, ...)
|
||||
local map, n = self.source_line_map, #self.lines
|
||||
if type(map) == "table" then
|
||||
for i = #map, n + 1, -1 do map[i] = nil end
|
||||
end
|
||||
return original_result(self, ...)
|
||||
end
|
||||
end
|
||||
|
||||
patch_source_line_map()
|
||||
|
||||
-- Session:rendered_to_source() rounds a linear interpolation between
|
||||
-- sync points; across a multi-row image/diagram the lower rows round to
|
||||
-- the NEXT source line (dd on the bottom half of an image deleted the
|
||||
-- blank line after it). Callers of the integer version (toggle cursor
|
||||
-- placement, so every hop key; the float footer) want the exact line,
|
||||
-- so snap to the raw map. Scroll sync uses the *_f variants, untouched.
|
||||
local function patch_exact_rendered_to_source()
|
||||
local ok, preview = pcall(require, "md-render.preview")
|
||||
local S = ok and type(preview) == "table" and preview._Session or nil
|
||||
if not S or S._thembones_exact_source_patch then return end
|
||||
if type(S.rendered_to_source) ~= "function" then return end
|
||||
S._thembones_exact_source_patch = true
|
||||
local original_rendered_to_source = S.rendered_to_source
|
||||
|
||||
S.rendered_to_source = function(self, rendered_line, ...)
|
||||
local map = self.content and self.content.source_line_map
|
||||
if type(map) == "table" and #map > 0 and type(rendered_line) == "number" then
|
||||
local v = map[math.max(1, math.min(math.floor(rendered_line), #map))]
|
||||
if type(v) == "number" and v > 0 then return v end
|
||||
end
|
||||
return original_rendered_to_source(self, rendered_line, ...)
|
||||
end
|
||||
end
|
||||
|
||||
patch_exact_rendered_to_source()
|
||||
|
||||
-- md-render's auto mode hops i/I/a/A/o/O from the render view to source
|
||||
-- with buffer-local maps that toggle() and then vim.schedule() an
|
||||
-- appended feedkeys (preview.lua install_auto_insert_keymaps). Keys
|
||||
-- typed in the same burst (fast typing, a paste through typeahead) were
|
||||
-- already queued, so they ran first in source Normal mode: `ohi<Esc>`
|
||||
-- turned "# Title" into "o# Title". After auto_on() installs those maps,
|
||||
-- replace them with the same hop fed synchronously at the FRONT of the
|
||||
-- typeahead (as the editing-key hop in `init` does), keeping a count.
|
||||
-- Only maps md-render itself installed (matched by desc) are replaced,
|
||||
-- and auto_off() still removes them.
|
||||
local function patch_auto_insert_hop()
|
||||
local ok, preview = pcall(require, "md-render.preview")
|
||||
if not ok or type(preview) ~= "table" or preview._thembones_insert_hop_patch then return end
|
||||
if type(preview.auto_on) ~= "function" or type(preview._toggle_sessions) ~= "table" then return end
|
||||
preview._thembones_insert_hop_patch = true
|
||||
local original_auto_on = preview.auto_on
|
||||
|
||||
local function install(render_buf)
|
||||
local stock = {}
|
||||
for _, map in ipairs(vim.api.nvim_buf_get_keymap(render_buf, "n")) do
|
||||
stock[map.lhs] = map.desc
|
||||
end
|
||||
for _, key in ipairs({ "i", "I", "a", "A", "o", "O" }) do
|
||||
local desc = "md-render auto: switch to source then " .. key
|
||||
if stock[key] == desc then
|
||||
vim.keymap.set("n", key, function()
|
||||
local count = vim.v.count
|
||||
preview.toggle()
|
||||
if vim.bo.filetype == "md-render" then return end -- toggle refused
|
||||
vim.api.nvim_feedkeys((count > 0 and tostring(count) or "") .. key, "ni", false)
|
||||
end, { buffer = render_buf, noremap = true, silent = true, desc = desc .. " (in order)" })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
preview.auto_on = function(...)
|
||||
original_auto_on(...)
|
||||
for src_buf, session in pairs(preview._toggle_sessions) do
|
||||
if vim.api.nvim_buf_is_valid(src_buf) and vim.b[src_buf].md_render_auto
|
||||
and session.buf and vim.api.nvim_buf_is_valid(session.buf) then
|
||||
install(session.buf)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
patch_auto_insert_hop()
|
||||
|
||||
-- md-render's Ghostty path intentionally re-transmits images on every
|
||||
-- placement so redraws stay visible. Its stock transport uses `t=f`,
|
||||
-- which means "the terminal reads this file path". Over SSH that path is
|
||||
@@ -753,6 +1040,84 @@ return {
|
||||
end
|
||||
end
|
||||
|
||||
-- md-render bug (v3.11.1, display_utils.lua place_images): image
|
||||
-- placements belong to a window, not to the render buffer in it. After
|
||||
-- :e / :b / a picker puts another file in that window, the window's
|
||||
-- WinScrolled/CursorMoved redraw keeps placing the images at the same
|
||||
-- rows over the new file. Only place them while the window still shows
|
||||
-- the buffer they were set up for, clear them when that buffer leaves
|
||||
-- the window, and put them back when it returns.
|
||||
local function patch_image_window_guard()
|
||||
local img = require("md-render.image")
|
||||
local du = require("md-render.display_utils")
|
||||
if img._thembones_win_guard_patch then return end
|
||||
img._thembones_win_guard_patch = true
|
||||
|
||||
local owner_buf = {} -- win -> buffer whose images that window shows
|
||||
local original_setup_images = du.setup_images
|
||||
du.setup_images = function(win, content, ns, opts)
|
||||
if vim.api.nvim_win_is_valid(win) then
|
||||
owner_buf[win] = opts and opts.buf or vim.api.nvim_win_get_buf(win)
|
||||
end
|
||||
return original_setup_images(win, content, ns, opts)
|
||||
end
|
||||
|
||||
local original_put_image = img.put_image
|
||||
img.put_image = function(image_id, win, ...)
|
||||
local owner = owner_buf[win]
|
||||
if owner and vim.api.nvim_win_is_valid(win) and vim.api.nvim_win_get_buf(win) ~= owner then
|
||||
return
|
||||
end
|
||||
return original_put_image(image_id, win, ...)
|
||||
end
|
||||
|
||||
local function session_for(buf)
|
||||
for _, session in pairs(require("md-render").preview._toggle_sessions or {}) do
|
||||
if session.buf == buf and session.image_state then return session end
|
||||
end
|
||||
end
|
||||
|
||||
local group = vim.api.nvim_create_augroup("MdRenderImageWindowGuard", { clear = true })
|
||||
vim.api.nvim_create_autocmd("BufWinLeave", {
|
||||
group = group,
|
||||
desc = "md-render: clear images when the render buffer leaves its window",
|
||||
callback = function(ev)
|
||||
local session = session_for(ev.buf)
|
||||
if not session then return end
|
||||
local state = session.image_state
|
||||
img.begin_batch()
|
||||
for _, id in pairs(state.image_ids or {}) do
|
||||
img.clear_placements(id)
|
||||
end
|
||||
for _, anim in pairs(state.anims or {}) do
|
||||
for _, id in ipairs(anim.frame_ids or {}) do
|
||||
img.clear_placements(id)
|
||||
end
|
||||
end
|
||||
img.flush_batch()
|
||||
end,
|
||||
})
|
||||
vim.api.nvim_create_autocmd("BufWinEnter", {
|
||||
group = group,
|
||||
desc = "md-render: re-place images when the render buffer returns",
|
||||
callback = function(ev)
|
||||
local session = session_for(ev.buf)
|
||||
if not session then return end
|
||||
local win = vim.api.nvim_get_current_win()
|
||||
if session.win == win then
|
||||
session.image_state.schedule_redraw()
|
||||
elseif not (vim.api.nvim_win_is_valid(session.win) and vim.api.nvim_win_get_buf(session.win) == session.buf) then
|
||||
-- Came back in a different window: move the images here, as
|
||||
-- md-render's own toggle does.
|
||||
session:cleanup_images()
|
||||
session:bind_window(win)
|
||||
end
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
patch_image_window_guard()
|
||||
|
||||
local function mermaid_sources(buf)
|
||||
local sources = {}
|
||||
local in_mermaid = false
|
||||
@@ -786,7 +1151,18 @@ return {
|
||||
|
||||
for _, source in ipairs(mermaid_sources(buf)) do
|
||||
if not image.get_mermaid_cached(source) then
|
||||
image.render_mermaid(source)
|
||||
-- Async so Esc / :w don't freeze for the mmdc run (~1s, up to 30s
|
||||
-- on the npx fallback). md-render's placement pass
|
||||
-- (display_utils.lua produce()) calls the same patched
|
||||
-- render_mermaid_async, which de-duplicates in-flight jobs per
|
||||
-- cache path (mermaid_inflight above), so it joins this job and
|
||||
-- rebuilds when the PNG lands. Until then the diagram shows
|
||||
-- md-render's "Rendering mermaid diagram..." placeholder.
|
||||
if image.render_mermaid_async then
|
||||
image.render_mermaid_async(source, function() end)
|
||||
else
|
||||
image.render_mermaid(source)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -794,6 +1170,10 @@ return {
|
||||
local function ensure_md_render_auto(buf)
|
||||
if not vim.api.nvim_buf_is_valid(buf) then return end
|
||||
if vim.bo[buf].filetype ~= "markdown" then return end
|
||||
-- Skip scratch markdown buffers (LSP hover, completion docs, noice) and
|
||||
-- buffers that aren't current: auto_on always targets the current buffer.
|
||||
if vim.bo[buf].buftype ~= "" then return end
|
||||
if vim.api.nvim_get_current_buf() ~= buf then return end
|
||||
|
||||
if not vim.b[buf].md_render_auto then
|
||||
local cache_group = vim.api.nvim_create_augroup("MdRenderMermaidCache" .. buf, { clear = true })
|
||||
@@ -803,7 +1183,7 @@ return {
|
||||
callback = function()
|
||||
prime_mermaid_cache(buf)
|
||||
end,
|
||||
desc = "Prime md-render Mermaid cache before redraw",
|
||||
desc = "Prime md-render Mermaid cache before redraw (async)",
|
||||
})
|
||||
end
|
||||
|
||||
@@ -818,7 +1198,7 @@ return {
|
||||
pcall(vim.api.nvim_win_del_var, win, "md_render_state")
|
||||
end
|
||||
|
||||
pcall(md.preview.auto_on, { max_width = 140 })
|
||||
pcall(md.preview.auto_on)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in new issue
Block a user