From 2d3d75258b551673e2d8466cd94e4301451416a8 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 14:33:17 +0800 Subject: =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=87=AA=E5=8A=A8=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E7=9A=84=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/core/completion.lua | 16 +++++---- server/src/method/textDocument/completion.lua | 48 +++++++++++++++++++++++---- server/src/service.lua | 4 +-- server/test/completion/init.lua | 4 +-- server/test/crossfile/completion.lua | 2 ++ 5 files changed, 57 insertions(+), 17 deletions(-) (limited to 'server') 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) diff --git a/server/test/completion/init.lua b/server/test/completion/init.lua index e34789bd..e636e4f3 100644 --- a/server/test/completion/init.lua +++ b/server/test/completion/init.lua @@ -247,7 +247,7 @@ t:@ label = 'b', kind = CompletionItemKind.Method, documentation = EXISTS, - } + }, } TEST [[ @@ -433,7 +433,7 @@ self.results.list[#@] finish = 21, newText = 'self.results.list+1] = ', }, - } + }, } TEST [[ diff --git a/server/test/crossfile/completion.lua b/server/test/crossfile/completion.lua index 6976fd7f..82a1fbc0 100644 --- a/server/test/crossfile/completion.lua +++ b/server/test/crossfile/completion.lua @@ -68,6 +68,7 @@ function TEST(data) ws.root = ROOT local mainUri + local mainBuf local pos for _, info in ipairs(data) do local uri = ws:uriEncode(fs.path(info.path)) @@ -76,6 +77,7 @@ function TEST(data) pos = script:find('@', 1, true) script = script:gsub('@', '') mainUri = uri + mainBuf = script end lsp:saveText(uri, 1, script) ws:addFile(uri) -- cgit v1.2.3