summaryrefslogtreecommitdiff
path: root/script-beta
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2020-07-23 19:17:20 +0800
committer最萌小汐 <sumneko@hotmail.com>2020-07-23 19:17:20 +0800
commit88c9bd52b5a148f33ce88a265962a32af4832afd (patch)
tree4b2d12f2c08fb261b15f3fe8087d5bed890f0efb /script-beta
parent73fb79c13e846774b99d3c0dab886c639611ed48 (diff)
downloadlua-language-server-88c9bd52b5a148f33ce88a265962a32af4832afd.zip
整理代码
Diffstat (limited to 'script-beta')
-rw-r--r--script-beta/core/definition.lua21
-rw-r--r--script-beta/core/reference.lua21
-rw-r--r--script-beta/parser/guide.lua64
3 files changed, 102 insertions, 4 deletions
diff --git a/script-beta/core/definition.lua b/script-beta/core/definition.lua
index 6a895256..22bb48ab 100644
--- a/script-beta/core/definition.lua
+++ b/script-beta/core/definition.lua
@@ -4,6 +4,24 @@ local files = require 'files'
local vm = require 'vm'
local findSource = require 'core.find-source'
+local function sortResults(results)
+ -- 先按照顺序排序
+ table.sort(results, function (a, b)
+ return a.target.start < b.target.start
+ end)
+ -- 如果2个结果处于嵌套状态,则取范围小的那个
+ local lf
+ for i = #results, 1, -1 do
+ local res = results[i].target
+ local f = res.finish
+ if lf and f > lf then
+ table.remove(results, i)
+ else
+ lf = f
+ end
+ end
+end
+
local accept = {
['local'] = true,
['setlocal'] = true,
@@ -97,5 +115,8 @@ return function (uri, offset)
if #results == 0 then
return nil
end
+
+ sortResults(results)
+
return results
end
diff --git a/script-beta/core/reference.lua b/script-beta/core/reference.lua
index 73e1130e..5c18de1b 100644
--- a/script-beta/core/reference.lua
+++ b/script-beta/core/reference.lua
@@ -7,6 +7,24 @@ local function isValidFunction(source, offset)
return offset >= source.start and offset < source.start + #'function'
end
+local function sortResults(results)
+ -- 先按照顺序排序
+ table.sort(results, function (a, b)
+ return a.target.start < b.target.start
+ end)
+ -- 如果2个结果处于嵌套状态,则取范围小的那个
+ local lf
+ for i = #results, 1, -1 do
+ local res = results[i].target
+ local f = res.finish
+ if lf and f > lf then
+ table.remove(results, i)
+ else
+ lf = f
+ end
+ end
+end
+
local accept = {
['local'] = true,
['setlocal'] = true,
@@ -62,5 +80,8 @@ return function (uri, offset)
if #results == 0 then
return nil
end
+
+ sortResults(results)
+
return results
end
diff --git a/script-beta/parser/guide.lua b/script-beta/parser/guide.lua
index 874c3811..41c84b2c 100644
--- a/script-beta/parser/guide.lua
+++ b/script-beta/parser/guide.lua
@@ -1017,7 +1017,7 @@ function m.searchFields(status, obj, key, interface)
return {}
end
simple[#simple+1] = key and ('s|' .. key) or '*'
- m.searchSameFields(newStatus, simple, 'ref')
+ m.searchSameFields(newStatus, simple, 'field')
local results = newStatus.results
m.cleanResults(results)
return results
@@ -1241,6 +1241,18 @@ function m.checkSameSimpleInGlobal(status, name, start, queue)
end
end
+function m.searchSameFieldsInValue(status, ref, start, queue)
+ local value = m.getObjectValue(ref)
+ if not value then
+ return
+ end
+ queue[#queue+1] = {
+ obj = value,
+ start = start,
+ force = true,
+ }
+end
+
function m.checkSameSimple(status, simple, data, mode, results, queue)
local ref = data.obj
local start = data.start
@@ -1253,6 +1265,8 @@ function m.checkSameSimple(status, simple, data, mode, results, queue)
force = false
-- 穿透 self:func 与 mt:func
m.searchSameFieldsCrossMethod(status, ref, i, queue)
+ -- 穿透赋值
+ m.searchSameFieldsInValue(status, ref, i, queue)
if i == #simple then
break
end
@@ -1286,7 +1300,7 @@ function m.checkSameSimple(status, simple, data, mode, results, queue)
if data.call then
results[#results+1] = ref
end
- else
+ elseif mode == 'ref' then
if ref.type == 'setfield'
or ref.type == 'getfield'
or ref.type == 'tablefield' then
@@ -1298,9 +1312,47 @@ function m.checkSameSimple(status, simple, data, mode, results, queue)
or ref.type == 'getindex'
or ref.type == 'tableindex' then
results[#results+1] = ref.index
- else
+ elseif ref.type == 'setglobal'
+ or ref.type == 'getglobal'
+ or ref.type == 'local'
+ or ref.type == 'setlocal'
+ or ref.type == 'getlocal' then
+ results[#results+1] = ref
+ elseif ref.type == 'function' then
+ results[#results+1] = ref
+ elseif ref.type == 'call' then
+ if ref.node.special == 'rawset'
+ or ref.node.special == 'rawget' then
+ results[#results+1] = ref
+ end
+ end
+ if data.call then
results[#results+1] = ref
end
+ elseif mode == 'field' then
+ if ref.type == 'setfield'
+ or ref.type == 'getfield'
+ or ref.type == 'tablefield' then
+ results[#results+1] = ref.field
+ elseif ref.type == 'setmethod'
+ or ref.type == 'getmethod' then
+ results[#results+1] = ref.method
+ elseif ref.type == 'setindex'
+ or ref.type == 'getindex'
+ or ref.type == 'tableindex' then
+ results[#results+1] = ref.index
+ elseif ref.type == 'setglobal'
+ or ref.type == 'getglobal'
+ or ref.type == 'local'
+ or ref.type == 'setlocal'
+ or ref.type == 'getlocal' then
+ results[#results+1] = ref
+ elseif ref.type == 'call' then
+ if ref.node.special == 'rawset'
+ or ref.node.special == 'rawget' then
+ results[#results+1] = ref
+ end
+ end
end
end
@@ -1344,12 +1396,16 @@ function m.searchSameFields(status, simple, mode)
m.checkSameSimpleInCall(status, first, 1, queue)
end
end
+ local mark = {}
for i = 1, 999 do
local data = queue[i]
if not data then
return
end
- m.checkSameSimple(status, simple, data, mode, status.results, queue)
+ if not mark[data.obj] then
+ mark[data.obj] = true
+ m.checkSameSimple(status, simple, data, mode, status.results, queue)
+ end
end
end