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