Skip to content

Commit

Permalink
Improve markdown block format
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed May 18, 2024
1 parent 85a39e3 commit ac4e178
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lua/fittencode/engines/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ local function on_error(err)
end
Log.debug('Action elapsed time: {}', elapsed_time)
Log.debug('Action depth: {}', depth)
chat:commit('> Q.E.D.' .. '(' .. elapsed_time .. ' ms)' .. '\n', true)
chat:commit('> Q.E.D.' .. '(' .. elapsed_time .. ' ms)' .. '\n', true, true)
current_eval = current_eval + 1
end

Expand Down
21 changes: 20 additions & 1 deletion lua/fittencode/views/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,20 @@ function M:close()
-- self.buffer = nil
end

local stack = {}

local function push_stack(x)
if #stack == 0 or stack[#stack] ~= x then
table.insert(stack, x)
else
table.remove(stack)
end
end

---@param text? string|string[]
---@param linebreak? boolean
function M:commit(text, linebreak)
---@param force? boolean
function M:commit(text, linebreak, force)
local lines = nil
if type(text) == 'string' then
lines = vim.split(text, '\n')
Expand All @@ -79,6 +90,14 @@ function M:commit(text, linebreak)
else
return
end
vim.tbl_map(function(x)
if x == '```' then
push_stack(x)
end
end, lines)
if #stack > 0 and not force then
linebreak = false
end
if linebreak and #self.text > 0 and #lines > 0 then
if lines[1] ~= '' and not string.match(lines[1], '^```') and self.text[#self.text] ~= '' and not string.match(self.text[#self.text], '^```') then
table.insert(lines, 1, '')
Expand Down

0 comments on commit ac4e178

Please sign in to comment.