diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-07-19 15:36:57 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-07-19 15:36:57 +0800 |
commit | cbcdd7e20745bb7c2d6cce9f1ea970c9d54d6910 (patch) | |
tree | eae8bb57a62ea6bf515d1f0ee213e5189acfe7dc /script | |
parent | 9889dc0d1c9673575ebbfa62f3d47d59d0ff8664 (diff) | |
download | lua-language-server-cbcdd7e20745bb7c2d6cce9f1ea970c9d54d6910.zip |
fix #606
Diffstat (limited to 'script')
-rw-r--r-- | script/core/document-symbol.lua | 79 |
1 files changed, 30 insertions, 49 deletions
diff --git a/script/core/document-symbol.lua b/script/core/document-symbol.lua index 86d95a59..9f950d25 100644 --- a/script/core/document-symbol.lua +++ b/script/core/document-symbol.lua @@ -244,64 +244,45 @@ local function makeSymbol(uri) return symbols 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 parent = stacks[#stacks] - -- 移除已经超出生效范围的区间 - while symbol.selectionRange[1] > parent.valueRange[2] do - stacks[#stacks] = nil - parent = stacks[#stacks] - end - -- 向后看,找出当前可能生效的区间 - local nextRange - while #ranges > 0 - and symbol.selectionRange[1] >= ranges[#ranges].valueRange[1] do - if symbol.selectionRange[1] <= ranges[#ranges].valueRange[2] then - nextRange = ranges[#ranges] +local function packChild(symbols) + local index = 1 + local function insertChilds(min, max) + local list + while true do + local symbol = symbols[index] + if not symbol then + break + end + if symbol.selectionRange[1] < min + or symbol.selectionRange[2] > max then + break + end + if not list then + list = {} + end + list[#list+1] = symbol + index = index + 1 + if symbol.valueRange then + symbol.children = insertChilds(symbol.valueRange[1], symbol.valueRange[2]) end - ranges[#ranges] = nil - end - if nextRange then - stacks[#stacks+1] = nextRange - parent = nextRange - end - if parent == symbol then - -- function f() end 的情况,selectionRange 在 valueRange 内部, - -- 当前区间置为上一层 - parent = stacks[#stacks-1] - end - -- 把自己放到当前区间中 - if not parent.children then - parent.children = {} end - parent.children[#parent.children+1] = symbol + return list end - return root.children + + local root = insertChilds(0, math.maxinteger) + return root end local function packSymbols(symbols) - 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] + table.sort(symbols, function (a, b) + local o1 = a.valueRange and a.valueRange[1] or a.selectionRange[1] + local o2 = b.valueRange and b.valueRange[1] or b.selectionRange[1] + return o1 < o2 end) + await.delay() -- 处理嵌套 - return packChild(ranges, symbols) + return packChild(symbols) end return function (uri) |