Skip to content

Commit

Permalink
Object 86
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Jan 3, 2025
1 parent 5415d25 commit 221f4e2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 42 deletions.
56 changes: 22 additions & 34 deletions lua/fittencode/inline/controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ function Controller:new(opts)
augroups = {},
ns_ids = {},
keymaps = {},
request_handles = {},
filter_events = {}
}
setmetatable(obj, self)
Expand All @@ -39,7 +38,11 @@ function Controller:init()
level = 0,
})
self:register_observer(self.status)
self.generate_one_stage = Fn.debounce(Client.generate_one_stage, Config.delay_completion.delaytime, function(data) self:on_request_return(data) end)
self.generate_one_stage = Fn.debounce(Client.generate_one_stage, Config.delay_completion.delaytime, function(data)
if self.session then
self.session.request_handles[#self.session.request_handles + 1] = data
end
end)
self.augroups.completion = vim.api.nvim_create_augroup('Fittencode.Inline.Completion', { clear = true })
self.augroups.no_more_suggestion = vim.api.nvim_create_augroup('Fittencode.Inline.NoMoreSuggestion', { clear = true })
self.ns_ids.virt_text = vim.api.nvim_create_namespace('Fittencode.Inline.VirtText')
Expand All @@ -66,15 +69,11 @@ end

function Controller:dismiss_suggestions()
if self.session then
self.session:destory()
self.session:destructor()
self.session = nil
end
end

function Controller:on_request_return(data)
self.request_handles[#self.request_handles + 1] = data
end

function Controller:lazy_completion()
if not string.match(vim.fn.mode(), '^[iR]') then
return
Expand All @@ -84,7 +83,7 @@ function Controller:lazy_completion()

-- 2. input char ~= next char
if self.session then
self.session:destory()
self.session:destructor()
self.session = nil
end
end
Expand Down Expand Up @@ -223,13 +222,8 @@ function Controller:is_filetype_excluded(buf)
end

function Controller:cleanup_session()
for _, handle in ipairs(self.request_handles) do
handle:abort()
end
self.request_handles = {}

if self.session then
self.session:destory()
self.session:destructor()
self.session = nil
end
end
Expand All @@ -255,19 +249,20 @@ function Controller:triggering_completion(options)

self:cleanup_session()

local timing = {
get_completion_version = {},
generate_one_stage = {}
}
timing.triggering = vim.uv.hrtime()
self.session = Session:new({
buf = buf,
reflect = function(_) self:reflect(_) end,
})

self.session.timing.on_create = vim.uv.hrtime()

Promise:new(function(resolve, reject)
local gcv_options = {
on_create = function()
timing.get_completion_version.on_create = vim.uv.hrtime()
self.session.timing.get_completion_version.on_create = vim.uv.hrtime()
end,
on_once = function(stdout)
timing.get_completion_version.on_once = vim.uv.hrtime()
self.session.timing.get_completion_version.on_once = vim.uv.hrtime()
local json = table.concat(stdout, '')
local _, version = pcall(vim.fn.json_decode, json)
if not _ or version == nil then
Expand All @@ -278,22 +273,22 @@ function Controller:triggering_completion(options)
end
end,
on_error = function()
timing.get_completion_version.on_error = vim.uv.hrtime()
self.session.timing.get_completion_version.on_error = vim.uv.hrtime()
reject()
end
}
self.request_handles[#self.request_handles + 1] = Client.get_completion_version(gcv_options)
self.session.request_handles[#self.session.request_handles + 1] = Client.get_completion_version(gcv_options)
end):forward(function(version)
return Promise:new(function(resolve, reject)
Log.debug('Triggering completion for position {}', position)
local gos_options = {
completion_version = version,
prompt = self:generate_prompt(buf, position),
on_create = function()
timing.generate_one_stage.on_create = vim.uv.hrtime()
self.session.timing.generate_one_stage.on_create = vim.uv.hrtime()
end,
on_once = function(stdout)
timing.generate_one_stage.on_once = vim.uv.hrtime()
self.session.timing.generate_one_stage.on_once = vim.uv.hrtime()
local _, json = pcall(vim.json.decode, table.concat(stdout, ''))
if not _ then
Log.error('Failed to decode completion response: {}', json)
Expand All @@ -309,7 +304,7 @@ function Controller:triggering_completion(options)
resolve(completion)
end,
on_error = function()
timing.generate_one_stage.on_error = vim.uv.hrtime()
self.session.timing.generate_one_stage.on_error = vim.uv.hrtime()
reject()
end
}
Expand All @@ -324,14 +319,7 @@ function Controller:triggering_completion(options)
completion = completion,
})
local view = View:new({ buf = buf })
self.session = Session:new({
buf = buf,
model = model,
view = view,
timing = timing,
reflect = function(_) self:reflect(_) end,
})
self.session:init()
self.session:init(model, view)
Log.debug('New session created {}', self.session)
Fn.schedule_call(options.on_success)
end, function()
Expand Down
22 changes: 17 additions & 5 deletions lua/fittencode/inline/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ Session.__index = Session
function Session:new(opts)
local obj = {
buf = opts.buf,
model = opts.model,
view = opts.view,
timing = opts.timing,
reflect = opts.reflect,
timing = {
get_completion_version = {},
generate_one_stage = {}
},
request_handles = {}
}
setmetatable(obj, Session)
return obj
end

function Session:init()
function Session:init(model, view)
self.model = model
self.view = view
self:set_keymaps()
self:set_autocmds()
end
Expand Down Expand Up @@ -92,7 +96,15 @@ function Session:cache_hit(row, col)
-- return self.model:eq_commit_pos(row, col)
end

function Session:destory()
function Session:abort_and_clear_requests()
for _, handle in ipairs(self.request_handles) do
handle:abort()
end
self.request_handles = {}
end

function Session:destructor()
self:abort_and_clear_requests()
self.model:clear()
self:update_view()
self:restore_keymaps()
Expand Down
10 changes: 7 additions & 3 deletions lua/fittencode/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,14 @@
---@field model FittenCode.Inline.Model
---@field view FittenCode.Inline.View
---@field buf number
---@field request_handle number
---@field request_handles table<FittenCode.HTTP.RequestHandle?>
---@field timing FittenCode.Inline.Session.Timing
---@field keymaps table
---@field extmark_ids table
---@field generated_text table<string>

---@class FittenCode.Inline.Session.Timing
---@field on_create number
---@field get_completion_version table
---@field generate_one_stage table

---@class FittenCode.Inline.Model
---@field mode 'lines' | 'multi_segments' | 'edit_completion'
Expand Down

0 comments on commit 221f4e2

Please sign in to comment.