Skip to content

Commit 7dc490d

Browse files
committed
fix #1824
1 parent 7f3ed1f commit 7dc490d

File tree

3 files changed

+76
-15
lines changed

3 files changed

+76
-15
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* `FIX` [#1805]
1919
* `FIX` [#1808]
2020
* `FIX` [#1811]
21+
* `FIX` [#1824]
2122

2223
[#831]: https://github.com/sumneko/lua-language-server/issues/831
2324
[#1729]: https://github.com/sumneko/lua-language-server/issues/1729
@@ -27,6 +28,7 @@
2728
[#1805]: https://github.com/sumneko/lua-language-server/issues/1805
2829
[#1808]: https://github.com/sumneko/lua-language-server/issues/1808
2930
[#1811]: https://github.com/sumneko/lua-language-server/issues/1811
31+
[#1824]: https://github.com/sumneko/lua-language-server/issues/1824
3032

3133
`2022-11-29`
3234
## 3.6.4

script/core/completion/completion.lua

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ local function findNearestSource(state, position)
8383
return source
8484
end
8585

86-
local function findNearestTableField(state, position)
87-
local uri = state.uri
88-
local text = files.getText(uri)
86+
local function findNearestTable(state, position)
87+
local uri = state.uri
88+
local text = files.getText(uri)
8989
if not text then
9090
return nil
9191
end
@@ -101,13 +101,47 @@ local function findNearestTableField(state, position)
101101
local sposition = guide.offsetToPosition(state, soffset)
102102
local source
103103
guide.eachSourceContain(state.ast, sposition, function (src)
104-
if src.type == 'table'
105-
or src.type == 'tablefield'
106-
or src.type == 'tableindex'
107-
or src.type == 'tableexp' then
104+
if src.type == 'table' then
108105
source = src
109106
end
110107
end)
108+
109+
if not source then
110+
return nil
111+
end
112+
113+
for _, field in ipairs(source) do
114+
if field.start <= position and (field.range or field.finish) >= position then
115+
if field.type == 'tableexp' then
116+
if field.value.type == 'getlocal'
117+
or field.value.type == 'getglobal' then
118+
if field.finish >= position then
119+
return source
120+
else
121+
return nil
122+
end
123+
end
124+
end
125+
if field.type == 'tablefield' then
126+
if field.finish >= position then
127+
return source
128+
else
129+
return nil
130+
end
131+
end
132+
if field.type == 'tableindex' then
133+
if field.index.type == 'string' then
134+
if field.index.finish >= position then
135+
return source
136+
else
137+
return nil
138+
end
139+
end
140+
end
141+
return nil
142+
end
143+
end
144+
111145
return source
112146
end
113147

@@ -1557,20 +1591,15 @@ local function tryCallArg(state, position, results)
15571591
end
15581592

15591593
local function tryTable(state, position, results)
1560-
local source = findNearestTableField(state, position)
1561-
if not source then
1594+
local tbl = findNearestTable(state, position)
1595+
if not tbl then
15621596
return false
15631597
end
1564-
if source.type ~= 'table'
1565-
and (not source.parent or source.parent.type ~= 'table') then
1598+
if tbl.type ~= 'table' then
15661599
return
15671600
end
15681601
local mark = {}
15691602
local fields = {}
1570-
local tbl = source
1571-
if source.type ~= 'table' then
1572-
tbl = source.parent
1573-
end
15741603

15751604
local defs = vm.getFields(tbl)
15761605
for _, field in ipairs(defs) do

test/completion/common.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4084,3 +4084,33 @@ local foo
40844084
foo = {"<??>"}
40854085
]]
40864086
(EXISTS)
4087+
4088+
TEST [[
4089+
---@class c
4090+
---@field abc fun()
4091+
---@field abc2 fun()
4092+
4093+
---@param p c
4094+
local function f(p) end
4095+
4096+
f({
4097+
abc = function(s)
4098+
local abc3
4099+
abc<??>
4100+
end,
4101+
})
4102+
]]
4103+
{
4104+
{
4105+
label = 'abc3',
4106+
kind = define.CompletionItemKind.Variable,
4107+
},
4108+
{
4109+
label = 'abc',
4110+
kind = define.CompletionItemKind.Text,
4111+
},
4112+
{
4113+
label = 'abc2',
4114+
kind = define.CompletionItemKind.Text,
4115+
},
4116+
}

0 commit comments

Comments
 (0)