Skip to content

Commit

Permalink
feat: implement AcceptCompletion request. (#132)
Browse files Browse the repository at this point in the history
* clean up code.

There's no need to set up a nil function on the hook on `menu_closed`
event and remove that hook.

* feat: implement `AcceptCompletion` request.
  • Loading branch information
milanglacier authored Dec 26, 2023
1 parent 445dca5 commit a6922a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
9 changes: 8 additions & 1 deletion lua/codeium/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function Server:new()
log.debug(j.pid .. ": " .. v)
end

local api_server_url = 'https://' .. config.options.api.host .. ':' .. config.options.api.port
local api_server_url = "https://" .. config.options.api.host .. ":" .. config.options.api.port
job = io.job({
update.get_bin_info().bin,
"--api_server_url",
Expand Down Expand Up @@ -327,6 +327,13 @@ function Server:new()
end
end

function m.accept_completion(completion_id)
request("AcceptCompletion", {
metadata = get_request_metadata(),
completion_id = completion_id,
}, noop)
end

function m.shutdown()
current_cookie = nil
if job then
Expand Down
26 changes: 18 additions & 8 deletions lua/codeium/source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ local function codeium_to_cmp(comp, offset, right)
local documentation = comp.completion.text

local label = string.sub(documentation, offset)
if string.sub(label, - #right) == right then
label = string.sub(label, 1, - #right - 1)
if string.sub(label, -#right) == right then
label = string.sub(label, 1, -#right - 1)
end

-- We get the completion part that has the largest offset
Expand Down Expand Up @@ -68,6 +68,7 @@ local function codeium_to_cmp(comp, offset, right)
kind_text = "Codeium",
kind_hl_group = "CmpItemKindCodeium",
},
codeium_completion_id = comp.completion.completionId,
}
end

Expand All @@ -92,6 +93,20 @@ function Source:get_position_encoding_kind()
return "utf-8"
end

require("cmp").event:on("confirm_done", function(event)
if
event.entry
and event.entry.source
and event.entry.source.name == "codeium"
and event.entry.completion_item
and event.entry.completion_item.codeium_completion_id
and event.entry.source.source
and event.entry.source.source.server
then
event.entry.source.source.server.accept_completion(event.entry.completion_item.codeium_completion_id)
end
end)

function Source:complete(params, callback)
local context = params.context
local offset = params.offset
Expand Down Expand Up @@ -124,9 +139,6 @@ function Source:complete(params, callback)
table.insert(lines, "")
local text = table.concat(lines, line_ending)

local cancel
local remove_event = require("cmp").event:on("menu_closed", cancel)

local function handle_completions(completion_items)
local duplicates = {}
local completions = {}
Expand All @@ -139,7 +151,7 @@ function Source:complete(params, callback)
callback(completions)
end

cancel = self.server.request_completion(
self.server.request_completion(
{
editor_language = filetype,
language = language,
Expand All @@ -149,8 +161,6 @@ function Source:complete(params, callback)
},
editor_options,
function(success, json)
remove_event()

if not success then
callback(nil)
end
Expand Down

0 comments on commit a6922a2

Please sign in to comment.