Skip to content

Commit

Permalink
Step 41
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed May 30, 2024
1 parent b1a8ea5 commit c1bc6d4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 69 deletions.
1 change: 1 addition & 0 deletions lua/fittencode/color.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ M.FittenSuggestion = 'FittenSuggestion'
M.FittenSuggestionBackground = 'FittenSuggestionBackground'
M.FittenNoMoreSuggestion = 'FittenNoMoreSuggestion'
M.FittenSuggestionStage = 'FittenSuggestionStage'
M.FittenSuggestionStageBackground = 'FittenSuggestionStageBackground'

-- Define FittenCode colors
local colors = {}
Expand Down
93 changes: 26 additions & 67 deletions lua/fittencode/engines/inline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ end

local function suggestions_modify_enabled()
if not M.is_inline_enabled() then
return
return false
end

if not M.has_suggestions() then
Log.debug('No suggestions')
return
return false
end

return true
end

---@param task_id integer
Expand All @@ -68,16 +70,6 @@ local function process_suggestions(task_id, suggestions)
})
end

local function make_text_opts(ss)
if Config.options.inline_completion.accept_mode == 'commit' then
return {
lines = {
ss.commit,
}
}
end
end

---@param ss SuggestionsSegments
---@return RenderVirtTextOptions?
local function make_virt_opts(ss)
Expand Down Expand Up @@ -109,8 +101,11 @@ local function apply_new_suggestions(task_id, row, col, suggestions)
col = col,
suggestions = suggestions,
})
Log.debug('Cache: {}', model.cache)
if suggestions_modify_enabled() then
Lines.render_virt_text(make_virt_opts(model:get_suggestions_segments()))
local ss = make_virt_opts(model:get_suggestions_segments())
Log.debug('Apply new suggestions: {}', ss)
Lines.render_virt_text(ss)
end
end
end
Expand Down Expand Up @@ -246,32 +241,39 @@ local function ignoreevent_wrap(fx)
return ret
end

local function _accept_impl(range, direction)
local function _accept_impl(range, direction, mode)
if not suggestions_modify_enabled() then
return
end
if Config.options.inline_completion.accept_mode == 'commit' and direction == 'backward' then
mode = mode or Config.options.inline_completion.accept_mode
if mode == 'commit' and direction == 'backward' then
return
end
Lines.clear_virt_text()
ignoreevent_wrap(function()
local ss = model:accept({
mode = mode,
range = range,
direction = direction,
})
if not ss then
return
end
local virt_opts = make_virt_opts(ss)
if Config.options.inline_completion.accept_mode == 'commit' then
local text_opts = make_text_opts(ss)
local cusors = Lines.set_text(text_opts)
if mode == 'commit' then
local window = api.nvim_get_current_win()
local buffer = api.nvim_win_get_buf(window)
local cusors = Lines.set_text({
window = window,
buffer = buffer,
lines = ss.commit,
})
if not cusors then
return
end
model:update_triggered_cursor(unpack(cusors[2]))
Lines.render_virt_text(virt_opts)
elseif Config.options.inline_completion.accept_mode == 'stage' then
elseif mode == 'stage' then
Lines.render_virt_text(virt_opts)
end
end)
Expand Down Expand Up @@ -339,61 +341,18 @@ function M.is_inline_enabled()
return true
end

-- TODO: Support for Chinese input
---@return boolean?
function M.lazy_inline_completion()
Log.debug('Lazy inline completion...')

if not suggestions_modify_enabled() then
return
end

local is_advance = function(row, col)
local cached_row, cached_col = cache:get_cursor()
if cached_row == row and cached_col + 1 == col then
return 1
elseif cached_row and cached_col and row == cached_row + 1 and col == 0 then
return 2
end
return 0
end
-- local ss = model:accept({
-- mode = 'commit', -- Force commit
-- range = 'char',
-- direction = 'forward',
-- })

local row, col = Base.get_cursor()
Log.debug('Lazy inline completion row: {}, col: {}', row, col)
Log.debug('Cached row: {}, col: {}', cache:get_cursor())

local adv_type = is_advance(row, col)
if adv_type > 0 then
local cur_line = api.nvim_buf_get_lines(0, row, row + 1, false)[1]
local cache_line = cache:get_line(1)
if not cache_line or #cache_line == 0 then
return false
end
if adv_type == 1 then
Log.debug('Lazy advance type 1')
local cur_char = string.sub(cur_line, col, col)
local cache_char = string.sub(cache_line, 1, 1)
if cur_char == cache_char then
Log.debug('Current char matches cached char: {}', cur_char)
if #cache_line > 1 then
cache_line = string.sub(cache_line, 2)
else
cache_line = nil
end
cache:update_line(1, cache_line)
cache:update_cursor(row, col)
Lines.render_virt_text(cache:get_lines())
return true
end
elseif adv_type == 2 then
Log.debug('Lazy advance type 2')
-- Neovim will auto indent the new line, so the cached line that contains spaces will be invalid, we can't reusing it.
-- cache:update_line(1, nil)
-- cache:update_cursor(row, col)
-- View.render_virt_text(cache:get_lines())
-- return true
end
end
return false
end

Expand Down
6 changes: 4 additions & 2 deletions lua/fittencode/engines/inline/model.lua
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ function InlineModel:get_suggestions()
end

function InlineModel:cache_hit(row, col)
return self.cache.stage_cursor[1] == row and self.cache.stage_cursor[2] == col
return self.cache.stage_cursor and
self.cache.stage_cursor[1] == row and
self.cache.stage_cursor[2] == col
end

function InlineModel:make_new_trim_commmited_suggestions()
Expand All @@ -339,7 +341,7 @@ function InlineModel:get_suggestions_segments()
stage = self.cache.stage_cursor
}
}
return make_segments(updated)
return make_segments(updated, self.cache.utf_end)
end

return InlineModel
2 changes: 2 additions & 0 deletions lua/fittencode/engines/inline/suggestions_cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function SuggestionsCache.new()
self.row = nil
self.col = nil
self.count = 0
self.commit_cursor = { 0, 0 }
self.stage_cursor = { 0, 0 }
return self
end

Expand Down

0 comments on commit c1bc6d4

Please sign in to comment.