Skip to content

Commit

Permalink
Merge pull request #56 from luozhiya/action_chat
Browse files Browse the repository at this point in the history
Start chat without setting the language
  • Loading branch information
luozhiya authored May 15, 2024
2 parents aa3796c + 928cae4 commit 3d52389
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
5 changes: 4 additions & 1 deletion lua/fittencode/engines/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,12 @@ local function on_error(err)
end
end

---@param line string
---@param line? string
---@return number?
local function find_nospace(line)
if not line then
return
end
for i = 1, #line do
if line:sub(i, i) ~= ' ' then
return i
Expand Down
21 changes: 15 additions & 6 deletions lua/fittencode/prompt_providers/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@ function M:execute(ctx)
-- content = table.concat(api.nvim_buf_get_text(ctx.buffer, 0, 0, -1, -1, {}), '\n')
content = table.concat(api.nvim_buf_get_text(ctx.buffer, ctx.range[1], 0, ctx.range[2], -1, {}), '\n')
end
local name = ctx.prompt_ty:sub(#NAME + 2)
Log.debug('Action Name: {}', name)
local filetype = ctx.filetype or ''
-- Log.debug('Action Filetype: {}', filetype)
content = '```' .. filetype .. '\n' .. content .. '\n```'
-- Log.debug('Action Content: {}', content)
Log.debug('Action Filetype: {}', filetype)
local language = ctx.action_opts.language or filetype
-- Log.debug('Action Language: {}', language)
Log.debug('Action Language: {}', language)
local content_prefix = '```'
local content_suffix = '```'
if name ~= 'StartChat' then
content_prefix = '```' .. language
end
content = content_prefix .. '\n' .. content .. '\n' .. content_suffix
local map_action_prompt = {
StartChat = 'Answers the question above',
DocumentCode = 'Document the code above',
Expand All @@ -84,8 +90,11 @@ function M:execute(ctx)
ImproveCode = 'Improve the code above',
RefactorCode = 'Refactor the code above',
}
local key = map_action_prompt[ctx.prompt_ty:sub(#NAME + 2)]
local lang_suffix = #language > 0 and ' in ' .. language or ''
local key = map_action_prompt[name]
local lang_suffix = ''
if name ~= 'StartChat' then
lang_suffix = #language > 0 and ' in ' .. language or ''
end
local prompt = ctx.prompt or ((type(key) == 'function' and key(ctx.action_opts) or key) .. lang_suffix)
-- Log.debug('Action Prompt: {}', prompt)
prefix = content .. '\n`' .. 'Dear FittenCode, Please ' .. prompt .. ':\n'
Expand Down
14 changes: 10 additions & 4 deletions lua/fittencode/views/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ end

function M:show()
if self.win == nil then
self.buffer = api.nvim_create_buf(false, true)
if not self.buffer then
self.buffer = api.nvim_create_buf(false, true)
api.nvim_buf_set_name(self.buffer, 'FittenCodeChat')
end

vim.cmd('topleft vsplit')
vim.cmd('vertical resize ' .. 40)
self.win = api.nvim_get_current_win()
api.nvim_win_set_buf(self.win, self.buffer)
api.nvim_buf_set_name(self.buffer, 'FittenCodeChat')

api.nvim_set_option_value('filetype', 'markdown', { buf = self.buffer })
api.nvim_set_option_value('modifiable', false, { buf = self.buffer })
Expand All @@ -45,8 +48,10 @@ function M:show()
end, { buffer = self.buffer })

if #self.text > 0 then
api.nvim_buf_set_lines(self.buffer, 0, -1, false, self.text)
-- api.nvim_set_option_value('modifiable', true, { buf = self.buffer })
-- api.nvim_buf_set_lines(self.buffer, 0, -1, false, self.text)
api.nvim_win_set_cursor(self.win, { #self.text, 0 })
-- api.nvim_set_option_value('modifiable', false, { buf = self.buffer })
end
end
end
Expand All @@ -57,7 +62,8 @@ function M:close()
end
api.nvim_win_close(self.win, true)
self.win = nil
self.buffer = nil
-- api.nvim_buf_delete(self.buffer, { force = true })
-- self.buffer = nil
end

---@param text? string|string[]
Expand Down

0 comments on commit 3d52389

Please sign in to comment.