Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions lua/glance/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local ALLOWED_TOP_LEVEL = {
windows = true,
filetree = true,
log = true,
merge = true,
keymaps = true,
pane_navigation = true,
hunk_navigation = true,
Expand Down Expand Up @@ -85,6 +86,32 @@ local ALLOWED_LOG = {
max_commits = true,
}

local ALLOWED_MERGE = {
keymaps = true,
}

local ALLOWED_MERGE_KEYMAPS = {
accept_ours = true,
accept_theirs = true,
accept_both_ours_then_theirs = true,
accept_both_theirs_then_ours = true,
ignore_ours = true,
ignore_theirs = true,
reset_conflict = true,
mark_resolved = true,
}

local MERGE_KEYMAP_ORDER = {
'accept_ours',
'accept_theirs',
'accept_both_ours_then_theirs',
'accept_both_theirs_then_ours',
'ignore_ours',
'ignore_theirs',
'reset_conflict',
'mark_resolved',
}

local ALLOWED_FILETREE_WINDOW = {
width = true,
number = true,
Expand Down Expand Up @@ -222,6 +249,18 @@ local BASE_DEFAULTS = {
log = {
max_commits = 200,
},
merge = {
keymaps = {
accept_ours = '<leader>o',
accept_theirs = '<leader>t',
accept_both_ours_then_theirs = '<leader>b',
accept_both_theirs_then_ours = '<leader>B',
ignore_ours = '<leader>O',
ignore_theirs = '<leader>T',
reset_conflict = '<leader>r',
mark_resolved = '<leader>m',
},
},
keymaps = {
open_file = '<CR>',
quit = 'q',
Expand Down Expand Up @@ -403,6 +442,44 @@ local function validate_log(options)
validate_integer(log.max_commits, 'log.max_commits', 1)
end

local function validate_merge(options)
local merge = options.merge
local keymaps = merge.keymaps or {}
local seen = {}

validate_known_keys(merge, ALLOWED_MERGE, 'merge')
validate_known_keys(keymaps, ALLOWED_MERGE_KEYMAPS, 'merge.keymaps')

for _, key in ipairs(MERGE_KEYMAP_ORDER) do
local value = keymaps[key]
validate_string(value, 'merge.keymaps.' .. key)
if seen[value] then
fail('merge.keymaps.' .. key .. ' conflicts with merge.keymaps.' .. seen[value])
end
seen[value] = key
end

for name, lhs in pairs(keymaps) do
for key, value in pairs(options.keymaps or {}) do
if value == lhs then
fail('merge.keymaps.' .. name .. ' conflicts with keymaps.' .. key)
end
end

for key, value in pairs(options.pane_navigation or {}) do
if value == lhs then
fail('merge.keymaps.' .. name .. ' conflicts with pane_navigation.' .. key)
end
end

for key, value in pairs(options.hunk_navigation or {}) do
if value == lhs then
fail('merge.keymaps.' .. name .. ' conflicts with hunk_navigation.' .. key)
end
end
end
end

local function validate_keymaps(options)
local keymaps = options.keymaps
local seen = {}
Expand Down Expand Up @@ -527,6 +604,7 @@ local function validate_options(options)
validate_windows(options)
validate_filetree(options)
validate_log(options)
validate_merge(options)
validate_keymaps(options)
validate_pane_navigation(options)
validate_hunk_navigation(options)
Expand Down
37 changes: 37 additions & 0 deletions lua/glance/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,33 @@ function M.start()
end
end

local function hex_to_rgb(hex)
if type(hex) ~= 'string' then
return nil
end

local value = hex:gsub('#', '')
if #value ~= 6 then
return nil
end

return tonumber(value:sub(1, 2), 16), tonumber(value:sub(3, 4), 16), tonumber(value:sub(5, 6), 16)
end

local function blend_hex(base_hex, tint_hex, alpha)
local base_r, base_g, base_b = hex_to_rgb(base_hex)
local tint_r, tint_g, tint_b = hex_to_rgb(tint_hex)
if not base_r or not tint_r then
return base_hex
end

local function mix(base, tint)
return math.floor((base * (1 - alpha)) + (tint * alpha) + 0.5)
end

return string.format('#%02X%02X%02X', mix(base_r, tint_r), mix(base_g, tint_g), mix(base_b, tint_b))
end

function M.setup_highlights()
local palette = require('glance.config').options.theme.palette
local bg = palette.bg
Expand All @@ -128,6 +155,9 @@ function M.setup_highlights()
local param = palette.accent
local selection = palette.selection
local line_hl = palette.line_highlight
local merge_unresolved_bg = blend_hex(bg, palette.logo, 0.16)
local merge_handled_bg = blend_hex(bg, palette.added, 0.16)
local merge_manual_bg = blend_hex(bg, type_color, 0.14)

-- Editor
vim.api.nvim_set_hl(0, 'Normal', { bg = bg, fg = fg })
Expand Down Expand Up @@ -227,6 +257,13 @@ function M.setup_highlights()
vim.api.nvim_set_hl(0, 'GlanceAccentText', { fg = param, bold = true })
vim.api.nvim_set_hl(0, 'GlanceLegendText', { fg = comment, bg = bg })
vim.api.nvim_set_hl(0, 'GlanceLegendHint', { fg = palette.accent, bg = bg, bold = true })
vim.api.nvim_set_hl(0, 'GlanceConflictMarkerUnresolved', { fg = palette.logo, bg = bg, bold = true })
vim.api.nvim_set_hl(0, 'GlanceConflictMarkerHandled', { fg = palette.added, bg = bg, bold = true })
vim.api.nvim_set_hl(0, 'GlanceConflictMarkerManual', { fg = type_color, bg = bg, bold = true })
vim.api.nvim_set_hl(0, 'GlanceConflictStateUnresolved', { bg = merge_unresolved_bg })
vim.api.nvim_set_hl(0, 'GlanceConflictStateHandled', { bg = merge_handled_bg })
vim.api.nvim_set_hl(0, 'GlanceConflictStateManual', { bg = merge_manual_bg })
vim.api.nvim_set_hl(0, 'GlanceConflictActiveNumber', { fg = palette.split_hover, bg = bg, bold = true })

-- Minimap highlights
require('glance.minimap').setup_highlights()
Expand Down
67 changes: 67 additions & 0 deletions lua/glance/merge/actions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
local model = require('glance.merge.model')

local M = {}

local DEFINITIONS = {
{ id = 'accept_ours', short = 'ours' },
{ id = 'accept_theirs', short = 'theirs' },
{ id = 'accept_both_ours_then_theirs', short = 'both o/t' },
{ id = 'accept_both_theirs_then_ours', short = 'both t/o' },
{ id = 'ignore_ours', short = 'skip ours' },
{ id = 'ignore_theirs', short = 'skip theirs' },
{ id = 'reset_conflict', short = 'reset' },
{ id = 'mark_resolved', short = 'resolve' },
}

local function display_key(lhs)
if type(lhs) ~= 'string' then
return ''
end

return lhs:gsub('<Leader>', '\\'):gsub('<leader>', '\\')
end

function M.available(conflict)
local actions = {}
if type(conflict) ~= 'table' then
return actions
end

for _, definition in ipairs(DEFINITIONS) do
local id = definition.id
if id == 'mark_resolved' then
if conflict.state == 'manual_unresolved' then
actions[#actions + 1] = definition
end
elseif id == 'ignore_ours' or id == 'ignore_theirs' then
if conflict.state ~= 'manual_unresolved' and conflict.state ~= 'manual_resolved' then
actions[#actions + 1] = definition
end
else
actions[#actions + 1] = definition
end
end

return actions
end

function M.hint_text(conflict, keymaps)
local parts = {}
for _, definition in ipairs(M.available(conflict)) do
local lhs = keymaps and keymaps[definition.id] or nil
local key = display_key(lhs)
if key ~= '' then
parts[#parts + 1] = string.format('%s %s', key, definition.short)
else
parts[#parts + 1] = definition.short
end
end

return table.concat(parts, ' | ')
end

function M.apply(merge_model, index, action)
return model.apply_action(merge_model, index, action)
end

return M
Loading
Loading