106 lines
3.1 KiB
Lua
106 lines
3.1 KiB
Lua
local client = require"MarkdownPreviewer.app"
|
|
local throttle = require"MarkdownPreviewer.throttle"
|
|
|
|
local nvim_buf_get_lines = vim.api.nvim_buf_get_lines
|
|
local nvim_create_augroup = vim.api.nvim_create_augroup
|
|
local nvim_create_autocmd = vim.api.nvim_create_autocmd
|
|
local nvim_del_augroup_by_id = vim.api.nvim_del_augroup_by_id
|
|
local concat = table.concat
|
|
|
|
local function get_buf_content(bufnr)
|
|
local data = concat(nvim_buf_get_lines(bufnr, 0, -1, false), '\n'):gsub('%s*$', '')
|
|
return data
|
|
end
|
|
|
|
module = {}
|
|
|
|
local server_connection
|
|
|
|
function module.setup()
|
|
server_connection = client.setup()
|
|
end
|
|
|
|
local function SetAutoCmd(augroup, server, bufnr)
|
|
nvim_create_autocmd("BufWritePost", {
|
|
group = augroup,
|
|
buffer = bufnr,
|
|
callback = function()
|
|
server:show(get_buf_content(bufnr))
|
|
end,
|
|
})
|
|
|
|
nvim_create_autocmd({"CursorMoved", "CursorMovedI"}, {
|
|
group = augroup,
|
|
buffer = bufnr,
|
|
callback = function()
|
|
server:scroll({vim.fn.line('.'), vim.fn.line('$')})
|
|
end,
|
|
})
|
|
|
|
local function show()
|
|
server:show(get_buf_content(bufnr))
|
|
end
|
|
|
|
nvim_create_autocmd({"TextChanged", "TextChangedI", "TextChangedP" }, {
|
|
group = augroup,
|
|
buffer = bufnr,
|
|
callback = function()
|
|
throttle.run_fn(show)
|
|
end,
|
|
})
|
|
end
|
|
|
|
local function FirstSend(server, bufnr)
|
|
print("Sending data to server from buffer " .. bufnr)
|
|
server:base(vim.fn.fnamemodify(vim.uri_to_fname(vim.uri_from_bufnr(bufnr)), 'p:h'))
|
|
server:show(get_buf_content(bufnr))
|
|
server:scroll(vim.fn.line('.'))
|
|
end
|
|
|
|
function module.open()
|
|
augroup = nvim_create_augroup("MarkdownPreviewActiveAugroup", {clear = true})
|
|
chgbuffergroup = nvim_create_augroup("MarkdownChangeBufferActiveAugroup", {clear = true})
|
|
|
|
server_connection:init(function()
|
|
if augroup then
|
|
augroup = nvim_del_augroup_by_id(augroup)
|
|
end
|
|
|
|
if chgbuffergroup then
|
|
chgbuffergroup = nvim_del_augroup_by_id(chgbuffergroup)
|
|
end
|
|
end)
|
|
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
|
FirstSend(server_connection, bufnr)
|
|
SetAutoCmd(augroup, server_connection, bufnr)
|
|
|
|
nvim_create_autocmd("BufEnter", {
|
|
group = chgbuffergroup,
|
|
callback = function()
|
|
local new_buf_nr = vim.api.nvim_get_current_buf()
|
|
local ft = vim.api.nvim_buf_get_option(new_buf_nr, "filetype")
|
|
if ft == "markdown" then
|
|
print("filetype new buffer: " .. ft)
|
|
FirstSend(server_connection, new_buf_nr)
|
|
if augroup then
|
|
augroup = nvim_del_augroup_by_id(augroup)
|
|
end
|
|
augroup = nvim_create_augroup("MarkdownPreviewActiveAugroup", {clear = True})
|
|
SetAutoCmd(augroup, server_connection, new_buf_nr)
|
|
else
|
|
print("Not a markdown buffer")
|
|
augroup = nvim_del_augroup_by_id(augroup)
|
|
end
|
|
end,
|
|
})
|
|
end
|
|
|
|
function module.close()
|
|
if server_connection then
|
|
server_connection:stop()
|
|
end
|
|
end
|
|
|
|
return module
|