diff --git a/lua/fittencode/config.lua b/lua/fittencode/config.lua index 8ea6c380..54593349 100644 --- a/lua/fittencode/config.lua +++ b/lua/fittencode/config.lua @@ -47,6 +47,11 @@ local defaults = { -- Auto triggering completion ---@type boolean auto_triggering_completion = true, + -- Accept Mode + -- Available options: + -- * 'commit' (default) + -- * 'stage' + accept_mode = 'commit', }, delay_completion = { -- Delay time for inline completion (in milliseconds). diff --git a/lua/fittencode/engines/inline/init.lua b/lua/fittencode/engines/inline/init.lua index 222109dd..e4258e5a 100644 --- a/lua/fittencode/engines/inline/init.lua +++ b/lua/fittencode/engines/inline/init.lua @@ -71,16 +71,18 @@ local function process_suggestions(task_id, suggestions) }) end -local function apply_suggestion(task_id, row, col, suggestion) - if suggestion then +local function apply_new_suggestions(task_id, row, col, suggestions) + if suggestions then model:recalculate({ task_id = task_id, row = row, col = col, - suggestion = suggestion, + suggestions = suggestions, }) - if M.is_inline_enabled() then - Lines.render_virt_text(suggestion) + if suggestions_modify_enabled() then + Lines.render_virt_text({ + suggestions = suggestions, + }) end end end @@ -97,7 +99,7 @@ local function _generate_one_stage(row, col, on_success, on_error) local processed = process_suggestions(id, suggestions) if processed then status:update(SC.SUGGESTIONS_READY) - apply_suggestion(task_id, row, col, processed) + apply_new_suggestions(task_id, row, col, processed) schedule(on_success, processed) else status:update(SC.NO_MORE_SUGGESTIONS) @@ -183,13 +185,11 @@ function M.triggering_completion() local prompt = ' (Currently no completion options available)' local fx = function() Lines.render_virt_text({ - suggestions = { - { prompt } - }, + suggestions = { prompt }, hi = { Color.FittenNoMoreSuggestion, }, - hl_mode = { 'replace' }, + hl_mode = 'replace', show_time = 2000, }) end @@ -231,12 +231,18 @@ local function _accept_impl(range, direction) if not suggestions_modify_enabled() then return end + if Config.options.inline_completion.accept_mode == 'commit' and direction == 'backward' then + return + end Lines.clear_virt_text() ignoreevent_wrap(function() local updated = model:accept({ range = range, direction = direction, }) + if not updated then + return + end local virt_opts = make_virt_opts(updated) if model.mode == 'commit' then local text_opts = make_text_opts(updated) diff --git a/lua/fittencode/engines/inline/model.lua b/lua/fittencode/engines/inline/model.lua index 085e9d91..0ef6bd6d 100644 --- a/lua/fittencode/engines/inline/model.lua +++ b/lua/fittencode/engines/inline/model.lua @@ -1,3 +1,4 @@ +local Config = require('fittencode.config') local SuggestionsCache = require('fittencode.engines.inline.suggestions_cache') local Unicode = require('fittencode.unicode') @@ -7,7 +8,6 @@ local Unicode = require('fittencode.unicode') ---@class InlineModel ---@field cache? SuggestionsCache ----@field mode AcceptMode ---@field direction AcceptRange ---@field range AcceptDirection local InlineModel = {} @@ -15,7 +15,6 @@ local InlineModel = {} function InlineModel:new() local o = { cache = SuggestionsCache:new() - -- mode = config. } self.__index = self return setmetatable(o, self) @@ -25,14 +24,14 @@ end ---@field task_id number ---@field row number ---@field col number ----@field suggestion string[] +---@field suggestions string[] ---@param opts InlineModelRecalculateOptions function InlineModel:recalculate(opts) local task_id = opts.task_id local row = opts.row local col = opts.col - local suggestion = opts.suggestion + local suggestion = opts.suggestions self.cache.task_id = task_id self.cache.triggered_cursor = { row, col } @@ -219,6 +218,9 @@ end ---@param opts InlineModelAcceptOptions function InlineModel:accept(opts) + if Config.options.inline_completion.accept_mode == 'commit' and opts.direction == 'backward' then + return nil + end local row, col = unpack(self.cache.stage_cursor) row, col = pre_accept(self.cache.lines, row, col, opts.direction) if opts.range == 'char' then @@ -241,14 +243,14 @@ function InlineModel:accept(opts) } } self.cache.stage_cursor = { row, col } - if self.mode == 'commit' then + if Config.options.inline_completion.accept_mode == 'commit' then local pre_commit = self.cache.commit_cursor self.cache.commit_cursor = { row, col } -- (pre_commit, commit] updated.segments.pre_commit = pre_commit updated.segments.commit = self.cache.commit_cursor -- self.cache.triggered_cursor -- update triggered_cursor - elseif self.mode == 'stage' then + elseif Config.options.inline_completion.accept_mode == 'stage' then -- [..., stage] -- (stage, ...] updated.segments.stage = self.cache.stage_cursor diff --git a/lua/fittencode/views/lines.lua b/lua/fittencode/views/lines.lua index 99b2d8bb..3d78ac91 100644 --- a/lua/fittencode/views/lines.lua +++ b/lua/fittencode/views/lines.lua @@ -38,20 +38,28 @@ function M.tab() end ---@param suggestions? Suggestions ----@param hi? string +---@param segments? integer[][] +---@param hi? string[] ---@return VirtText|nil -local function generate_virt_text(suggestions, hi) +local function generate_virt_text(suggestions, segments, hi) if suggestions == nil then return end + segments = segments or {} + hi = hi or {} + local current_segment = 1 ---@type VirtText local virt_text = {} - for _, line in ipairs(suggestions) do + for i, line in ipairs(suggestions) do + if #segments > 0 then + if i == segments[current_segment][1] then + end + end local color = Color.FittenSuggestion if is_whitespace_line(line) then color = Color.FittenSuggestionWhitespace end - color = hi or color + color = hi[i] or color table.insert(virt_text, { { line, color } }) end return virt_text @@ -234,9 +242,10 @@ end ---@class RenderVirtTextOptions ---@field show_time? integer ----@field suggestions? Suggestions[] +---@field suggestions? Suggestions +---@field segments? integer[][] ---@field hi? string[] ----@field hl_mode? string[] +---@field hl_mode? string ---@param opts? RenderVirtTextOptions function M.render_virt_text(opts) @@ -247,10 +256,10 @@ function M.render_virt_text(opts) local hl_mode = opts.hl_mode or 'combine' ---@type VirtText? - local committed_virt_text = generate_virt_text(suggestions, hi) - move_to_center_vertical(vim.tbl_count(committed_virt_text or {})) + local virt_text = generate_virt_text(suggestions, segments, hi) + move_to_center_vertical(vim.tbl_count(virt_text or {})) api.nvim_buf_clear_namespace(0, namespace, 0, -1) - set_extmark(committed_virt_text, hl_mode) + set_extmark(virt_text, hl_mode) if show_time and show_time > 0 then vim.defer_fn(function()