feat(commands): add :Preview open subcommand (#6)
Problem: after closing a viewer, there was no way to re-open the last compiled output without recompiling. Solution: track the most recent output file per buffer in a `last_output` table that persists after compilation finishes. Add `compiler.open()`, `M.open()`, and wire it into the command dispatch.
This commit is contained in:
parent
0b16ff7178
commit
cfe101c6c4
5 changed files with 76 additions and 2 deletions
|
|
@ -12,6 +12,9 @@ local watching = {}
|
|||
---@type table<integer, true>
|
||||
local opened = {}
|
||||
|
||||
---@type table<integer, string>
|
||||
local last_output = {}
|
||||
|
||||
---@param val string[]|fun(ctx: preview.Context): string[]
|
||||
---@param ctx preview.Context
|
||||
---@return string[]
|
||||
|
|
@ -61,6 +64,10 @@ function M.compile(bufnr, name, provider, ctx)
|
|||
output_file = eval_string(provider.output, ctx)
|
||||
end
|
||||
|
||||
if output_file ~= '' then
|
||||
last_output[bufnr] = output_file
|
||||
end
|
||||
|
||||
log.dbg('compiling buffer %d with provider "%s": %s', bufnr, name, table.concat(cmd, ' '))
|
||||
|
||||
local obj = vim.system(
|
||||
|
|
@ -117,6 +124,7 @@ function M.compile(bufnr, name, provider, ctx)
|
|||
once = true,
|
||||
callback = function()
|
||||
M.stop(bufnr)
|
||||
last_output[bufnr] = nil
|
||||
end,
|
||||
})
|
||||
|
||||
|
|
@ -235,6 +243,18 @@ function M.clean(bufnr, name, provider, ctx)
|
|||
)
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@return boolean
|
||||
function M.open(bufnr)
|
||||
local output = last_output[bufnr]
|
||||
if not output then
|
||||
log.dbg('no last output file for buffer %d', bufnr)
|
||||
return false
|
||||
end
|
||||
vim.ui.open(output)
|
||||
return true
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@return preview.Status
|
||||
function M.status(bufnr)
|
||||
|
|
@ -254,6 +274,7 @@ M._test = {
|
|||
active = active,
|
||||
watching = watching,
|
||||
opened = opened,
|
||||
last_output = last_output,
|
||||
}
|
||||
|
||||
return M
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue