diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-01-21 14:33:17 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-01-21 14:33:17 +0800 |
commit | 2d3d75258b551673e2d8466cd94e4301451416a8 (patch) | |
tree | 0e1d6007ac12ccc4df1337f0c7a9061a24d2ef54 /server/src | |
parent | 6dac6c8725c002b34f0a31058a1aef9e5594d921 (diff) | |
download | lua-language-server-2d3d75258b551673e2d8466cd94e4301451416a8.zip |
优化自动完成的性能
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/core/completion.lua | 16 | ||||
-rw-r--r-- | server/src/method/textDocument/completion.lua | 48 | ||||
-rw-r--r-- | server/src/service.lua | 4 |
3 files changed, 53 insertions, 15 deletions
diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua index 28a1f354..b8ccde33 100644 --- a/server/src/core/completion.lua +++ b/server/src/core/completion.lua @@ -464,16 +464,16 @@ local function searchInResult(result, source, vm, pos, callback) end end -local function searchAllWords(result, vm, callback) - if result.key == '' then +local function searchAllWords(text, vm, callback) + if text == '' then return end - if type(result.key) ~= 'string' then + if type(text) ~= 'string' then return end for source in pairs(vm.results.sources) do if source.type == 'name' then - if result.key ~= source[1] and matchKey(result.key, source[1]) then + if text ~= source[1] and matchKey(text, source[1]) then callback(source[1], CompletionItemKind.Text) end end @@ -534,7 +534,7 @@ local function clearList(list, source) end end -return function (vm, pos) +return function (vm, pos, buf) local list = {} local callback = makeList(list) local inCall = findCall(vm, pos) @@ -551,8 +551,12 @@ return function (vm, pos) if result then callback = makeList(list, source) searchInResult(result, source, vm, pos, callback) - searchAllWords(result, vm, callback) + searchAllWords(result.key, vm, callback) clearList(list, source) + else + if buf then + searchAllWords(buf:sub(pos-1, pos-1), vm, callback) + end end end if #list == 0 then diff --git a/server/src/method/textDocument/completion.lua b/server/src/method/textDocument/completion.lua index dd348a6f..2891b4b2 100644 --- a/server/src/method/textDocument/completion.lua +++ b/server/src/method/textDocument/completion.lua @@ -1,4 +1,5 @@ local core = require 'core' +local parser = require 'parser' local function posToRange(lines, start, finish) local start_row, start_col = lines:rowcol(start) @@ -15,20 +16,53 @@ local function posToRange(lines, start, finish) } end +local function findStartPos(pos, buf) + for i = pos-1, 1, -1 do + local c = buf:sub(i, i) + if c:find '%a' then + goto CONTINUE + end + if c == '.' then + return nil + end + do return i + 1 end + ::CONTINUE:: + end + return pos +end + return function (lsp, params) local uri = params.textDocument.uri - local vm, lines = lsp:loadVM(uri) + local vm, lines, buf = lsp:getVM(uri) if not vm then - return nil + vm, lines, buf = lsp:loadVM(uri) + if not vm then + return nil + end end -- lua是从1开始的,因此都要+1 local position = lines:position(params.position.line + 1, params.position.character + 1) - local items = core.completion(vm, position) - if not items then - return nil + + local startPos = findStartPos(position, buf) + if not startPos then + vm, lines, buf = lsp:loadVM(uri) + if not vm then + return nil + end end - if #items == 0 then - return nil + + local items = core.completion(vm, startPos or position, buf) + if not items or #items == 0 then + vm, lines, buf = lsp:loadVM(uri) + if not vm then + return nil + end + position = lines:position(params.position.line + 1, params.position.character + 1) + startPos = findStartPos(position, buf) + items = core.completion(vm, startPos or position, buf) + if not items or #items == 0 then + return nil + end end for i, item in ipairs(items) do item.sortText = ('%04d'):format(i) diff --git a/server/src/service.lua b/server/src/service.lua index 1aa13417..96eafc1e 100644 --- a/server/src/service.lua +++ b/server/src/service.lua @@ -263,7 +263,7 @@ function mt:loadVM(uri) return nil end self:compileVM(uri) - return obj.vm, obj.lines + return obj.vm, obj.lines, obj.text end function mt:_markCompiled(uri) @@ -363,7 +363,7 @@ function mt:getVM(uri) if not obj then return nil end - return obj.vm + return obj.vm, obj.lines, obj.text end function mt:getAstErrors(uri) |