From cdd7730bd799c93c620edda7a8aa474f72495a1c Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 29 Aug 2024 20:32:00 +0800 Subject: [PATCH] feat: choose all theirs (#361) --- README.md | 1 + lua/avante/config.lua | 1 + lua/avante/diff.lua | 65 ++++++++++++++++++++++++------------------- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 6aedfc4..962b331 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,7 @@ _See [config.lua#L9](./lua/avante/config.lua) for the full config_ diff = { ours = "co", theirs = "ct", + all_theirs = "ca", both = "cb", cursor = "cc", next = "]x", diff --git a/lua/avante/config.lua b/lua/avante/config.lua index 673acc2..68c252c 100644 --- a/lua/avante/config.lua +++ b/lua/avante/config.lua @@ -95,6 +95,7 @@ M.defaults = { diff = { ours = "co", theirs = "ct", + all_theirs = "ca", none = "c0", both = "cb", cursor = "cc", diff --git a/lua/avante/diff.lua b/lua/avante/diff.lua index d8fbdc4..7fc1ebd 100644 --- a/lua/avante/diff.lua +++ b/lua/avante/diff.lua @@ -26,7 +26,7 @@ local map = vim.keymap.set -- Types -----------------------------------------------------------------------------// ----@alias ConflictSide "'ours'"|"'theirs'"|"'both'"|"'cursor'"|"'base'"|"'none'" +---@alias ConflictSide "'ours'"|"'theirs'"|"'all_theirs'"|"'both'"|"'cursor'"|"'base'"|"'none'" --- @class AvanteConflictHighlights --- @field current string @@ -67,6 +67,7 @@ local map = vim.keymap.set local SIDES = { OURS = "ours", THEIRS = "theirs", + ALL_THEIRS = "all_theirs", BOTH = "both", BASE = "base", NONE = "none", @@ -331,11 +332,11 @@ local function register_cursor_move_events(bufnr) end local hint = string.format( - "[<%s> for OURS, <%s> for THEIRS, <%s> for BOTH, <%s> for CURSOR, <%s> for PREV, <%s> for NEXT]", + "[<%s>: OURS, <%s>: THEIRS, <%s>: CURSOR, <%s>: ALL THEIRS, <%s>: PREV, <%s>: NEXT]", Config.diff.mappings.ours, Config.diff.mappings.theirs, - Config.diff.mappings.both, Config.diff.mappings.cursor, + Config.diff.mappings.all_theirs, Config.diff.mappings.prev, Config.diff.mappings.next ) @@ -424,6 +425,9 @@ local function set_commands() command("AvanteConflictChooseTheirs", function() M.choose("theirs") end, { nargs = 0 }) + command("AvanteConflictChooseAllTheirs", function() + M.choose("all_theirs") + end, { nargs = 0 }) command("AvanteConflictChooseBoth", function() M.choose("both") end, { nargs = 0 }) @@ -457,6 +461,12 @@ local function set_plug_mappings() map({ "n", "v" }, "(git-conflict-both)", "AvanteConflictChooseBoth", opts("Choose Both")) map({ "n", "v" }, "(git-conflict-none)", "AvanteConflictChooseNone", opts("Choose None")) map({ "n", "v" }, "(git-conflict-theirs)", "AvanteConflictChooseTheirs", opts("Choose Theirs")) + map( + { "n", "v" }, + "(git-conflict-all-theirs)", + "AvanteConflictChooseAllTheirs", + opts("Choose All Theirs") + ) map("n", "(git-conflict-cursor)", "AvanteConflictChooseCursor", opts("Choose Cursor")) map("n", "(git-conflict-next-conflict)", "AvanteConflictNextConflict", opts("Next Conflict")) map("n", "(git-conflict-prev-conflict)", "AvanteConflictPrevConflict", opts("Previous Conflict")) @@ -473,6 +483,7 @@ local function setup_buffer_mappings(bufnr) map({ "n", "v" }, Config.diff.mappings.both, "(git-conflict-both)", opts("Choose Both")) map({ "n", "v" }, Config.diff.mappings.none, "(git-conflict-none)", opts("Choose None")) map({ "n", "v" }, Config.diff.mappings.theirs, "(git-conflict-theirs)", opts("Choose Theirs")) + map({ "n", "v" }, Config.diff.mappings.all_theirs, "(git-conflict-all-theirs)", opts("Choose All Theirs")) map({ "v", "v" }, Config.diff.mappings.ours, "(git-conflict-ours)", opts("Choose Ours")) map("n", Config.diff.mappings.cursor, "(git-conflict-cursor)", opts("Choose Cursor")) -- map('V', Config.diff.mappings.ours, '(git-conflict-ours)', opts('Choose Ours')) @@ -637,30 +648,7 @@ function M.choose(side) return left and right end) while position ~= nil do - local lines = {} - if vim.tbl_contains({ SIDES.OURS, SIDES.THEIRS, SIDES.BASE }, side) then - local data = position[name_map[side]] - lines = Utils.get_buf_lines(data.content_start, data.content_end + 1) - elseif side == SIDES.BOTH then - local first = Utils.get_buf_lines(position.current.content_start, position.current.content_end + 1) - local second = Utils.get_buf_lines(position.incoming.content_start, position.incoming.content_end + 1) - lines = vim.list_extend(first, second) - elseif side == SIDES.NONE then - lines = {} - else - return - end - - local pos_start = position.current.range_start < 0 and 0 or position.current.range_start - local pos_end = position.incoming.range_end + 1 - - api.nvim_buf_set_lines(0, pos_start, pos_end, false, lines) - api.nvim_buf_del_extmark(0, NAMESPACE, position.marks.incoming.label) - api.nvim_buf_del_extmark(0, NAMESPACE, position.marks.current.label) - if position.marks.ancestor.label then - api.nvim_buf_del_extmark(0, NAMESPACE, position.marks.ancestor.label) - end - parse_buffer(bufnr) + M.process_position(bufnr, side, position, false) position = find_position(bufnr, function(line, pos) local left = pos.current.range_start >= start - 1 local right = pos.incoming.range_end <= finish + 1 @@ -678,6 +666,27 @@ function M.choose(side) if not position then return end + if side == SIDES.ALL_THEIRS then + ---@diagnostic disable-next-line: unused-local + local pos = find_position(bufnr, function(line, pos) + return true + end) + while pos ~= nil do + M.process_position(bufnr, "theirs", pos, false) + ---@diagnostic disable-next-line: unused-local + pos = find_position(bufnr, function(line, pos) + return true + end) + end + else + M.process_position(bufnr, side, position, true) + end +end + +---@param side ConflictSide +---@param position ConflictPosition +---@param enable_autojump boolean +function M.process_position(bufnr, side, position, enable_autojump) local lines = {} if vim.tbl_contains({ SIDES.OURS, SIDES.THEIRS, SIDES.BASE }, side) then local data = position[name_map[side]] @@ -715,7 +724,7 @@ function M.choose(side) api.nvim_buf_del_extmark(0, NAMESPACE, position.marks.ancestor.label) end parse_buffer(bufnr) - if Config.diff.autojump then + if enable_autojump and Config.diff.autojump then M.find_next(side) vim.cmd([[normal! zz]]) end