Skip to content

Commit

Permalink
Step 22
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed May 28, 2024
1 parent a9463a2 commit bf86fc3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
5 changes: 5 additions & 0 deletions lua/fittencode/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
26 changes: 16 additions & 10 deletions lua/fittencode/engines/inline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions lua/fittencode/engines/inline/model.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local Config = require('fittencode.config')
local SuggestionsCache = require('fittencode.engines.inline.suggestions_cache')
local Unicode = require('fittencode.unicode')

Expand All @@ -7,15 +8,13 @@ local Unicode = require('fittencode.unicode')

---@class InlineModel
---@field cache? SuggestionsCache
---@field mode AcceptMode
---@field direction AcceptRange
---@field range AcceptDirection
local InlineModel = {}

function InlineModel:new()
local o = {
cache = SuggestionsCache:new()
-- mode = config.
}
self.__index = self
return setmetatable(o, self)
Expand All @@ -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 }
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
27 changes: 18 additions & 9 deletions lua/fittencode/views/lines.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit bf86fc3

Please sign in to comment.