diff options
Diffstat (limited to 'script')
-rw-r--r-- | script/core/highlight.lua | 57 | ||||
-rw-r--r-- | script/core/noder.lua | 28 | ||||
-rw-r--r-- | script/parser/guide.lua | 19 |
3 files changed, 51 insertions, 53 deletions
diff --git a/script/core/highlight.lua b/script/core/highlight.lua index b070c77e..45001134 100644 --- a/script/core/highlight.lua +++ b/script/core/highlight.lua @@ -4,6 +4,7 @@ local vm = require 'vm' local define = require 'proto.define' local findSource = require 'core.find-source' local util = require 'utility' +local guide = require 'parser.guide' local function eachRef(source, callback) local results = searcher.requestReference(source) @@ -12,20 +13,6 @@ local function eachRef(source, callback) end end -local function eachField(source, callback) - if not source then - return - end - local isGlobal = searcher.isGlobal(source) - local results = searcher.requestReference(source) - for i = 1, #results do - local res = results[i] - if isGlobal == searcher.isGlobal(res) then - callback(res) - end - end -end - local function eachLocal(source, callback) callback(source) if source.ref then @@ -43,21 +30,21 @@ local function find(source, uri, callback) eachLocal(source.node, callback) elseif source.type == 'field' or source.type == 'method' then - eachField(source.parent, callback) + eachRef(source.parent, callback) elseif source.type == 'getindex' or source.type == 'setindex' or source.type == 'tableindex' then - eachField(source, callback) + eachRef(source, callback) elseif source.type == 'setglobal' or source.type == 'getglobal' then - eachField(source, callback) + eachRef(source, callback) elseif source.type == 'goto' or source.type == 'label' then eachRef(source, callback) elseif source.type == 'string' and source.parent and source.parent.index == source then - eachField(source.parent, callback) + eachRef(source.parent, callback) elseif source.type == 'string' or source.type == 'boolean' or source.type == 'number' @@ -107,7 +94,7 @@ local function makeIf(source, text, callback) end local function findKeyWord(ast, text, offset, callback) - searcher.eachSourceContain(ast.ast, offset, function (source) + guide.eachSourceContain(ast.ast, offset, function (source) if source.type == 'do' or source.type == 'function' or source.type == 'loop' @@ -238,6 +225,16 @@ local accept = { ['nil'] = true, } +local function isLiteralValue(source) + if not guide.isLiteral(source) then + return false + end + if source.parent.index == source then + return false + end + return true +end + return function (uri, offset) local ast = files.getAst(uri) if not ast then @@ -249,10 +246,25 @@ return function (uri, offset) local source = findSource(ast, offset, accept) if source then + local isGlobal = guide.isGlobal(source) + local isLiteral = isLiteralValue(source) find(source, uri, function (target) + if not target then + return + end if target.dummy then return end + if mark[target] then + return + end + mark[target] = true + if isGlobal ~= guide.isGlobal(target) then + return + end + if isLiteral ~= isLiteralValue(target) then + return + end local kind if target.type == 'getfield' then target = target.field @@ -315,13 +327,6 @@ return function (uri, offset) else return end - if not target then - return - end - if mark[target] then - return - end - mark[target] = true results[#results+1] = { start = target.start, finish = target.finish, diff --git a/script/core/noder.lua b/script/core/noder.lua index 844f6c77..68783545 100644 --- a/script/core/noder.lua +++ b/script/core/noder.lua @@ -41,25 +41,6 @@ local function getNode(noders, id) return noders[id] end ----是否是全局变量(包括 _G.XXX 形式) ----@param source parser.guide.object ----@return boolean -local function isGlobal(source) - if source.type == 'setglobal' - or source.type == 'getglobal' then - if source.node and source.node.tag == '_ENV' then - return true - end - end - if source.type == 'field' then - source = source.parent - end - if source.special == '_G' then - return true - end - return false -end - ---获取语法树单元的key ---@param source parser.guide.object ---@return string? key @@ -266,7 +247,7 @@ local function checkMode(source) end return id end - if isGlobal(source) then + if guide.isGlobal(source) then return 'g:' end return 'l:' @@ -865,13 +846,6 @@ function m.getUriAndID(id) return uri, newID end ----是否是全局变量(包括 _G.XXX 形式) ----@param source parser.guide.object ----@return boolean -function m.isGlobal(source) - return isGlobal(source) -end - ---获取source的ID ---@param source parser.guide.object ---@return string diff --git a/script/parser/guide.lua b/script/parser/guide.lua index c0a1cd91..8d2708cf 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -968,4 +968,23 @@ function m.getPath(a, b, sameFunction) return mode, resultA, resultB end +---是否是全局变量(包括 _G.XXX 形式) +---@param source parser.guide.object +---@return boolean +function m.isGlobal(source) + if source.type == 'setglobal' + or source.type == 'getglobal' then + if source.node and source.node.tag == '_ENV' then + return true + end + end + if source.type == 'field' then + source = source.parent + end + if source.special == '_G' then + return true + end + return false +end + return m |