summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-11-08 15:49:18 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-11-08 15:49:18 +0800
commite5f5d4251beaf882f6b125ce7552127b9ea21240 (patch)
treed58956ddf8b9661fbbbb1ebcc70efdf6b9fdb8f3
parent5fd1d62add115ba4a77236e927f87359e4f37018 (diff)
downloadlua-language-server-e5f5d4251beaf882f6b125ce7552127b9ea21240.zip
fix duplicate fields in table hover
-rw-r--r--changelog.md1
-rw-r--r--script/core/hover/table.lua13
-rw-r--r--script/vm/infer.lua10
-rw-r--r--test/hover/init.lua17
4 files changed, 36 insertions, 5 deletions
diff --git a/changelog.md b/changelog.md
index 458b0b6b..a1a8825f 100644
--- a/changelog.md
+++ b/changelog.md
@@ -2,6 +2,7 @@
## 3.6.1
* `FIX` wrong diagnostics for `pcall` and `xpcall`
+* `FIX` duplicate fields in table hover
## 3.6.0
`2022-11-8`
diff --git a/script/core/hover/table.lua b/script/core/hover/table.lua
index 04d2d00d..e59a26d0 100644
--- a/script/core/hover/table.lua
+++ b/script/core/hover/table.lua
@@ -94,15 +94,24 @@ local function getVisibleKeyMap(source, fields)
local uri = guide.getUri(source)
local keys = {}
local map = {}
+ local ignored = {}
for _, field in ipairs(fields) do
local key = vm.viewKey(field, uri)
- if vm.isVisible(source, field) then
+ local rawKey = guide.getKeyName(field)
+ if rawKey and rawKey ~= key then
+ ignored[rawKey] = true
+ map[rawKey] = nil
+ end
+ if not ignored[key]
+ and vm.isVisible(source, field) then
if key and not map[key] then
map[key] = true
- keys[#keys+1] = key
end
end
end
+ for key in pairs(map) do
+ keys[#keys+1] = key
+ end
table.sort(keys, function (a, b)
if a == b then
return false
diff --git a/script/vm/infer.lua b/script/vm/infer.lua
index 1c83faaa..00f1b014 100644
--- a/script/vm/infer.lua
+++ b/script/vm/infer.lua
@@ -542,11 +542,17 @@ function vm.viewKey(source, uri)
end
end
if source.type == 'tableindex' then
- local name = vm.getKeyName(source)
+ local index = source.index
+ local name = vm.getKeyName(index)
if not name then
return nil
end
- local key = util.viewLiteral(name)
+ local key
+ if index.type == 'string' then
+ key = util.viewString(name, index[2])
+ else
+ key = util.viewLiteral(name)
+ end
return ('[%s]'):format(key), name
end
if source.type == 'tableexp' then
diff --git a/test/hover/init.lua b/test/hover/init.lua
index 4ce2de8c..751299bb 100644
--- a/test/hover/init.lua
+++ b/test/hover/init.lua
@@ -1714,7 +1714,7 @@ local <?t?> = {
local t: {
x: string = "e",
y: string = "f",
- ["z"]: string = "g",
+ ['z']: string = "g",
[10]: string = "d",
[1]: string = "a",
[2]: string = "b",
@@ -2415,3 +2415,18 @@ local t: { [string]: string }[] {
foo: unknown,
}
]]
+
+TEST [[
+local t = {
+ ['x'] = 1,
+ ['y'] = 2,
+}
+
+print(t.x, <?t?>.y)
+]]
+[[
+local t: {
+ ['x']: integer = 1,
+ ['y']: integer = 2,
+}
+]]