|
| 1 | +local env = require('render-markdown.lib.env') |
| 2 | +local manager = require('render-markdown.core.manager') |
| 3 | + |
| 4 | +---@class render.md.Preview |
| 5 | +local M = {} |
| 6 | + |
| 7 | +---@private |
| 8 | +M.group = vim.api.nvim_create_augroup('RenderMarkdownPreview', {}) |
| 9 | + |
| 10 | +---@private |
| 11 | +---@type table<integer, integer> |
| 12 | +M.buffers = {} |
| 13 | + |
| 14 | +---@param buf integer |
| 15 | +---@return integer? |
| 16 | +function M.get(buf) |
| 17 | + for src, dst in pairs(M.buffers) do |
| 18 | + if buf == dst then |
| 19 | + return src |
| 20 | + end |
| 21 | + end |
| 22 | + return nil |
| 23 | +end |
| 24 | + |
| 25 | +---@param src_buf? integer |
| 26 | +function M.attach(src_buf) |
| 27 | + src_buf = src_buf or env.buf.current() |
| 28 | + if not manager.attached(src_buf) then |
| 29 | + return |
| 30 | + end |
| 31 | + if M.buffers[src_buf] then |
| 32 | + vim.api.nvim_buf_delete(M.buffers[src_buf], {}) |
| 33 | + return |
| 34 | + end |
| 35 | + |
| 36 | + -- disable rendering for source buffer |
| 37 | + manager.set_buf(src_buf, false) |
| 38 | + |
| 39 | + local src_win = env.buf.win(src_buf) |
| 40 | + local dst_buf = vim.api.nvim_create_buf(false, true) |
| 41 | + local dst_win = vim.api.nvim_open_win(dst_buf, false, { |
| 42 | + split = 'right', |
| 43 | + }) |
| 44 | + M.buffers[src_buf] = dst_buf |
| 45 | + |
| 46 | + env.buf.set(dst_buf, 'bufhidden', 'wipe') |
| 47 | + env.buf.set(dst_buf, 'buftype', 'nofile') |
| 48 | + env.buf.set(dst_buf, 'filetype', env.buf.get(src_buf, 'filetype')) |
| 49 | + env.buf.set(dst_buf, 'modifiable', false) |
| 50 | + env.buf.set(dst_buf, 'swapfile', false) |
| 51 | + |
| 52 | + M.copy_cursor(src_win, dst_win) |
| 53 | + vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, { |
| 54 | + group = M.group, |
| 55 | + buffer = src_buf, |
| 56 | + callback = function(args) |
| 57 | + if env.valid(src_buf, src_win) and env.valid(dst_buf, dst_win) then |
| 58 | + M.copy_cursor(src_win, dst_win) |
| 59 | + M.copy_event(args, dst_buf) |
| 60 | + end |
| 61 | + end, |
| 62 | + }) |
| 63 | + |
| 64 | + M.copy_lines(src_buf, dst_buf) |
| 65 | + vim.api.nvim_create_autocmd({ 'TextChanged', 'TextChangedI' }, { |
| 66 | + group = M.group, |
| 67 | + buffer = src_buf, |
| 68 | + callback = function(args) |
| 69 | + if env.valid(src_buf, src_win) and env.valid(dst_buf, dst_win) then |
| 70 | + -- also need to copy cursor due to event ordering |
| 71 | + M.copy_lines(src_buf, dst_buf) |
| 72 | + M.copy_cursor(src_win, dst_win) |
| 73 | + M.copy_event(args, dst_buf) |
| 74 | + end |
| 75 | + end, |
| 76 | + }) |
| 77 | + |
| 78 | + vim.api.nvim_create_autocmd('BufWipeout', { |
| 79 | + group = M.group, |
| 80 | + buffer = dst_buf, |
| 81 | + once = true, |
| 82 | + callback = function() |
| 83 | + M.buffers[src_buf] = nil |
| 84 | + vim.api.nvim_clear_autocmds({ group = M.group, buffer = src_buf }) |
| 85 | + -- enable rendering for source buffer |
| 86 | + manager.set_buf(src_buf, true) |
| 87 | + end, |
| 88 | + }) |
| 89 | +end |
| 90 | + |
| 91 | +---@private |
| 92 | +---@param src integer |
| 93 | +---@param dst integer |
| 94 | +function M.copy_lines(src, dst) |
| 95 | + local src_lines = vim.api.nvim_buf_get_lines(src, 0, -1, false) |
| 96 | + local dst_lines = vim.api.nvim_buf_get_lines(dst, 0, -1, false) |
| 97 | + |
| 98 | + local src_text = table.concat(src_lines, '\n') .. '\n' |
| 99 | + local dst_text = table.concat(dst_lines, '\n') .. '\n' |
| 100 | + local diff = vim.diff(dst_text, src_text, { result_type = 'indices' }) |
| 101 | + assert(type(diff) == 'table', 'diff must provide indices') |
| 102 | + |
| 103 | + env.buf.set(dst, 'modifiable', true) |
| 104 | + for i = 1, #diff do |
| 105 | + local hunk = diff[#diff - i + 1] |
| 106 | + local start_a, count_a, start_b, count_b = unpack(hunk) |
| 107 | + local line_start = start_a - 1 |
| 108 | + local line_end = start_a + count_a - 1 |
| 109 | + if count_a == 0 then |
| 110 | + line_start = line_start + 1 |
| 111 | + line_end = line_end + 1 |
| 112 | + end |
| 113 | + vim.api.nvim_buf_set_lines(dst, line_start, line_end, false, { |
| 114 | + unpack(src_lines, start_b, start_b + count_b - 1), |
| 115 | + }) |
| 116 | + end |
| 117 | + env.buf.set(dst, 'modifiable', false) |
| 118 | +end |
| 119 | + |
| 120 | +---@private |
| 121 | +---@param src integer |
| 122 | +---@param dst integer |
| 123 | +function M.copy_cursor(src, dst) |
| 124 | + local cursor = vim.api.nvim_win_get_cursor(src) |
| 125 | + pcall(vim.api.nvim_win_set_cursor, dst, cursor) |
| 126 | +end |
| 127 | + |
| 128 | +---@private |
| 129 | +---@param args vim.api.keyset.create_autocmd.callback_args |
| 130 | +---@param buf integer |
| 131 | +function M.copy_event(args, buf) |
| 132 | + vim.api.nvim_exec_autocmds(args.event, { buffer = buf }) |
| 133 | +end |
| 134 | + |
| 135 | +return M |
0 commit comments