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.
This commit is contained in:
parent
d3324aafa3
commit
6a395af98f
2 changed files with 26 additions and 0 deletions
|
|
@ -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<string, CpLanguage>
|
||||
---@field platforms table<string, CpPlatform>
|
||||
---@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' } },
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue