aboutsummaryrefslogtreecommitdiffstats
path: root/lua/git-yodel.lua
blob: e0e8e80957dcd4c3f4a32892b07b346603977ce3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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