feat(editor): Add auto_insert option for commit/describe buffers (#88)

This commit is contained in:
Brad Robinson
2026-03-12 14:22:02 +01:00
committed by GitHub
parent bbba4051c8
commit ec44937e1b
2 changed files with 32 additions and 2 deletions
+24 -1
View File
@@ -9,11 +9,15 @@ local buffer = require("jj.core.buffer")
---@field deleted? table Highlight settings for deleted lines
---@field renamed? table Highlight settings for renamed lines
--- @class jj.ui.editor.opts
---@field auto_insert? boolean Smart insert: enter insert mode when description is empty, stay in normal mode when one exists
M.highlights = {
-- Only init this one by default since it's not handled natively by neovim
renamed = { fg = "#d29922", ctermfg = "Yellow" },
}
M.highlights_initialized = false
M.auto_insert = true
-- Initialize highlight groups once
local function init_highlights()
@@ -43,7 +47,7 @@ local function init_highlights()
end
--- Setup function to configure highlights and other options
---@param opts? { highlights: jj.ui.editor.highlights } Configuration options
---@param opts? { highlights: jj.ui.editor.highlights, auto_insert?: boolean } Configuration options
function M.setup(opts)
opts = opts or {}
@@ -52,6 +56,11 @@ function M.setup(opts)
M.highlights = vim.tbl_deep_extend("force", M.highlights, opts.highlights)
end
-- Store auto_insert option
if opts.auto_insert ~= nil then
M.auto_insert = opts.auto_insert
end
-- Reset highlights flag to force re-initialization with new highlights
if M.highlights_initialized then
M.highlights_initialized = false
@@ -125,6 +134,20 @@ function M.open_editor(initial_text, on_write, on_unload, keymaps)
-- Apply highlights initially
apply_highlights(buf)
-- Smart insert mode: insert when description is empty, normal mode otherwise
if M.auto_insert then
vim.schedule(function()
if not vim.api.nvim_buf_is_valid(buf) then
return
end
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
local desc = require("jj.utils").extract_description_from_describe(lines)
if not desc or desc == "" then
vim.cmd("startinsert")
end
end)
end
-- Reapply highlights when text changes
vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
buffer = buf,