From 44323b9a112ad554dd486f3ab6f74a5b3433b223 Mon Sep 17 00:00:00 2001 From: Harrison Cramer Date: Tue, 25 Apr 2023 21:54:29 -0400 Subject: [PATCH] Added command to open diffview --- lua/gitlab/discussions.lua | 1 + lua/gitlab/init.lua | 1 + lua/gitlab/keymaps.lua | 26 ++++++++++++++++++++++++++ lua/gitlab/state.lua | 3 +++ lua/gitlab/utils/init.lua | 16 ++++++++++++++++ 5 files changed, 47 insertions(+) diff --git a/lua/gitlab/discussions.lua b/lua/gitlab/discussions.lua index 08e6638..35c0a4f 100644 --- a/lua/gitlab/discussions.lua +++ b/lua/gitlab/discussions.lua @@ -191,6 +191,7 @@ M.build_note = function(note) end local noteHeader = "@" .. note.author.username .. " on " .. u.format_date(note.created_at) + local note_node = NuiTree.Node( { text = noteHeader, diff --git a/lua/gitlab/init.lua b/lua/gitlab/init.lua index f83eca5..a73c10d 100644 --- a/lua/gitlab/init.lua +++ b/lua/gitlab/init.lua @@ -100,6 +100,7 @@ M.setup = function(args, build_only) end keymaps.set_keymap_keys(args.keymaps) + keymaps.set_keymaps() end M.current_file_path = function() diff --git a/lua/gitlab/keymaps.lua b/lua/gitlab/keymaps.lua index a29c801..55e5f35 100644 --- a/lua/gitlab/keymaps.lua +++ b/lua/gitlab/keymaps.lua @@ -19,4 +19,30 @@ M.set_keymap_keys = function(keyTable) state.keymaps = u.merge_tables(state.keymaps, keyTable) end +M.set_keymaps = function() + local ok, _ = pcall(require, "diffview") + vim.keymap.set("n", state.keymaps.review.toggle, function() + if not ok then + require("notify")("You must have diffview.nvim installed to use this command!", "error") + return + end + local isDiff = vim.fn.getwinvar(nil, "&diff") + local bufName = vim.api.nvim_buf_get_name(0) + local has_develop = u.branch_exists("main") -- TODO: Write this function + if not has_develop then + require("notify")('No ' .. state.BASE_BRANCH .. ' branch, cannot review.', "error") + return + end + if isDiff ~= 0 or u.string_starts(bufName, "diff") then + vim.cmd.tabclose() + vim.cmd.tabprev() + else + vim.cmd.DiffviewOpen(state.BASE_BRANCH) + u.press_enter() + end + end) +end + + + return M diff --git a/lua/gitlab/state.lua b/lua/gitlab/state.lua index 863f1f7..e1dd299 100644 --- a/lua/gitlab/state.lua +++ b/lua/gitlab/state.lua @@ -23,6 +23,9 @@ M.keymaps = { focus_prev = { "k", "", "" }, close = { "", "" }, submit = { "", "" }, + }, + review = { + toggle = "glt" } } diff --git a/lua/gitlab/utils/init.lua b/lua/gitlab/utils/init.lua index 9c1daeb..2e2a71b 100644 --- a/lua/gitlab/utils/init.lua +++ b/lua/gitlab/utils/init.lua @@ -10,6 +10,20 @@ local function get_git_root() end end +local branch_exists = function(b) + local is_git_branch = io.popen("git rev-parse --is-inside-work-tree 2>/dev/null"):read("*a") + if is_git_branch == "true\n" then + for line in io.popen("git branch 2>/dev/null"):lines() do + line = line:gsub("%s+", "") + if line == b then + return true + end + end + end + return false +end + + local function get_relative_file_path() local git_root = get_git_root() if git_root ~= nil then @@ -118,6 +132,7 @@ end local function jump_to_file(filename, line_number) + if line_number == nil then line_number = 1 end vim.api.nvim_command("wincmd l") local bufnr = vim.fn.bufnr(filename) if bufnr ~= -1 then @@ -244,5 +259,6 @@ M.create_popup_state = create_popup_state M.exit = exit M.read_file = read_file M.split_diff_view_filename = split_diff_view_filename +M.branch_exists = branch_exists M.P = P return M