summaryrefslogtreecommitdiff
path: root/server-beta/src
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-10-08 15:05:21 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-10-08 15:05:21 +0800
commit005f94454fe50382c64e71f9d36fb7064895134f (patch)
treeb6e1f9985a0372fa8bf65b117784915a939d7b50 /server-beta/src
parent0690696de36519b88bbb6638fccef14cd22a9474 (diff)
downloadlua-language-server-005f94454fe50382c64e71f9d36fb7064895134f.zip
支持 setindex 与 getindex
Diffstat (limited to 'server-beta/src')
-rw-r--r--server-beta/src/core/engineer.lua62
-rw-r--r--server-beta/src/parser/ast.lua26
-rw-r--r--server-beta/src/parser/guide.lua40
3 files changed, 99 insertions, 29 deletions
diff --git a/server-beta/src/core/engineer.lua b/server-beta/src/core/engineer.lua
index 9f05b40c..0f0bff95 100644
--- a/server-beta/src/core/engineer.lua
+++ b/server-beta/src/core/engineer.lua
@@ -52,9 +52,11 @@ mt['local'] = function (self, source, mode, callback)
for _, ref in ipairs(source.ref) do
if ref.type == 'getlocal' then
local parent = ref.parent
- if parent.type == 'setfield' then
- callback(parent)
- elseif parent.type == 'getfield' then
+ local tp = parent.type
+ if tp == 'setfield'
+ or tp == 'getfield'
+ or tp == 'setindex'
+ or tp == 'getindex' then
callback(parent)
end
end
@@ -69,18 +71,22 @@ mt['setlocal'] = mt['getlocal']
mt['_G'] = function (self, source, mode, callback)
if mode == 'def' then
local parent = source.parent
- if parent.type == 'setfield' then
+ if parent.type == 'setfield'
+ or parent.type == 'setindex' then
callback(parent, 'set')
- elseif parent.type == 'getfield' then
+ elseif parent.type == 'getfield'
+ or parent.type == 'getindex' then
self:search(parent, 'special', mode, callback)
elseif parent.type == 'callargs' then
self:search(parent.parent, 'special', mode, callback)
end
elseif mode == 'ref' then
local parent = source.parent
- if parent.type == 'setfield' then
+ if parent.type == 'setfield'
+ or parent.type == 'setindex' then
callback(parent, 'set')
- elseif parent.type == 'getfield' then
+ elseif parent.type == 'getfield'
+ or parent.type == 'getindex' then
callback(parent, 'get')
elseif parent.type == 'getfield' then
self:search(parent, 'special', mode, callback)
@@ -110,7 +116,6 @@ mt['getglobal'] = function (self, source, mode, callback)
callback(ref, 'set')
elseif ref.type == 'getglobal' then
callback(ref, 'get')
- elseif ref.type == 'getglobal' then
self:search(ref, 'special', mode, callback)
elseif ref.type == 'getlocal' then
self:search(ref, '_G', mode, callback)
@@ -120,9 +125,11 @@ mt['getglobal'] = function (self, source, mode, callback)
elseif mode == 'field' then
self:search(source, 'getglobal', 'ref', function (src)
local parent = src.parent
- if parent.type == 'setfield' then
- callback(parent)
- elseif parent.type == 'getfield' then
+ local tp = parent.type
+ if tp == 'setfield'
+ or tp == 'getfield'
+ or tp == 'setindex'
+ or tp == 'getindex' then
callback(parent)
end
end)
@@ -130,7 +137,7 @@ mt['getglobal'] = function (self, source, mode, callback)
end
mt['setglobal'] = mt['getglobal']
mt['field'] = function (self, source, mode, callback)
- local node = source.node.node
+ local node = source.parent.node
local key = guide.getKeyName(source)
self:eachRef(node, 'field', function (src)
if key == guide.getKeyName(src) then
@@ -148,13 +155,40 @@ mt['special'] = function (self, source, mode, callback)
return
end
if mode == 'def' then
- if name == '_G' then
+ if name == 's|_G' then
self:search(source, '_G', mode, callback)
- elseif name == 'rawset' then
+ elseif name == 's|rawset' then
callback(source.parent, 'set')
end
end
end
+mt['asindex'] = function (self, source, mode, callback)
+ local parent = source.parent
+ if not parent then
+ return
+ end
+ if parent.type ~= 'setindex' and parent.type ~= 'getindex' then
+ return
+ end
+ local node = parent.node
+ local key = guide.getKeyName(source)
+ self:eachRef(node, 'field', function (src)
+ if key == guide.getKeyName(src) then
+ if mode == 'def' then
+ if src.type == 'setfield' then
+ callback(src.field, 'set')
+ elseif src.type == 'setindex' then
+ callback(src.index, 'set')
+ end
+ end
+ end
+ end)
+end
+mt['number'] = mt['asindex']
+mt['boolean'] = mt['asindex']
+mt['string'] = function (self, source, mode, callback)
+ mt['asindex'](self, source, mode, callback)
+end
function mt:getSpecial(source)
local node = source.node
diff --git a/server-beta/src/parser/ast.lua b/server-beta/src/parser/ast.lua
index 727cec9e..4297ab42 100644
--- a/server-beta/src/parser/ast.lua
+++ b/server-beta/src/parser/ast.lua
@@ -567,29 +567,33 @@ local Defs = {
}
if field then
field.type = 'field'
- field.node = obj
+ field.parent = obj
end
return obj
end,
GetIndex = function (start, index, finish)
- return {
+ local obj = {
type = 'getindex',
start = start,
finish = finish - 1,
index = index,
}
+ index.parent = obj
+ return obj
end,
GetMethod = function (colon, method)
- if method then
- method.type = 'method'
- end
- return {
+ local obj = {
type = 'getmethod',
method = method,
colon = colon,
start = colon.start,
finish = (method or colon).finish,
}
+ if method then
+ method.type = 'method'
+ method.parent = obj
+ end
+ return obj
end,
Single = function (unit)
unit.type = 'getname'
@@ -908,25 +912,29 @@ local Defs = {
value = value,
}
field.type = 'field'
- field.node = obj
+ field.parent = obj
return obj
end,
Index = function (start, index, finish)
- return {
+ local obj = {
type = 'index',
start = start,
finish = finish - 1,
index = index,
}
+ index.parent = obj
+ return obj
end,
NewIndex = function (start, index, value, finish)
- return {
+ local obj = {
type = 'tableindex',
start = start,
finish = finish-1,
index = index,
value = value,
}
+ index.parent = obj
+ return obj
end,
FuncArgs = function (start, args, finish)
args.type = 'funcargs'
diff --git a/server-beta/src/parser/guide.lua b/server-beta/src/parser/guide.lua
index f06264cd..a89af6b4 100644
--- a/server-beta/src/parser/guide.lua
+++ b/server-beta/src/parser/guide.lua
@@ -1,6 +1,7 @@
local error = error
local type = type
local next = next
+local tostring = tostring
_ENV = nil
@@ -359,12 +360,39 @@ function m.lineRange(lines, row)
end
function m.getKeyName(obj)
- if obj.type == 'getglobal' or obj.type == 'setglobal' then
- return obj[1]
- elseif obj.type == 'getfield' or obj.type == 'setfield' then
- return obj.field[1]
- elseif obj.type == 'field' then
- return obj[1]
+ local tp = obj.type
+ if tp == 'getglobal'
+ or tp == 'setglobal' then
+ return 's|' .. obj[1]
+ elseif tp == 'getfield'
+ or tp == 'setfield' then
+ return 's|' .. obj.field[1]
+ elseif tp == 'getindex'
+ or tp == 'setindex' then
+ return m.getKeyName(obj.index)
+ elseif tp == 'field' then
+ return 's|' .. obj[1]
+ elseif tp == 'string' then
+ local s = obj[1]
+ if s then
+ return 's|' .. s
+ else
+ return s
+ end
+ elseif tp == 'number' then
+ local n = obj[1]
+ if n then
+ return ('n|%q'):format(obj[1])
+ else
+ return 'n'
+ end
+ elseif tp == 'boolean' then
+ local b = obj[1]
+ if b then
+ return 'b|' .. tostring(b)
+ else
+ return 'b'
+ end
end
return nil
end