feat: update git formatting

This commit is contained in:
Barrett Ruth 2026-01-27 17:18:52 -05:00
parent 3348ac3e51
commit d89a40b21f
4 changed files with 84 additions and 39 deletions

View file

@ -18,7 +18,7 @@
---@field overrides? table<string, CpPlatformOverrides>
---@class PanelConfig
---@field diff_mode "none"|"vim"|"git"
---@field diff_modes string[]
---@field max_output_lines integer
---@class DiffGitConfig
@ -173,7 +173,7 @@ M.defaults = {
add_test_key = 'ga',
save_and_exit_key = 'q',
},
panel = { diff_mode = 'none', max_output_lines = 50 },
panel = { diff_modes = { 'side-by-side', 'git', 'vim' }, max_output_lines = 50 },
diff = {
git = {
args = { 'diff', '--no-index', '--word-diff=plain', '--word-diff-regex=.', '--no-prefix' },
@ -313,15 +313,26 @@ function M.setup(user_config)
setup_io_output = { cfg.hooks.setup_io_output, { 'function', 'nil' }, true },
})
local layouts = require('cp.ui.layouts')
local valid_modes_str = table.concat(vim.tbl_keys(layouts.DIFF_MODES), ',')
if type(cfg.ui.panel.diff_modes) == 'table' then
local invalid = {}
for _, mode in ipairs(cfg.ui.panel.diff_modes) do
if not layouts.DIFF_MODES[mode] then
table.insert(invalid, mode)
end
end
if #invalid > 0 then
error(
('invalid diff modes [%s] - must be one of: {%s}'):format(
table.concat(invalid, ','),
valid_modes_str
)
)
end
end
vim.validate({
ansi = { cfg.ui.ansi, 'boolean' },
diff_mode = {
cfg.ui.panel.diff_mode,
function(v)
return vim.tbl_contains({ 'none', 'vim', 'git' }, v)
end,
"diff_mode must be 'none', 'vim', or 'git'",
},
max_output_lines = {
cfg.ui.panel.max_output_lines,
function(v)
@ -383,6 +394,13 @@ function M.setup(user_config)
end,
'nil or non-empty string',
},
picker = {
cfg.ui.picker,
function(v)
return v == nil or v == 'telescope' or v == 'fzf-lua'
end,
"nil, 'telescope', or 'fzf-lua'",
},
})
for id, lang in pairs(cfg.languages) do
@ -443,7 +461,18 @@ function M.get_language_for_platform(platform_id, language_id)
}
end
local effective = cfg.runtime.effective[platform_id][language_id]
local platform_effective = cfg.runtime.effective[platform_id]
if not platform_effective then
return {
valid = false,
error = string.format(
'No runtime config for platform %s (plugin not initialized)',
platform_id
),
}
end
local effective = platform_effective[language_id]
if not effective then
return {
valid = false,

View file

@ -3,13 +3,13 @@ local M = {}
local helpers = require('cp.helpers')
local utils = require('cp.utils')
local MODE_LABELS = {
none = 'none',
M.DIFF_MODES = {
['side-by-side'] = 'side-by-side',
vim = 'vim',
git = 'git',
}
local function create_none_diff_layout(parent_win, expected_content, actual_content)
local function create_side_by_side_layout(parent_win, expected_content, actual_content)
local expected_buf = utils.create_buffer_with_options()
local actual_buf = utils.create_buffer_with_options()
helpers.clearcol(expected_buf)
@ -27,9 +27,13 @@ local function create_none_diff_layout(parent_win, expected_content, actual_cont
vim.api.nvim_set_option_value('filetype', 'cp', { buf = expected_buf })
vim.api.nvim_set_option_value('filetype', 'cp', { buf = actual_buf })
local label = MODE_LABELS.none
vim.api.nvim_set_option_value('winbar', ('expected [%s]'):format(label), { win = expected_win })
vim.api.nvim_set_option_value('winbar', ('actual [%s]'):format(label), { win = actual_win })
local label = M.DIFF_MODES['side-by-side']
vim.api.nvim_set_option_value(
'winbar',
('expected (diff: %s)'):format(label),
{ win = expected_win }
)
vim.api.nvim_set_option_value('winbar', ('actual (diff: %s)'):format(label), { win = actual_win })
local expected_lines = vim.split(expected_content, '\n', { plain = true, trimempty = true })
local actual_lines = vim.split(actual_content, '\n', { plain = true })
@ -40,7 +44,7 @@ local function create_none_diff_layout(parent_win, expected_content, actual_cont
return {
buffers = { expected_buf, actual_buf },
windows = { expected_win, actual_win },
mode = 'none',
mode = 'side-by-side',
cleanup = function()
pcall(vim.api.nvim_win_close, expected_win, true)
pcall(vim.api.nvim_win_close, actual_win, true)
@ -68,9 +72,13 @@ local function create_vim_diff_layout(parent_win, expected_content, actual_conte
vim.api.nvim_set_option_value('filetype', 'cp', { buf = expected_buf })
vim.api.nvim_set_option_value('filetype', 'cp', { buf = actual_buf })
local label = MODE_LABELS.vim
vim.api.nvim_set_option_value('winbar', ('expected [%s]'):format(label), { win = expected_win })
vim.api.nvim_set_option_value('winbar', ('actual [%s]'):format(label), { win = actual_win })
local label = M.DIFF_MODES.vim
vim.api.nvim_set_option_value(
'winbar',
('expected (diff: %s)'):format(label),
{ win = expected_win }
)
vim.api.nvim_set_option_value('winbar', ('actual (diff: %s)'):format(label), { win = actual_win })
local expected_lines = vim.split(expected_content, '\n', { plain = true, trimempty = true })
local actual_lines = vim.split(actual_content, '\n', { plain = true })
@ -113,8 +121,8 @@ local function create_git_diff_layout(parent_win, expected_content, actual_conte
vim.api.nvim_win_set_buf(diff_win, diff_buf)
vim.api.nvim_set_option_value('filetype', 'cp', { buf = diff_buf })
local label = MODE_LABELS.git
vim.api.nvim_set_option_value('winbar', ('diff [%s]'):format(label), { win = diff_win })
local label = M.DIFF_MODES.git
vim.api.nvim_set_option_value('winbar', ('diff: %s'):format(label), { win = diff_win })
local diff_backend = require('cp.ui.diff')
local backend = diff_backend.get_best_backend('git')
@ -166,12 +174,14 @@ end
function M.create_diff_layout(mode, parent_win, expected_content, actual_content)
if mode == 'single' then
return create_single_layout(parent_win, actual_content)
elseif mode == 'none' then
return create_none_diff_layout(parent_win, expected_content, actual_content)
elseif mode == 'side-by-side' then
return create_side_by_side_layout(parent_win, expected_content, actual_content)
elseif mode == 'git' then
return create_git_diff_layout(parent_win, expected_content, actual_content)
else
elseif mode == 'vim' then
return create_vim_diff_layout(parent_win, expected_content, actual_content)
else
return create_side_by_side_layout(parent_win, expected_content, actual_content)
end
end
@ -204,12 +214,13 @@ function M.update_diff_panes(
actual_content = actual_content
end
local desired_mode = is_compilation_failure and 'single' or config.ui.panel.diff_mode
local default_mode = config.ui.panel.diff_modes[1]
local desired_mode = is_compilation_failure and 'single' or (current_mode or default_mode)
local highlight = require('cp.ui.highlight')
local diff_namespace = highlight.create_namespace()
local ansi_namespace = vim.api.nvim_create_namespace('cp_ansi_highlights')
if current_diff_layout and current_mode ~= desired_mode then
if current_diff_layout and current_diff_layout.mode ~= desired_mode then
local saved_pos = vim.api.nvim_win_get_cursor(0)
current_diff_layout.cleanup()
current_diff_layout = nil
@ -264,7 +275,7 @@ function M.update_diff_panes(
ansi_namespace
)
end
elseif desired_mode == 'none' then
elseif desired_mode == 'side-by-side' then
local expected_lines = vim.split(expected_content, '\n', { plain = true, trimempty = true })
local actual_lines = vim.split(actual_content, '\n', { plain = true })
utils.update_buffer_content(current_diff_layout.buffers[1], expected_lines, {})

View file

@ -900,15 +900,15 @@ function M.toggle_panel(panel_opts)
M.toggle_panel()
end, { buffer = buf, silent = true })
vim.keymap.set('n', 't', function()
local modes = { 'none', 'git', 'vim' }
local modes = config.ui.panel.diff_modes
local current_idx = 1
for i, mode in ipairs(modes) do
if config.ui.panel.diff_mode == mode then
if current_mode == mode then
current_idx = i
break
end
end
config.ui.panel.diff_mode = modes[(current_idx % #modes) + 1]
current_mode = modes[(current_idx % #modes) + 1]
refresh_panel()
end, { buffer = buf, silent = true })
vim.keymap.set('n', '<c-n>', function()