Skip to content

Commit

Permalink
Merge pull request #81 from luozhiya/del_c
Browse files Browse the repository at this point in the history
Delete conversations
  • Loading branch information
luozhiya authored Jun 12, 2024
2 parents 43eacab + 1e592ef commit 4b682b6
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 34 deletions.
102 changes: 82 additions & 20 deletions lua/fittencode/engines/actions/content.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
Expand All @@ -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
8 changes: 7 additions & 1 deletion lua/fittencode/engines/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions lua/fittencode/preprocessing/markdown_prettify.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local Log = require('fittencode.log')
local Merge = require('fittencode.preprocessing.merge')

---@param lines? string[]
Expand Down
29 changes: 16 additions & 13 deletions lua/fittencode/views/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4b682b6

Please sign in to comment.