Skip to content

Commit e6e7a51

Browse files
committed
completion: improve performance of workspace words
1 parent 3aae6e2 commit e6e7a51

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

script/core/completion.lua

+24-17
Original file line numberDiff line numberDiff line change
@@ -601,23 +601,30 @@ local function checkCommon(ast, word, text, offset, results)
601601
for _, data in ipairs(keyWordMap) do
602602
used[data[1]] = true
603603
end
604-
if config.config.completion.workspaceWord then
604+
if config.config.completion.workspaceWord and #word >= 2 then
605+
local myHead = word:sub(1, 2)
605606
for uri in files.eachFile() do
607+
if myUri and files.eq(myUri, uri) then
608+
goto CONTINUE
609+
end
606610
local cache = files.getCache(uri)
607611
if not cache.commonWords then
608612
cache.commonWords = {}
609613
local mark = {}
610614
for str in files.getText(uri):gmatch '([%a_][%w_]+)' do
611-
if not mark[str] then
615+
if #str >= 3 and not mark[str] then
612616
mark[str] = true
613-
cache.commonWords[#cache.commonWords+1] = str
617+
local head = str:sub(1, 2)
618+
if not cache.commonWords[head] then
619+
cache.commonWords[head] = {}
620+
end
621+
cache.commonWords[head][#cache.commonWords[head]+1] = str
614622
end
615623
end
616624
end
617-
for _, str in ipairs(cache.commonWords) do
618-
if #str >= 3
619-
and not used[str]
620-
and (str ~= word or (myUri and not files.eq(myUri, uri))) then
625+
for _, str in ipairs(cache.commonWords[myHead] or {}) do
626+
if not used[str]
627+
and str ~= word then
621628
used[str] = true
622629
if matchKey(word, str) then
623630
results[#results+1] = {
@@ -627,6 +634,7 @@ local function checkCommon(ast, word, text, offset, results)
627634
end
628635
end
629636
end
637+
::CONTINUE::
630638
end
631639
for uri in files.eachDll() do
632640
local words = files.getDllWords(uri) or {}
@@ -642,16 +650,15 @@ local function checkCommon(ast, word, text, offset, results)
642650
end
643651
end
644652
end
645-
else
646-
for str, pos in text:gmatch '([%a_][%w_]+)()' do
647-
if #str >= 3 and not used[str] and pos - 1 ~= offset then
648-
used[str] = true
649-
if matchKey(word, str) then
650-
results[#results+1] = {
651-
label = str,
652-
kind = define.CompletionItemKind.Text,
653-
}
654-
end
653+
end
654+
for str, pos in text:gmatch '([%a_][%w_]+)()' do
655+
if #str >= 3 and not used[str] and pos - 1 ~= offset then
656+
used[str] = true
657+
if matchKey(word, str) then
658+
results[#results+1] = {
659+
label = str,
660+
kind = define.CompletionItemKind.Text,
661+
}
655662
end
656663
end
657664
end

0 commit comments

Comments
 (0)