From 8f206cfde9e41902bcf3f5314d9684d7d6cdbb29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 27 Jan 2021 18:08:00 +0800 Subject: move cache into core --- script/core/completion.lua | 73 +++++++++++++++++++++++++++----------------- script/provider/provider.lua | 13 +------- 2 files changed, 46 insertions(+), 40 deletions(-) (limited to 'script') diff --git a/script/core/completion.lua b/script/core/completion.lua index 1a750923..e44e2227 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -593,8 +593,8 @@ local function checkTableField(ast, word, start, results) end) end -local function checkCommon(ast, word, text, offset, results) - local myUri = ast and ast.uri +local function checkCommon(myUri, word, text, offset, results) + results.enableCommon = true local used = {} for _, result in ipairs(results) do used[result.label] = true @@ -1184,7 +1184,7 @@ local function tryWord(ast, text, offset, results) if isInString(ast, offset) then if not hasSpace then if #results == 0 then - checkCommon(ast, word, text, offset, results) + checkCommon(ast.uri, word, text, offset, results) end end else @@ -1215,7 +1215,7 @@ local function tryWord(ast, text, offset, results) end end if not hasSpace then - checkCommon(ast, word, text, offset, results) + checkCommon(ast.uri, word, text, offset, results) end end end @@ -1687,7 +1687,7 @@ local function tryComment(ast, text, offset, results) if doc and doc.type ~= 'doc.comment' then return end - checkCommon(ast, word, text, offset, results) + checkCommon(ast.uri, word, text, offset, results) end local function makeCache(uri, offset, results) @@ -1698,7 +1698,7 @@ local function makeCache(uri, offset, results) end local text = files.getText(uri) local word = findWord(text, offset) - if not word or #word <= 2 then + if not word then cache.results = nil return end @@ -1714,23 +1714,41 @@ local function getCache(uri, offset) end local text = files.getText(uri) local word = findWord(text, offset) - if not word or #word <= 2 then + if not word then return nil end if word:sub(1, #cache.word) ~= cache.word then return nil end + + if cache.results.enableCommon then + local results = cache.results + for i = #results, 1, -1 do + local res = results[i] + if res.type == define.CompletionItemKind.Text then + results[i] = results[#results] + results[#results] = nil + end + end + checkCommon(nil, word, text, offset, results) + end + return cache.results end +local function clearCache() + local cache = workspace.getCache 'completion' + cache.results = nil +end + local function completion(uri, offset) - tracy.ZoneBeginN 'completion.getAst' + local results = getCache(uri, offset) + if results then + return results + end local ast = files.getAst(uri) - tracy.ZoneEnd() - tracy.ZoneBeginN 'completion.getText' local text = files.getText(uri) - tracy.ZoneEnd() - local results = {} + results = {} clearStack() tracy.ZoneBeginN 'completion' if ast then @@ -1747,39 +1765,38 @@ local function completion(uri, offset) else local word = findWord(text, offset) if word then - checkCommon(ast, word, text, offset, results) + checkCommon(nil, word, text, offset, results) end end tracy.ZoneEnd() if #results == 0 then + clearCache() return nil end + makeCache(uri, offset, results) return results end local function resolve(id) - return resolveStack(id) -end - -local function resolveCache(item) + local item = resolveStack(id) local cache = workspace.getCache 'completion' - if not cache.results then - return - end - for i, res in ipairs(cache.results) do - if res.data and res.data.id == item.data.id then - item.data.id = nil - cache.results[i] = item - break + if cache.results then + for _, res in ipairs(cache.results) do + if res.data and res.data.id == item.data.id then + for k, v in pairs(item) do + res[k] = v + end + res.data = nil + break + end end end + return item end return { completion = completion, resolve = resolve, - makeCache = makeCache, - getCache = getCache, - resolveCache = resolveCache, + clearCache = clearCache, } diff --git a/script/provider/provider.lua b/script/provider/provider.lua index 8ba5bb1c..47fe457f 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -361,26 +361,17 @@ proto.on('textDocument/completion', function (params) --log.debug('completion:', params.context and params.context.triggerKind, params.context and params.context.triggerCharacter) local uri = params.textDocument.uri if not files.exists(uri) then - core.makeCache(nil) return nil end - local offset = files.offset(uri, params.position) - local cache = core.getCache(uri, offset) - if cache then - return { - isIncomplete = false, - items = cache, - } - end await.setPriority(1000) local clock = os.clock() + local offset = files.offset(uri, params.position) local result = core.completion(uri, offset) local passed = os.clock() - clock if passed > 0.1 then log.warn(('Completion takes %.3f sec.'):format(passed)) end if not result then - core.makeCache(nil) return nil end local easy = false @@ -443,7 +434,6 @@ proto.on('textDocument/completion', function (params) end items[i] = item end - core.makeCache(uri, offset, items) return { isIncomplete = false, items = items, @@ -481,7 +471,6 @@ proto.on('completionItem/resolve', function (item) end return t end)() - core.resolveCache(item) return item end) -- cgit v1.2.3