summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server/src/method/initialized.lua3
-rw-r--r--server/src/workspace.lua63
-rw-r--r--server/test/crossfile/init.lua69
-rw-r--r--server/test/main.lua1
4 files changed, 103 insertions, 33 deletions
diff --git a/server/src/method/initialized.lua b/server/src/method/initialized.lua
index f79d035d..906fb3a3 100644
--- a/server/src/method/initialized.lua
+++ b/server/src/method/initialized.lua
@@ -6,7 +6,8 @@ return function (lsp)
if folders then
local folder = folders[1]
if folder then
- lsp.workspace = workspace(lsp, folder.name, folder.uri)
+ lsp.workspace = workspace(lsp, folder.name)
+ lsp.workspace:init(folder.uri)
end
end
end)
diff --git a/server/src/workspace.lua b/server/src/workspace.lua
index ff26ff53..42ce3343 100644
--- a/server/src/workspace.lua
+++ b/server/src/workspace.lua
@@ -1,7 +1,29 @@
local fs = require 'bee.filesystem'
local async = require 'async'
-local function uriDecode(uri)
+local function split(str, sep)
+ local t = {}
+ for s in str:gmatch('[^' .. sep .. ']+') do
+ t[#t+1] = s
+ end
+ return t
+end
+
+local function similarity(a, b)
+ local ta = split(a, '/\\')
+ local tb = split(b, '/\\')
+ for i = 1, #ta do
+ if ta[i] ~= tb[i] then
+ return i - 1
+ end
+ end
+ return #ta
+end
+
+local mt = {}
+mt.__index = mt
+
+function mt:uriDecode(uri)
if uri:sub(1, 8) ~= 'file:///' then
log.error('uri decode failed: ', uri)
return nil
@@ -24,7 +46,7 @@ local function uriDecode(uri)
return fs.absolute(path)
end
-local function uriEncode(path)
+function mt:uriEncode(path)
local names = {}
local cur = fs.absolute(path)
while true do
@@ -45,30 +67,8 @@ local function uriEncode(path)
return 'file:///' .. table.concat(names, '/')
end
-local function split(str, sep)
- local t = {}
- for s in str:gmatch('[^' .. sep .. ']+') do
- t[#t+1] = s
- end
- return t
-end
-
-local function similarity(a, b)
- local ta = split(a, '/\\')
- local tb = split(b, '/\\')
- for i = 1, #ta do
- if ta[i] ~= tb[i] then
- return i - 1
- end
- end
- return #ta
-end
-
-local mt = {}
-mt.__index = mt
-
function mt:init(rootUri)
- self.root = uriDecode(rootUri)
+ self.root = self:uriDecode(rootUri)
if not self.root then
return
end
@@ -94,7 +94,7 @@ function mt:init(rootUri)
for _, filename in ipairs(list) do
local path = fs.absolute(fs.path(filename))
local name = path:string():lower()
- self.files[name] = uriEncode(path)
+ self.files[name] = self:uriEncode(path)
end
self:reset()
end)
@@ -102,13 +102,13 @@ end
function mt:addFile(uri)
if uri:sub(-4) == '.lua' then
- local name = uriDecode(uri):string():lower()
+ local name = self:uriDecode(uri):string():lower()
self.files[name] = uri
end
end
function mt:removeFile(uri)
- local name = uriDecode(uri):string():lower()
+ local name = self:uriDecode(uri):string():lower()
self.files[name] = nil
end
@@ -137,7 +137,7 @@ function mt:findPath(baseUri, searchers)
end)
uri = results[1]
end
- self.lsp:readText(uri, uriDecode(uri))
+ self.lsp:readText(uri, self:uriDecode(uri))
return uri
end
@@ -179,12 +179,12 @@ function mt:reset()
end
function mt:relativePathByUri(uri)
- local path = uriDecode(uri)
+ local path = self:uriDecode(uri)
local relate = fs.relative(path, self.root)
return relate
end
-return function (lsp, name, uri)
+return function (lsp, name)
local workspace = setmetatable({
lsp = lsp,
name = name,
@@ -197,6 +197,5 @@ return function (lsp, name, uri)
'?/?.lua',
},
}, mt)
- workspace:init(uri)
return workspace
end
diff --git a/server/test/crossfile/init.lua b/server/test/crossfile/init.lua
new file mode 100644
index 00000000..e550f8d8
--- /dev/null
+++ b/server/test/crossfile/init.lua
@@ -0,0 +1,69 @@
+local service = require 'service'
+local workspace = require 'workspace'
+local fs = require 'bee.filesystem'
+local matcher = require 'matcher'
+
+rawset(_G, 'TEST', true)
+
+local function catch_target(script, sep)
+ local list = {}
+ local cur = 1
+ local cut = 0
+ while true do
+ local start, finish = script:find(('<%%%s.-%%%s>'):format(sep, sep), cur)
+ if not start then
+ break
+ end
+ list[#list+1] = { start - cut, finish - 4 - cut }
+ cur = finish + 1
+ cut = cut + 4
+ end
+ local new_script = script:gsub(('<%%%s(.-)%%%s>'):format(sep, sep), '%1')
+ return new_script, list
+end
+
+function TEST(data)
+ local lsp = service()
+ local ws = workspace(lsp, 'test')
+ lsp.workspace = ws
+
+ local targetScript, targetList = catch_target(data[1].content, '!')
+ local targetUri = ws:uriEncode(fs.path(data[1].path))
+ if data[1].target then
+ targetList = data[1].target
+ else
+ targetList = targetList[1]
+ end
+
+ local sourceScript, sourceList = catch_target(data[2].content, '?')
+ local sourceUri = ws:uriEncode(fs.path(data[2].path))
+
+ lsp:saveText(targetUri, 1, targetScript)
+ lsp:saveText(sourceUri, 1, sourceScript)
+ ws:addFile(targetUri)
+ ws:addFile(sourceUri)
+ lsp:compileVM(targetUri)
+ lsp:compileVM(sourceUri)
+
+ local sourceVM = lsp:loadVM(sourceUri)
+ assert(sourceVM)
+ local sourcePos = (sourceList[1][1] + sourceList[1][2]) // 2
+ local positions = matcher.definition(sourceVM, sourcePos)
+ assert(positions)
+ local start, finish, valueUri = positions[1][1], positions[1][2], positions[1][3]
+ assert(valueUri == targetUri)
+ assert(start == targetList[1])
+ assert(finish == targetList[2])
+end
+
+TEST {
+ {
+ path = 'a.lua',
+ content = '',
+ target = {0, 0},
+ },
+ {
+ path = 'b.lua',
+ content = 'require <?"a"?>',
+ },
+}
diff --git a/server/test/main.lua b/server/test/main.lua
index da005d51..02d55665 100644
--- a/server/test/main.lua
+++ b/server/test/main.lua
@@ -33,6 +33,7 @@ local function main()
test 'hover'
test 'completion'
test 'signature'
+ test 'crossfile'
print('测试完成')
end