summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2018-12-24 18:19:30 +0800
committer最萌小汐 <sumneko@hotmail.com>2018-12-24 18:19:30 +0800
commitb7782be38cb8f9d3f94b615976cdfed00e4c183f (patch)
tree34a963d3aa899e297702ed08de06a8a3eca20acf /server/src
parentf8390c046278c3b34076d4e250e247a66ecae923 (diff)
downloadlua-language-server-b7782be38cb8f9d3f94b615976cdfed00e4c183f.zip
提示require的候选目标
Diffstat (limited to 'server/src')
-rw-r--r--server/src/matcher/completion.lua33
-rw-r--r--server/src/method/initialize.lua11
-rw-r--r--server/src/workspace.lua13
3 files changed, 45 insertions, 12 deletions
diff --git a/server/src/matcher/completion.lua b/server/src/matcher/completion.lua
index 1ee06dbc..64ed9428 100644
--- a/server/src/matcher/completion.lua
+++ b/server/src/matcher/completion.lua
@@ -217,6 +217,25 @@ local function searchAsSuffix(result, callback)
end)
end
+local function searchAsArg(vm, inCall, inString, callback)
+ local special = inCall.func.lib and inCall.func.lib.special
+ if not special then
+ return
+ end
+ if special == 'require' then
+ if not vm.lsp or not vm.lsp.workspace then
+ return
+ end
+ local results = vm.lsp.workspace:matchPath(inString[1])
+ if not results then
+ return
+ end
+ for _, v in ipairs(results) do
+ callback(v, CompletionItemKind.Module)
+ end
+ end
+end
+
local function findClosePos(vm, pos)
local curDis = math.maxinteger
local parent = nil
@@ -268,13 +287,13 @@ local function isContainPos(obj, pos)
return false
end
-local function isInString(vm, pos)
+local function getString(vm, pos)
for _, source in ipairs(vm.results.strings) do
if isContainPos(source, pos) then
- return true
+ return source
end
end
- return false
+ return nil
end
local function findArgCount(args, pos)
@@ -319,14 +338,14 @@ end
return function (vm, pos)
local result, source = findResult(vm, pos)
- local inCall
+ local inCall, inString
if not result then
result, source = findClosePos(vm, pos)
if not result then
return nil
end
- if isInString(vm, pos) then
- do return nil end
+ inString = getString(vm, pos)
+ if inString then
local calls = findCall(vm, pos)
if not calls then
return nil
@@ -364,7 +383,7 @@ return function (vm, pos)
end
if inCall then
- searchAsArg(vm, pos, result, callback)
+ searchAsArg(vm, inCall, inString, callback)
else
if result.type == 'local' then
searchAsGlobal(vm, pos, result, callback)
diff --git a/server/src/method/initialize.lua b/server/src/method/initialize.lua
index 4bc3ff2e..3de9a89e 100644
--- a/server/src/method/initialize.lua
+++ b/server/src/method/initialize.lua
@@ -1,3 +1,12 @@
+local function allWords()
+ local str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
+ local list = {}
+ for c in str:gmatch '.' do
+ list[#list+1] = c
+ end
+ return table.unpack(list)
+end
+
return function (lsp)
lsp._inited = true
return {
@@ -26,7 +35,7 @@ return function (lsp)
-- 自动完成
completionProvider = {
resolveProvider = false,
- triggerCharacters = { '.', ':' },
+ triggerCharacters = { '.', ':', allWords() },
},
-- 工作目录
workspace = {
diff --git a/server/src/workspace.lua b/server/src/workspace.lua
index 76ee7b0b..862a163b 100644
--- a/server/src/workspace.lua
+++ b/server/src/workspace.lua
@@ -143,7 +143,7 @@ end
function mt:compileLuaPath()
for i, luapath in ipairs(self.luapath) do
- self.compiledpath[i] = '^' .. luapath:gsub('%?', '(.-)'):gsub('%.', '%%.') .. '$'
+ self.compiledpath[i] = '^' .. luapath:gsub('%.', '%%.'):gsub('%?', '(.-)') .. '$'
end
end
@@ -155,13 +155,14 @@ function mt:convertPathAsRequire(filename, start)
if not list then
list = {}
end
- list[#list+1] = str
+ list[#list+1] = str:gsub('/', '.')
end
end
return list
end
-function mt:matchPath(baseUri, str)
+function mt:matchPath(str)
+ str = str:lower()
local first = str:match '[^%.]+'
if not first then
return nil
@@ -169,7 +170,7 @@ function mt:matchPath(baseUri, str)
local rootLen = #self.root:string()
local results = {}
for filename in pairs(self.files) do
- local start = filename:find('/' .. first, true, rootLen + 1)
+ local start = filename:find('/' .. first, rootLen + 1, true)
if start then
local list = self:convertPathAsRequire(filename, start + 1)
if list then
@@ -182,6 +183,10 @@ function mt:matchPath(baseUri, str)
end
end
end
+ if #results == 0 then
+ return nil
+ end
+ table.sort(results)
return results
end