summaryrefslogtreecommitdiff
path: root/server/src/method/textDocument
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-01-21 14:33:17 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-01-21 14:33:17 +0800
commit2d3d75258b551673e2d8466cd94e4301451416a8 (patch)
tree0e1d6007ac12ccc4df1337f0c7a9061a24d2ef54 /server/src/method/textDocument
parent6dac6c8725c002b34f0a31058a1aef9e5594d921 (diff)
downloadlua-language-server-2d3d75258b551673e2d8466cd94e4301451416a8.zip
优化自动完成的性能
Diffstat (limited to 'server/src/method/textDocument')
-rw-r--r--server/src/method/textDocument/completion.lua48
1 files changed, 41 insertions, 7 deletions
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)