local buf = require('modules.buf') local win = require('modules.win') local sim = require('utils.simple') local M = {} local C = {} function M.setup(c) c = c or {} C.diffpreview = { border = c.diffpreview.border or 'single', width = c.diffpreview.width or 'auto', height = c.diffpreview.height or 'auto', } end -- window columns offset local function winc() local a = 0 local num = vim.o.number or vim.o.relativenumber -- if numberline or relativenumbers are enabled make sure we account for it if num then a = vim.o.numberwidth end -- same for signcolumn if vim.o.signcolumn then a = a + string.match(vim.o.signcolumn, '[0-9]') end -- if both we will have some spacing room between them if num and vim.o.signcolumn then a = a + 1 end -- if we have either we need to account for padding next to the buffer if num or vim.o.signcolumn then a = a + 1 end return sim.longbufl(0) + a end -- set window width local function winw(b) if C.diffpreview.width == 'auto' then return sim.lower( sim.longbufl(b), vim.o.columns - winc() - 2 ) else return C.diffpreview.width end end -- set window height local function winh(b, x) x = x or 0 if C.diffpreview.height == 'auto' then return sim.lower( -- lines in buffer - cmd height - statusline vim.o.lines - vim.o.cmdheight - x - 4, vim.api.nvim_buf_line_count(b) ) else return C.diffpreview.height end end vim.api.nvim_create_autocmd('FileType', { pattern = { 'gitcommit', 'gitrebase' }, callback = function() local bufa = buf.create(true, false, 'Commit Diff') local conta = sim.cmdcontent('git diff --cached') if conta == nil then return end -- make sure we're setting from nothing vim.api.nvim_buf_set_option(bufa, 'ft', 'diff') vim.api.nvim_buf_set_lines(bufa, 0, -1, false, conta) buf.lock(bufa) local winopts = { row = 1, col = winc(), width = winw(bufa), height = winh(bufa), border = C.diffpreview.border, relative = 'win', style = 'minimal', } local wina = win.create(bufa, false, winopts) -- NOTE: we monitor a few events to make sure the window changes positions -- properly vim.api.nvim_create_autocmd({ 'VimResized', 'CursorMovedI', 'CursorMoved', }, { callback = function() if vim.o.columns < 100 then win.hide(wina) wina = win.create(bufa, false, { row = vim.api.nvim_buf_line_count(0) + 1, col = 2, width = vim.o.columns - 4, height = winh(bufa, vim.api.nvim_buf_line_count(0)), border = C.diffpreview.border, relative = 'win', style = 'minimal', }) else win.hide(wina) wina = win.create(bufa, false, { row = 1, col = winc(), width = winw(bufa), height = winh(bufa), border = C.diffpreview.border, relative = 'win', style = 'minimal', }) end end }) end }) return M