diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-09-24 16:27:15 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-09-24 16:27:15 +0800 |
commit | 1e64e3a9926e03b28fcb601d5ecad7e6d081dc22 (patch) | |
tree | 5458e2677fa94b8b3addf2ba72e6603d449479d2 /script-beta/core | |
parent | b43dc72c536b99e4ce74eb1cfe88e2cea91fe593 (diff) | |
download | lua-language-server-1e64e3a9926e03b28fcb601d5ecad7e6d081dc22.zip |
解决文件符号的多重赋值问题
Diffstat (limited to 'script-beta/core')
-rw-r--r-- | script-beta/core/document-symbol.lua | 85 |
1 files changed, 64 insertions, 21 deletions
diff --git a/script-beta/core/document-symbol.lua b/script-beta/core/document-symbol.lua index 2aeb3ae1..161b467d 100644 --- a/script-beta/core/document-symbol.lua +++ b/script-beta/core/document-symbol.lua @@ -122,11 +122,9 @@ local function buildValue(source, symbols) details[3] = ': {' details[4] = buildTable(source.value) details[5] = '}' + valueRange = { source.value.start, source.value.finish } end range = { range[1], source.value.finish } - valueRange = { source.value.start, source.value.finish } - else - valueRange = range end symbols[#symbols+1] = { name = name, @@ -178,34 +176,79 @@ local function buildSource(source, used, symbols) end end -local function packChild(symbols, finish) +local function packValues(values) + local root = { + valueRange = { 0, math.maxinteger }, + } + local stacks = { root } await.delay() - local t - while true do - local symbol = symbols[#symbols] - if not symbol then - break + await.delay() + for _, value in ipairs(values) do + local parent = stacks[#stacks] + while value.valueRange[1] > parent.valueRange[2] do + stacks[#stacks] = nil + parent = stacks[#stacks] end - if symbol.valueRange[1] > finish then - break + stacks[#stacks+1] = value + end + return root +end + +local function packChild(ranges, symbols) + await.delay() + table.sort(symbols, function (a, b) + return a.selectionRange[1] < b.selectionRange[1] + end) + await.delay() + local root = { + valueRange = { 0, math.maxinteger }, + children = {}, + } + local stacks = { root } + for _, symbol in ipairs(symbols) do + -- 向后看,找出当前可能生效的区间 + local nextRange + while #ranges > 0 + and symbol.selectionRange[1] >= ranges[#ranges].valueRange[1] do + nextRange = ranges[#ranges] + ranges[#ranges] = nil + end + if nextRange then + stacks[#stacks+1] = nextRange + end + local parent = stacks[#stacks] + if parent == symbol then + -- function f() end 的情况,selectionRange 在 valueRange 内部, + -- 当前区间置为上一层 + parent = stacks[#stacks-1] end - symbols[#symbols] = nil - symbol.children = packChild(symbols, symbol.valueRange[2]) - if not t then - t = {} + -- 移除已经超出生效范围的区间 + while symbol.selectionRange[1] > parent.valueRange[2] do + stacks[#stacks] = nil + parent = stacks[#stacks] end - t[#t+1] = symbol + -- 把自己放到当前区间中 + if not parent.children then + parent.children = {} + end + parent.children[#parent.children+1] = symbol end - return t + return root.children end local function packSymbols(symbols) - -- 按照start位置反向排序 - table.sort(symbols, function (a, b) - return a.selectionRange[1] > b.selectionRange[1] + local ranges = {} + for _, symbol in ipairs(symbols) do + if symbol.valueRange then + ranges[#ranges+1] = symbol + end + end + await.delay() + table.sort(ranges, function (a, b) + return a.valueRange[1] > b.valueRange[1] end) -- 处理嵌套 - return packChild(symbols, math.maxinteger) + return packChild(ranges, symbols) end return function (uri) |