summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/vm/def.lua4
-rw-r--r--script/vm/ref.lua124
-rw-r--r--test/references/common.lua30
-rw-r--r--test/references/init.lua4
4 files changed, 75 insertions, 87 deletions
diff --git a/script/vm/def.lua b/script/vm/def.lua
index 9c4ff376..67ca002e 100644
--- a/script/vm/def.lua
+++ b/script/vm/def.lua
@@ -46,7 +46,9 @@ simpleMap = util.switch()
: case 'field'
: call(function (source, pushResult)
local parent = source.parent
- simpleMap[parent.type](parent, pushResult)
+ if parent.type ~= 'tablefield' then
+ simpleMap[parent.type](parent, pushResult)
+ end
end)
: case 'setfield'
: case 'getfield'
diff --git a/script/vm/ref.lua b/script/vm/ref.lua
index be709950..9705094c 100644
--- a/script/vm/ref.lua
+++ b/script/vm/ref.lua
@@ -6,6 +6,7 @@ local guide = require 'parser.guide'
local localID = require 'vm.local-id'
local globalMgr = require 'vm.global-manager'
local nodeMgr = require 'vm.node'
+local files = require 'files'
local simpleMap
@@ -13,6 +14,7 @@ local function searchGetLocal(source, node, pushResult)
local key = guide.getKeyName(source)
for _, ref in ipairs(node.node.ref) do
if ref.type == 'getlocal'
+ and not guide.isSet(ref.next)
and guide.getKeyName(ref.next) == key then
pushResult(ref.next)
end
@@ -46,7 +48,9 @@ simpleMap = util.switch()
: case 'field'
: call(function (source, pushResult)
local parent = source.parent
- simpleMap[parent.type](parent, pushResult)
+ if parent.type ~= 'tablefield' then
+ simpleMap[parent.type](parent, pushResult)
+ end
end)
: case 'setfield'
: case 'getfield'
@@ -83,55 +87,56 @@ simpleMap = util.switch()
end)
: getMap()
-local searchFieldMap = util.switch()
- : case 'table'
- : call(function (node, key, pushResult)
- for _, field in ipairs(node) do
- if field.type == 'tablefield'
- or field.type == 'tableindex' then
- if guide.getKeyName(field) == key then
- pushResult(field)
- end
+local function searchField(source, pushResult)
+ local key = guide.getKeyName(source)
+
+ ---@param src parser.object
+ local function checkDef(src)
+ for _, def in ipairs(vm.getDefs(src)) do
+ if def == source then
+ pushResult(src)
+ return
end
end
- end)
- : case 'global'
- ---@param node vm.node
- ---@param key string
- : call(function (node, key, pushResult)
- if node.cate == 'variable' then
- local newGlobal = globalMgr.getGlobal('variable', node.name, key)
- if newGlobal then
- for _, set in ipairs(newGlobal:getSets()) do
- pushResult(set)
- end
- end
+ end
+
+ local pat = '[:.]%s*' .. key
+
+ local function findWord(uri)
+ local text = files.getText(uri)
+ if not text then
+ return
end
- if node.cate == 'type' then
- compiler.getClassFields(node, key, pushResult)
+ if not text:match(pat) then
+ return
end
- end)
- : case 'local'
- : call(function (node, key, pushResult)
- local sources = localID.getSources(node, key)
- if sources then
- for _, src in ipairs(sources) do
- pushResult(src)
- end
+ local state = files.getState(uri)
+ if not state then
+ return
end
- end)
- : case 'doc.type.table'
- : call(function (node, key, pushResult)
- for _, field in ipairs(node.fields) do
- local fieldKey = field.name
- if fieldKey.type == 'doc.field.name' then
- if fieldKey[1] == key then
- pushResult(field)
- end
+ guide.eachSourceType(state.ast, 'getfield', function (src)
+ if src.field[1] == key then
+ checkDef(src)
+ end
+ end)
+ guide.eachSourceType(state.ast, 'getmethod', function (src)
+ if src.method[1] == key then
+ checkDef(src)
end
+ end)
+ guide.eachSourceType(state.ast, 'getindex', function (src)
+ if src.index.type == 'string' and src.index[1] == key then
+ checkDef(src)
+ end
+ end)
+ end
+
+ for uri in files.eachFile(guide.getUri(source)) do
+ if not vm.isMetaFile(uri) then
+ findWord(uri)
end
- end)
- : getMap()
+ end
+end
local searchByParentNode
local nodeMap = util.switch()
@@ -147,28 +152,22 @@ local nodeMap = util.switch()
: case 'getindex'
: case 'setindex'
: call(function (source, pushResult)
+ local key = guide.getKeyName(source)
+ if type(key) ~= 'string' then
+ return
+ end
+
local parentNode = compiler.compileNode(source.node)
if not parentNode then
return
end
- local key = guide.getKeyName(source)
- for pn in nodeMgr.eachNode(parentNode) do
- if searchFieldMap[pn.type] then
- searchFieldMap[pn.type](pn, key, pushResult)
- end
- end
+
+ searchField(source, pushResult)
end)
- : case 'doc.see.field'
+ : case 'tablefield'
+ : case 'tableindex'
: call(function (source, pushResult)
- local parentNode = compiler.compileNode(source.parent.name)
- if not parentNode then
- return
- end
- for pn in nodeMgr.eachNode(parentNode) do
- if searchFieldMap[pn.type] then
- searchFieldMap[pn.type](pn, source[1], pushResult)
- end
- end
+ searchField(source, pushResult)
end)
: getMap()
@@ -189,7 +188,9 @@ local function searchByLocalID(source, pushResult)
return
end
for _, src in ipairs(idSources) do
- pushResult(src)
+ if not guide.isSet(src) then
+ pushResult(src)
+ end
end
end
@@ -209,9 +210,6 @@ local function searchByNode(source, pushResult)
end
for n in nodeMgr.eachNode(node) do
if n.type == 'global' then
- for _, set in ipairs(n:getSets()) do
- pushResult(set)
- end
for _, get in ipairs(n:getGets()) do
pushResult(get)
end
diff --git a/test/references/common.lua b/test/references/common.lua
index b46aa81f..e95a4031 100644
--- a/test/references/common.lua
+++ b/test/references/common.lua
@@ -1,35 +1,35 @@
local config = require "config"
TEST [[
-local <?a?> = 1
+local <!<?a?>!> = 1
<!a!> = <!a!>
]]
TEST [[
<?a?> = 1
-<!a!> = <!a!>
+a = <!a!>
]]
TEST [[
local t
t.<?a?> = 1
-t.<!a!> = t.<!a!>
+t.a = t.<!a!>
]]
TEST [[
t.<?a?> = 1
-t.<!a!> = t.<!a!>
+t.a = t.<!a!>
]]
TEST [[
:: <!LABEL!> ::
-goto <?LABEL?>
+goto <!<?LABEL?>!>
if true then
goto <!LABEL!>
end
]]
TEST [[
-:: <?LABEL?> ::
+:: <!<?LABEL?>!> ::
goto <!LABEL!>
if true then
goto <!LABEL!>
@@ -38,25 +38,13 @@ end
TEST [[
local a = 1
-local <?a?> = 1
+local <!<?a?>!> = 1
<!a!> = <!a!>
]]
TEST [[
-local <!a!>
-local <?b?> = <!a!>
-]]
-
-TEST [[
-local <?a?>
-local <!b!> = <!a!>
-]]
-
-TEST [[
-local t = {
- <!a!> = 1
-}
-print(t.<?a?>)
+local <!<?a?>!>
+local b = <!a!>
]]
TEST [[
diff --git a/test/references/init.lua b/test/references/init.lua
index 81dfb481..ed324d36 100644
--- a/test/references/init.lua
+++ b/test/references/init.lua
@@ -22,8 +22,8 @@ function TEST(script)
local newScript, catched = catch(script, '!?~')
files.setText('', newScript)
- local input = catched['?'] + catched['~']
- local expect = catched['!'] + catched['?']
+ local input = catched['?']
+ local expect = catched['!']
local results = core('', input[1][1])
if results then
local positions = {}