summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server/src/core/references.lua60
-rw-r--r--server/test/main.lua1
-rw-r--r--server/test/references/init.lua73
3 files changed, 102 insertions, 32 deletions
diff --git a/server/src/core/references.lua b/server/src/core/references.lua
index 1d1915f0..8a94c69c 100644
--- a/server/src/core/references.lua
+++ b/server/src/core/references.lua
@@ -1,66 +1,62 @@
local findSource = require 'core.find_source'
-local function parseResult(vm, result, declarat, callback)
- local tp = result.type
- if tp == 'local' then
- vm:eachInfo(result, function (info)
- if info.source.uri == '' or not info.source.uri then
- return
- end
+local function parseResult(vm, source, declarat, callback)
+ if source:bindLabel() then
+ source:bindLabel():eachInfo(function (info)
if declarat or info.type == 'get' then
callback(info.source)
end
end)
- result.value:eachInfo(function (info)
- if info.source.uri == '' or not info.source.uri then
- return
- end
+ return
+ end
+ if source:bindLocal() then
+ local loc = source:bindLocal()
+ loc:eachInfo(function (info)
if declarat or info.type == 'get' then
callback(info.source)
end
end)
- elseif tp == 'field' then
- vm:eachInfo(result, function (info)
- if info.source.uri == '' or not info.source.uri then
- return
- end
+ loc:getValue():eachInfo(function (info)
if declarat or info.type == 'get' then
callback(info.source)
end
end)
- result.value:eachInfo(function (info)
- if info.source.uri == '' or not info.source.uri then
- return
- end
+ return
+ end
+ if source:bindValue() then
+ source:bindValue():eachInfo(function (info)
if declarat or info.type == 'get' then
callback(info.source)
end
end)
- elseif tp == 'label' then
- vm:eachInfo(result, function (info)
- if declarat or info.type == 'goto' then
- callback(info.source)
+ local parent = source:get 'parent'
+ parent:eachInfo(function (info)
+ if info[1] == source[1] then
+ if (declarat and info.type == 'set child') or info.type == 'get child' then
+ callback(info.source)
+ end
end
end)
+ return
end
end
return function (vm, pos, declarat)
- local result = findSource(vm, pos)
- if not result then
+ local source = findSource(vm, pos)
+ if not source then
return nil
end
local positions = {}
local mark = {}
- parseResult(vm, result, declarat, function (source)
- if mark[source] then
+ parseResult(vm, source, declarat, function (src)
+ if mark[src] then
return
end
- mark[source] = true
+ mark[src] = true
positions[#positions+1] = {
- source.start,
- source.finish,
- source.uri,
+ src.start,
+ src.finish,
+ src.uri,
}
end)
return positions
diff --git a/server/test/main.lua b/server/test/main.lua
index e6fdcbe2..f6ba8466 100644
--- a/server/test/main.lua
+++ b/server/test/main.lua
@@ -28,6 +28,7 @@ local function main()
test 'full'
test 'definition'
test 'rename'
+ test 'references'
test 'diagnostics'
test 'type_inference'
test 'find_lib'
diff --git a/server/test/references/init.lua b/server/test/references/init.lua
new file mode 100644
index 00000000..2699fb70
--- /dev/null
+++ b/server/test/references/init.lua
@@ -0,0 +1,73 @@
+local core = require 'core'
+local parser = require 'parser'
+local buildVM = require 'vm'
+
+local function catch_target(script)
+ local list = {}
+ local cur = 1
+ while true do
+ local start, finish = script:find('<[!?].-[!?]>', cur)
+ if not start then
+ break
+ end
+ list[#list+1] = { start + 2, finish - 2 }
+ cur = finish + 1
+ end
+ return list
+end
+
+local function founded(targets, results)
+ if #targets ~= #results then
+ return false
+ end
+ for _, target in ipairs(targets) do
+ for _, result in ipairs(results) do
+ if target[1] == result[1] and target[2] == result[2] then
+ goto NEXT
+ end
+ end
+ do return false end
+ ::NEXT::
+ end
+ return true
+end
+
+function TEST(script)
+ local target = catch_target(script)
+ local start = script:find('<?', 1, true)
+ local finish = script:find('?>', 1, true)
+ local pos = (start + finish) // 2 + 1
+ local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ')
+ local ast = parser:ast(new_script)
+ assert(ast)
+ local vm = buildVM(ast)
+ assert(vm)
+
+ local positions = core.references(vm, pos, true)
+ if positions then
+ assert(founded(target, positions))
+ else
+ assert(#target == 0)
+ end
+end
+
+TEST [[
+local <!a!> = 1
+<?a?> = <?a?>
+]]
+
+TEST [[
+t.<!a!> = 1
+t.<?a?> = t.<?a?>
+]]
+
+TEST [[
+:: <!LABEL!> ::
+goto <?LABEL?>
+]]
+
+TEST [[
+local a = 1
+local <!a!> = 1
+<?a?> = <?a?>
+]]