summaryrefslogtreecommitdiffstats
path: root/lua/core
diff options
context:
space:
mode:
authorSquibid <me@zacharyscheiman.com>2023-07-16 21:28:36 -0400
committerSquibid <me@zacharyscheiman.com>2023-07-16 21:28:36 -0400
commita6eca921d607388b0e024760b588d3ffb2a6de50 (patch)
treec89a87f6938d61a41034606e15919bab68489133 /lua/core
parent1b288fe932ebe91e76f4af2e166ad242656e6240 (diff)
downloadnvim-a6eca921d607388b0e024760b588d3ffb2a6de50.tar.gz
nvim-a6eca921d607388b0e024760b588d3ffb2a6de50.tar.bz2
nvim-a6eca921d607388b0e024760b588d3ffb2a6de50.zip
create floating window with commit diff when editing git commit message
Diffstat (limited to 'lua/core')
-rw-r--r--lua/core/auto.lua51
1 files changed, 51 insertions, 0 deletions
diff --git a/lua/core/auto.lua b/lua/core/auto.lua
index 8206ad6..d833aa2 100644
--- a/lua/core/auto.lua
+++ b/lua/core/auto.lua
@@ -18,6 +18,57 @@ auto('FileType', { -- start git messages in insert mode
command = 'startinsert | 1'
})
+auto('FileType', { -- show git cached diff
+ group = 'bufcheck',
+ pattern = { 'gitcommit', 'gitrebase', },
+ callback = function()
+ local function lower(a, b)
+ if a > b then
+ return b
+ else
+ return a
+ end
+ end
+
+ local function tablelength(T)
+ local count = 0
+ for _ in pairs(T) do count = count + 1 end
+ return count
+ end
+
+ local diff = io.popen('git diff --cached', "r")
+ local diffl = {}
+ for i in diff:lines() do
+ table.insert(diffl, i)
+ end
+ diff:close()
+
+ if next(diffl) == nil then
+ vim.notify('No diff to show :(', vim.log.levels.INFO, {
+ title = 'Neovim Config' })
+ return
+ end
+
+ local buf = a.nvim_create_buf(true, false)
+ a.nvim_buf_set_lines(buf, 0, -1, false, diffl)
+ a.nvim_buf_set_name(buf, 'Git Commit Diff')
+ a.nvim_buf_set_option(buf, 'ft', 'diff')
+ a.nvim_buf_set_option(buf, 'readonly', true)
+ a.nvim_buf_set_option(buf, 'modifiable', false)
+ a.nvim_buf_set_option(buf, 'modified', false)
+ local win = a.nvim_open_win(buf, false, {
+ row = 1,
+ col = 80,
+ relative = 'win',
+ width = 80,
+ height = lower(vim.o.lines - vim.o.cmdheight - 4, tablelength(diffl)),
+ border = 'shadow',
+ style = 'minimal',
+ })
+ end,
+})
+
+
auto('BufRead', { -- return to last place
pattern = '*',
command = [[call setpos(".", getpos("'\""))]]