diff --git a/lua/fittencode/engines/actions.lua b/lua/fittencode/engines/actions.lua index cb1130c5..3c433010 100644 --- a/lua/fittencode/engines/actions.lua +++ b/lua/fittencode/engines/actions.lua @@ -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 diff --git a/lua/fittencode/prompt_providers/actions.lua b/lua/fittencode/prompt_providers/actions.lua index 78e8f914..50a79605 100644 --- a/lua/fittencode/prompt_providers/actions.lua +++ b/lua/fittencode/prompt_providers/actions.lua @@ -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', @@ -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' diff --git a/lua/fittencode/views/chat.lua b/lua/fittencode/views/chat.lua index c5631423..150f57a1 100644 --- a/lua/fittencode/views/chat.lua +++ b/lua/fittencode/views/chat.lua @@ -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 }) @@ -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 @@ -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[]