From ec44937e1b4358910f62da65afd0a741ba26c28e Mon Sep 17 00:00:00 2001 From: Brad Robinson <31802085+bnrobinson93@users.noreply.github.com> Date: Thu, 12 Mar 2026 08:22:02 -0500 Subject: [PATCH] feat(editor): Add auto_insert option for commit/describe buffers (#88) --- lua/jj/init.lua | 9 ++++++++- lua/jj/ui/editor.lua | 25 ++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lua/jj/init.lua b/lua/jj/init.lua index d2efd69..bbb6f01 100644 --- a/lua/jj/init.lua +++ b/lua/jj/init.lua @@ -11,6 +11,7 @@ local browse = require("jj.browse") --- @field cmd? jj.cmd.opts Options for command module --- @field picker? jj.picker.config Options for picker module --- @field terminal? jj.ui.terminal.opts Options for the terminal +--- @field editor? jj.ui.editor.opts Options for the editor module --- @field highlights? jj.highlights Options for the highlights --- @field diff? jj.diff.config Options for the diff module @@ -23,6 +24,9 @@ M.config = { picker = { snacks = {}, }, + editor = { + auto_insert = false, + }, highlights = { editor = { renamed = { fg = "#d29922", ctermfg = "Yellow" }, @@ -45,7 +49,10 @@ function M.setup(opts) -- Setup for sub-modules picker.setup(opts and opts.picker or {}) - editor.setup({ highlights = M.config.highlights.editor }) + editor.setup({ + highlights = M.config.highlights.editor, + auto_insert = M.config.editor.auto_insert, + }) cmd.setup(opts and opts.cmd or {}) terminal.setup(opts and opts.terminal or {}) diff.setup(M.config.diff) diff --git a/lua/jj/ui/editor.lua b/lua/jj/ui/editor.lua index 475d797..8370689 100644 --- a/lua/jj/ui/editor.lua +++ b/lua/jj/ui/editor.lua @@ -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,