Skip to content

Commit

Permalink
fix incorrect isIncomplete flags and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zbirenbaum committed Sep 9, 2023
1 parent 842beea commit be98d3a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 60 deletions.
69 changes: 11 additions & 58 deletions lua/copilot_cmp/completion_functions.lua
Original file line number Diff line number Diff line change
@@ -1,77 +1,30 @@
local format = require("copilot_cmp.format")
local pattern = require("copilot_cmp.pattern")
local util = require("copilot.util")
local api = require("copilot.api")
-- local test = require('copilot_cmp.test_format')

local methods = {
opts = {
fix_pairs = true,
}
}

local function handle_suffix(text, suffix)
local tbl = format.split(text)
tbl[1] = pattern.set_suffix(tbl[1], suffix)
local res = ''
for i, v in ipairs(tbl) do
res = res .. v
if i < #tbl then
res = res .. '\n'
end
end
return res
end
methods.getCompletionsCycling = function (self, params, callback)
local respond_callback = function(err, response)

local format_completions = function(completions, ctx)
local format_item = function(item)
if methods.opts.fix_pairs then
item.text = handle_suffix(item.text, ctx.cursor_after_line)
item.displayText = handle_suffix(item.displayText, ctx.cursor_after_line)
if err or not response or not response.completions then
return callback({isIncomplete = false, items = {}})
end

local multi_line = format.to_multi_line(item, ctx)
local items = vim.tbl_map(function(item)
return format.format_item(item, params.context, methods.opts)
end, vim.tbl_values(response.completions))

return {
copilot = true, -- for comparator, only availiable in panel, not cycling
score = item.score or nil,
label = multi_line.label,
filterText = multi_line.newText,
kind = 1,
cmp = {
kind_hl_group = "CmpItemKindCopilot",
kind_text = 'Copilot',
},
textEdit = {
-- use trim text here so it doesn't add extra indent
newText = multi_line.text,
insert = multi_line.insert,
replace = multi_line.replace,
},
documentation = {
kind = "markdown",
value = "```" .. vim.bo.filetype .. "\n" .. multi_line.preview .. "\n```"
},
dup = 0,
}
return callback({
isIncomplete = false,
items = items
})
end

return {
IsIncomplete = true,
items = #completions > 0 and vim.tbl_map(function(item)
return format_item(item)
end, completions) or {}
}
end

methods.getCompletionsCycling = function (self, params, callback)
local respond_callback = function(err, response)
if err or not response or vim.tbl_isempty(response.completions) then
return callback({isIncomplete = true, items = {}})
end
local completions = vim.tbl_values(response.completions)
return callback(format_completions(completions, params.context))
end
api.get_completions_cycling(self.client, util.get_doc_params(), respond_callback)
return callback({isIncomplete = true, items = {}})
end
Expand Down
48 changes: 46 additions & 2 deletions lua/copilot_cmp/format.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local pattern = require("copilot_cmp.pattern")
local format= {}

local string = require('string')

local label_text = function (text)
local shorten = function (str)
local short_prefix = string.sub(str, 0, 20)
Expand Down Expand Up @@ -125,4 +124,49 @@ format.to_multi_line = function (item, ctx)
}
end

local function handle_suffix(text, suffix)
local tbl = format.split(text)
tbl[1] = pattern.set_suffix(tbl[1], suffix)
local res = ''
for i, v in ipairs(tbl) do
res = res .. v
if i < #tbl then
res = res .. '\n'
end
end
return res
end

format.format_item = function(item, ctx, opts)
if opts.fix_pairs then
item.text = handle_suffix(item.text, ctx.cursor_after_line)
item.displayText = handle_suffix(item.displayText, ctx.cursor_after_line)
end

local multi_line = format.to_multi_line(item, ctx)

return {
copilot = true, -- for comparator, only availiable in panel, not cycling
score = item.score or nil,
label = multi_line.label,
filterText = multi_line.newText,
kind = 1,
cmp = {
kind_hl_group = "CmpItemKindCopilot",
kind_text = 'Copilot',
},
textEdit = {
-- use trim text here so it doesn't add extra indent
newText = multi_line.text,
insert = multi_line.insert,
replace = multi_line.replace,
},
documentation = {
kind = "markdown",
value = "```" .. vim.bo.filetype .. "\n" .. multi_line.preview .. "\n```"
},
dup = 0,
}
end

return format
4 changes: 4 additions & 0 deletions lua/copilot_cmp/pattern.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ local fmt_char = {
['}'] = '%}',
}

-- check if text has pair for char c
local function text_has_pair(text, c)
if not text or not c then return false end
return text:find(ch_pairs[c]) ~= nil
end

-- check if text has char c
local function text_has_char(text, c)
if not text or not c then return false end
c = fmt_char[c] or c
Expand All @@ -36,6 +38,8 @@ local function get_text_after_cursor()
return suffix
end

-- get text after cursor and check if it has pair for char c
-- if present add it to text so it is there after replacement
function pattern.set_suffix(text, line_suffix)
for i = 1, #line_suffix do
local c = line_suffix:sub(i,i)
Expand Down

0 comments on commit be98d3a

Please sign in to comment.