From 71d24c50b9b44dbd1b14958e7941fb7c9a1ab11c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 21 Jan 2019 17:01:12 +0800 Subject: =?UTF-8?q?=E4=BF=AE=E6=AD=A3bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/method/textDocument/completion.lua | 49 +++++++++++++++++---------- 1 file changed, 31 insertions(+), 18 deletions(-) (limited to 'server/src/method/textDocument') diff --git a/server/src/method/textDocument/completion.lua b/server/src/method/textDocument/completion.lua index 2e1eb5f0..8af4db95 100644 --- a/server/src/method/textDocument/completion.lua +++ b/server/src/method/textDocument/completion.lua @@ -17,48 +17,60 @@ local function posToRange(lines, start, finish) end local function findStartPos(pos, buf) - local res = pos for i = pos-1, 1, -1 do local c = buf:sub(i, i) if c:find '%a' then - res = i + goto CONTINUE end if c == '.' or c == ':' then - break + return nil + end + do return i + 1 end + ::CONTINUE:: + end + return 1 +end + +local function findWord(position, text) + for i = position-1, 1, -1 do + local c = text:sub(i, i) + if not c:find '%w' then + return text:sub(i+1, position) end end - return res + return text:sub(1, position) end return function (lsp, params) local uri = params.textDocument.uri - local vm, lines, buf = lsp:getVM(uri) - if not vm then - vm, lines, buf = lsp:loadVM(uri) - if not vm then - return nil - end + local text = lsp:getText(uri) + if not text then + return nil end + + local lines = parser:lines(text, 'utf8') -- lua是从1开始的,因此都要+1 local position = lines:position(params.position.line + 1, params.position.character + 1) + local word = findWord(position, text) + local startPos = findStartPos(position, text) - local startPos = findStartPos(position, buf) - if not startPos then - vm, lines, buf = lsp:loadVM(uri) + local vm = lsp:getVM(uri) + if not vm or not startPos then + vm = lsp:loadVM(uri) if not vm then return nil end end + startPos = startPos or position - local items = core.completion(vm, startPos or position, buf) + local items = core.completion(vm, startPos, word) if not items or #items == 0 then - vm, lines, buf = lsp:loadVM(uri) + vm = 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) + startPos = startPos or position + items = core.completion(vm, startPos, word) if not items or #items == 0 then return nil end @@ -71,6 +83,7 @@ return function (lsp, params) item.textEdit.finish = nil end end + local response = { isIncomplete = false, items = items, -- cgit v1.2.3