From fd4a16253c4ca90068c3a238bd9681ebc5c0d856 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 12 Nov 2020 12:26:49 +0000 Subject: [PATCH] Add ability to preview hunks in a floating window --- README.md | 3 ++- lua/gitsigns.lua | 21 +++++++++++++++++++++ lua/gitsigns/defaults.lua | 1 + lua/gitsigns/popup.lua | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 lua/gitsigns/popup.lua diff --git a/README.md b/README.md index 2d01845..4c6b5cf 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ require('gitsigns').setup { ['n hs'] = 'lua require"gitsigns".stage_hunk()', ['n hu'] = 'lua require"gitsigns".undo_stage_hunk()', ['n hr'] = 'lua require"gitsigns".reset_hunk()', + ['n hp'] = 'lua require"gitsigns".preview_hunk()', }, watch_index = { interval = 1000 @@ -89,7 +90,7 @@ set statusline+=%{get(b:,'gitsigns_status','')} - [x] Add action for undoing a stage of a hunk - [x] Add action for ~~undoing~~ reseting a hunk -- [ ] Add action for showing diff (or original text) in a floating window +- [x] Add action for showing diff (or original text) in a floating window - [ ] Add ability to show staged hunks with different signs (maybe in a different sign column?) - [x] Add support for repeat.vim - [ ] Apply buffer updates incrementally diff --git a/lua/gitsigns.lua b/lua/gitsigns.lua index 57b3b00..5878a24 100644 --- a/lua/gitsigns.lua +++ b/lua/gitsigns.lua @@ -7,6 +7,7 @@ local default_config = require('gitsigns/defaults') local mk_repeatable = require('gitsigns/repeat').mk_repeatable local DB = require('gitsigns/debounce') local apply_mappings = require('gitsigns/mappings') +local popup = require('gitsigns/popup') local throttle_leading = DB.throttle_leading local debounce_trailing = DB.debounce_trailing @@ -736,6 +737,24 @@ local function setup(cfg) vim.cmd('autocmd ExitPre * lua require("gitsigns").detach_all()') end +function preview_hunk() + local hunk = get_hunk() + + if not hunk then + return + end + + local winid, bufnr = popup.create(hunk.lines, { relative = 'cursor' }) + + vim.fn.nvim_buf_set_option(bufnr, 'filetype', 'diff') + vim.fn.nvim_win_set_option(winid, 'number', false) + vim.fn.nvim_win_set_option(winid, 'relativenumber', false) +end + +function dump_cache() + print(vim.inspect(cache)) +end + return { update = update, stage_hunk = mk_repeatable(stage_hunk), @@ -743,7 +762,9 @@ return { reset_hunk = mk_repeatable(reset_hunk), next_hunk = next_hunk, prev_hunk = prev_hunk, + preview_hunk = preview_hunk, attach = attach, detach_all = detach_all, setup = setup, + dump_cache = dump_cache } diff --git a/lua/gitsigns/defaults.lua b/lua/gitsigns/defaults.lua index f7f2e24..4f656a5 100644 --- a/lua/gitsigns/defaults.lua +++ b/lua/gitsigns/defaults.lua @@ -17,6 +17,7 @@ return { ['n hs'] = 'lua require"gitsigns".stage_hunk()', ['n hu'] = 'lua require"gitsigns".undo_stage_hunk()', ['n hr'] = 'lua require"gitsigns".reset_hunk()', + ['n hp'] = 'lua require"gitsigns".preview_hunk()', }, watch_index = { interval = 1000 diff --git a/lua/gitsigns/popup.lua b/lua/gitsigns/popup.lua new file mode 100644 index 0000000..c31cbf0 --- /dev/null +++ b/lua/gitsigns/popup.lua @@ -0,0 +1,35 @@ +local popup = {} + +local api = vim.api + +function popup.create(what, vim_options) + local bufnr = api.nvim_create_buf(false, true) + assert(bufnr, "Failed to create buffer") + + api.nvim_buf_set_lines(bufnr, 0, -1, true, what) + + local width = 0 + for _, l in pairs(what) do + if #l > width then + width = #l + end + end + + local win_id = api.nvim_open_win(bufnr, false, { + relative = vim_options.relative, + row = 0, + col = 0, + height = #what, + width = width + }) + + vim.lsp.util.close_preview_autocmd({'CursorMoved', 'CursorMovedI'}, win_id) + + if vim_options.highlight then + api.nvim_win_set_option(win_id, 'winhl', string.format('Normal:%s', vim_options.highlight)) + end + + return win_id, bufnr +end + +return popup