feat(diff): autojump (#48)

centralized configuration

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
Aaron Pham 2024-08-17 14:14:02 -04:00 committed by GitHub
parent f5d9d2139a
commit 1aaf6a8227
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 68 additions and 108 deletions

View File

@ -1,5 +1,6 @@
---NOTE: user will be merged with defaults and ---NOTE: user will be merged with defaults and
---we add a default var_accessor for this table to config values. ---we add a default var_accessor for this table to config values.
---
---@class avante.CoreConfig: avante.Config ---@class avante.CoreConfig: avante.Config
local M = {} local M = {}
@ -26,6 +27,7 @@ M.defaults = {
max_tokens = 4096, max_tokens = 4096,
}, },
highlights = { highlights = {
---@type AvanteConflictHighlights
diff = { diff = {
current = "DiffText", -- need have background color current = "DiffText", -- need have background color
incoming = "DiffAdd", -- need have background color incoming = "DiffAdd", -- need have background color
@ -35,6 +37,7 @@ M.defaults = {
ask = "<leader>aa", ask = "<leader>aa",
edit = "<leader>ae", edit = "<leader>ae",
refresh = "<leader>ar", refresh = "<leader>ar",
--- @class AvanteConflictMappings
diff = { diff = {
ours = "co", ours = "co",
theirs = "ct", theirs = "ct",
@ -51,14 +54,33 @@ M.defaults = {
windows = { windows = {
width = 30, -- default % based on available width width = 30, -- default % based on available width
}, },
--- @class AvanteConflictUserConfig
diff = {
debug = false,
autojump = true,
---@type string | fun(): any
list_opener = "copen",
},
} }
---@type avante.Config ---@type avante.Config
M.options = {} M.options = {}
---@class avante.ConflictConfig: AvanteConflictUserConfig
---@field mappings AvanteConflictMappings
---@field highlights AvanteConflictHighlights
M.diff = {}
---@param opts? avante.Config ---@param opts? avante.Config
function M.setup(opts) function M.setup(opts)
M.options = vim.tbl_deep_extend("force", M.defaults, opts or {}) M.options = vim.tbl_deep_extend("force", M.defaults, opts or {})
M.diff = vim.tbl_deep_extend(
"force",
{},
M.options.diff,
{ mappings = M.options.mappings.diff, highlights = M.options.highlights.diff }
)
end end
M = setmetatable(M, { M = setmetatable(M, {

View File

@ -5,11 +5,12 @@ local M = {}
local color = require("avante.diff.colors") local color = require("avante.diff.colors")
local utils = require("avante.diff.utils") local utils = require("avante.diff.utils")
local Config = require("avante.config")
local fn = vim.fn local fn = vim.fn
local api = vim.api local api = vim.api
local fmt = string.format local fmt = string.format
local map = vim.keymap.set local map = vim.keymap.set
local job = utils.job
-----------------------------------------------------------------------------// -----------------------------------------------------------------------------//
-- REFERENCES: -- REFERENCES:
-----------------------------------------------------------------------------// -----------------------------------------------------------------------------//
@ -28,7 +29,7 @@ local job = utils.job
---@alias ConflictSide "'ours'"|"'theirs'"|"'both'"|"'base'"|"'none'" ---@alias ConflictSide "'ours'"|"'theirs'"|"'both'"|"'base'"|"'none'"
--- @class ConflictHighlights --- @class AvanteConflictHighlights
--- @field current string --- @field current string
--- @field incoming string --- @field incoming string
--- @field ancestor string? --- @field ancestor string?
@ -60,31 +61,10 @@ local job = utils.job
--- @field tick integer --- @field tick integer
--- @field bufnr integer --- @field bufnr integer
--- @class AvanteConflictMappings
--- @field ours string
--- @field theirs string
--- @field none string
--- @field both string
--- @field next string
--- @field prev string
--- @class AvanteConflictConfig
--- @field default_mappings AvanteConflictMappings
--- @field disable_diagnostics boolean
--- @field list_opener string|function
--- @field highlights ConflictHighlights
--- @field debug boolean
--- @class AvanteConflictUserConfig
--- @field default_mappings boolean|AvanteConflictMappings
--- @field disable_diagnostics boolean
--- @field list_opener string|function
--- @field highlights ConflictHighlights
--- @field debug boolean
-----------------------------------------------------------------------------// -----------------------------------------------------------------------------//
-- Constants -- Constants
-----------------------------------------------------------------------------// -----------------------------------------------------------------------------//
---@enum AvanteConflictSides
local SIDES = { local SIDES = {
OURS = "ours", OURS = "ours",
THEIRS = "theirs", THEIRS = "theirs",
@ -123,30 +103,6 @@ local DEFAULT_INCOMING_BG_COLOR = 3229523 -- #314753
local DEFAULT_ANCESTOR_BG_COLOR = 6824314 -- #68217A local DEFAULT_ANCESTOR_BG_COLOR = 6824314 -- #68217A
-----------------------------------------------------------------------------// -----------------------------------------------------------------------------//
--- @type AvanteConflictMappings
local DEFAULT_MAPPINGS = {
ours = "co",
theirs = "ct",
none = "c0",
both = "cb",
next = "]x",
prev = "[x",
}
--- @type AvanteConflictConfig
local config = {
debug = false,
default_mappings = DEFAULT_MAPPINGS,
default_commands = true,
disable_diagnostics = false,
list_opener = "copen",
highlights = {
current = "DiffText",
incoming = "DiffAdd",
ancestor = nil,
},
}
--- @return table<string, ConflictBufferCache> --- @return table<string, ConflictBufferCache>
local function create_visited_buffers() local function create_visited_buffers()
return setmetatable({}, { return setmetatable({}, {
@ -367,20 +323,19 @@ local function set_cursor(position, side)
api.nvim_win_set_cursor(0, { target.range_start + 1, 0 }) api.nvim_win_set_cursor(0, { target.range_start + 1, 0 })
end end
local show_keybinding_hint_extmark_id = nil
local function register_cursor_move_events(bufnr) local function register_cursor_move_events(bufnr)
local show_keybinding_hint_extmark_id = nil
local function show_keybinding_hint(lnum) local function show_keybinding_hint(lnum)
if show_keybinding_hint_extmark_id then if show_keybinding_hint_extmark_id then
api.nvim_buf_del_extmark(bufnr, KEYBINDING_NAMESPACE, show_keybinding_hint_extmark_id) api.nvim_buf_del_extmark(bufnr, KEYBINDING_NAMESPACE, show_keybinding_hint_extmark_id)
end end
local hint = string.format( local hint = string.format(
" [Press <%s> for CHOICE OURS, <%s> for CHOICE THEIRS, <%s> for PREV, <%s> for NEXT] ", " [Press <%s> for OURS, <%s> for THEIRS, <%s> for PREV, <%s> for NEXT] ",
config.default_mappings.ours, Config.diff.mappings.ours,
config.default_mappings.theirs, Config.diff.mappings.theirs,
config.default_mappings.prev, Config.diff.mappings.prev,
config.default_mappings.next Config.diff.mappings.next
) )
local win_width = api.nvim_win_get_width(0) local win_width = api.nvim_win_get_width(0)
local col = win_width - #hint - math.ceil(win_width * 0.3) - 4 local col = win_width - #hint - math.ceil(win_width * 0.3) - 4
@ -413,8 +368,8 @@ end
---Get the conflict marker positions for a buffer if any and update the buffers state ---Get the conflict marker positions for a buffer if any and update the buffers state
---@param bufnr integer ---@param bufnr integer
---@param range_start integer ---@param range_start? integer
---@param range_end integer ---@param range_end? integer
local function parse_buffer(bufnr, range_start, range_end) local function parse_buffer(bufnr, range_start, range_end)
local lines = utils.get_buf_lines(range_start or 0, range_end or -1, bufnr) local lines = utils.get_buf_lines(range_start or 0, range_end or -1, bufnr)
local prev_conflicts = visited_buffers[bufnr].positions ~= nil and #visited_buffers[bufnr].positions > 0 local prev_conflicts = visited_buffers[bufnr].positions ~= nil and #visited_buffers[bufnr].positions > 0
@ -453,10 +408,10 @@ local function set_commands()
M.conflicts_to_qf_items(function(items) M.conflicts_to_qf_items(function(items)
if #items > 0 then if #items > 0 then
fn.setqflist(items, "r") fn.setqflist(items, "r")
if type(config.list_opener) == "function" then if type(Config.diff.list_opener) == "function" then
config.list_opener() Config.diff.list_opener()
else else
vim.cmd(config.list_opener) vim.cmd(Config.diff.list_opener)
end end
end end
end) end)
@ -501,19 +456,21 @@ local function set_plug_mappings()
map("n", "<Plug>(git-conflict-prev-conflict)", "<Cmd>AvanteConflictPrevConflict<CR>", opts("Previous Conflict")) map("n", "<Plug>(git-conflict-prev-conflict)", "<Cmd>AvanteConflictPrevConflict<CR>", opts("Previous Conflict"))
end end
---@param bufnr integer given buffer id
local function setup_buffer_mappings(bufnr) local function setup_buffer_mappings(bufnr)
---@param desc string
local function opts(desc) local function opts(desc)
return { silent = true, buffer = bufnr, desc = "Git Conflict: " .. desc } return { silent = true, buffer = bufnr, desc = "Git Conflict: " .. desc }
end end
map({ "n", "v" }, config.default_mappings.ours, "<Plug>(git-conflict-ours)", opts("Choose Ours")) map({ "n", "v" }, Config.diff.mappings.ours, "<Plug>(git-conflict-ours)", opts("Choose Ours"))
map({ "n", "v" }, config.default_mappings.both, "<Plug>(git-conflict-both)", opts("Choose Both")) map({ "n", "v" }, Config.diff.mappings.both, "<Plug>(git-conflict-both)", opts("Choose Both"))
map({ "n", "v" }, config.default_mappings.none, "<Plug>(git-conflict-none)", opts("Choose None")) map({ "n", "v" }, Config.diff.mappings.none, "<Plug>(git-conflict-none)", opts("Choose None"))
map({ "n", "v" }, config.default_mappings.theirs, "<Plug>(git-conflict-theirs)", opts("Choose Theirs")) map({ "n", "v" }, Config.diff.mappings.theirs, "<Plug>(git-conflict-theirs)", opts("Choose Theirs"))
map({ "v", "v" }, config.default_mappings.ours, "<Plug>(git-conflict-ours)", opts("Choose Ours")) map({ "v", "v" }, Config.diff.mappings.ours, "<Plug>(git-conflict-ours)", opts("Choose Ours"))
-- map('V', config.default_mappings.ours, '<Plug>(git-conflict-ours)', opts('Choose Ours')) -- map('V', Config.diff.mappings.ours, '<Plug>(git-conflict-ours)', opts('Choose Ours'))
map("n", config.default_mappings.prev, "<Plug>(git-conflict-prev-conflict)", opts("Previous Conflict")) map("n", Config.diff.mappings.prev, "<Plug>(git-conflict-prev-conflict)", opts("Previous Conflict"))
map("n", config.default_mappings.next, "<Plug>(git-conflict-next-conflict)", opts("Next Conflict")) map("n", Config.diff.mappings.next, "<Plug>(git-conflict-next-conflict)", opts("Next Conflict"))
vim.b[bufnr].conflict_mappings_set = true vim.b[bufnr].conflict_mappings_set = true
end end
@ -528,7 +485,7 @@ local function clear_buffer_mappings(bufnr)
if not bufnr or not vim.b[bufnr].conflict_mappings_set then if not bufnr or not vim.b[bufnr].conflict_mappings_set then
return return
end end
for _, mapping in pairs(config.default_mappings) do for _, mapping in pairs(Config.diff.mappings) do
if is_mapped(mapping) then if is_mapped(mapping) then
api.nvim_buf_del_keymap(bufnr, "n", mapping) api.nvim_buf_del_keymap(bufnr, "n", mapping)
end end
@ -541,7 +498,7 @@ end
-----------------------------------------------------------------------------// -----------------------------------------------------------------------------//
---Derive the colour of the section label highlights based on each sections highlights ---Derive the colour of the section label highlights based on each sections highlights
---@param highlights ConflictHighlights ---@param highlights AvanteConflictHighlights
local function set_highlights(highlights) local function set_highlights(highlights)
local current_color = utils.get_hl(highlights.current) local current_color = utils.get_hl(highlights.current)
local incoming_color = utils.get_hl(highlights.incoming) local incoming_color = utils.get_hl(highlights.incoming)
@ -560,21 +517,10 @@ local function set_highlights(highlights)
api.nvim_set_hl(0, ANCESTOR_LABEL_HL, { background = ancestor_label_bg, default = true }) api.nvim_set_hl(0, ANCESTOR_LABEL_HL, { background = ancestor_label_bg, default = true })
end end
---@param user_config AvanteConflictUserConfig function M.setup()
function M.setup(user_config) set_highlights(Config.diff.highlights)
local _user_config = user_config or {}
if _user_config.default_mappings == true then
_user_config.default_mappings = DEFAULT_MAPPINGS
end
config = vim.tbl_deep_extend("force", config, _user_config)
set_highlights(config.highlights)
if config.default_commands then
set_commands() set_commands()
end
set_plug_mappings() set_plug_mappings()
@ -582,7 +528,7 @@ function M.setup(user_config)
api.nvim_create_autocmd("ColorScheme", { api.nvim_create_autocmd("ColorScheme", {
group = AUGROUP_NAME, group = AUGROUP_NAME,
callback = function() callback = function()
set_highlights(config.highlights) set_highlights(Config.diff.highlights)
end, end,
}) })
@ -591,12 +537,8 @@ function M.setup(user_config)
pattern = "AvanteConflictDetected", pattern = "AvanteConflictDetected",
callback = function() callback = function()
local bufnr = api.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
if config.disable_diagnostics then vim.diagnostic.enable(not vim.diagnostic.is_enabled(), { bufnr = bufnr })
vim.diagnostic.disable(bufnr)
end
if config.default_mappings then
setup_buffer_mappings(bufnr) setup_buffer_mappings(bufnr)
end
end, end,
}) })
@ -605,12 +547,8 @@ function M.setup(user_config)
pattern = "AvanteConflictResolved", pattern = "AvanteConflictResolved",
callback = function() callback = function()
local bufnr = api.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
if config.disable_diagnostics then vim.diagnostic.enable(not vim.diagnostic.is_enabled(), { bufnr = bufnr })
vim.diagnostic.enable(bufnr)
end
if config.default_mappings then
clear_buffer_mappings(bufnr) clear_buffer_mappings(bufnr)
end
end, end,
}) })
@ -745,6 +683,10 @@ function M.choose(side)
end) end)
end end
end, 50) end, 50)
if Config.diff.autojump then
M.find_next(side)
vim.cmd([[normal! zz]])
end
return return
end end
local position = get_current_position(bufnr) local position = get_current_position(bufnr)
@ -775,6 +717,10 @@ function M.choose(side)
api.nvim_buf_del_extmark(0, NAMESPACE, position.marks.ancestor.label) api.nvim_buf_del_extmark(0, NAMESPACE, position.marks.ancestor.label)
end end
parse_buffer(bufnr) parse_buffer(bufnr)
if Config.diff.autojump then
M.find_next(side)
vim.cmd([[normal! zz]])
end
end end
function M.conflict_count(bufnr) function M.conflict_count(bufnr)

View File

@ -162,18 +162,9 @@ function M.setup(opts)
if M._once then if M._once then
return return
end end
Diff.setup({
debug = false, -- log output to console
default_mappings = Config.mappings.diff, -- disable buffer local mapping created by this plugin
default_commands = true, -- disable commands created by this plugin
disable_diagnostics = true, -- This will disable the diagnostics in a buffer whilst it is conflicted
list_opener = "copen",
highlights = Config.highlights.diff,
})
local selection = Selection:new() Diff.setup()
selection:setup() M.selection = Selection:new():setup()
M.selection = selection
-- setup helpers -- setup helpers
H.autocmds() H.autocmds()

View File

@ -83,6 +83,7 @@ function Selection:setup()
self:close_hints_popup() self:close_hints_popup()
end, end,
}) })
return self
end end
function Selection:delete_autocmds() function Selection:delete_autocmds()