Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve for source providing huge list of items #1980

Merged
merged 4 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ jobs:
neovim: true

- name: Setup lua
uses: leafo/gh-actions-lua@v8
uses: leafo/gh-actions-lua@v10
with:
luaVersion: "luajit-2.1.0-beta3"
luaVersion: "luajit-openresty"

- name: Setup luarocks
uses: leafo/gh-actions-luarocks@v4
Expand Down
10 changes: 5 additions & 5 deletions lua/cmp/config/compare.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local compare = {}
---offset: Entries with smaller offset will be ranked higher.
---@type cmp.ComparatorFunction
compare.offset = function(entry1, entry2)
local diff = entry1:get_offset() - entry2:get_offset()
local diff = entry1.offset - entry2.offset
if diff < 0 then
return true
elseif diff > 0 then
Expand Down Expand Up @@ -180,8 +180,8 @@ compare.locality = setmetatable({
}, {
---@type fun(self: table, entry1: cmp.Entry, entry2: cmp.Entry): boolean|nil
__call = function(self, entry1, entry2)
local local1 = self.locality_map[entry1:get_word()]
local local2 = self.locality_map[entry2:get_word()]
local local1 = self.locality_map[entry1.word]
local local2 = self.locality_map[entry2.word]
if local1 ~= local2 then
if local1 == nil then
return false
Expand Down Expand Up @@ -255,8 +255,8 @@ compare.scopes = setmetatable({
}, {
---@type fun(self: table, entry1: cmp.Entry, entry2: cmp.Entry): boolean|nil
__call = function(self, entry1, entry2)
local local1 = self.scopes_map[entry1:get_word()]
local local2 = self.scopes_map[entry2:get_word()]
local local1 = self.scopes_map[entry1.word]
local local2 = self.scopes_map[entry2.word]
if local1 ~= local2 then
if local1 == nil then
return false
Expand Down
34 changes: 17 additions & 17 deletions lua/cmp/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ core.on_keymap = function(self, keys, fallback)
commit_character = chars,
}, function()
local ctx = self:get_context()
local word = e:get_word()
local word = e.word
if string.sub(ctx.cursor_before_line, -#word, ctx.cursor.col - 1) == word and is_printable then
fallback()
else
Expand Down Expand Up @@ -358,7 +358,7 @@ core.confirm = function(self, e, option, callback)
end
e.confirmed = true

debug.log('entry.confirm', e:get_completion_item())
debug.log('entry.confirm', e.completion_item)

async.sync(function(done)
e:resolve(done)
Expand All @@ -374,8 +374,8 @@ core.confirm = function(self, e, option, callback)
-- Emulate `<C-y>` behavior to save `.` register.
local ctx = context.new()
local keys = {}
table.insert(keys, keymap.backspace(ctx.cursor_before_line:sub(e:get_offset())))
table.insert(keys, e:get_word())
table.insert(keys, keymap.backspace(ctx.cursor_before_line:sub(e.offset)))
table.insert(keys, e.word)
table.insert(keys, keymap.undobreak())
feedkeys.call(table.concat(keys, ''), 'in')
end)
Expand All @@ -384,26 +384,26 @@ core.confirm = function(self, e, option, callback)
local ctx = context.new()
if api.is_cmdline_mode() then
local keys = {}
table.insert(keys, keymap.backspace(ctx.cursor_before_line:sub(e:get_offset())))
table.insert(keys, string.sub(e.context.cursor_before_line, e:get_offset()))
table.insert(keys, keymap.backspace(ctx.cursor_before_line:sub(e.offset)))
table.insert(keys, string.sub(e.context.cursor_before_line, e.offset))
feedkeys.call(table.concat(keys, ''), 'in')
else
vim.cmd([[silent! undojoin]])
-- This logic must be used nvim_buf_set_text.
-- If not used, the snippet engine's placeholder wil be broken.
vim.api.nvim_buf_set_text(0, e.context.cursor.row - 1, e:get_offset() - 1, ctx.cursor.row - 1, ctx.cursor.col - 1, {
e.context.cursor_before_line:sub(e:get_offset()),
vim.api.nvim_buf_set_text(0, e.context.cursor.row - 1, e.offset - 1, ctx.cursor.row - 1, ctx.cursor.col - 1, {
e.context.cursor_before_line:sub(e.offset),
})
vim.api.nvim_win_set_cursor(0, { e.context.cursor.row, e.context.cursor.col - 1 })
end
end)
feedkeys.call('', 'n', function()
-- Apply additionalTextEdits.
local ctx = context.new()
if #(e:get_completion_item().additionalTextEdits or {}) == 0 then
if #(e.completion_item.additionalTextEdits or {}) == 0 then
e:resolve(function()
local new = context.new()
local text_edits = e:get_completion_item().additionalTextEdits or {}
local text_edits = e.completion_item.additionalTextEdits or {}
if #text_edits == 0 then
return
end
Expand All @@ -428,12 +428,12 @@ core.confirm = function(self, e, option, callback)
end)
else
vim.cmd([[silent! undojoin]])
vim.lsp.util.apply_text_edits(e:get_completion_item().additionalTextEdits, ctx.bufnr, e.source:get_position_encoding_kind())
vim.lsp.util.apply_text_edits(e.completion_item.additionalTextEdits, ctx.bufnr, e.source:get_position_encoding_kind())
end
end)
feedkeys.call('', 'n', function()
local ctx = context.new()
local completion_item = misc.copy(e:get_completion_item())
local completion_item = misc.copy(e.completion_item)
if not completion_item.textEdit then
completion_item.textEdit = {}
local insertText = completion_item.insertText
Expand All @@ -444,9 +444,9 @@ core.confirm = function(self, e, option, callback)
end
local behavior = option.behavior or config.get().confirmation.default_behavior
if behavior == types.cmp.ConfirmBehavior.Replace then
completion_item.textEdit.range = e:get_replace_range()
completion_item.textEdit.range = e.replace_range
else
completion_item.textEdit.range = e:get_insert_range()
completion_item.textEdit.range = e.insert_range
end

local diff_before = math.max(0, e.context.cursor.col - (completion_item.textEdit.range.start.character + 1))
Expand All @@ -460,15 +460,15 @@ core.confirm = function(self, e, option, callback)
if false then
--To use complex expansion debug.
vim.print({ -- luacheck: ignore
item = e:get_completion_item(),
item = e.completion_item,
diff_before = diff_before,
diff_after = diff_after,
new_text = new_text,
text_edit_new_text = completion_item.textEdit.newText,
range_start = completion_item.textEdit.range.start.character,
range_end = completion_item.textEdit.range['end'].character,
original_range_start = e:get_completion_item().textEdit.range.start.character,
original_range_end = e:get_completion_item().textEdit.range['end'].character,
original_range_start = e.completion_item.textEdit.range.start.character,
original_range_end = e.completion_item.textEdit.range['end'].character,
cursor_line = ctx.cursor_line,
cursor_col0 = ctx.cursor.col - 1,
})
Expand Down
Loading
Loading