From 6a395af98fab9ae11fe788d8ceaf24c3aac50b87 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Tue, 3 Mar 2026 00:09:57 -0500 Subject: [PATCH] feat(config): add templates.cursor_marker for post-template cursor placement Problem: after apply_template writes a file's content to the buffer, cursor positioning was left entirely to the user's setup_code hook, forcing everyone to reimplement the same placeholder-stripping logic. Solution: add an optional templates.cursor_marker config key. When set, apply_template scans the written lines for the marker, strips it, and positions the cursor there via bufwinid so it works in both the provisional and existing-file paths. --- lua/cp/config.lua | 11 +++++++++++ lua/cp/setup.lua | 15 +++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lua/cp/config.lua b/lua/cp/config.lua index fcd1e96..280ecd5 100644 --- a/lua/cp/config.lua +++ b/lua/cp/config.lua @@ -9,6 +9,9 @@ ---@field commands CpLangCommands ---@field template? string +---@class CpTemplatesConfig +---@field cursor_marker? string + ---@class CpPlatformOverrides ---@field extension? string ---@field commands? CpLangCommands @@ -86,6 +89,7 @@ ---@class cp.Config ---@field languages table ---@field platforms table +---@field templates? CpTemplatesConfig ---@field hooks Hooks ---@field debug boolean ---@field open_url boolean @@ -320,6 +324,13 @@ function M.setup(user_config) error('[cp.nvim] At least one platform must be configured') end + if cfg.templates ~= nil then + vim.validate({ templates = { cfg.templates, 'table' } }) + if cfg.templates.cursor_marker ~= nil then + vim.validate({ cursor_marker = { cfg.templates.cursor_marker, 'string' } }) + end + end + vim.validate({ hooks = { cfg.hooks, { 'table' } }, ui = { cfg.ui, { 'table' } }, diff --git a/lua/cp/setup.lua b/lua/cp/setup.lua index 44c993d..43a50c0 100644 --- a/lua/cp/setup.lua +++ b/lua/cp/setup.lua @@ -21,6 +21,21 @@ local function apply_template(bufnr, lang_id, platform) end local lines = vim.fn.readfile(path) vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) + local marker = config.templates and config.templates.cursor_marker + if marker then + for lnum, line in ipairs(lines) do + local col = line:find(marker, 1, true) + if col then + local new_line = line:sub(1, col - 1) .. line:sub(col + #marker) + vim.api.nvim_buf_set_lines(bufnr, lnum - 1, lnum, false, { new_line }) + local winid = vim.fn.bufwinid(bufnr) + if winid ~= -1 then + vim.api.nvim_win_set_cursor(winid, { lnum, col - 1 }) + end + break + end + end + end end ---Get the language of the current file from cache