summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-01-27 16:24:02 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-01-27 16:24:02 +0800
commit219c802e4e6a67aa9c4a1b4a5bd7886f8181b7c4 (patch)
tree4acfbd7290555928768f866085c5567499b85f82 /script
parentb436b123d67e1c5aa3945da404109fbeb12bdb53 (diff)
downloadlua-language-server-219c802e4e6a67aa9c4a1b4a5bd7886f8181b7c4.zip
improve performance of completion
Diffstat (limited to 'script')
-rw-r--r--script/core/completion.lua54
-rw-r--r--script/provider/provider.lua20
-rw-r--r--script/vm/getGlobals.lua19
3 files changed, 78 insertions, 15 deletions
diff --git a/script/core/completion.lua b/script/core/completion.lua
index 00e5d8f7..1a750923 100644
--- a/script/core/completion.lua
+++ b/script/core/completion.lua
@@ -1690,6 +1690,39 @@ local function tryComment(ast, text, offset, results)
checkCommon(ast, word, text, offset, results)
end
+local function makeCache(uri, offset, results)
+ local cache = workspace.getCache 'completion'
+ if not uri then
+ cache.results = nil
+ return
+ end
+ local text = files.getText(uri)
+ local word = findWord(text, offset)
+ if not word or #word <= 2 then
+ cache.results = nil
+ return
+ end
+ cache.results = results
+ cache.offset = offset
+ cache.word = word
+end
+
+local function getCache(uri, offset)
+ local cache = workspace.getCache 'completion'
+ if not cache.results then
+ return nil
+ end
+ local text = files.getText(uri)
+ local word = findWord(text, offset)
+ if not word or #word <= 2 then
+ return nil
+ end
+ if word:sub(1, #cache.word) ~= cache.word then
+ return nil
+ end
+ return cache.results
+end
+
local function completion(uri, offset)
tracy.ZoneBeginN 'completion.getAst'
local ast = files.getAst(uri)
@@ -1729,7 +1762,24 @@ local function resolve(id)
return resolveStack(id)
end
+local function resolveCache(item)
+ 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
+ end
+ end
+end
+
return {
- completion = completion,
- resolve = resolve,
+ completion = completion,
+ resolve = resolve,
+ makeCache = makeCache,
+ getCache = getCache,
+ resolveCache = resolveCache,
}
diff --git a/script/provider/provider.lua b/script/provider/provider.lua
index 9939d845..7c7721a4 100644
--- a/script/provider/provider.lua
+++ b/script/provider/provider.lua
@@ -347,22 +347,31 @@ end)
proto.on('textDocument/completion', function (params)
--log.info(util.dump(params))
- local core = require 'core.completion'
+ local core = require 'core.completion'
--log.debug('textDocument/completion')
--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
@@ -418,7 +427,6 @@ proto.on('textDocument/completion', function (params)
else
easy = false
item.data = {
- version = files.globalVersion,
uri = uri,
id = res.id,
}
@@ -426,6 +434,7 @@ proto.on('textDocument/completion', function (params)
end
items[i] = item
end
+ core.makeCache(uri, offset, items)
return {
isIncomplete = false,
items = items,
@@ -437,12 +446,8 @@ proto.on('completionItem/resolve', function (item)
if not item.data then
return item
end
- local globalVersion = item.data.version
local id = item.data.id
local uri = item.data.uri
- if globalVersion ~= files.globalVersion then
- return item
- end
--await.setPriority(1000)
local resolved = core.resolve(id)
if not resolved then
@@ -467,6 +472,7 @@ proto.on('completionItem/resolve', function (item)
end
return t
end)()
+ core.resolveCache(item)
return item
end)
diff --git a/script/vm/getGlobals.lua b/script/vm/getGlobals.lua
index fcaffdf7..22e819f5 100644
--- a/script/vm/getGlobals.lua
+++ b/script/vm/getGlobals.lua
@@ -1,4 +1,5 @@
local guide = require 'parser.guide'
+local await = require "await"
---@type vm
local vm = require 'vm.vm'
local files = require 'files'
@@ -230,11 +231,17 @@ files.watch(function (ev, uri)
getGlobalSetsCache['*'] = nil
end
end
- for name in pairs(getGlobalsOfFile(uri)) do
- getGlobalCache[name] = nil
- end
- for name in pairs(getGlobalSetsOfFile(uri)) do
- getGlobalSetsCache[name] = nil
- end
+ await.call(function ()
+ local id = 'refreshNewGlobals:' .. uri
+ await.close(id)
+ await.setID(id)
+ await.sleep(1.0)
+ for name in pairs(getGlobalsOfFile(uri)) do
+ getGlobalCache[name] = nil
+ end
+ for name in pairs(getGlobalSetsOfFile(uri)) do
+ getGlobalSetsCache[name] = nil
+ end
+ end)
end
end)