Skip to content

Commit

Permalink
Refactor Preprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Jun 9, 2024
1 parent 92401f2 commit 85d38c2
Show file tree
Hide file tree
Showing 20 changed files with 647 additions and 440 deletions.
10 changes: 6 additions & 4 deletions lua/fittencode/actions/identify_programming_language.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ local function _identify_current_buffer()
API.identify_programming_language({
headless = true,
content = content,
preprocess_format = {
trim_trailing_whitespace = true,
filter = {
exclude_markdown_code_blocks_marker = true,
}
},
on_success = function(suggestions)
if not suggestions or #suggestions == 0 then
return
Expand All @@ -55,10 +61,6 @@ local function _identify_current_buffer()
return
end
lang = lang:lower()
lang = lang:gsub('^%s*(.-)%s*$', '%1')
if #lang == 0 then
return
end
lang = lang:gsub('c%+%+', 'cpp')
lang = lang:match('^(%w+)')
api.nvim_set_option_value('filetype', lang, {
Expand Down
145 changes: 45 additions & 100 deletions lua/fittencode/engines/actions/content.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ local Log = require('fittencode.log')

---@class ActionsContent
---@field chat Chat
---@field buffer_content string[][]
---@field conversations Conversation[]
---@field has_suggestions boolean[]
---@field current_eval number
---@field cursors table[]
---@field last_lines string[]?
---@field on_start function
---@field on_suggestions function
---@field on_status function
Expand All @@ -26,95 +26,70 @@ local ViewBlock = {
function M:new(chat)
local obj = {
chat = chat,
buffer_content = {},
conversations = {},
current_eval = nil,
cursors = {},
has_suggestions = {},
last_lines = nil,
}
self.__index = self
return setmetatable(obj, self)
end

---@class ChatCommitFormat
---@field firstlinebreak? boolean
---@field firstlinecompress? boolean
---@field fenced_code? boolean
---@field start_space? boolean

---@class ChatCommitOptions
---@field lines? string|string[]
---@field format? ChatCommitFormat

local fenced_code_open = false

---@param opts? ChatCommitOptions|string
---@param content string[]
---@return string[]?
local function format_lines(opts, content)
if not opts then
return
end

if type(opts) == 'string' then
---@diagnostic disable-next-line: param-type-mismatch
opts = { lines = vim.split(opts, '\n') }
end
---@param line string
local function _end(line)
return line:match('```$')
end

---@type string[]
---@diagnostic disable-next-line: assign-type-mismatch
local lines = opts.lines or {}
local firstlinebreak = opts.format and opts.format.firstlinebreak
local fenced_code = opts.format and opts.format.fenced_code
local firstlinecompress = opts.format and opts.format.firstlinecompress
---@param line string
local function _start(line)
return line:match('^```')
end

if #lines == 0 then
return
local function format_lines(last_lines, lines, format)
if not format then
return lines
end

vim.tbl_map(function(x)
if x:match('^```') or x:match('```$') then
fenced_code_open = not fenced_code_open
end
end, lines)

local fenced_sloved = false
if fenced_code_open then
if fenced_code then
if lines[1] ~= '' then
local last = last_lines[#last_lines]
local first = lines[1]
if format.start_space then
if not _end(last) then
table.insert(lines, 1, '')
if not _start(first) and first ~= '' then
table.insert(lines, 1, '')
end
else
if first ~= '' then
table.insert(lines, 1, '')
end
table.insert(lines, 2, '```')
fenced_code_open = false
fenced_sloved = true
end
end

if not fenced_code_open and not fenced_sloved and firstlinebreak and
#content > 0 and #lines > 1 then
local last_lines = content[#content]
local last_line = last_lines[#last_lines]
if not string.match(lines[1], '^```') and not string.match(lines[2], '^```') and not string.match(last_line, '^```') then
table.insert(lines, 1, '')
end
end

if firstlinecompress and #lines > 1 then
if lines[1] == '' and string.match(lines[2], '^```') then
table.remove(lines, 1)
end
end

return lines
end

---@param opts? ChatCommitOptions|string
function M:commit(opts)
local lines = format_lines(opts, self.buffer_content)
if not lines then
if not opts then
return
end

table.insert(self.buffer_content, lines)
local lines = nil
local format = nil
if type(opts) == 'string' then
---@diagnostic disable-next-line: param-type-mismatch
lines = vim.split(opts, '\n')
elseif type(opts) == 'table' then
lines = opts.lines
format = opts.format
end
lines = format_lines(self.last_lines, lines, format)
self.last_lines = lines
return self.chat:commit(lines)
end

Expand All @@ -123,9 +98,7 @@ function M:on_start(opts)
return
end
self.current_eval = opts.current_eval
self.current_action = opts.current_action
self.conversations[self.current_eval] = Conversation:new(self.current_eval, opts.action)
self.conversations[self.current_eval].current_action = opts.current_action
self.conversations[self.current_eval].location = opts.location
self.conversations[self.current_eval].prompt = opts.prompt
self.conversations[self.current_eval].headless = opts.headless
Expand All @@ -136,7 +109,7 @@ function M:on_start(opts)
end

local source_info = ' (' .. opts.location[1] .. ' ' .. opts.location[2] .. ':' .. opts.location[3] .. ')'
local c_in = '# In`[' .. self.current_action .. ']`:= ' .. opts.action .. source_info
local c_in = '# In`[' .. self.current_eval .. ']`:= ' .. opts.action .. source_info
if not self.chat:is_empty() then
self:commit('\n\n')
end
Expand All @@ -163,18 +136,12 @@ function M:on_start(opts)
'',
}
})
local c_out = '# Out`[' .. self.current_action .. ']`='
local c_out = '# Out`[' .. self.current_eval .. ']`='
cursor = self:commit({
lines = {
c_out,
}
})
self:commit({
lines = {
'',
'',
}
})
self.cursors[self.current_eval][ViewBlock.OUT] = cursor
end

Expand All @@ -185,26 +152,20 @@ function M:on_end(opts)

self.conversations[self.current_eval].elapsed_time = opts.elapsed_time
self.conversations[self.current_eval].depth = opts.depth
self.conversations[self.current_eval].suggestions = opts.suggestions

if self.conversations[self.current_eval].headless then
return
end

self:commit({
lines = {
'',
'',
},
format = {
firstlinebreak = true,
fenced_code = true,
}
})
local qed = '> Q.E.D.' .. '(' .. opts.elapsed_time .. ' ms)'
local cursor = self:commit({
lines = {
qed,
},
format = {
start_space = true,
}
})
self.cursors[self.current_eval][ViewBlock.QED] = cursor
end
Expand All @@ -220,7 +181,6 @@ function M:on_suggestions(suggestions)
if not suggestions then
return
end
self.conversations[self.current_eval].suggestions[#self.conversations[self.current_eval].suggestions + 1] = suggestions

if self.conversations[self.current_eval].headless then
return
Expand All @@ -231,7 +191,7 @@ function M:on_suggestions(suggestions)
local cursor = self:commit({
lines = suggestions,
format = {
firstlinecompress = true,
start_space = true,
}
})
self.cursors[self.current_eval][ViewBlock.OUT_CONTENT] = cursor
Expand All @@ -258,28 +218,13 @@ function M:on_status(msg)
'```',
},
format = {
firstlinebreak = true,
fenced_code = true,
start_space = true,
}
})
end

local function merge_lines(suggestions)
local merged = {}
for _, lines in ipairs(suggestions) do
for i, line in ipairs(lines) do
if i == 1 and #merged ~= 0 then
merged[#merged] = merged[#merged] .. line
else
merged[#merged + 1] = line
end
end
end
return merged
end

function M:get_current_suggestions()
return merge_lines(self.conversations[self.current_eval].suggestions)
return self.conversations[self.current_eval].suggestions
end

function M:get_conversation_index(row, col)
Expand Down
3 changes: 1 addition & 2 deletions lua/fittencode/engines/actions/conversation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
---@field action string
---@field references integer[]
---@field prompt string[]
---@field suggestions string[]
---@field suggestions Suggestions
---@field elapsed_time integer
---@field depth integer
---@field location table -- [filename, row_start, row_end]
---@field headless boolean
---@field current_action integer
local M = {}

function M:new(id, actions, references)
Expand Down
Loading

0 comments on commit 85d38c2

Please sign in to comment.