diff --git a/lua/copilot_cmp/completion_functions.lua b/lua/copilot_cmp/completion_functions.lua index 54d1494..c7ee231 100644 --- a/lua/copilot_cmp/completion_functions.lua +++ b/lua/copilot_cmp/completion_functions.lua @@ -1,8 +1,6 @@ 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 = { @@ -10,68 +8,23 @@ local methods = { } } -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 diff --git a/lua/copilot_cmp/format.lua b/lua/copilot_cmp/format.lua index b330cef..1e3b5ba 100644 --- a/lua/copilot_cmp/format.lua +++ b/lua/copilot_cmp/format.lua @@ -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) @@ -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 diff --git a/lua/copilot_cmp/pattern.lua b/lua/copilot_cmp/pattern.lua index c179453..f9ad420 100644 --- a/lua/copilot_cmp/pattern.lua +++ b/lua/copilot_cmp/pattern.lua @@ -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 @@ -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)