Skip to content

Commit

Permalink
fix #606
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jul 19, 2021
1 parent 9889dc0 commit cbcdd7e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 49 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# changelog

## 2.3.1
* `FIX` [#606](https://github.com/sumneko/lua-language-server/issues/606)

## 2.3.0
`2021-7-16`
* `NEW` `VSCode`: click the status bar icon to operate:
Expand Down
79 changes: 30 additions & 49 deletions script/core/document-symbol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions test/document_symbol/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -636,3 +636,42 @@ end)
children = EXISTS,
}
}

TEST [[
local root = {
inner_function = function ()
local function function_inside_function()
end
end
}
]]
{
[1] = {
name = 'root',
detail = 'local {inner_function}',
kind = define.SymbolKind.Variable,
range = {7, 123},
selectionRange = {7, 10},
valueRange = {14, 123},
children = {
[1] = {
name = 'inner_function',
detail = 'function ()',
kind = define.SymbolKind.Function,
range = {20, 117},
selectionRange = {20, 33},
valueRange = {37, 117},
children = {
[1] = {
name = 'function_inside_function',
detail = 'function ()',
kind = define.SymbolKind.Function,
range = {63, 109},
selectionRange = {72, 95},
valueRange = {63, 109},
},
},
},
},
}
}

0 comments on commit cbcdd7e

Please sign in to comment.