Refactor: moving injects and start the parser

- moving all the inject functions inside its proper folder
- starting the parser refactoring
This commit is contained in:
2026-03-23 23:48:25 -04:00
parent 5fa7d7d347
commit d93fed165f
13 changed files with 394 additions and 366 deletions

View File

@@ -4,98 +4,8 @@ local M = {}
local utils = require("codetyper.support.utils")
local logger = require("codetyper.support.logger")
-- Get current codetyper configuration at call time
local function get_config()
local ok, codetyper = pcall(require, "codetyper")
if ok and codetyper.get_config then
return codetyper.get_config() or {}
end
-- Fall back to defaults if codetyper isn't available
local defaults = require("codetyper.config.defaults")
return defaults.get_defaults()
end
--- Find all prompts in buffer content
---@param content string Buffer content
---@param open_tag string Opening tag
---@param close_tag string Closing tag
---@return CoderPrompt[] List of found prompts
function M.find_prompts(content, open_tag, close_tag)
logger.func_entry("parser", "find_prompts", {
content_length = #content,
open_tag = open_tag,
close_tag = close_tag,
})
local prompts = {}
local escaped_open = utils.escape_pattern(open_tag)
local escaped_close = utils.escape_pattern(close_tag)
local lines = vim.split(content, "\n", { plain = true })
local in_prompt = false
local current_prompt = nil
local prompt_content = {}
logger.debug("parser", "find_prompts: parsing " .. #lines .. " lines")
for line_num, line in ipairs(lines) do
if not in_prompt then
-- Look for opening tag
local start_col = line:find(escaped_open)
if start_col then
logger.debug("parser", "find_prompts: found opening tag at line " .. line_num .. ", col " .. start_col)
in_prompt = true
current_prompt = {
start_line = line_num,
start_col = start_col,
content = "",
}
-- Get content after opening tag on same line
local after_tag = line:sub(start_col + #open_tag)
local end_col = after_tag:find(escaped_close)
if end_col then
-- Single line prompt
current_prompt.content = after_tag:sub(1, end_col - 1)
current_prompt.end_line = line_num
current_prompt.end_col = start_col + #open_tag + end_col + #close_tag - 2
table.insert(prompts, current_prompt)
logger.debug("parser", "find_prompts: single-line prompt completed at line " .. line_num)
in_prompt = false
current_prompt = nil
else
table.insert(prompt_content, after_tag)
end
end
else
-- Look for closing tag
local end_col = line:find(escaped_close)
if end_col then
-- Found closing tag
local before_tag = line:sub(1, end_col - 1)
table.insert(prompt_content, before_tag)
current_prompt.content = table.concat(prompt_content, "\n")
current_prompt.end_line = line_num
current_prompt.end_col = end_col + #close_tag - 1
table.insert(prompts, current_prompt)
logger.debug(
"parser",
"find_prompts: multi-line prompt completed at line " .. line_num .. ", total lines: " .. #prompt_content
)
in_prompt = false
current_prompt = nil
prompt_content = {}
else
table.insert(prompt_content, line)
end
end
end
logger.debug("parser", "find_prompts: found " .. #prompts .. " prompts total")
logger.func_exit("parser", "find_prompts", "found " .. #prompts .. " prompts")
return prompts
end
local get_config = require("codetyper.utils.get_config").get_config
local find_prompts = require("codetyper.parser.find_prompts")
--- Find prompts in a buffer
---@param bufnr number Buffer number
@@ -112,7 +22,7 @@ function M.find_prompts_in_buffer(bufnr)
)
local cfg = get_config()
local result = M.find_prompts(content, cfg.patterns.open_tag, cfg.patterns.close_tag)
local result = find_prompts(content, cfg.patterns.open_tag, cfg.patterns.close_tag)
logger.func_exit("parser", "find_prompts_in_buffer", "found " .. #result .. " prompts")
return result