summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/src/matcher/completion.lua33
-rw-r--r--server/src/method/initialize.lua11
-rw-r--r--server/src/workspace.lua13
-rw-r--r--server/test/crossfile/completion.lua20
-rw-r--r--server/test/crossfile/init.lua2
5 files changed, 65 insertions, 14 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
diff --git a/server/test/crossfile/completion.lua b/server/test/crossfile/completion.lua
index d98de0cc..37f1f5d7 100644
--- a/server/test/crossfile/completion.lua
+++ b/server/test/crossfile/completion.lua
@@ -1,4 +1,4 @@
-local service = require 'service'
+local service = require 'server'
local workspace = require 'workspace'
local fs = require 'bee.filesystem'
local matcher = require 'matcher'
@@ -129,3 +129,21 @@ TEST {
},
}
}
+
+TEST {
+ {
+ path = 'abc.lua',
+ content = '',
+ },
+ {
+ path = 'test.lua',
+ content = 'require "A@"',
+ main = true,
+ },
+ completion = {
+ {
+ label = 'abc',
+ kind = CompletionItemKind.Module,
+ },
+ }
+}
diff --git a/server/test/crossfile/init.lua b/server/test/crossfile/init.lua
index 488635c6..b74514af 100644
--- a/server/test/crossfile/init.lua
+++ b/server/test/crossfile/init.lua
@@ -1,3 +1,3 @@
require 'crossfile.definition'
require 'crossfile.hover'
---require 'crossfile.completion'
+require 'crossfile.completion'