diff --git a/lua/fittencode/engines/actions/content.lua b/lua/fittencode/engines/actions/content.lua index 225611d3..d391c7c7 100644 --- a/lua/fittencode/engines/actions/content.lua +++ b/lua/fittencode/engines/actions/content.lua @@ -12,7 +12,6 @@ local Log = require('fittencode.log') ---@field on_suggestions function ---@field on_status function ---@field on_end function ----@field get_current_suggestions function local M = {} local ViewBlock = { @@ -223,38 +222,54 @@ function M:on_status(msg) }) end -function M:get_current_suggestions() - return self.conversations[self.current_eval].suggestions -end - function M:get_conversation_index(row, col) - for i, cursor in ipairs(self.cursors) do - if cursor and #cursor == 5 then - if row >= cursor[ViewBlock.IN][1][1] and row <= cursor[ViewBlock.QED][2][1] then - return i + for k, v in pairs(self.cursors) do + if v and #v == 5 then + if row >= v[ViewBlock.IN][1][1] and row <= v[ViewBlock.QED][2][1] then + return k end end end end -function M:get_conversations_range(direction, row, col) - local i = self:get_conversation_index(row, col) - if not i then - return - end - if direction == 'forward' then - i = i + 1 +function M:get_conversations_range_by_index(direction, base) + local next = nil + if direction == 'current' then + next = base + elseif direction == 'forward' then + for j = base + 1, self.current_eval do + if self.cursors[j] then + next = j + break + end + end elseif direction == 'backward' then - i = i - 1 + for j = base - 1, 1, -1 do + if self.cursors[j] then + next = j + break + end + end + end + if not next then + return end - if self.cursors[i] then + if self.cursors[next] then return { - { self.cursors[i][ViewBlock.IN][1][1], 0 }, - { self.cursors[i][ViewBlock.QED][2][1], 0 } + { self.cursors[next][ViewBlock.IN][1][1], 0 }, + { self.cursors[next][ViewBlock.QED][2][1], 0 } } end end +function M:get_conversations_range(direction, row, col) + local base = self:get_conversation_index(row, col) + if not base then + return + end + return self:get_conversations_range_by_index(direction, base) +end + function M:get_conversations(range, row, col) if range == 'all' then return self.conversations @@ -267,4 +282,51 @@ function M:get_conversations(range, row, col) end end +function M:delete_conversations(range, row, col) + if range == 'all' then + self.conversations = {} + self.has_suggestions = {} + self.cursors = {} + self.last_lines = nil + elseif range == 'current' then + local base = self:get_conversation_index(row, col) + if not base then + return + end + local current = self:get_conversations_range_by_index('current', base) + if not current then + return + end + local forward = self:get_conversations_range_by_index('forward', base) + local backward = self:get_conversations_range_by_index('backward', base) + if not forward then + if backward then + current[1][1] = backward[2][1] + 1 + current[1][2] = 0 + end + else + current[2][1] = forward[1][1] - 1 + current[2][2] = 0 + local yoffset = current[2][1] - current[1][1] + 1 + for j = base + 1, self.current_eval do + if self.cursors[j] then + for b = ViewBlock.IN, ViewBlock.QED do + self.cursors[j][b][1][1] = self.cursors[j][b][1][1] - yoffset + self.cursors[j][b][2][1] = self.cursors[j][b][2][1] - yoffset + end + end + end + end + self.conversations[base] = nil + self.has_suggestions[base] = nil + self.cursors[base] = nil + self.last_lines = nil + return current + end +end + +function M:set_last_lines(lines) + self.last_lines = lines +end + return M diff --git a/lua/fittencode/engines/actions/init.lua b/lua/fittencode/engines/actions/init.lua index dbfcd395..d4941117 100644 --- a/lua/fittencode/engines/actions/init.lua +++ b/lua/fittencode/engines/actions/init.lua @@ -742,7 +742,13 @@ local CHAT_MODEL = { end, get_conversations = function(range, row, col) return content:get_conversations(range, row, col) - end + end, + delete_conversations = function(range, row, col) + return content:delete_conversations(range, row, col) + end, + set_last_lines = function(lines) + return content:set_last_lines(lines) + end, } function ActionsEngine.setup() diff --git a/lua/fittencode/preprocessing/markdown_prettify.lua b/lua/fittencode/preprocessing/markdown_prettify.lua index 97de615c..9690bb96 100644 --- a/lua/fittencode/preprocessing/markdown_prettify.lua +++ b/lua/fittencode/preprocessing/markdown_prettify.lua @@ -1,3 +1,4 @@ +local Log = require('fittencode.log') local Merge = require('fittencode.preprocessing.merge') ---@param lines? string[] diff --git a/lua/fittencode/views/chat.lua b/lua/fittencode/views/chat.lua index 0a9d6770..3ee1f6e5 100644 --- a/lua/fittencode/views/chat.lua +++ b/lua/fittencode/views/chat.lua @@ -277,22 +277,25 @@ function M:copy_all_conversations() end function M:delete_conversation() - -- local range = _call_model(self, 'delete_conversations', 'current', Base.get_cursor(self.window)) - -- if not range then - -- return - -- end - -- local start_row = range[1][1] - -- local end_row = range[2][1] - -- _modify_buffer(self.buffer, function() - -- api.nvim_buf_set_lines(self.buffer, start_row, end_row + 1, false, {}) - -- end) + local range = _call_model(self, 'delete_conversations', 'current', Base.get_cursor(self.window)) + if not range then + return + end + local start_row = range[1][1] + local end_row = range[2][1] + _modify_buffer(self.buffer, function() + api.nvim_buf_set_lines(self.buffer, start_row, end_row + 1, false, {}) + end) + local last = api.nvim_buf_get_lines(self.buffer, -2, -1, false) + table.insert(last, 1, '') + _call_model(self, 'set_last_lines', last) end function M:delete_all_conversations() - -- _call_model(self, 'delete_conversations', 'all') - -- _modify_buffer(self.buffer, function() - -- api.nvim_buf_set_lines(self.buffer, 0, -1, false, {}) - -- end) + _call_model(self, 'delete_conversations', 'all') + _modify_buffer(self.buffer, function() + api.nvim_buf_set_lines(self.buffer, 0, -1, false, {}) + end) end return M