Compare commits
6 commits
build/nix-
...
fix/lifecy
| Author | SHA1 | Date | |
|---|---|---|---|
| f2432969bd | |||
| 781a9c4818 | |||
| 1456b3070a | |||
| bc22350692 | |||
| b1fe5597c6 | |||
| 9d4f2e77cc |
6 changed files with 143 additions and 4 deletions
|
|
@ -57,6 +57,12 @@ function M.setup()
|
|||
end,
|
||||
desc = 'Toggle, compile, clean, open, or check status of document preview',
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd('VimLeavePre', {
|
||||
callback = function()
|
||||
require('preview.compiler').stop_all()
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
|
|||
|
|
@ -58,6 +58,14 @@ end
|
|||
function M.compile(bufnr, name, provider, ctx, opts)
|
||||
opts = opts or {}
|
||||
|
||||
if vim.fn.executable(provider.cmd[1]) ~= 1 then
|
||||
vim.notify(
|
||||
'[preview.nvim]: "' .. provider.cmd[1] .. '" is not executable (run :checkhealth preview)',
|
||||
vim.log.levels.ERROR
|
||||
)
|
||||
return
|
||||
end
|
||||
|
||||
if vim.bo[bufnr].modified then
|
||||
vim.cmd('silent! update')
|
||||
end
|
||||
|
|
@ -170,7 +178,7 @@ function M.compile(bufnr, name, provider, ctx, opts)
|
|||
|
||||
active[bufnr] = { obj = obj, provider = name, output_file = output_file, is_reload = true }
|
||||
|
||||
vim.api.nvim_create_autocmd('BufWipeout', {
|
||||
vim.api.nvim_create_autocmd('BufUnload', {
|
||||
buffer = bufnr,
|
||||
once = true,
|
||||
callback = function()
|
||||
|
|
@ -230,7 +238,12 @@ function M.compile(bufnr, name, provider, ctx, opts)
|
|||
r.inject(output_file)
|
||||
r.broadcast()
|
||||
end
|
||||
if provider.open and not opened[bufnr] and output_file ~= '' then
|
||||
if
|
||||
provider.open
|
||||
and not opened[bufnr]
|
||||
and output_file ~= ''
|
||||
and vim.uv.fs_stat(output_file)
|
||||
then
|
||||
if provider.open == true then
|
||||
vim.ui.open(output_file)
|
||||
elseif type(provider.open) == 'table' then
|
||||
|
|
@ -279,7 +292,7 @@ function M.compile(bufnr, name, provider, ctx, opts)
|
|||
|
||||
active[bufnr] = { obj = obj, provider = name, output_file = output_file }
|
||||
|
||||
vim.api.nvim_create_autocmd('BufWipeout', {
|
||||
vim.api.nvim_create_autocmd('BufUnload', {
|
||||
buffer = bufnr,
|
||||
once = true,
|
||||
callback = function()
|
||||
|
|
@ -374,7 +387,7 @@ function M.toggle(bufnr, name, provider, ctx_builder)
|
|||
log.dbg('watching buffer %d with provider "%s"', bufnr, name)
|
||||
vim.notify('[preview.nvim]: watching with "' .. name .. '"', vim.log.levels.INFO)
|
||||
|
||||
vim.api.nvim_create_autocmd('BufWipeout', {
|
||||
vim.api.nvim_create_autocmd('BufUnload', {
|
||||
buffer = bufnr,
|
||||
once = true,
|
||||
callback = function()
|
||||
|
|
@ -452,6 +465,10 @@ function M.open(bufnr, open_config)
|
|||
log.dbg('no last output file for buffer %d', bufnr)
|
||||
return false
|
||||
end
|
||||
if not vim.uv.fs_stat(output) then
|
||||
log.dbg('output file no longer exists for buffer %d: %s', bufnr, output)
|
||||
return false
|
||||
end
|
||||
if type(open_config) == 'table' then
|
||||
local open_cmd = vim.list_extend({}, open_config)
|
||||
table.insert(open_cmd, output)
|
||||
|
|
|
|||
|
|
@ -146,6 +146,10 @@ end
|
|||
---@param bufnr? integer
|
||||
function M.compile(bufnr)
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||
if vim.api.nvim_buf_get_name(bufnr) == '' then
|
||||
vim.notify('[preview.nvim]: buffer has no file name', vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
local name = M.resolve_provider(bufnr)
|
||||
if not name then
|
||||
vim.notify('[preview.nvim]: no provider configured for this filetype', vim.log.levels.WARN)
|
||||
|
|
@ -165,6 +169,10 @@ end
|
|||
---@param bufnr? integer
|
||||
function M.clean(bufnr)
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||
if vim.api.nvim_buf_get_name(bufnr) == '' then
|
||||
vim.notify('[preview.nvim]: buffer has no file name', vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
local name = M.resolve_provider(bufnr)
|
||||
if not name then
|
||||
vim.notify('[preview.nvim]: no provider configured for this filetype', vim.log.levels.WARN)
|
||||
|
|
@ -178,6 +186,10 @@ end
|
|||
---@param bufnr? integer
|
||||
function M.toggle(bufnr)
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||
if vim.api.nvim_buf_get_name(bufnr) == '' then
|
||||
vim.notify('[preview.nvim]: buffer has no file name', vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
local name = M.resolve_provider(bufnr)
|
||||
if not name then
|
||||
vim.notify('[preview.nvim]: no provider configured for this filetype', vim.log.levels.WARN)
|
||||
|
|
@ -190,6 +202,10 @@ end
|
|||
---@param bufnr? integer
|
||||
function M.open(bufnr)
|
||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||
if vim.api.nvim_buf_get_name(bufnr) == '' then
|
||||
vim.notify('[preview.nvim]: buffer has no file name', vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
local name = M.resolve_provider(bufnr)
|
||||
local open_config = name and config.providers[name] and config.providers[name].open
|
||||
if not compiler.open(bufnr, open_config) then
|
||||
|
|
|
|||
|
|
@ -11,6 +11,19 @@ describe('commands', function()
|
|||
local cmds = vim.api.nvim_get_commands({})
|
||||
assert.is_not_nil(cmds.Preview)
|
||||
end)
|
||||
|
||||
it('registers VimLeavePre autocmd', function()
|
||||
require('preview.commands').setup()
|
||||
local aus = vim.api.nvim_get_autocmds({ event = 'VimLeavePre' })
|
||||
local found = false
|
||||
for _, au in ipairs(aus) do
|
||||
if au.callback then
|
||||
found = true
|
||||
break
|
||||
end
|
||||
end
|
||||
assert.is_true(found)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('dispatch', function()
|
||||
|
|
|
|||
|
|
@ -99,6 +99,35 @@ describe('compiler', function()
|
|||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('notifies and returns when binary is not executable', function()
|
||||
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
||||
vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test_nobin.txt')
|
||||
vim.bo[bufnr].modified = false
|
||||
|
||||
local notified = false
|
||||
local orig = vim.notify
|
||||
vim.notify = function(msg)
|
||||
if msg:find('not executable') then
|
||||
notified = true
|
||||
end
|
||||
end
|
||||
|
||||
local provider = { cmd = { 'totally_nonexistent_binary_xyz_preview' } }
|
||||
local ctx = {
|
||||
bufnr = bufnr,
|
||||
file = '/tmp/preview_test_nobin.txt',
|
||||
root = '/tmp',
|
||||
ft = 'text',
|
||||
}
|
||||
|
||||
compiler.compile(bufnr, 'nobin', provider, ctx)
|
||||
vim.notify = orig
|
||||
|
||||
assert.is_true(notified)
|
||||
assert.is_nil(compiler._test.active[bufnr])
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('fires PreviewCompileFailed on non-zero exit', function()
|
||||
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
||||
vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test_fail.txt')
|
||||
|
|
|
|||
|
|
@ -108,4 +108,62 @@ describe('preview', function()
|
|||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('unnamed buffer guard', function()
|
||||
before_each(function()
|
||||
helpers.reset_config({ typst = true })
|
||||
preview = require('preview')
|
||||
end)
|
||||
|
||||
local function capture_notify(fn)
|
||||
local msg = nil
|
||||
local orig = vim.notify
|
||||
vim.notify = function(m)
|
||||
msg = m
|
||||
end
|
||||
fn()
|
||||
vim.notify = orig
|
||||
return msg
|
||||
end
|
||||
|
||||
it('compile warns on unnamed buffer', function()
|
||||
local bufnr = helpers.create_buffer({}, 'typst')
|
||||
local msg = capture_notify(function()
|
||||
preview.compile(bufnr)
|
||||
end)
|
||||
assert.is_not_nil(msg)
|
||||
assert.is_truthy(msg:find('no file name'))
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('toggle warns on unnamed buffer', function()
|
||||
local bufnr = helpers.create_buffer({}, 'typst')
|
||||
local msg = capture_notify(function()
|
||||
preview.toggle(bufnr)
|
||||
end)
|
||||
assert.is_not_nil(msg)
|
||||
assert.is_truthy(msg:find('no file name'))
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('clean warns on unnamed buffer', function()
|
||||
local bufnr = helpers.create_buffer({}, 'typst')
|
||||
local msg = capture_notify(function()
|
||||
preview.clean(bufnr)
|
||||
end)
|
||||
assert.is_not_nil(msg)
|
||||
assert.is_truthy(msg:find('no file name'))
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
it('open warns on unnamed buffer', function()
|
||||
local bufnr = helpers.create_buffer({}, 'typst')
|
||||
local msg = capture_notify(function()
|
||||
preview.open(bufnr)
|
||||
end)
|
||||
assert.is_not_nil(msg)
|
||||
assert.is_truthy(msg:find('no file name'))
|
||||
helpers.delete_buffer(bufnr)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue