Compare commits
13 commits
build/nix-
...
fix/tmpfil
| Author | SHA1 | Date | |
|---|---|---|---|
| 20f44f44a2 | |||
| c1734eec8f | |||
|
|
272153a158 | ||
|
|
535d4cfd5e | ||
|
|
39406c559c | ||
|
|
aeea1bd8fa | ||
|
|
36e49cbd41 | ||
|
|
d1fd2b2a73 | ||
|
|
d102c9525b | ||
|
|
12cb20d154 | ||
|
|
1fbc307bad | ||
|
|
00caad18bf | ||
|
|
8c9847e321 |
11 changed files with 725 additions and 340 deletions
23
README.md
23
README.md
|
|
@ -1,6 +1,6 @@
|
||||||
# preview.nvim
|
# preview.nvim
|
||||||
|
|
||||||
**Universal document previewer for Neovim**
|
**Universal previewer for Neovim**
|
||||||
|
|
||||||
An extensible framework for compiling and previewing _any_ documents (LaTeX,
|
An extensible framework for compiling and previewing _any_ documents (LaTeX,
|
||||||
Typst, Markdown, etc.)—diagnostics included.
|
Typst, Markdown, etc.)—diagnostics included.
|
||||||
|
|
@ -81,3 +81,24 @@ vim.g.preview = {
|
||||||
typst = { open = { 'sioyek', '--new-instance' } },
|
typst = { open = { 'sioyek', '--new-instance' } },
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**Q: Markdown compilation drops `.html` files in my working directory. How do I
|
||||||
|
send output to `/tmp` instead?**
|
||||||
|
|
||||||
|
Override the `output` field. The `args` function references `ctx.output`, so the
|
||||||
|
compiled file lands wherever `output` points:
|
||||||
|
|
||||||
|
```lua
|
||||||
|
vim.g.preview = {
|
||||||
|
github = {
|
||||||
|
output = function(ctx)
|
||||||
|
return '/tmp/' .. vim.fn.fnamemodify(ctx.file, ':t:r') .. '.html'
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Q: How do I set up SyncTeX (forward/inverse search)?**
|
||||||
|
|
||||||
|
See `:help preview-synctex` for full recipes covering Zathura, Sioyek, and
|
||||||
|
Okular.
|
||||||
|
|
|
||||||
176
doc/preview.txt
176
doc/preview.txt
|
|
@ -22,10 +22,13 @@ CONTENTS *preview-contents*
|
||||||
3. Install ............................................... |preview-install|
|
3. Install ............................................... |preview-install|
|
||||||
4. Configuration ........................................... |preview-config|
|
4. Configuration ........................................... |preview-config|
|
||||||
5. Presets ............................................... |preview-presets|
|
5. Presets ............................................... |preview-presets|
|
||||||
|
- Math rendering ....................................... |preview-math|
|
||||||
6. Commands ............................................. |preview-commands|
|
6. Commands ............................................. |preview-commands|
|
||||||
7. Lua API ................................................... |preview-api|
|
7. Lua API ................................................... |preview-api|
|
||||||
8. Events ............................................... |preview-events|
|
8. Events ............................................... |preview-events|
|
||||||
9. Health ............................................... |preview-health|
|
9. Health ............................................... |preview-health|
|
||||||
|
10. FAQ ..................................................... |preview-faq|
|
||||||
|
11. SyncTeX ............................................. |preview-synctex|
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
REQUIREMENTS *preview-requirements*
|
REQUIREMENTS *preview-requirements*
|
||||||
|
|
@ -68,6 +71,10 @@ Provider fields: ~
|
||||||
receives a |preview.Context| and
|
receives a |preview.Context| and
|
||||||
returns a string[].
|
returns a string[].
|
||||||
|
|
||||||
|
{extra_args} (string[]|function) Appended to {args} after evaluation.
|
||||||
|
Useful for adding flags to a preset
|
||||||
|
without replacing its defaults.
|
||||||
|
|
||||||
{cwd} (string|function) Working directory. If a function,
|
{cwd} (string|function) Working directory. If a function,
|
||||||
receives a |preview.Context|.
|
receives a |preview.Context|.
|
||||||
Default: git root or file directory.
|
Default: git root or file directory.
|
||||||
|
|
@ -177,13 +184,49 @@ override individual fields by passing a table instead: >lua
|
||||||
`latex` latexmk -pdf → PDF (with clean support)
|
`latex` latexmk -pdf → PDF (with clean support)
|
||||||
`pdflatex` pdflatex → PDF (single pass, no latexmk)
|
`pdflatex` pdflatex → PDF (single pass, no latexmk)
|
||||||
`tectonic` tectonic → PDF (Rust-based LaTeX engine)
|
`tectonic` tectonic → PDF (Rust-based LaTeX engine)
|
||||||
`markdown` pandoc → HTML (standalone, embedded)
|
`markdown` pandoc → HTML (standalone, KaTeX math)
|
||||||
`github` pandoc → HTML (GitHub-styled, `-f gfm` input)
|
`github` pandoc → HTML (GitHub-styled, `-f gfm`, KaTeX math)
|
||||||
`asciidoctor` asciidoctor → HTML (AsciiDoc with SSE reload)
|
`asciidoctor` asciidoctor → HTML (AsciiDoc with SSE reload)
|
||||||
`plantuml` plantuml → SVG (UML diagrams, `.puml`)
|
`plantuml` plantuml → SVG (UML diagrams, `.puml`)
|
||||||
`mermaid` mmdc → SVG (Mermaid diagrams, `.mmd`)
|
`mermaid` mmdc → SVG (Mermaid diagrams, `.mmd`)
|
||||||
`quarto` quarto render → HTML (scientific publishing)
|
`quarto` quarto render → HTML (scientific publishing)
|
||||||
|
|
||||||
|
Math rendering (pandoc presets): ~
|
||||||
|
*preview-math*
|
||||||
|
|
||||||
|
The `markdown` and `github` presets use `--katex` by default, which inserts a
|
||||||
|
`<script>` tag that loads KaTeX from a CDN at view time. The browser fetches
|
||||||
|
the assets once and caches them, so math renders instantly on subsequent loads.
|
||||||
|
Requires internet on first view.
|
||||||
|
|
||||||
|
For offline use, swap in `--mathml` via `extra_args`. MathML is rendered
|
||||||
|
natively by the browser with no external dependencies: >lua
|
||||||
|
|
||||||
|
vim.g.preview = {
|
||||||
|
github = { extra_args = { '--mathml' } },
|
||||||
|
}
|
||||||
|
<
|
||||||
|
|
||||||
|
Note: pandoc's math flags (`--katex`, `--mathml`, `--mathjax`) are mutually
|
||||||
|
exclusive — last flag wins. Adding `--mathml` via `extra_args` (which is
|
||||||
|
appended after `args`) overrides `--katex`.
|
||||||
|
|
||||||
|
Self-contained output with `--embed-resources`: >lua
|
||||||
|
|
||||||
|
vim.g.preview = {
|
||||||
|
github = { extra_args = { '--embed-resources' } },
|
||||||
|
}
|
||||||
|
<
|
||||||
|
|
||||||
|
This inlines all external resources into the HTML. With `--katex` this adds
|
||||||
|
~15s of compile time per save (pandoc fetches KaTeX from the CDN during
|
||||||
|
compilation). Pair with `--mathml` to avoid the penalty: >lua
|
||||||
|
|
||||||
|
vim.g.preview = {
|
||||||
|
github = { extra_args = { '--embed-resources', '--mathml' } },
|
||||||
|
}
|
||||||
|
<
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
COMMANDS *preview-commands*
|
COMMANDS *preview-commands*
|
||||||
|
|
||||||
|
|
@ -272,5 +315,134 @@ Checks: ~
|
||||||
- Each configured provider's binary is executable
|
- Each configured provider's binary is executable
|
||||||
- Each configured provider's opener binary (if any) is executable
|
- Each configured provider's opener binary (if any) is executable
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
FAQ *preview-faq*
|
||||||
|
|
||||||
|
Q: Markdown/GFM compilation drops `.html` files in my working directory.
|
||||||
|
How do I send output to `/tmp` instead?
|
||||||
|
|
||||||
|
A: Override the `output` field. The `args` function references `ctx.output`,
|
||||||
|
so the compiled file lands wherever `output` points: >lua
|
||||||
|
|
||||||
|
vim.g.preview = {
|
||||||
|
github = {
|
||||||
|
output = function(ctx)
|
||||||
|
return '/tmp/' .. vim.fn.fnamemodify(ctx.file, ':t:r') .. '.html'
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
<
|
||||||
|
|
||||||
|
==============================================================================
|
||||||
|
SYNCTEX *preview-synctex*
|
||||||
|
|
||||||
|
SyncTeX enables bidirectional navigation between LaTeX source and the
|
||||||
|
compiled PDF. The `latex` preset compiles with `-synctex=1` by default.
|
||||||
|
|
||||||
|
Forward search (editor -> viewer) requires caching the output path.
|
||||||
|
Inverse search (viewer -> editor) requires a fixed Neovim server socket.
|
||||||
|
|
||||||
|
The following configs leverage the below basic setup: ~
|
||||||
|
|
||||||
|
>lua
|
||||||
|
vim.fn.serverstart('/tmp/nvim-preview.sock')
|
||||||
|
|
||||||
|
local synctex_pdf = {}
|
||||||
|
vim.api.nvim_create_autocmd('User', {
|
||||||
|
pattern = 'PreviewCompileSuccess',
|
||||||
|
callback = function(args)
|
||||||
|
synctex_pdf[args.data.bufnr] = args.data.output
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
<
|
||||||
|
|
||||||
|
The recipes below bind `<leader>s` for forward search. To scroll the PDF
|
||||||
|
automatically on cursor movement, call the forward search function from a
|
||||||
|
|CursorMoved| or |CursorHold| autocmd instead.
|
||||||
|
|
||||||
|
Viewer-specific recipes: ~
|
||||||
|
|
||||||
|
*preview-synctex-zathura*
|
||||||
|
Zathura ~
|
||||||
|
|
||||||
|
Inverse search: Ctrl+click.
|
||||||
|
|
||||||
|
>lua
|
||||||
|
vim.keymap.set('n', '<leader>s', function()
|
||||||
|
local pdf = synctex_pdf[vim.api.nvim_get_current_buf()]
|
||||||
|
if pdf then
|
||||||
|
vim.fn.jobstart({
|
||||||
|
'zathura', '--synctex-forward',
|
||||||
|
vim.fn.line('.') .. ':0:' .. vim.fn.expand('%:p'), pdf,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
vim.g.preview = {
|
||||||
|
latex = {
|
||||||
|
open = {
|
||||||
|
'zathura',
|
||||||
|
'--synctex-editor-command',
|
||||||
|
'nvim --server /tmp/nvim-preview.sock'
|
||||||
|
.. [[ --remote-expr "execute('b +%{line} %{input}')"]],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
<
|
||||||
|
|
||||||
|
*preview-synctex-sioyek*
|
||||||
|
Sioyek ~
|
||||||
|
|
||||||
|
Inverse search: right-click with synctex mode active.
|
||||||
|
|
||||||
|
Add to `~/.config/sioyek/prefs_user.config`: >
|
||||||
|
inverse_search_command nvim --server /tmp/nvim-preview.sock --remote-expr "execute('b +%2 %1')"
|
||||||
|
<
|
||||||
|
|
||||||
|
>lua
|
||||||
|
vim.keymap.set('n', '<leader>s', function()
|
||||||
|
local pdf = synctex_pdf[vim.api.nvim_get_current_buf()]
|
||||||
|
if pdf then
|
||||||
|
vim.fn.jobstart({
|
||||||
|
'sioyek',
|
||||||
|
'--instance-name', 'preview',
|
||||||
|
'--forward-search-file', vim.fn.expand('%:p'),
|
||||||
|
'--forward-search-line', tostring(vim.fn.line('.')),
|
||||||
|
pdf,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
vim.g.preview = {
|
||||||
|
latex = {
|
||||||
|
open = { 'sioyek', '--instance-name', 'preview' },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
<
|
||||||
|
|
||||||
|
*preview-synctex-okular*
|
||||||
|
Okular ~
|
||||||
|
|
||||||
|
Inverse search (Shift+click): one-time GUI setup via
|
||||||
|
Settings -> Configure Okular -> Editor -> Custom Text Editor: >
|
||||||
|
nvim --server /tmp/nvim-preview.sock --remote-expr "execute('b +%l %f')"
|
||||||
|
<
|
||||||
|
|
||||||
|
>lua
|
||||||
|
vim.keymap.set('n', '<leader>s', function()
|
||||||
|
local pdf = synctex_pdf[vim.api.nvim_get_current_buf()]
|
||||||
|
if pdf then
|
||||||
|
vim.fn.jobstart({
|
||||||
|
'okular', '--unique',
|
||||||
|
('%s#src:%d:%s'):format(pdf, vim.fn.line('.'), vim.fn.expand('%:p')),
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
vim.g.preview = {
|
||||||
|
latex = { open = { 'okular', '--unique' } },
|
||||||
|
}
|
||||||
|
<
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
vim:tw=78:ts=8:ft=help:norl:
|
vim:tw=78:ts=8:ft=help:norl:
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,8 @@
|
||||||
{
|
{
|
||||||
formatter = forEachSystem (pkgs: pkgs.nixfmt-tree);
|
formatter = forEachSystem (pkgs: pkgs.nixfmt-tree);
|
||||||
|
|
||||||
devShells = forEachSystem (pkgs:
|
devShells = forEachSystem (
|
||||||
|
pkgs:
|
||||||
let
|
let
|
||||||
devTools = [
|
devTools = [
|
||||||
(pkgs.luajit.withPackages (
|
(pkgs.luajit.withPackages (
|
||||||
|
|
@ -48,8 +49,11 @@
|
||||||
pkgs.quarto
|
pkgs.quarto
|
||||||
pkgs.plantuml
|
pkgs.plantuml
|
||||||
pkgs.mermaid-cli
|
pkgs.mermaid-cli
|
||||||
|
pkgs.zathura
|
||||||
|
pkgs.sioyek
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
});
|
}
|
||||||
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,45 +3,118 @@ local M = {}
|
||||||
local diagnostic = require('preview.diagnostic')
|
local diagnostic = require('preview.diagnostic')
|
||||||
local log = require('preview.log')
|
local log = require('preview.log')
|
||||||
|
|
||||||
---@type table<integer, preview.Process>
|
---@class preview.BufState
|
||||||
local active = {}
|
---@field watching boolean
|
||||||
|
---@field process? table
|
||||||
|
---@field is_reload? boolean
|
||||||
|
---@field provider? string
|
||||||
|
---@field output? string
|
||||||
|
---@field viewer? table
|
||||||
|
---@field viewer_open? boolean
|
||||||
|
---@field open_watcher? uv.uv_fs_event_t
|
||||||
|
---@field output_watcher? uv.uv_fs_event_t
|
||||||
|
---@field has_errors? boolean
|
||||||
|
---@field debounce? uv.uv_timer_t
|
||||||
|
---@field bwp_autocmd? integer
|
||||||
|
---@field cleanup_autocmd? integer
|
||||||
|
|
||||||
---@type table<integer, integer>
|
---@type table<integer, preview.BufState>
|
||||||
local watching = {}
|
local state = {}
|
||||||
|
|
||||||
---@type table<integer, true>
|
|
||||||
local opened = {}
|
|
||||||
|
|
||||||
---@type table<integer, string>
|
|
||||||
local last_output = {}
|
|
||||||
|
|
||||||
---@type table<integer, table>
|
|
||||||
local viewer_procs = {}
|
|
||||||
|
|
||||||
---@type table<integer, uv.uv_fs_event_t>
|
|
||||||
local open_watchers = {}
|
|
||||||
|
|
||||||
local debounce_timers = {}
|
|
||||||
|
|
||||||
local DEBOUNCE_MS = 500
|
local DEBOUNCE_MS = 500
|
||||||
|
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
local function stop_open_watcher(bufnr)
|
---@return preview.BufState
|
||||||
local w = open_watchers[bufnr]
|
local function get_state(bufnr)
|
||||||
if w then
|
if not state[bufnr] then
|
||||||
w:stop()
|
state[bufnr] = { watching = false }
|
||||||
w:close()
|
|
||||||
open_watchers[bufnr] = nil
|
|
||||||
end
|
end
|
||||||
|
return state[bufnr]
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param bufnr integer
|
||||||
|
local function stop_open_watcher(bufnr)
|
||||||
|
local s = state[bufnr]
|
||||||
|
if not (s and s.open_watcher) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
s.open_watcher:stop()
|
||||||
|
s.open_watcher:close()
|
||||||
|
s.open_watcher = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param bufnr integer
|
||||||
|
local function stop_output_watcher(bufnr)
|
||||||
|
local s = state[bufnr]
|
||||||
|
if not (s and s.output_watcher) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
s.output_watcher:stop()
|
||||||
|
s.output_watcher:close()
|
||||||
|
s.output_watcher = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
local function close_viewer(bufnr)
|
local function close_viewer(bufnr)
|
||||||
local obj = viewer_procs[bufnr]
|
local s = state[bufnr]
|
||||||
if obj then
|
if not (s and s.viewer) then
|
||||||
local kill = obj.kill
|
return
|
||||||
kill(obj, 'sigterm')
|
end
|
||||||
viewer_procs[bufnr] = nil
|
s.viewer:kill('sigterm')
|
||||||
|
s.viewer = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param bufnr integer
|
||||||
|
---@param name string
|
||||||
|
---@param provider preview.ProviderConfig
|
||||||
|
---@param ctx preview.Context
|
||||||
|
---@param output string
|
||||||
|
---@return integer
|
||||||
|
local function handle_errors(bufnr, name, provider, ctx, output)
|
||||||
|
local errors_mode = provider.errors
|
||||||
|
if errors_mode == nil then
|
||||||
|
errors_mode = 'diagnostic'
|
||||||
|
end
|
||||||
|
if not (provider.error_parser and errors_mode) then
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
if errors_mode == 'diagnostic' then
|
||||||
|
return diagnostic.set(bufnr, name, provider.error_parser, output, ctx)
|
||||||
|
elseif errors_mode == 'quickfix' then
|
||||||
|
local ok, diags = pcall(provider.error_parser, output, ctx)
|
||||||
|
if ok and diags and #diags > 0 then
|
||||||
|
local items = {}
|
||||||
|
for _, d in ipairs(diags) do
|
||||||
|
table.insert(items, {
|
||||||
|
bufnr = bufnr,
|
||||||
|
lnum = d.lnum + 1,
|
||||||
|
col = d.col + 1,
|
||||||
|
text = d.message,
|
||||||
|
type = d.severity == vim.diagnostic.severity.WARN and 'W' or 'E',
|
||||||
|
})
|
||||||
|
end
|
||||||
|
vim.fn.setqflist(items, 'r')
|
||||||
|
local win = vim.fn.win_getid()
|
||||||
|
vim.cmd.cwindow()
|
||||||
|
vim.fn.win_gotoid(win)
|
||||||
|
return #diags
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param bufnr integer
|
||||||
|
---@param provider preview.ProviderConfig
|
||||||
|
local function clear_errors(bufnr, provider)
|
||||||
|
local errors_mode = provider.errors
|
||||||
|
if errors_mode == nil then
|
||||||
|
errors_mode = 'diagnostic'
|
||||||
|
end
|
||||||
|
if errors_mode == 'diagnostic' then
|
||||||
|
diagnostic.clear(bufnr)
|
||||||
|
elseif errors_mode == 'quickfix' then
|
||||||
|
vim.fn.setqflist({}, 'r')
|
||||||
|
vim.cmd.cwindow()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -54,7 +127,23 @@ local function do_open(bufnr, output_file, open_config)
|
||||||
elseif type(open_config) == 'table' then
|
elseif type(open_config) == 'table' then
|
||||||
local open_cmd = vim.list_extend({}, open_config)
|
local open_cmd = vim.list_extend({}, open_config)
|
||||||
table.insert(open_cmd, output_file)
|
table.insert(open_cmd, output_file)
|
||||||
viewer_procs[bufnr] = vim.system(open_cmd)
|
log.dbg('opening viewer for buffer %d: %s', bufnr, table.concat(open_cmd, ' '))
|
||||||
|
local proc
|
||||||
|
proc = vim.system(
|
||||||
|
open_cmd,
|
||||||
|
{},
|
||||||
|
vim.schedule_wrap(function()
|
||||||
|
local s = state[bufnr]
|
||||||
|
if s and s.viewer == proc then
|
||||||
|
log.dbg('viewer exited for buffer %d, resetting viewer_open', bufnr)
|
||||||
|
s.viewer = nil
|
||||||
|
s.viewer_open = nil
|
||||||
|
else
|
||||||
|
log.dbg('viewer exited for buffer %d (stale proc, ignoring)', bufnr)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
)
|
||||||
|
get_state(bufnr).viewer = proc
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -90,10 +179,31 @@ local function resolve_reload_cmd(provider, ctx)
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param bufnr integer
|
||||||
|
---@param s preview.BufState
|
||||||
|
local function stop_watching(bufnr, s)
|
||||||
|
s.watching = false
|
||||||
|
M.stop(bufnr)
|
||||||
|
stop_open_watcher(bufnr)
|
||||||
|
stop_output_watcher(bufnr)
|
||||||
|
close_viewer(bufnr)
|
||||||
|
s.viewer_open = nil
|
||||||
|
if s.bwp_autocmd then
|
||||||
|
pcall(vim.api.nvim_del_autocmd, s.bwp_autocmd)
|
||||||
|
s.bwp_autocmd = nil
|
||||||
|
end
|
||||||
|
if s.debounce then
|
||||||
|
s.debounce:stop()
|
||||||
|
s.debounce:close()
|
||||||
|
s.debounce = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
---@param name string
|
---@param name string
|
||||||
---@param provider preview.ProviderConfig
|
---@param provider preview.ProviderConfig
|
||||||
---@param ctx preview.Context
|
---@param ctx preview.Context
|
||||||
|
---@param opts? {oneshot?: boolean}
|
||||||
function M.compile(bufnr, name, provider, ctx, opts)
|
function M.compile(bufnr, name, provider, ctx, opts)
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
|
|
||||||
|
|
@ -105,11 +215,19 @@ function M.compile(bufnr, name, provider, ctx, opts)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if vim.bo[bufnr].modified then
|
local buf_file = vim.api.nvim_buf_get_name(bufnr)
|
||||||
vim.cmd('silent! update')
|
if buf_file ~= '' and buf_file == ctx.file then
|
||||||
|
if vim.bo[bufnr].modified then
|
||||||
|
vim.cmd('silent! update')
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||||
|
vim.fn.writefile(lines, ctx.file)
|
||||||
end
|
end
|
||||||
|
|
||||||
if active[bufnr] then
|
local s = get_state(bufnr)
|
||||||
|
|
||||||
|
if s.process then
|
||||||
log.dbg('killing existing process for buffer %d before recompile', bufnr)
|
log.dbg('killing existing process for buffer %d before recompile', bufnr)
|
||||||
M.stop(bufnr)
|
M.stop(bufnr)
|
||||||
end
|
end
|
||||||
|
|
@ -127,7 +245,7 @@ function M.compile(bufnr, name, provider, ctx, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
if output_file ~= '' then
|
if output_file ~= '' then
|
||||||
last_output[bufnr] = output_file
|
s.output = output_file
|
||||||
end
|
end
|
||||||
|
|
||||||
local reload_cmd
|
local reload_cmd
|
||||||
|
|
@ -155,74 +273,25 @@ function M.compile(bufnr, name, provider, ctx, opts)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
stderr_acc[#stderr_acc + 1] = data
|
stderr_acc[#stderr_acc + 1] = data
|
||||||
local errors_mode = provider.errors
|
local count = handle_errors(bufnr, name, provider, ctx, table.concat(stderr_acc))
|
||||||
if errors_mode == nil then
|
if count > 0 and not s.has_errors then
|
||||||
errors_mode = 'diagnostic'
|
s.has_errors = true
|
||||||
end
|
vim.notify('[preview.nvim]: compilation failed', vim.log.levels.ERROR)
|
||||||
if provider.error_parser and errors_mode then
|
|
||||||
local output = table.concat(stderr_acc)
|
|
||||||
if errors_mode == 'diagnostic' then
|
|
||||||
diagnostic.set(bufnr, name, provider.error_parser, output, ctx)
|
|
||||||
elseif errors_mode == 'quickfix' then
|
|
||||||
local ok, diags = pcall(provider.error_parser, output, ctx)
|
|
||||||
if ok and diags and #diags > 0 then
|
|
||||||
local items = {}
|
|
||||||
for _, d in ipairs(diags) do
|
|
||||||
table.insert(items, {
|
|
||||||
bufnr = bufnr,
|
|
||||||
lnum = d.lnum + 1,
|
|
||||||
col = d.col + 1,
|
|
||||||
text = d.message,
|
|
||||||
type = d.severity == vim.diagnostic.severity.WARN and 'W' or 'E',
|
|
||||||
})
|
|
||||||
end
|
|
||||||
vim.fn.setqflist(items, 'r')
|
|
||||||
local win = vim.fn.win_getid()
|
|
||||||
vim.cmd.cwindow()
|
|
||||||
vim.fn.win_gotoid(win)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end),
|
end),
|
||||||
},
|
},
|
||||||
vim.schedule_wrap(function(result)
|
vim.schedule_wrap(function(result)
|
||||||
if active[bufnr] and active[bufnr].obj == obj then
|
local cs = state[bufnr]
|
||||||
active[bufnr] = nil
|
if cs and cs.process == obj then
|
||||||
|
cs.process = nil
|
||||||
end
|
end
|
||||||
if not vim.api.nvim_buf_is_valid(bufnr) then
|
if not vim.api.nvim_buf_is_valid(bufnr) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if result.code ~= 0 then
|
if result.code ~= 0 then
|
||||||
log.dbg('long-running process failed for buffer %d (exit code %d)', bufnr, result.code)
|
log.dbg('long-running process failed for buffer %d (exit code %d)', bufnr, result.code)
|
||||||
local errors_mode = provider.errors
|
vim.notify('[preview.nvim]: compilation failed', vim.log.levels.ERROR)
|
||||||
if errors_mode == nil then
|
handle_errors(bufnr, name, provider, ctx, (result.stdout or '') .. (result.stderr or ''))
|
||||||
errors_mode = 'diagnostic'
|
|
||||||
end
|
|
||||||
if provider.error_parser and errors_mode then
|
|
||||||
local output = (result.stdout or '') .. (result.stderr or '')
|
|
||||||
if errors_mode == 'diagnostic' then
|
|
||||||
diagnostic.set(bufnr, name, provider.error_parser, output, ctx)
|
|
||||||
elseif errors_mode == 'quickfix' then
|
|
||||||
local ok, diagnostics = pcall(provider.error_parser, output, ctx)
|
|
||||||
if ok and diagnostics and #diagnostics > 0 then
|
|
||||||
local items = {}
|
|
||||||
for _, d in ipairs(diagnostics) do
|
|
||||||
table.insert(items, {
|
|
||||||
bufnr = bufnr,
|
|
||||||
lnum = d.lnum + 1,
|
|
||||||
col = d.col + 1,
|
|
||||||
text = d.message,
|
|
||||||
type = d.severity == vim.diagnostic.severity.WARN and 'W' or 'E',
|
|
||||||
})
|
|
||||||
end
|
|
||||||
vim.fn.setqflist(items, 'r')
|
|
||||||
local win = vim.fn.win_getid()
|
|
||||||
vim.cmd.cwindow()
|
|
||||||
vim.fn.win_gotoid(win)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
vim.api.nvim_exec_autocmds('User', {
|
vim.api.nvim_exec_autocmds('User', {
|
||||||
pattern = 'PreviewCompileFailed',
|
pattern = 'PreviewCompileFailed',
|
||||||
data = {
|
data = {
|
||||||
|
|
@ -236,7 +305,7 @@ function M.compile(bufnr, name, provider, ctx, opts)
|
||||||
end)
|
end)
|
||||||
)
|
)
|
||||||
|
|
||||||
if provider.open and not opts.oneshot and not opened[bufnr] and output_file ~= '' then
|
if provider.open and not opts.oneshot and not s.viewer_open and output_file ~= '' then
|
||||||
local pre_stat = vim.uv.fs_stat(output_file)
|
local pre_stat = vim.uv.fs_stat(output_file)
|
||||||
local pre_mtime = pre_stat and pre_stat.mtime.sec or 0
|
local pre_mtime = pre_stat and pre_stat.mtime.sec or 0
|
||||||
local out_dir = vim.fn.fnamemodify(output_file, ':h')
|
local out_dir = vim.fn.fnamemodify(output_file, ':h')
|
||||||
|
|
@ -244,7 +313,7 @@ function M.compile(bufnr, name, provider, ctx, opts)
|
||||||
stop_open_watcher(bufnr)
|
stop_open_watcher(bufnr)
|
||||||
local watcher = vim.uv.new_fs_event()
|
local watcher = vim.uv.new_fs_event()
|
||||||
if watcher then
|
if watcher then
|
||||||
open_watchers[bufnr] = watcher
|
s.open_watcher = watcher
|
||||||
watcher:start(
|
watcher:start(
|
||||||
out_dir,
|
out_dir,
|
||||||
{},
|
{},
|
||||||
|
|
@ -252,8 +321,12 @@ function M.compile(bufnr, name, provider, ctx, opts)
|
||||||
if err or vim.fn.fnamemodify(filename or '', ':t') ~= out_name then
|
if err or vim.fn.fnamemodify(filename or '', ':t') ~= out_name then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if opened[bufnr] then
|
local cs = state[bufnr]
|
||||||
stop_open_watcher(bufnr)
|
if not cs then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if cs.viewer_open then
|
||||||
|
log.dbg('watcher fired for buffer %d but viewer already open', bufnr)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if not vim.api.nvim_buf_is_valid(bufnr) then
|
if not vim.api.nvim_buf_is_valid(bufnr) then
|
||||||
|
|
@ -262,42 +335,72 @@ function M.compile(bufnr, name, provider, ctx, opts)
|
||||||
end
|
end
|
||||||
local new_stat = vim.uv.fs_stat(output_file)
|
local new_stat = vim.uv.fs_stat(output_file)
|
||||||
if not (new_stat and new_stat.mtime.sec > pre_mtime) then
|
if not (new_stat and new_stat.mtime.sec > pre_mtime) then
|
||||||
|
log.dbg(
|
||||||
|
'watcher fired for buffer %d but mtime not newer (%d <= %d)',
|
||||||
|
bufnr,
|
||||||
|
new_stat and new_stat.mtime.sec or 0,
|
||||||
|
pre_mtime
|
||||||
|
)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
stop_open_watcher(bufnr)
|
log.dbg('watcher opening viewer for buffer %d', bufnr)
|
||||||
|
cs.viewer_open = true
|
||||||
stderr_acc = {}
|
stderr_acc = {}
|
||||||
local errors_mode = provider.errors
|
clear_errors(bufnr, provider)
|
||||||
if errors_mode == nil then
|
|
||||||
errors_mode = 'diagnostic'
|
|
||||||
end
|
|
||||||
if errors_mode == 'diagnostic' then
|
|
||||||
diagnostic.clear(bufnr)
|
|
||||||
elseif errors_mode == 'quickfix' then
|
|
||||||
vim.fn.setqflist({}, 'r')
|
|
||||||
vim.cmd.cwindow()
|
|
||||||
end
|
|
||||||
do_open(bufnr, output_file, provider.open)
|
do_open(bufnr, output_file, provider.open)
|
||||||
opened[bufnr] = true
|
|
||||||
end)
|
end)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
active[bufnr] = { obj = obj, provider = name, output_file = output_file, is_reload = true }
|
if output_file ~= '' then
|
||||||
|
local out_dir = vim.fn.fnamemodify(output_file, ':h')
|
||||||
vim.api.nvim_create_autocmd('BufUnload', {
|
local out_name = vim.fn.fnamemodify(output_file, ':t')
|
||||||
buffer = bufnr,
|
stop_output_watcher(bufnr)
|
||||||
once = true,
|
local ow = vim.uv.new_fs_event()
|
||||||
callback = function()
|
if ow then
|
||||||
M.stop(bufnr)
|
s.output_watcher = ow
|
||||||
stop_open_watcher(bufnr)
|
local last_mtime = 0
|
||||||
if not provider.detach then
|
local stat = vim.uv.fs_stat(output_file)
|
||||||
close_viewer(bufnr)
|
if stat then
|
||||||
|
last_mtime = stat.mtime.sec
|
||||||
end
|
end
|
||||||
last_output[bufnr] = nil
|
ow:start(
|
||||||
end,
|
out_dir,
|
||||||
})
|
{},
|
||||||
|
vim.schedule_wrap(function(err, filename, _events)
|
||||||
|
if err or vim.fn.fnamemodify(filename or '', ':t') ~= out_name then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if not vim.api.nvim_buf_is_valid(bufnr) then
|
||||||
|
stop_output_watcher(bufnr)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local new_stat = vim.uv.fs_stat(output_file)
|
||||||
|
if not (new_stat and new_stat.mtime.sec > last_mtime) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
last_mtime = new_stat.mtime.sec
|
||||||
|
log.dbg('output updated for buffer %d', bufnr)
|
||||||
|
vim.notify('[preview.nvim]: compilation complete', vim.log.levels.INFO)
|
||||||
|
stderr_acc = {}
|
||||||
|
s.has_errors = false
|
||||||
|
clear_errors(bufnr, provider)
|
||||||
|
vim.api.nvim_exec_autocmds('User', {
|
||||||
|
pattern = 'PreviewCompileSuccess',
|
||||||
|
data = { bufnr = bufnr, provider = name, output = output_file },
|
||||||
|
})
|
||||||
|
end)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
s.process = obj
|
||||||
|
s.provider = name
|
||||||
|
s.is_reload = true
|
||||||
|
s.has_errors = false
|
||||||
|
|
||||||
|
vim.notify('[preview.nvim]: compiling...', vim.log.levels.INFO)
|
||||||
vim.api.nvim_exec_autocmds('User', {
|
vim.api.nvim_exec_autocmds('User', {
|
||||||
pattern = 'PreviewCompileStarted',
|
pattern = 'PreviewCompileStarted',
|
||||||
data = { bufnr = bufnr, provider = name },
|
data = { bufnr = bufnr, provider = name },
|
||||||
|
|
@ -309,37 +412,28 @@ function M.compile(bufnr, name, provider, ctx, opts)
|
||||||
if provider.args then
|
if provider.args then
|
||||||
vim.list_extend(cmd, eval_list(provider.args, resolved_ctx))
|
vim.list_extend(cmd, eval_list(provider.args, resolved_ctx))
|
||||||
end
|
end
|
||||||
|
if provider.extra_args then
|
||||||
|
vim.list_extend(cmd, eval_list(provider.extra_args, resolved_ctx))
|
||||||
|
end
|
||||||
|
|
||||||
log.dbg('compiling buffer %d with provider "%s": %s', bufnr, name, table.concat(cmd, ' '))
|
log.dbg('compiling buffer %d with provider "%s": %s', bufnr, name, table.concat(cmd, ' '))
|
||||||
|
|
||||||
local obj
|
local obj
|
||||||
obj = vim.system(
|
obj = vim.system(
|
||||||
cmd,
|
cmd,
|
||||||
{
|
{ cwd = cwd, env = provider.env },
|
||||||
cwd = cwd,
|
|
||||||
env = provider.env,
|
|
||||||
},
|
|
||||||
vim.schedule_wrap(function(result)
|
vim.schedule_wrap(function(result)
|
||||||
if active[bufnr] and active[bufnr].obj == obj then
|
local cs = state[bufnr]
|
||||||
active[bufnr] = nil
|
if cs and cs.process == obj then
|
||||||
|
cs.process = nil
|
||||||
end
|
end
|
||||||
if not vim.api.nvim_buf_is_valid(bufnr) then
|
if not vim.api.nvim_buf_is_valid(bufnr) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local errors_mode = provider.errors
|
|
||||||
if errors_mode == nil then
|
|
||||||
errors_mode = 'diagnostic'
|
|
||||||
end
|
|
||||||
|
|
||||||
if result.code == 0 then
|
if result.code == 0 then
|
||||||
log.dbg('compilation succeeded for buffer %d', bufnr)
|
log.dbg('compilation succeeded for buffer %d', bufnr)
|
||||||
if errors_mode == 'diagnostic' then
|
vim.notify('[preview.nvim]: compilation complete', vim.log.levels.INFO)
|
||||||
diagnostic.clear(bufnr)
|
clear_errors(bufnr, provider)
|
||||||
elseif errors_mode == 'quickfix' then
|
|
||||||
vim.fn.setqflist({}, 'r')
|
|
||||||
vim.cmd.cwindow()
|
|
||||||
end
|
|
||||||
vim.api.nvim_exec_autocmds('User', {
|
vim.api.nvim_exec_autocmds('User', {
|
||||||
pattern = 'PreviewCompileSuccess',
|
pattern = 'PreviewCompileSuccess',
|
||||||
data = { bufnr = bufnr, provider = name, output = output_file },
|
data = { bufnr = bufnr, provider = name, output = output_file },
|
||||||
|
|
@ -350,42 +444,22 @@ function M.compile(bufnr, name, provider, ctx, opts)
|
||||||
r.inject(output_file)
|
r.inject(output_file)
|
||||||
r.broadcast()
|
r.broadcast()
|
||||||
end
|
end
|
||||||
|
cs = state[bufnr]
|
||||||
if
|
if
|
||||||
provider.open
|
provider.open
|
||||||
and not opts.oneshot
|
and not opts.oneshot
|
||||||
and not opened[bufnr]
|
and cs
|
||||||
|
and not cs.viewer_open
|
||||||
and output_file ~= ''
|
and output_file ~= ''
|
||||||
and vim.uv.fs_stat(output_file)
|
and vim.uv.fs_stat(output_file)
|
||||||
then
|
then
|
||||||
|
cs.viewer_open = true
|
||||||
do_open(bufnr, output_file, provider.open)
|
do_open(bufnr, output_file, provider.open)
|
||||||
opened[bufnr] = true
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
log.dbg('compilation failed for buffer %d (exit code %d)', bufnr, result.code)
|
log.dbg('compilation failed for buffer %d (exit code %d)', bufnr, result.code)
|
||||||
if provider.error_parser and errors_mode then
|
vim.notify('[preview.nvim]: compilation failed', vim.log.levels.ERROR)
|
||||||
local output = (result.stdout or '') .. (result.stderr or '')
|
handle_errors(bufnr, name, provider, ctx, (result.stdout or '') .. (result.stderr or ''))
|
||||||
if errors_mode == 'diagnostic' then
|
|
||||||
diagnostic.set(bufnr, name, provider.error_parser, output, ctx)
|
|
||||||
elseif errors_mode == 'quickfix' then
|
|
||||||
local ok, diagnostics = pcall(provider.error_parser, output, ctx)
|
|
||||||
if ok and diagnostics and #diagnostics > 0 then
|
|
||||||
local items = {}
|
|
||||||
for _, d in ipairs(diagnostics) do
|
|
||||||
table.insert(items, {
|
|
||||||
bufnr = bufnr,
|
|
||||||
lnum = d.lnum + 1,
|
|
||||||
col = d.col + 1,
|
|
||||||
text = d.message,
|
|
||||||
type = d.severity == vim.diagnostic.severity.WARN and 'W' or 'E',
|
|
||||||
})
|
|
||||||
end
|
|
||||||
vim.fn.setqflist(items, 'r')
|
|
||||||
local win = vim.fn.win_getid()
|
|
||||||
vim.cmd.cwindow()
|
|
||||||
vim.fn.win_gotoid(win)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
vim.api.nvim_exec_autocmds('User', {
|
vim.api.nvim_exec_autocmds('User', {
|
||||||
pattern = 'PreviewCompileFailed',
|
pattern = 'PreviewCompileFailed',
|
||||||
data = {
|
data = {
|
||||||
|
|
@ -399,20 +473,11 @@ function M.compile(bufnr, name, provider, ctx, opts)
|
||||||
end)
|
end)
|
||||||
)
|
)
|
||||||
|
|
||||||
active[bufnr] = { obj = obj, provider = name, output_file = output_file }
|
s.process = obj
|
||||||
|
s.provider = name
|
||||||
vim.api.nvim_create_autocmd('BufUnload', {
|
s.is_reload = false
|
||||||
buffer = bufnr,
|
|
||||||
once = true,
|
|
||||||
callback = function()
|
|
||||||
M.stop(bufnr)
|
|
||||||
if not provider.detach then
|
|
||||||
close_viewer(bufnr)
|
|
||||||
end
|
|
||||||
last_output[bufnr] = nil
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
|
vim.notify('[preview.nvim]: compiling...', vim.log.levels.INFO)
|
||||||
vim.api.nvim_exec_autocmds('User', {
|
vim.api.nvim_exec_autocmds('User', {
|
||||||
pattern = 'PreviewCompileStarted',
|
pattern = 'PreviewCompileStarted',
|
||||||
data = { bufnr = bufnr, provider = name },
|
data = { bufnr = bufnr, provider = name },
|
||||||
|
|
@ -421,39 +486,38 @@ end
|
||||||
|
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
function M.stop(bufnr)
|
function M.stop(bufnr)
|
||||||
local proc = active[bufnr]
|
local s = state[bufnr]
|
||||||
if not proc then
|
if not s then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
stop_output_watcher(bufnr)
|
||||||
|
local obj = s.process
|
||||||
|
if not obj then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
log.dbg('stopping process for buffer %d', bufnr)
|
log.dbg('stopping process for buffer %d', bufnr)
|
||||||
---@type fun(self: table, signal: string|integer)
|
obj:kill('sigterm')
|
||||||
local kill = proc.obj.kill
|
|
||||||
kill(proc.obj, 'sigterm')
|
|
||||||
|
|
||||||
local timer = vim.uv.new_timer()
|
local timer = vim.uv.new_timer()
|
||||||
if timer then
|
if timer then
|
||||||
timer:start(5000, 0, function()
|
timer:start(5000, 0, function()
|
||||||
timer:close()
|
timer:close()
|
||||||
if active[bufnr] and active[bufnr].obj == proc.obj then
|
local cs = state[bufnr]
|
||||||
kill(proc.obj, 'sigkill')
|
if cs and cs.process == obj then
|
||||||
active[bufnr] = nil
|
obj:kill('sigkill')
|
||||||
|
cs.process = nil
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.stop_all()
|
function M.stop_all()
|
||||||
for bufnr, _ in pairs(active) do
|
for bufnr, s in pairs(state) do
|
||||||
M.stop(bufnr)
|
stop_watching(bufnr, s)
|
||||||
end
|
if s.cleanup_autocmd then
|
||||||
for bufnr, _ in pairs(watching) do
|
pcall(vim.api.nvim_del_autocmd, s.cleanup_autocmd)
|
||||||
M.unwatch(bufnr)
|
end
|
||||||
end
|
state[bufnr] = nil
|
||||||
for bufnr, _ in pairs(open_watchers) do
|
|
||||||
stop_open_watcher(bufnr)
|
|
||||||
end
|
|
||||||
for bufnr, _ in pairs(viewer_procs) do
|
|
||||||
close_viewer(bufnr)
|
|
||||||
end
|
end
|
||||||
require('preview.reload').stop()
|
require('preview.reload').stop()
|
||||||
end
|
end
|
||||||
|
|
@ -464,76 +528,77 @@ end
|
||||||
---@param ctx_builder fun(bufnr: integer): preview.Context
|
---@param ctx_builder fun(bufnr: integer): preview.Context
|
||||||
function M.toggle(bufnr, name, provider, ctx_builder)
|
function M.toggle(bufnr, name, provider, ctx_builder)
|
||||||
local is_longrunning = type(provider.reload) == 'table' or type(provider.reload) == 'function'
|
local is_longrunning = type(provider.reload) == 'table' or type(provider.reload) == 'function'
|
||||||
|
local s = get_state(bufnr)
|
||||||
|
|
||||||
if is_longrunning then
|
if s.watching then
|
||||||
if active[bufnr] then
|
local output = s.output
|
||||||
M.stop(bufnr)
|
if not s.viewer_open and provider.open and output and vim.uv.fs_stat(output) then
|
||||||
vim.notify('[preview.nvim]: watching stopped', vim.log.levels.INFO)
|
log.dbg('toggle reopen viewer for buffer %d', bufnr)
|
||||||
|
s.viewer_open = true
|
||||||
|
do_open(bufnr, output, provider.open)
|
||||||
else
|
else
|
||||||
M.compile(bufnr, name, provider, ctx_builder(bufnr))
|
log.dbg('toggle off for buffer %d', bufnr)
|
||||||
vim.notify('[preview.nvim]: watching with "' .. name .. '"', vim.log.levels.INFO)
|
stop_watching(bufnr, s)
|
||||||
|
vim.notify('[preview.nvim]: watching stopped', vim.log.levels.INFO)
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if watching[bufnr] then
|
log.dbg('toggle on for buffer %d', bufnr)
|
||||||
M.unwatch(bufnr)
|
s.watching = true
|
||||||
vim.notify('[preview.nvim]: watching stopped', vim.log.levels.INFO)
|
|
||||||
return
|
if s.cleanup_autocmd then
|
||||||
|
vim.api.nvim_del_autocmd(s.cleanup_autocmd)
|
||||||
end
|
end
|
||||||
|
s.cleanup_autocmd = vim.api.nvim_create_autocmd('BufDelete', {
|
||||||
local au_id = vim.api.nvim_create_autocmd('BufWritePost', {
|
|
||||||
buffer = bufnr,
|
|
||||||
callback = function()
|
|
||||||
if debounce_timers[bufnr] then
|
|
||||||
debounce_timers[bufnr]:stop()
|
|
||||||
else
|
|
||||||
debounce_timers[bufnr] = vim.uv.new_timer()
|
|
||||||
end
|
|
||||||
debounce_timers[bufnr]:start(
|
|
||||||
DEBOUNCE_MS,
|
|
||||||
0,
|
|
||||||
vim.schedule_wrap(function()
|
|
||||||
local ctx = ctx_builder(bufnr)
|
|
||||||
M.compile(bufnr, name, provider, ctx)
|
|
||||||
end)
|
|
||||||
)
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
watching[bufnr] = au_id
|
|
||||||
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('BufUnload', {
|
|
||||||
buffer = bufnr,
|
buffer = bufnr,
|
||||||
once = true,
|
once = true,
|
||||||
callback = function()
|
callback = function()
|
||||||
M.unwatch(bufnr)
|
M.stop(bufnr)
|
||||||
stop_open_watcher(bufnr)
|
stop_open_watcher(bufnr)
|
||||||
|
stop_output_watcher(bufnr)
|
||||||
if not provider.detach then
|
if not provider.detach then
|
||||||
close_viewer(bufnr)
|
close_viewer(bufnr)
|
||||||
end
|
end
|
||||||
opened[bufnr] = nil
|
state[bufnr] = nil
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if not is_longrunning then
|
||||||
|
s.bwp_autocmd = vim.api.nvim_create_autocmd('BufWritePost', {
|
||||||
|
buffer = bufnr,
|
||||||
|
callback = function()
|
||||||
|
local ds = state[bufnr]
|
||||||
|
if not ds then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if ds.debounce then
|
||||||
|
ds.debounce:stop()
|
||||||
|
else
|
||||||
|
ds.debounce = vim.uv.new_timer()
|
||||||
|
end
|
||||||
|
ds.debounce:start(
|
||||||
|
DEBOUNCE_MS,
|
||||||
|
0,
|
||||||
|
vim.schedule_wrap(function()
|
||||||
|
M.compile(bufnr, name, provider, ctx_builder(bufnr))
|
||||||
|
end)
|
||||||
|
)
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
log.dbg('watching buffer %d with provider "%s"', bufnr, name)
|
||||||
|
end
|
||||||
|
|
||||||
M.compile(bufnr, name, provider, ctx_builder(bufnr))
|
M.compile(bufnr, name, provider, ctx_builder(bufnr))
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
function M.unwatch(bufnr)
|
function M.unwatch(bufnr)
|
||||||
local au_id = watching[bufnr]
|
local s = state[bufnr]
|
||||||
if not au_id then
|
if not s then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
vim.api.nvim_del_autocmd(au_id)
|
stop_watching(bufnr, s)
|
||||||
if debounce_timers[bufnr] then
|
|
||||||
debounce_timers[bufnr]:stop()
|
|
||||||
debounce_timers[bufnr]:close()
|
|
||||||
debounce_timers[bufnr] = nil
|
|
||||||
end
|
|
||||||
watching[bufnr] = nil
|
|
||||||
log.dbg('unwatched buffer %d', bufnr)
|
log.dbg('unwatched buffer %d', bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -582,7 +647,8 @@ end
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function M.open(bufnr, open_config)
|
function M.open(bufnr, open_config)
|
||||||
local output = last_output[bufnr]
|
local s = state[bufnr]
|
||||||
|
local output = s and s.output
|
||||||
if not output then
|
if not output then
|
||||||
log.dbg('no last output file for buffer %d', bufnr)
|
log.dbg('no last output file for buffer %d', bufnr)
|
||||||
return false
|
return false
|
||||||
|
|
@ -598,26 +664,20 @@ end
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
---@return preview.Status
|
---@return preview.Status
|
||||||
function M.status(bufnr)
|
function M.status(bufnr)
|
||||||
local proc = active[bufnr]
|
local s = state[bufnr]
|
||||||
if proc then
|
if not s then
|
||||||
return {
|
return { compiling = false, watching = false }
|
||||||
compiling = not proc.is_reload,
|
|
||||||
watching = watching[bufnr] ~= nil or proc.is_reload == true,
|
|
||||||
provider = proc.provider,
|
|
||||||
output_file = proc.output_file,
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
return { compiling = false, watching = watching[bufnr] ~= nil }
|
return {
|
||||||
|
compiling = s.process ~= nil and not s.is_reload,
|
||||||
|
watching = s.watching,
|
||||||
|
provider = s.provider,
|
||||||
|
output_file = s.output,
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
M._test = {
|
M._test = {
|
||||||
active = active,
|
state = state,
|
||||||
watching = watching,
|
|
||||||
opened = opened,
|
|
||||||
last_output = last_output,
|
|
||||||
debounce_timers = debounce_timers,
|
|
||||||
viewer_procs = viewer_procs,
|
|
||||||
open_watchers = open_watchers,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
|
|
@ -15,21 +15,23 @@ end
|
||||||
---@param error_parser fun(output: string, ctx: preview.Context): preview.Diagnostic[]
|
---@param error_parser fun(output: string, ctx: preview.Context): preview.Diagnostic[]
|
||||||
---@param output string
|
---@param output string
|
||||||
---@param ctx preview.Context
|
---@param ctx preview.Context
|
||||||
|
---@return integer
|
||||||
function M.set(bufnr, name, error_parser, output, ctx)
|
function M.set(bufnr, name, error_parser, output, ctx)
|
||||||
local ok, diagnostics = pcall(error_parser, output, ctx)
|
local ok, diagnostics = pcall(error_parser, output, ctx)
|
||||||
if not ok then
|
if not ok then
|
||||||
log.dbg('error_parser for "%s" failed: %s', name, diagnostics)
|
log.dbg('error_parser for "%s" failed: %s', name, diagnostics)
|
||||||
return
|
return 0
|
||||||
end
|
end
|
||||||
if not diagnostics or #diagnostics == 0 then
|
if not diagnostics or #diagnostics == 0 then
|
||||||
log.dbg('error_parser for "%s" returned no diagnostics', name)
|
log.dbg('error_parser for "%s" returned no diagnostics', name)
|
||||||
return
|
return 0
|
||||||
end
|
end
|
||||||
for _, d in ipairs(diagnostics) do
|
for _, d in ipairs(diagnostics) do
|
||||||
d.source = d.source or name
|
d.source = d.source or name
|
||||||
end
|
end
|
||||||
vim.diagnostic.set(ns, bufnr, diagnostics)
|
vim.diagnostic.set(ns, bufnr, diagnostics)
|
||||||
log.dbg('set %d diagnostics for buffer %d from provider "%s"', #diagnostics, bufnr, name)
|
log.dbg('set %d diagnostics for buffer %d from provider "%s"', #diagnostics, bufnr, name)
|
||||||
|
return #diagnostics
|
||||||
end
|
end
|
||||||
|
|
||||||
---@return integer
|
---@return integer
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
---@field ft? string
|
---@field ft? string
|
||||||
---@field cmd string[]
|
---@field cmd string[]
|
||||||
---@field args? string[]|fun(ctx: preview.Context): string[]
|
---@field args? string[]|fun(ctx: preview.Context): string[]
|
||||||
|
---@field extra_args? string[]|fun(ctx: preview.Context): string[]
|
||||||
---@field cwd? string|fun(ctx: preview.Context): string
|
---@field cwd? string|fun(ctx: preview.Context): string
|
||||||
---@field env? table<string, string>
|
---@field env? table<string, string>
|
||||||
---@field output? string|fun(ctx: preview.Context): string
|
---@field output? string|fun(ctx: preview.Context): string
|
||||||
|
|
@ -94,6 +95,7 @@ function M.setup(opts)
|
||||||
vim.validate(prefix .. '.cmd', provider.cmd, 'table')
|
vim.validate(prefix .. '.cmd', provider.cmd, 'table')
|
||||||
vim.validate(prefix .. '.cmd[1]', provider.cmd[1], 'string')
|
vim.validate(prefix .. '.cmd[1]', provider.cmd[1], 'string')
|
||||||
vim.validate(prefix .. '.args', provider.args, { 'table', 'function' }, true)
|
vim.validate(prefix .. '.args', provider.args, { 'table', 'function' }, true)
|
||||||
|
vim.validate(prefix .. '.extra_args', provider.extra_args, { 'table', 'function' }, true)
|
||||||
vim.validate(prefix .. '.cwd', provider.cwd, { 'string', 'function' }, true)
|
vim.validate(prefix .. '.cwd', provider.cwd, { 'string', 'function' }, true)
|
||||||
vim.validate(prefix .. '.output', provider.output, { 'string', 'function' }, true)
|
vim.validate(prefix .. '.output', provider.output, { 'string', 'function' }, true)
|
||||||
vim.validate(prefix .. '.error_parser', provider.error_parser, 'function', true)
|
vim.validate(prefix .. '.error_parser', provider.error_parser, 'function', true)
|
||||||
|
|
@ -137,27 +139,49 @@ function M.resolve_provider(bufnr)
|
||||||
return ft
|
return ft
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local ft_ext = {
|
||||||
|
markdown = 'md',
|
||||||
|
typst = 'typ',
|
||||||
|
tex = 'tex',
|
||||||
|
asciidoc = 'adoc',
|
||||||
|
plantuml = 'puml',
|
||||||
|
mermaid = 'mmd',
|
||||||
|
quarto = 'qmd',
|
||||||
|
}
|
||||||
|
|
||||||
---@param bufnr? integer
|
---@param bufnr? integer
|
||||||
---@return preview.Context
|
---@return preview.Context
|
||||||
function M.build_context(bufnr)
|
function M.build_context(bufnr)
|
||||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
||||||
local file = vim.api.nvim_buf_get_name(bufnr)
|
local file = vim.api.nvim_buf_get_name(bufnr)
|
||||||
local root = vim.fs.root(bufnr, { '.git' }) or vim.fn.fnamemodify(file, ':h')
|
local ft = vim.bo[bufnr].filetype
|
||||||
|
local root
|
||||||
|
|
||||||
|
if file ~= '' and vim.uv.fs_stat(file) then
|
||||||
|
root = vim.fs.root(bufnr, { '.git' }) or vim.fn.fnamemodify(file, ':h')
|
||||||
|
else
|
||||||
|
local basename
|
||||||
|
if file ~= '' then
|
||||||
|
basename = string.format('%d-%s', bufnr, vim.fn.fnamemodify(file, ':t'))
|
||||||
|
else
|
||||||
|
local ext = ft_ext[ft] or ft
|
||||||
|
basename = string.format('preview-%d.%s', bufnr, ext)
|
||||||
|
end
|
||||||
|
file = '/tmp/' .. basename
|
||||||
|
root = '/tmp'
|
||||||
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
bufnr = bufnr,
|
bufnr = bufnr,
|
||||||
file = file,
|
file = file,
|
||||||
root = root,
|
root = root,
|
||||||
ft = vim.bo[bufnr].filetype,
|
ft = ft,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param bufnr? integer
|
---@param bufnr? integer
|
||||||
function M.compile(bufnr)
|
function M.compile(bufnr)
|
||||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
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 name = M.resolve_provider(bufnr)
|
||||||
if not name then
|
if not name then
|
||||||
vim.notify('[preview.nvim]: no provider configured for this filetype', vim.log.levels.WARN)
|
vim.notify('[preview.nvim]: no provider configured for this filetype', vim.log.levels.WARN)
|
||||||
|
|
@ -177,10 +201,6 @@ end
|
||||||
---@param bufnr? integer
|
---@param bufnr? integer
|
||||||
function M.clean(bufnr)
|
function M.clean(bufnr)
|
||||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
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 name = M.resolve_provider(bufnr)
|
||||||
if not name then
|
if not name then
|
||||||
vim.notify('[preview.nvim]: no provider configured for this filetype', vim.log.levels.WARN)
|
vim.notify('[preview.nvim]: no provider configured for this filetype', vim.log.levels.WARN)
|
||||||
|
|
@ -194,10 +214,6 @@ end
|
||||||
---@param bufnr? integer
|
---@param bufnr? integer
|
||||||
function M.toggle(bufnr)
|
function M.toggle(bufnr)
|
||||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
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 name = M.resolve_provider(bufnr)
|
||||||
if not name then
|
if not name then
|
||||||
vim.notify('[preview.nvim]: no provider configured for this filetype', vim.log.levels.WARN)
|
vim.notify('[preview.nvim]: no provider configured for this filetype', vim.log.levels.WARN)
|
||||||
|
|
@ -210,10 +226,6 @@ end
|
||||||
---@param bufnr? integer
|
---@param bufnr? integer
|
||||||
function M.open(bufnr)
|
function M.open(bufnr)
|
||||||
bufnr = bufnr or vim.api.nvim_get_current_buf()
|
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 name = M.resolve_provider(bufnr)
|
||||||
local open_config = name and config.providers[name] and config.providers[name].open
|
local open_config = name and config.providers[name] and config.providers[name].open
|
||||||
if not compiler.open(bufnr, open_config) then
|
if not compiler.open(bufnr, open_config) then
|
||||||
|
|
|
||||||
|
|
@ -115,6 +115,24 @@ local function parse_asciidoctor(output)
|
||||||
return diagnostics
|
return diagnostics
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param output string
|
||||||
|
---@return preview.Diagnostic[]
|
||||||
|
local function parse_mermaid(output)
|
||||||
|
local lnum = output:match('Parse error on line (%d+)')
|
||||||
|
if not lnum then
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
local msg = output:match('(Expecting .+)') or 'parse error'
|
||||||
|
return {
|
||||||
|
{
|
||||||
|
lnum = tonumber(lnum) - 1,
|
||||||
|
col = 0,
|
||||||
|
message = msg,
|
||||||
|
severity = vim.diagnostic.severity.ERROR,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
---@type preview.ProviderConfig
|
---@type preview.ProviderConfig
|
||||||
M.typst = {
|
M.typst = {
|
||||||
ft = 'typst',
|
ft = 'typst',
|
||||||
|
|
@ -206,7 +224,7 @@ M.markdown = {
|
||||||
ft = 'markdown',
|
ft = 'markdown',
|
||||||
cmd = { 'pandoc' },
|
cmd = { 'pandoc' },
|
||||||
args = function(ctx)
|
args = function(ctx)
|
||||||
return { ctx.file, '-s', '--embed-resources', '-o', ctx.output }
|
return { ctx.file, '-s', '--katex', '-o', ctx.output }
|
||||||
end,
|
end,
|
||||||
output = function(ctx)
|
output = function(ctx)
|
||||||
return (ctx.file:gsub('%.md$', '.html'))
|
return (ctx.file:gsub('%.md$', '.html'))
|
||||||
|
|
@ -231,7 +249,7 @@ M.github = {
|
||||||
'gfm',
|
'gfm',
|
||||||
ctx.file,
|
ctx.file,
|
||||||
'-s',
|
'-s',
|
||||||
'--embed-resources',
|
'--katex',
|
||||||
'--css',
|
'--css',
|
||||||
'https://cdn.jsdelivr.net/gh/pixelbrackets/gfm-stylesheet@master/dist/gfm.css',
|
'https://cdn.jsdelivr.net/gh/pixelbrackets/gfm-stylesheet@master/dist/gfm.css',
|
||||||
'-o',
|
'-o',
|
||||||
|
|
@ -313,19 +331,7 @@ M.mermaid = {
|
||||||
return (ctx.file:gsub('%.mmd$', '.svg'))
|
return (ctx.file:gsub('%.mmd$', '.svg'))
|
||||||
end,
|
end,
|
||||||
error_parser = function(output)
|
error_parser = function(output)
|
||||||
local diagnostics = {}
|
return parse_mermaid(output)
|
||||||
for line in output:gmatch('[^\r\n]+') do
|
|
||||||
local lnum = line:match('^%s*Parse error on line (%d+)')
|
|
||||||
if lnum then
|
|
||||||
table.insert(diagnostics, {
|
|
||||||
lnum = tonumber(lnum) - 1,
|
|
||||||
col = 0,
|
|
||||||
message = line,
|
|
||||||
severity = vim.diagnostic.severity.ERROR,
|
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return diagnostics
|
|
||||||
end,
|
end,
|
||||||
clean = function(ctx)
|
clean = function(ctx)
|
||||||
return { 'rm', '-f', (ctx.file:gsub('%.mmd$', '.svg')) }
|
return { 'rm', '-f', (ctx.file:gsub('%.mmd$', '.svg')) }
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,13 @@ describe('compiler', function()
|
||||||
compiler = require('preview.compiler')
|
compiler = require('preview.compiler')
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
local function process_done(bufnr)
|
||||||
|
local s = compiler._test.state[bufnr]
|
||||||
|
return not s or s.process == nil
|
||||||
|
end
|
||||||
|
|
||||||
describe('compile', function()
|
describe('compile', function()
|
||||||
it('spawns a process and tracks it in active table', function()
|
it('spawns a process and tracks it in state', function()
|
||||||
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
||||||
vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test.txt')
|
vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test.txt')
|
||||||
vim.bo[bufnr].modified = false
|
vim.bo[bufnr].modified = false
|
||||||
|
|
@ -23,15 +28,16 @@ describe('compiler', function()
|
||||||
}
|
}
|
||||||
|
|
||||||
compiler.compile(bufnr, 'echo', provider, ctx)
|
compiler.compile(bufnr, 'echo', provider, ctx)
|
||||||
local active = compiler._test.active
|
local s = compiler._test.state[bufnr]
|
||||||
assert.is_not_nil(active[bufnr])
|
assert.is_not_nil(s)
|
||||||
assert.are.equal('echo', active[bufnr].provider)
|
assert.is_not_nil(s.process)
|
||||||
|
assert.are.equal('echo', s.provider)
|
||||||
|
|
||||||
vim.wait(2000, function()
|
vim.wait(2000, function()
|
||||||
return active[bufnr] == nil
|
return process_done(bufnr)
|
||||||
end, 50)
|
end, 50)
|
||||||
|
|
||||||
assert.is_nil(active[bufnr])
|
assert.is_nil(compiler._test.state[bufnr].process)
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
@ -49,6 +55,14 @@ describe('compiler', function()
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
local notified = false
|
||||||
|
local orig = vim.notify
|
||||||
|
vim.notify = function(msg)
|
||||||
|
if msg:find('compiling') then
|
||||||
|
notified = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local provider = { cmd = { 'echo', 'ok' } }
|
local provider = { cmd = { 'echo', 'ok' } }
|
||||||
local ctx = {
|
local ctx = {
|
||||||
bufnr = bufnr,
|
bufnr = bufnr,
|
||||||
|
|
@ -58,10 +72,12 @@ describe('compiler', function()
|
||||||
}
|
}
|
||||||
|
|
||||||
compiler.compile(bufnr, 'echo', provider, ctx)
|
compiler.compile(bufnr, 'echo', provider, ctx)
|
||||||
|
vim.notify = orig
|
||||||
assert.is_true(fired)
|
assert.is_true(fired)
|
||||||
|
assert.is_true(notified)
|
||||||
|
|
||||||
vim.wait(2000, function()
|
vim.wait(2000, function()
|
||||||
return compiler._test.active[bufnr] == nil
|
return process_done(bufnr)
|
||||||
end, 50)
|
end, 50)
|
||||||
|
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
|
|
@ -124,7 +140,7 @@ describe('compiler', function()
|
||||||
vim.notify = orig
|
vim.notify = orig
|
||||||
|
|
||||||
assert.is_true(notified)
|
assert.is_true(notified)
|
||||||
assert.is_nil(compiler._test.active[bufnr])
|
assert.is_true(process_done(bufnr))
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
@ -186,7 +202,7 @@ describe('compiler', function()
|
||||||
compiler.compile(bufnr, 'falsecmd', provider, ctx)
|
compiler.compile(bufnr, 'falsecmd', provider, ctx)
|
||||||
|
|
||||||
vim.wait(2000, function()
|
vim.wait(2000, function()
|
||||||
return compiler._test.active[bufnr] == nil
|
return process_done(bufnr)
|
||||||
end, 50)
|
end, 50)
|
||||||
|
|
||||||
assert.is_false(parser_called)
|
assert.is_false(parser_called)
|
||||||
|
|
@ -218,7 +234,7 @@ describe('compiler', function()
|
||||||
compiler.compile(bufnr, 'qfcmd', provider, ctx)
|
compiler.compile(bufnr, 'qfcmd', provider, ctx)
|
||||||
|
|
||||||
vim.wait(2000, function()
|
vim.wait(2000, function()
|
||||||
return compiler._test.active[bufnr] == nil
|
return process_done(bufnr)
|
||||||
end, 50)
|
end, 50)
|
||||||
|
|
||||||
local qflist = vim.fn.getqflist()
|
local qflist = vim.fn.getqflist()
|
||||||
|
|
@ -255,7 +271,7 @@ describe('compiler', function()
|
||||||
compiler.compile(bufnr, 'truecmd', provider, ctx)
|
compiler.compile(bufnr, 'truecmd', provider, ctx)
|
||||||
|
|
||||||
vim.wait(2000, function()
|
vim.wait(2000, function()
|
||||||
return compiler._test.active[bufnr] == nil
|
return process_done(bufnr)
|
||||||
end, 50)
|
end, 50)
|
||||||
|
|
||||||
assert.are.equal(0, #vim.fn.getqflist())
|
assert.are.equal(0, #vim.fn.getqflist())
|
||||||
|
|
@ -263,6 +279,58 @@ describe('compiler', function()
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe('long-running notifications', function()
|
||||||
|
it('notifies failure on stderr diagnostics', function()
|
||||||
|
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
||||||
|
vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test_longrun.txt')
|
||||||
|
vim.bo[bufnr].modified = false
|
||||||
|
|
||||||
|
local notified_fail = false
|
||||||
|
local orig = vim.notify
|
||||||
|
vim.notify = function(msg, level)
|
||||||
|
if msg:find('compilation failed') and level == vim.log.levels.ERROR then
|
||||||
|
notified_fail = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local provider = {
|
||||||
|
cmd = { 'sh' },
|
||||||
|
reload = function()
|
||||||
|
return { 'sh', '-c', 'echo "error: bad input" >&2; sleep 60' }
|
||||||
|
end,
|
||||||
|
error_parser = function()
|
||||||
|
return {
|
||||||
|
{ lnum = 0, col = 0, message = 'bad input', severity = vim.diagnostic.severity.ERROR },
|
||||||
|
}
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
local ctx = {
|
||||||
|
bufnr = bufnr,
|
||||||
|
file = '/tmp/preview_test_longrun.txt',
|
||||||
|
root = '/tmp',
|
||||||
|
ft = 'text',
|
||||||
|
}
|
||||||
|
|
||||||
|
compiler.compile(bufnr, 'testprov', provider, ctx)
|
||||||
|
|
||||||
|
vim.wait(3000, function()
|
||||||
|
return notified_fail
|
||||||
|
end, 50)
|
||||||
|
|
||||||
|
vim.notify = orig
|
||||||
|
assert.is_true(notified_fail)
|
||||||
|
|
||||||
|
local s = compiler._test.state[bufnr]
|
||||||
|
assert.is_true(s.has_errors)
|
||||||
|
|
||||||
|
compiler.stop(bufnr)
|
||||||
|
vim.wait(2000, function()
|
||||||
|
return process_done(bufnr)
|
||||||
|
end, 50)
|
||||||
|
helpers.delete_buffer(bufnr)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
describe('stop', function()
|
describe('stop', function()
|
||||||
it('does nothing when no process is active', function()
|
it('does nothing when no process is active', function()
|
||||||
assert.has_no.errors(function()
|
assert.has_no.errors(function()
|
||||||
|
|
@ -298,7 +366,7 @@ describe('compiler', function()
|
||||||
compiler.stop(bufnr)
|
compiler.stop(bufnr)
|
||||||
|
|
||||||
vim.wait(2000, function()
|
vim.wait(2000, function()
|
||||||
return compiler._test.active[bufnr] == nil
|
return process_done(bufnr)
|
||||||
end, 50)
|
end, 50)
|
||||||
|
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
|
|
@ -329,11 +397,12 @@ describe('compiler', function()
|
||||||
}
|
}
|
||||||
|
|
||||||
compiler.compile(bufnr, 'testprov', provider, ctx)
|
compiler.compile(bufnr, 'testprov', provider, ctx)
|
||||||
assert.is_not_nil(compiler._test.last_output[bufnr])
|
local s = compiler._test.state[bufnr]
|
||||||
assert.are.equal('/tmp/preview_test_open.pdf', compiler._test.last_output[bufnr])
|
assert.is_not_nil(s)
|
||||||
|
assert.are.equal('/tmp/preview_test_open.pdf', s.output)
|
||||||
|
|
||||||
vim.wait(2000, function()
|
vim.wait(2000, function()
|
||||||
return compiler._test.active[bufnr] == nil
|
return process_done(bufnr)
|
||||||
end, 50)
|
end, 50)
|
||||||
|
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
|
|
@ -341,7 +410,7 @@ describe('compiler', function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('toggle', function()
|
describe('toggle', function()
|
||||||
it('registers autocmd and tracks in watching table', function()
|
it('starts watching and sets watching flag', function()
|
||||||
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
local bufnr = helpers.create_buffer({ 'hello' }, 'text')
|
||||||
vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test_watch.txt')
|
vim.api.nvim_buf_set_name(bufnr, '/tmp/preview_test_watch.txt')
|
||||||
|
|
||||||
|
|
@ -351,7 +420,7 @@ describe('compiler', function()
|
||||||
end
|
end
|
||||||
|
|
||||||
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
|
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
|
||||||
assert.is_not_nil(compiler._test.watching[bufnr])
|
assert.is_true(compiler.status(bufnr).watching)
|
||||||
|
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
end)
|
end)
|
||||||
|
|
@ -366,10 +435,10 @@ describe('compiler', function()
|
||||||
end
|
end
|
||||||
|
|
||||||
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
|
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
|
||||||
assert.is_not_nil(compiler._test.watching[bufnr])
|
assert.is_true(compiler.status(bufnr).watching)
|
||||||
|
|
||||||
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
|
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
|
||||||
assert.is_nil(compiler._test.watching[bufnr])
|
assert.is_false(compiler.status(bufnr).watching)
|
||||||
|
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
end)
|
end)
|
||||||
|
|
@ -389,10 +458,10 @@ describe('compiler', function()
|
||||||
end
|
end
|
||||||
|
|
||||||
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
|
compiler.toggle(bufnr, 'echo', provider, ctx_builder)
|
||||||
assert.is_not_nil(compiler._test.watching[bufnr])
|
assert.is_true(compiler.status(bufnr).watching)
|
||||||
|
|
||||||
compiler.stop_all()
|
compiler.stop_all()
|
||||||
assert.is_nil(compiler._test.watching[bufnr])
|
assert.is_false(compiler.status(bufnr).watching)
|
||||||
|
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
|
|
@ -124,5 +124,47 @@ describe('diagnostic', function()
|
||||||
assert.are.equal(0, #diags)
|
assert.are.equal(0, #diags)
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('returns count of diagnostics set', function()
|
||||||
|
local bufnr = helpers.create_buffer({ 'line1', 'line2' })
|
||||||
|
|
||||||
|
local parser = function()
|
||||||
|
return {
|
||||||
|
{ lnum = 0, col = 0, message = 'err1', severity = vim.diagnostic.severity.ERROR },
|
||||||
|
{ lnum = 1, col = 0, message = 'err2', severity = vim.diagnostic.severity.WARN },
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
local ctx = { bufnr = bufnr, file = '/tmp/test.typ', root = '/tmp', ft = 'typst' }
|
||||||
|
local count = diagnostic.set(bufnr, 'typst', parser, 'errors', ctx)
|
||||||
|
assert.are.equal(2, count)
|
||||||
|
helpers.delete_buffer(bufnr)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('returns 0 on parser failure', function()
|
||||||
|
local bufnr = helpers.create_buffer({ 'line1' })
|
||||||
|
|
||||||
|
local parser = function()
|
||||||
|
error('boom')
|
||||||
|
end
|
||||||
|
|
||||||
|
local ctx = { bufnr = bufnr, file = '/tmp/test.tex', root = '/tmp', ft = 'tex' }
|
||||||
|
local count = diagnostic.set(bufnr, 'latexmk', parser, 'error', ctx)
|
||||||
|
assert.are.equal(0, count)
|
||||||
|
helpers.delete_buffer(bufnr)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('returns 0 on empty diagnostics', function()
|
||||||
|
local bufnr = helpers.create_buffer({ 'line1' })
|
||||||
|
|
||||||
|
local parser = function()
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
|
||||||
|
local ctx = { bufnr = bufnr, file = '/tmp/test.tex', root = '/tmp', ft = 'tex' }
|
||||||
|
local count = diagnostic.set(bufnr, 'latexmk', parser, 'error', ctx)
|
||||||
|
assert.are.equal(0, count)
|
||||||
|
helpers.delete_buffer(bufnr)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
|
|
@ -126,43 +126,43 @@ describe('preview', function()
|
||||||
return msg
|
return msg
|
||||||
end
|
end
|
||||||
|
|
||||||
it('compile warns on unnamed buffer', function()
|
it('compile falls through to provider check on unnamed buffer', function()
|
||||||
local bufnr = helpers.create_buffer({}, 'typst')
|
local bufnr = helpers.create_buffer({}, 'lua')
|
||||||
local msg = capture_notify(function()
|
local msg = capture_notify(function()
|
||||||
preview.compile(bufnr)
|
preview.compile(bufnr)
|
||||||
end)
|
end)
|
||||||
assert.is_not_nil(msg)
|
assert.is_not_nil(msg)
|
||||||
assert.is_truthy(msg:find('no file name'))
|
assert.is_truthy(msg:find('no provider configured'))
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('toggle warns on unnamed buffer', function()
|
it('toggle falls through to provider check on unnamed buffer', function()
|
||||||
local bufnr = helpers.create_buffer({}, 'typst')
|
local bufnr = helpers.create_buffer({}, 'lua')
|
||||||
local msg = capture_notify(function()
|
local msg = capture_notify(function()
|
||||||
preview.toggle(bufnr)
|
preview.toggle(bufnr)
|
||||||
end)
|
end)
|
||||||
assert.is_not_nil(msg)
|
assert.is_not_nil(msg)
|
||||||
assert.is_truthy(msg:find('no file name'))
|
assert.is_truthy(msg:find('no provider configured'))
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('clean warns on unnamed buffer', function()
|
it('clean falls through to provider check on unnamed buffer', function()
|
||||||
local bufnr = helpers.create_buffer({}, 'typst')
|
local bufnr = helpers.create_buffer({}, 'lua')
|
||||||
local msg = capture_notify(function()
|
local msg = capture_notify(function()
|
||||||
preview.clean(bufnr)
|
preview.clean(bufnr)
|
||||||
end)
|
end)
|
||||||
assert.is_not_nil(msg)
|
assert.is_not_nil(msg)
|
||||||
assert.is_truthy(msg:find('no file name'))
|
assert.is_truthy(msg:find('no provider configured'))
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('open warns on unnamed buffer', function()
|
it('open warns no output on unnamed buffer', function()
|
||||||
local bufnr = helpers.create_buffer({}, 'typst')
|
local bufnr = helpers.create_buffer({}, 'lua')
|
||||||
local msg = capture_notify(function()
|
local msg = capture_notify(function()
|
||||||
preview.open(bufnr)
|
preview.open(bufnr)
|
||||||
end)
|
end)
|
||||||
assert.is_not_nil(msg)
|
assert.is_not_nil(msg)
|
||||||
assert.is_truthy(msg:find('no file name'))
|
assert.is_truthy(msg:find('no output file'))
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
|
|
@ -294,13 +294,10 @@ describe('presets', function()
|
||||||
assert.are.same({ 'pandoc' }, presets.markdown.cmd)
|
assert.are.same({ 'pandoc' }, presets.markdown.cmd)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('returns args with standalone and embed-resources flags', function()
|
it('returns args with standalone and katex flags', function()
|
||||||
local args = presets.markdown.args(md_ctx)
|
local args = presets.markdown.args(md_ctx)
|
||||||
assert.is_table(args)
|
assert.is_table(args)
|
||||||
assert.are.same(
|
assert.are.same({ '/tmp/document.md', '-s', '--katex', '-o', '/tmp/document.html' }, args)
|
||||||
{ '/tmp/document.md', '-s', '--embed-resources', '-o', '/tmp/document.html' },
|
|
||||||
args
|
|
||||||
)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('returns html output path', function()
|
it('returns html output path', function()
|
||||||
|
|
@ -382,7 +379,7 @@ describe('presets', function()
|
||||||
assert.are.same({ 'pandoc' }, presets.github.cmd)
|
assert.are.same({ 'pandoc' }, presets.github.cmd)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('returns args with standalone, embed-resources, and css flags', function()
|
it('returns args with standalone, katex, and css flags', function()
|
||||||
local args = presets.github.args(md_ctx)
|
local args = presets.github.args(md_ctx)
|
||||||
assert.is_table(args)
|
assert.is_table(args)
|
||||||
assert.are.same({
|
assert.are.same({
|
||||||
|
|
@ -390,7 +387,7 @@ describe('presets', function()
|
||||||
'gfm',
|
'gfm',
|
||||||
'/tmp/document.md',
|
'/tmp/document.md',
|
||||||
'-s',
|
'-s',
|
||||||
'--embed-resources',
|
'--katex',
|
||||||
'--css',
|
'--css',
|
||||||
'https://cdn.jsdelivr.net/gh/pixelbrackets/gfm-stylesheet@master/dist/gfm.css',
|
'https://cdn.jsdelivr.net/gh/pixelbrackets/gfm-stylesheet@master/dist/gfm.css',
|
||||||
'-o',
|
'-o',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue