Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

Commit 994450a

Browse files
committed
rework: introducing cache mechanism for completion items
fix some stuff
1 parent 9ed834b commit 994450a

File tree

3 files changed

+70
-41
lines changed

3 files changed

+70
-41
lines changed

lua/completion/complete.lua

Lines changed: 63 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ local vim = vim
22
local api = vim.api
33
local util = require 'completion.util'
44
local ins = require 'completion.source.ins_complete'
5+
local match = require'completion.matching'
56

67
local M = {}
78

9+
local cache_complete_items = {}
10+
811
local function checkCallback(callback_array)
912
for _,val in ipairs(callback_array) do
1013
if not val then return false end
@@ -18,11 +21,15 @@ end
1821
local function getCompletionItems(items_array, prefix)
1922
local complete_items = {}
2023
for _,func in ipairs(items_array) do
21-
vim.list_extend(complete_items, func(prefix, util.fuzzy_score))
24+
vim.list_extend(complete_items, func(prefix))
2225
end
2326
return complete_items
2427
end
2528

29+
M.clearCache = function()
30+
cache_complete_items = {}
31+
end
32+
2633
-- perform completion
2734
M.performComplete = function(complete_source, complete_items_map, manager, bufnr, prefix, textMatch)
2835

@@ -31,48 +38,67 @@ M.performComplete = function(complete_source, complete_items_map, manager, bufnr
3138
-- ins-complete source
3239
ins.triggerCompletion(manager, complete_source.mode)
3340
elseif vim.fn.has_key(complete_source, "complete_items") > 0 then
34-
-- use callback_array to handle async behavior
35-
local callback_array = {}
36-
local items_array = {}
37-
-- collect getCompleteItems function of current completion source
38-
for _, item in ipairs(complete_source.complete_items) do
39-
local complete_items = complete_items_map[item]
40-
if complete_items ~= nil then
41-
if complete_items.callback == nil then
42-
table.insert(callback_array, true)
43-
else
44-
table.insert(callback_array, complete_items.callback)
45-
complete_items.trigger(prefix, textMatch, bufnr, manager)
41+
if #cache_complete_items == 0 then
42+
-- use callback_array to handle async behavior
43+
local callback_array = {}
44+
local items_array = {}
45+
-- collect getCompleteItems function of current completion source
46+
for _, item in ipairs(complete_source.complete_items) do
47+
local complete_items = complete_items_map[item]
48+
if complete_items ~= nil then
49+
if complete_items.callback == nil then
50+
table.insert(callback_array, true)
51+
else
52+
table.insert(callback_array, complete_items.callback)
53+
complete_items.trigger(prefix, textMatch, bufnr, manager)
54+
end
55+
table.insert(items_array, complete_items.item)
4656
end
47-
table.insert(items_array, complete_items.item)
4857
end
49-
end
5058

51-
local timer = vim.loop.new_timer()
52-
timer:start(20, 50, vim.schedule_wrap(function()
53-
if manager.insertChar == true and not timer:is_closing() then
54-
timer:stop()
55-
timer:close()
56-
end
57-
-- only perform complete when callback_array are all true
58-
if checkCallback(callback_array) == true and timer:is_closing() == false then
59-
if api.nvim_get_mode()['mode'] == 'i' or api.nvim_get_mode()['mode'] == 'ic' then
60-
local items = getCompletionItems(items_array, prefix)
61-
if vim.g.completion_sorting ~= "none" then
62-
util.sort_completion_items(items)
63-
end
64-
if #items ~= 0 then
65-
-- reset insertChar and handle auto changing source
66-
vim.fn.complete(textMatch+1, items)
67-
manager.changeSource = false
68-
else
69-
manager.changeSource = true
59+
local timer = vim.loop.new_timer()
60+
timer:start(20, 50, vim.schedule_wrap(function()
61+
if manager.insertChar == true and not timer:is_closing() then
62+
timer:stop()
63+
timer:close()
64+
end
65+
-- only perform complete when callback_array are all true
66+
if checkCallback(callback_array) == true and timer:is_closing() == false then
67+
if api.nvim_get_mode()['mode'] == 'i' or api.nvim_get_mode()['mode'] == 'ic' then
68+
local items = getCompletionItems(items_array, prefix)
69+
if vim.g.completion_sorting ~= "none" then
70+
util.sort_completion_items(items)
71+
end
72+
if #items ~= 0 then
73+
-- reset insertChar and handle auto changing source
74+
cache_complete_items = items
75+
vim.fn.complete(textMatch+1, items)
76+
manager.changeSource = false
77+
else
78+
manager.changeSource = true
79+
end
7080
end
81+
timer:stop()
82+
timer:close()
83+
end
84+
end))
85+
else
86+
if api.nvim_get_mode()['mode'] == 'i' or api.nvim_get_mode()['mode'] == 'ic' then
87+
local items = {}
88+
for _, item in ipairs(cache_complete_items) do
89+
match.matching(items, prefix, item)
90+
end
91+
if #items ~= 0 then
92+
-- reset insertChar and handle auto changing source
93+
cache_complete_items = items
94+
vim.fn.complete(textMatch+1, items)
95+
manager.changeSource = false
96+
else
97+
cache_complete_items = {}
98+
manager.changeSource = true
7199
end
72-
timer:stop()
73-
timer:close()
74100
end
75-
end))
101+
end
76102
end
77103
end
78104

lua/completion/source.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ local triggerCurrentCompletion = function(manager, bufnr, line_to_cursor, prefix
107107
return
108108
end
109109
if triggered then
110+
complete.clearCache()
110111
M.chain_complete_index = 1
111112
end
112113

@@ -127,6 +128,7 @@ end
127128

128129
-- Activate when manually triggered completion or manually changing completion source
129130
function M.triggerCompletion(force, manager)
131+
complete.clearCache()
130132
if force then
131133
M.chain_complete_index = 1
132134
end
@@ -148,6 +150,8 @@ function M.autoCompletion(manager)
148150
-- reset completion when deleting character in insert mode
149151
if #prefix < M.prefixLength and vim.fn.pumvisible() == 0 then
150152
M.chain_complete_index = 1
153+
-- not sure if I should clear cache here
154+
complete.clearCache()
151155
-- api.nvim_input("<c-g><c-g>")
152156
if vim.g.completion_trigger_on_delete == 1 then
153157
M.triggerCompletion(false, manager)
@@ -156,8 +160,9 @@ function M.autoCompletion(manager)
156160
end
157161
M.prefixLength = #prefix
158162

159-
-- force reset chain completion if entering a new word
163+
-- force reset chain completion and clear completion cache if entering a new word
160164
if (#prefix < length) then
165+
complete.clearCache()
161166
M.chain_complete_index = 1
162167
M.stop_complete = false
163168
manager.changeSource = false

lua/completion/source/lsp.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ local function text_document_completion_list_to_complete_items(result, prefix)
5353
info = documentation
5454
elseif type(documentation) == 'table' and type(documentation.value) == 'string' then
5555
info = documentation.value
56-
-- else
57-
-- TODO(ashkan) Validation handling here?
5856
end
5957
end
6058
item.info = info
@@ -95,7 +93,7 @@ M.triggerFunction = function(prefix, _, bufnr, _)
9593
M.callback = true
9694
return
9795
end
98-
local matches = text_document_completion_list_to_complete_items(result, prefix, util.fuzzy_score)
96+
local matches = text_document_completion_list_to_complete_items(result, prefix)
9997
M.items = matches
10098
M.callback = true
10199
end)

0 commit comments

Comments
 (0)