From ce15f32cb6c117f730a6af881ac347c2aff18153 Mon Sep 17 00:00:00 2001 From: luozhiya Date: Mon, 13 May 2024 17:38:47 +0800 Subject: [PATCH 1/3] Handle nest type in Markdown properly --- lua/fittencode/engines/actions.lua | 44 +++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/lua/fittencode/engines/actions.lua b/lua/fittencode/engines/actions.lua index c8353d03..d870d881 100644 --- a/lua/fittencode/engines/actions.lua +++ b/lua/fittencode/engines/actions.lua @@ -165,6 +165,39 @@ local function on_error(err) end end +local function _find_nospace(line) + for i = 1, #line do + if line:sub(i, i) ~= ' ' then + return i + end + end +end + +local function _get_tslangs(buffer, start_row, end_row) + local row = start_row + local col = 0 + + for i = start_row, end_row do + local line = api.nvim_buf_get_lines(buffer, i, i + 1, false)[1] + local pos = _find_nospace(line) + if pos then + row = i + col = pos + break + end + end + + local info = vim.inspect_pos(buffer, row, col) + local ts = info.treesitter + local langs = {} + for _, node in ipairs(ts) do + if not vim.tbl_contains(langs, node.lang) then + langs[#langs + 1] = node.lang + end + end + return langs +end + ---@param action number ---@param opts? ActionOptions ---@return nil @@ -207,11 +240,20 @@ function ActionsEngine.start_action(action, opts) chat:show() vim.fn.win_gotoid(window) + local filetype = api.nvim_get_option_value('filetype', { buf = buffer }) + Log.debug('Action filetype: {}', filetype) + local langs = _get_tslangs(buffer, sln, eln) + Log.debug('Action langs: {}', langs) + if filetype == 'markdown' and #langs >= 2 then + filetype = vim.tbl_filter(function(lang) return lang ~= 'markdown' end, langs)[1] + end + Log.debug('Action filetype: {}', filetype) + local prompt_opts = { window = window, buffer = buffer, range = { sln - 1, eln - 1 }, - filetype = vim.bo.filetype, + filetype = filetype, prompt_ty = get_action_type(action), solved_content = opts and opts.content, solved_prefix = nil, From d9a7d9ddff1099ddba358d07927e71a1cd9d7f97 Mon Sep 17 00:00:00 2001 From: luozhiya Date: Mon, 13 May 2024 17:42:28 +0800 Subject: [PATCH 2/3] Code Clean --- lua/fittencode/base.lua | 2 +- lua/fittencode/engines/actions.lua | 6 ++-- lua/fittencode/fs/path.lua | 3 +- lua/fittencode/rest/manager.lua | 4 ++- lua/fittencode/views/chat.lua | 44 ++++++++++++++++-------------- 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/lua/fittencode/base.lua b/lua/fittencode/base.lua index 4dd99255..f97e5abd 100644 --- a/lua/fittencode/base.lua +++ b/lua/fittencode/base.lua @@ -48,7 +48,7 @@ end ---@param name string ---@param hi table function M.set_hi(name, hi) - if vim.fn.has('nvim-0.10') == 1 then + if fn.has('nvim-0.10') == 1 then -- https://github.com/neovim/neovim/pull/25229 -- https://github.com/luozhiya/fittencode.nvim/issues/20 -- hi.force = true diff --git a/lua/fittencode/engines/actions.lua b/lua/fittencode/engines/actions.lua index d870d881..0cef3cb3 100644 --- a/lua/fittencode/engines/actions.lua +++ b/lua/fittencode/engines/actions.lua @@ -232,13 +232,13 @@ function ActionsEngine.start_action(action, opts) local vmode = { 'v', 'V', '' } Log.debug('mode: {}', api.nvim_get_mode().mode) if vim.tbl_contains(vmode, api.nvim_get_mode().mode) then - sln = vim.fn.getpos("'<")[2] - eln = vim.fn.getpos("'>")[2] + sln = fn.getpos("'<")[2] + eln = fn.getpos("'>")[2] Log.debug('v mode sln: {}, eln: {}', sln, eln) end chat:show() - vim.fn.win_gotoid(window) + fn.win_gotoid(window) local filetype = api.nvim_get_option_value('filetype', { buf = buffer }) Log.debug('Action filetype: {}', filetype) diff --git a/lua/fittencode/fs/path.lua b/lua/fittencode/fs/path.lua index 0106d83e..9f0a5d1c 100644 --- a/lua/fittencode/fs/path.lua +++ b/lua/fittencode/fs/path.lua @@ -1,4 +1,5 @@ local api = vim.api +local fn = vim.fn local Base = require('fittencode.base') @@ -41,7 +42,7 @@ end function M.name(buffer) local path = api.nvim_buf_get_name(buffer) - return vim.fn.fnamemodify(path, ':t') + return fn.fnamemodify(path, ':t') end return M diff --git a/lua/fittencode/rest/manager.lua b/lua/fittencode/rest/manager.lua index db225fa5..1da2eecc 100644 --- a/lua/fittencode/rest/manager.lua +++ b/lua/fittencode/rest/manager.lua @@ -1,3 +1,5 @@ +local api = vim.api + local Config = require('fittencode.config') local Log = require('fittencode.log') @@ -26,7 +28,7 @@ end function M.setup() if not vim.tbl_contains(vim.tbl_keys(builtin_backends), Config.options.rest.backend) then local msg = 'Invalid rest backend: ' .. Config.options.rest.backend - vim.api.nvim_err_writeln(msg) + api.nvim_err_writeln(msg) end end diff --git a/lua/fittencode/views/chat.lua b/lua/fittencode/views/chat.lua index 4848e57c..c5631423 100644 --- a/lua/fittencode/views/chat.lua +++ b/lua/fittencode/views/chat.lua @@ -1,3 +1,5 @@ +local api = vim.api + local Base = require('fittencode.base') local Log = require('fittencode.log') @@ -21,30 +23,30 @@ end function M:show() if self.win == nil then - self.buffer = vim.api.nvim_create_buf(false, true) + self.buffer = api.nvim_create_buf(false, true) vim.cmd('topleft vsplit') vim.cmd('vertical resize ' .. 40) - self.win = vim.api.nvim_get_current_win() - vim.api.nvim_win_set_buf(self.win, self.buffer) - vim.api.nvim_buf_set_name(self.buffer, 'FittenCodeChat') + self.win = api.nvim_get_current_win() + api.nvim_win_set_buf(self.win, self.buffer) + api.nvim_buf_set_name(self.buffer, 'FittenCodeChat') - vim.api.nvim_set_option_value('filetype', 'markdown', { buf = self.buffer }) - vim.api.nvim_set_option_value('modifiable', false, { buf = self.buffer }) - vim.api.nvim_set_option_value('wrap', true, { win = self.win }) - vim.api.nvim_set_option_value('linebreak', true, { win = self.win }) - vim.api.nvim_set_option_value('cursorline', true, { win = self.win }) - vim.api.nvim_set_option_value('spell', false, { win = self.win }) - vim.api.nvim_set_option_value('number', false, { win = self.win }) - vim.api.nvim_set_option_value('relativenumber', false, { win = self.win }) - vim.api.nvim_set_option_value('conceallevel', 3, { win = self.win }) + api.nvim_set_option_value('filetype', 'markdown', { buf = self.buffer }) + api.nvim_set_option_value('modifiable', false, { buf = self.buffer }) + api.nvim_set_option_value('wrap', true, { win = self.win }) + api.nvim_set_option_value('linebreak', true, { win = self.win }) + api.nvim_set_option_value('cursorline', true, { win = self.win }) + api.nvim_set_option_value('spell', false, { win = self.win }) + api.nvim_set_option_value('number', false, { win = self.win }) + api.nvim_set_option_value('relativenumber', false, { win = self.win }) + api.nvim_set_option_value('conceallevel', 3, { win = self.win }) Base.map('n', 'q', function() self:close() end, { buffer = self.buffer }) if #self.text > 0 then - vim.api.nvim_buf_set_lines(self.buffer, 0, -1, false, self.text) - vim.api.nvim_win_set_cursor(self.win, { #self.text, 0 }) + api.nvim_buf_set_lines(self.buffer, 0, -1, false, self.text) + api.nvim_win_set_cursor(self.win, { #self.text, 0 }) end end end @@ -53,7 +55,7 @@ function M:close() if self.win == nil then return end - vim.api.nvim_win_close(self.win, true) + api.nvim_win_close(self.win, true) self.win = nil self.buffer = nil end @@ -75,16 +77,16 @@ function M:commit(text, linebreak) end end if self.buffer then - vim.api.nvim_set_option_value('modifiable', true, { buf = self.buffer }) + api.nvim_set_option_value('modifiable', true, { buf = self.buffer }) if #self.text == 0 then - vim.api.nvim_buf_set_lines(self.buffer, 0, -1, false, lines) + api.nvim_buf_set_lines(self.buffer, 0, -1, false, lines) else - vim.api.nvim_buf_set_lines(self.buffer, -1, -1, false, lines) + api.nvim_buf_set_lines(self.buffer, -1, -1, false, lines) end - vim.api.nvim_set_option_value('modifiable', false, { buf = self.buffer }) + api.nvim_set_option_value('modifiable', false, { buf = self.buffer }) end table.move(lines, 1, #lines, #self.text + 1, self.text) - vim.api.nvim_win_set_cursor(self.win, { #self.text, 0 }) + api.nvim_win_set_cursor(self.win, { #self.text, 0 }) end local function _sub_match(s, pattern) From da692ff53d1e87ea7c9acab025b7280b5f90eed0 Mon Sep 17 00:00:00 2001 From: luozhiya Date: Mon, 13 May 2024 17:43:18 +0800 Subject: [PATCH 3/3] Refactor `setup_keyfilters` --- lua/fittencode/bindings.lua | 2 +- lua/fittencode/init.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/fittencode/bindings.lua b/lua/fittencode/bindings.lua index e09fd468..32ec27f2 100644 --- a/lua/fittencode/bindings.lua +++ b/lua/fittencode/bindings.lua @@ -221,7 +221,7 @@ function M.setup_keymaps() Base.map('i', '', API.accept_word) end -function M.setup_onkey() +function M.setup_keyfilters() -- '<80>kd', '<80>kD' in Lua local keycodes = { '', diff --git a/lua/fittencode/init.lua b/lua/fittencode/init.lua index f6d2b95a..c00327b3 100644 --- a/lua/fittencode/init.lua +++ b/lua/fittencode/init.lua @@ -26,7 +26,7 @@ function M.setup(opts) Bindings.setup_keymaps() end if Config.options.inline_completion.disable_completion_when_delete then - Bindings.setup_onkey() + Bindings.setup_keyfilters() end elseif Config.options.completion_mode == 'source' then require('fittencode.sources').setup()