summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2020-09-24 16:27:15 +0800
committer最萌小汐 <sumneko@hotmail.com>2020-09-24 16:27:15 +0800
commit1e64e3a9926e03b28fcb601d5ecad7e6d081dc22 (patch)
tree5458e2677fa94b8b3addf2ba72e6603d449479d2
parentb43dc72c536b99e4ce74eb1cfe88e2cea91fe593 (diff)
downloadlua-language-server-1e64e3a9926e03b28fcb601d5ecad7e6d081dc22.zip
解决文件符号的多重赋值问题
-rw-r--r--script-beta/core/document-symbol.lua85
-rw-r--r--test-beta/document_symbol/init.lua46
2 files changed, 96 insertions, 35 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)
diff --git a/test-beta/document_symbol/init.lua b/test-beta/document_symbol/init.lua
index 525344c6..7579f300 100644
--- a/test-beta/document_symbol/init.lua
+++ b/test-beta/document_symbol/init.lua
@@ -209,7 +209,6 @@ local z
kind = SymbolKind.Variable,
range = {7, 11},
selectionRange = {7, 7},
- valueRange = {11, 11},
},
[2] = {
name = 'f',
@@ -225,7 +224,6 @@ local z
kind = SymbolKind.Variable,
range = {42, 48},
selectionRange = {42, 42},
- valueRange = {46, 48},
},
[2] = {
name = 'y',
@@ -251,7 +249,6 @@ local z
kind = SymbolKind.Variable,
range = {87, 94},
selectionRange = {87, 87},
- valueRange = {91, 94},
},
[4] = {
name = 'z',
@@ -259,7 +256,6 @@ local z
kind = SymbolKind.Variable,
range = {102, 102},
selectionRange = {102, 102},
- valueRange = {102, 102},
},
}
@@ -285,7 +281,6 @@ local t = {
kind = SymbolKind.Property,
range = {17, 21},
selectionRange = {17, 17},
- valueRange = {21, 21},
},
[2] = {
name = 'b',
@@ -293,7 +288,6 @@ local t = {
kind = SymbolKind.Property,
range = {28, 32},
selectionRange = {28, 28},
- valueRange = {32, 32},
},
[3] = {
name = 'c',
@@ -301,7 +295,6 @@ local t = {
kind = SymbolKind.Property,
range = {39, 43},
selectionRange = {39, 39},
- valueRange = {43, 43},
},
}
}
@@ -337,7 +330,6 @@ local t = {
kind = SymbolKind.Property,
range = {31, 35},
selectionRange = {31, 31},
- valueRange = {35, 35},
}
}
},
@@ -365,7 +357,6 @@ g = 1
kind = SymbolKind.Variable,
range = {25, 29},
selectionRange = {25, 25},
- valueRange = {29, 29},
}
}
@@ -389,7 +380,6 @@ end
kind = SymbolKind.Constant,
range = {12, 12},
selectionRange = {12, 12},
- valueRange = {12, 12},
},
[2] = {
name = 'b',
@@ -397,7 +387,6 @@ end
kind = SymbolKind.Constant,
range = {15, 15},
selectionRange = {15, 15},
- valueRange = {15, 15},
},
[3] = {
name = 'x',
@@ -405,7 +394,6 @@ end
kind = SymbolKind.Variable,
range = {33, 39},
selectionRange = {33, 33},
- valueRange = {37, 39},
}
}
},
@@ -434,7 +422,6 @@ local v = t
kind = SymbolKind.Variable,
range = {44, 48},
selectionRange = {44, 44},
- valueRange = {48, 48},
},
}
@@ -448,6 +435,37 @@ local function
kind = SymbolKind.Variable,
range = {7, 7},
selectionRange = {7, 7},
- valueRange = {7, 7},
},
}
+
+TEST [[
+local a, b = {
+ x = 1,
+ y = 1,
+ z = 1,
+}, {
+ x = 1,
+ y = 1,
+ z = 1,
+}
+
+]]{
+ [1] = {
+ name = 'a',
+ detail = 'local a: {x, y, z}',
+ kind = SymbolKind.Variable,
+ range = {7, 49},
+ selectionRange = {7, 7},
+ valueRange = {14, 49},
+ children = EXISTS,
+ },
+ [2] = {
+ name = 'b',
+ detail = 'local b: {x, y, z}',
+ kind = SymbolKind.Variable,
+ range = {10, 87},
+ selectionRange = {10, 10},
+ valueRange = {52, 87},
+ children = EXISTS,
+ }
+}