local M = {}
local lines_cache = nil
function M.get_lines(buf)
if not lines_cache then
lines_cache = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
end
return lines_cache
end
function M.invalidate()
lines_cache = nil
end
function M.find_line(buf, pattern, start)
local ll = M.get_lines(buf)
for i = (start or 1), #ll do
if ll[i]:find(pattern, 1, false) then
return i - 1 end
end
return nil
end
function M.find_pos(buf, needle, start)
local ll = M.get_lines(buf)
for i = (start or 1), #ll do
local col = ll[i]:find(needle, 1, true)
if col then
return i - 1, col - 1 end
end
return nil, nil
end
function M.append(buf, text_lines)
local count = vim.api.nvim_buf_line_count(buf)
vim.api.nvim_buf_set_lines(buf, count, count, false, text_lines)
M.invalidate()
return count
end
function M.remove(buf, from, to)
vim.api.nvim_buf_set_lines(buf, from, to, false, {})
M.invalidate()
end
return M