From e39a2e4d39b05be1c5423be188a8d74cace578fe Mon Sep 17 00:00:00 2001 From: luozhiya Date: Sat, 1 Jun 2024 18:44:13 +0800 Subject: [PATCH] Step 60 --- lua/fittencode/bindings.lua | 5 +---- lua/fittencode/engines/inline/init.lua | 23 ++++++++++++++++++----- lua/fittencode/engines/inline/model.lua | 24 ++++++++++++++++++------ lua/fittencode/views/lines.lua | 3 ++- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/lua/fittencode/bindings.lua b/lua/fittencode/bindings.lua index 26a5c74..05ed4b2 100644 --- a/lua/fittencode/bindings.lua +++ b/lua/fittencode/bindings.lua @@ -59,11 +59,8 @@ function M.setup_autocmds() api.nvim_create_autocmd({ 'TextChangedI' }, { group = Base.augroup('TextChanged'), pattern = '*', - callback = function() + callback = function(param) -- Log.debug('TextChangedI') - if not Config.options.inline_completion.auto_triggering_completion then - return - end if ignore then return end diff --git a/lua/fittencode/engines/inline/init.lua b/lua/fittencode/engines/inline/init.lua index 890c7ea..07fbea3 100644 --- a/lua/fittencode/engines/inline/init.lua +++ b/lua/fittencode/engines/inline/init.lua @@ -149,7 +149,9 @@ local generate_one_stage_timer = nil function M.generate_one_stage(row, col, force, delaytime, on_success, on_error) if not force and model:cache_hit(row, col) and M.has_suggestions() then status:update(SC.SUGGESTIONS_READY) - render_virt_text_segments(model:get_suggestions_segments()) + Log.debug('CACHE HIT') + local segments = model:get_suggestions_segments() + render_virt_text_segments(segments) schedule(on_success, model:make_new_trim_commmited_suggestions()) return else @@ -264,10 +266,21 @@ local function _accept_impl(range, direction, mode) return end Lines.clear_virt_text() + local commited = false if mode == 'stage' and range == 'all' then - set_text_event_filter(model:get_suggestions_segments().stage) - model:sync_commit() - else + local segments = model:get_suggestions_segments() + if not segments then + return + end + if model:is_initial_state() then + mode = 'commit' + else + set_text_event_filter(segments.stage) + model:sync_commit() + commited = true + end + end + if not commited then local updated = model:accept({ mode = mode, range = range, @@ -360,6 +373,7 @@ function M.lazy_inline_completion() if not suggestions_modify_enabled() then return end + Lines.clear_virt_text() local window = api.nvim_get_current_win() local buffer = api.nvim_win_get_buf(window) local row, col = Base.get_cursor(window) @@ -368,7 +382,6 @@ function M.lazy_inline_completion() return end if model:is_advance(row, col, char) then - Lines.clear_virt_text() model:accept({ mode = 'commit', range = 'char', diff --git a/lua/fittencode/engines/inline/model.lua b/lua/fittencode/engines/inline/model.lua index ee4cd88..6d6ef84 100644 --- a/lua/fittencode/engines/inline/model.lua +++ b/lua/fittencode/engines/inline/model.lua @@ -122,11 +122,7 @@ end ---@param cache SuggestionsCache ---@return number?, number? local function _next_all(cache) - local lines = cache.lines - if not lines then - return nil - end - return #lines, #lines[#lines] + return end local NEXT = { @@ -153,6 +149,11 @@ local function _accept(cache, row, col, direction, range) if direction == 'forward' then local next = next_fx(cache, row, col) if next == nil then + if range == 'all' then + row = #lines + col = #lines[row] + return row, col + end row = row + 1 if row <= #lines then if range == 'word' then @@ -377,7 +378,7 @@ function InlineModel:accept(opts) if not next_row or not next_col then return end - local cursor = { next_col, next_col } + local cursor = { next_row, next_col } ---@type AcceptIncrementalUpdates local updated = { @@ -502,4 +503,15 @@ function InlineModel:sync_commit() self.cache.commit_cursor = self.cache.stage_cursor end +function InlineModel:is_initial_state() + local stage_cursor = self.cache.stage_cursor + if not stage_cursor or not stage_cursor[1] or not stage_cursor[2] then + return true + end + if (stage_cursor[1] == 0 or stage_cursor[1] == 1) and stage_cursor[2] == 0 then + return true + end + return false +end + return InlineModel diff --git a/lua/fittencode/views/lines.lua b/lua/fittencode/views/lines.lua index dc3d35e..fc81cf3 100644 --- a/lua/fittencode/views/lines.lua +++ b/lua/fittencode/views/lines.lua @@ -298,11 +298,12 @@ function M.render_virt_text(opts) M.clear_virt_text() end, show_time) end + + -- api.nvim_command('redraw!') end function M.clear_virt_text() api.nvim_buf_clear_namespace(0, namespace, 0, -1) - -- api.nvim_command('redraw!') end -- When we edit some complex documents, extmark will not be able to draw correctly.