diff options
-rw-r--r-- | script-beta/core/completion.lua | 14 | ||||
-rw-r--r-- | script-beta/parser/guide.lua | 38 |
2 files changed, 23 insertions, 29 deletions
diff --git a/script-beta/core/completion.lua b/script-beta/core/completion.lua index 35e4e21f..1f78b4d4 100644 --- a/script-beta/core/completion.lua +++ b/script-beta/core/completion.lua @@ -67,18 +67,24 @@ local function findParent(ast, text, offset) end local function checkLocal(ast, word, offset, results) - guide.getVisibleLocalNames(ast.ast, offset, function (name) + local locals = guide.getVisibleLocals(ast.ast, offset) + for name in pairs(locals) do if matchKey(word, name) then results[#results+1] = { label = name, kind = ckind.Variable, } end - end) + end +end + +local function isSameSource(source, pos) + return source.start <= pos and source.finish >= pos end local function checkField(ast, text, word, offset, results) - local parent, oop = findParent(ast, text, offset - #word) + local myStart = offset - #word + 1 + local parent, oop = findParent(ast, text, myStart - 1) if not parent then parent = guide.getLocal(ast.ast, '_ENV', offset) if not parent then @@ -90,7 +96,7 @@ local function checkField(ast, text, word, offset, results) local key = info.key if key and key:sub(1, 1) == 's' - and info.source.finish ~= offset then + and not isSameSource(info.source, myStart) then local name = key:sub(3) if not used[name] and matchKey(word, name) then results[#results+1] = { diff --git a/script-beta/parser/guide.lua b/script-beta/parser/guide.lua index b4b45d0b..443c2293 100644 --- a/script-beta/parser/guide.lua +++ b/script-beta/parser/guide.lua @@ -236,33 +236,21 @@ function m.getLocal(block, name, pos) end --- 获取指定区块中所有的可见局部变量名称 -function m.getVisibleLocalNames(block, pos, callback) - block = m.getBlock(block) - local used = {} - for _ = 1, 1000 do - if not block then - return nil - end - local locals = block.locals - local res - if not locals then - goto CONTINUE - end - for i = 1, #locals do - local loc = locals[i] - if loc.effect > pos then - break - end - local name = loc[1] - if not used[name] then - used[name] = true - callback(name) +function m.getVisibleLocals(block, pos) + local result = {} + m.eachSourceContain(m.getRoot(block), pos, function (source) + local locals = source.locals + if locals then + for i = 1, #locals do + local loc = locals[i] + local name = loc[1] + if loc.effect <= pos then + result[name] = loc + end end end - ::CONTINUE:: - block = m.getParentBlock(block) - end - error('guide.getLocal overstack') + end) + return result end --- 获取指定区块中可见的标签 |