summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-07-19 15:36:57 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-07-19 15:36:57 +0800
commitcbcdd7e20745bb7c2d6cce9f1ea970c9d54d6910 (patch)
treeeae8bb57a62ea6bf515d1f0ee213e5189acfe7dc
parent9889dc0d1c9673575ebbfa62f3d47d59d0ff8664 (diff)
downloadlua-language-server-cbcdd7e20745bb7c2d6cce9f1ea970c9d54d6910.zip
fix #606
-rw-r--r--changelog.md3
-rw-r--r--script/core/document-symbol.lua79
-rw-r--r--test/document_symbol/init.lua39
3 files changed, 72 insertions, 49 deletions
diff --git a/changelog.md b/changelog.md
index 7fda6831..10861ec2 100644
--- a/changelog.md
+++ b/changelog.md
@@ -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:
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)
diff --git a/test/document_symbol/init.lua b/test/document_symbol/init.lua
index 718089e2..d745d53f 100644
--- a/test/document_symbol/init.lua
+++ b/test/document_symbol/init.lua
@@ -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},
+ },
+ },
+ },
+ },
+ }
+}