summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-11-22 20:22:13 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-11-22 20:22:13 +0800
commit928b40c6f186c4692212ff1f9751da4755aa6841 (patch)
treeea28a522e24e9330786f830ad6d0b436257856a0
parentbc8d5250ed5040290738c5167f0aaee0ffe72f31 (diff)
downloadlua-language-server-928b40c6f186c4692212ff1f9751da4755aa6841.zip
resolve #727
-rw-r--r--changelog.md1
-rw-r--r--script/core/completion.lua53
-rw-r--r--script/core/noder.lua19
-rw-r--r--test/completion/common.lua16
-rw-r--r--test/definition/luadoc.lua2
5 files changed, 38 insertions, 53 deletions
diff --git a/changelog.md b/changelog.md
index 0ee1eddd..e3d9dff3 100644
--- a/changelog.md
+++ b/changelog.md
@@ -6,6 +6,7 @@
+ `Lua.hint.await`: display `await` when calling a function marked as async
* `NEW` add supports for `lovr`
* `NEW` file encoding supports `utf16le` and `utf16be`
+* `NEW` completion supports table fields in calling, see [#727](https://github.com/sumneko/lua-language-server/issues/727)
* `NEW` `LuaDoc` annotations:
+ `---@async`: mark a function as async
+ `---@nodiscard`: the return value of the marking function cannot be discarded
diff --git a/script/core/completion.lua b/script/core/completion.lua
index 4dd55070..f7464064 100644
--- a/script/core/completion.lua
+++ b/script/core/completion.lua
@@ -1452,18 +1452,6 @@ local function getCallArgInfo(call, position)
return #call.args + 1, nil, oop
end
-local function getFuncParamByCallIndex(func, index)
- if not func.args or #func.args == 0 then
- return nil
- end
- if index > #func.args then
- if func.args[#func.args].type == '...' then
- return func.args[#func.args]
- end
- end
- return func.args[index]
-end
-
local function checkTableLiteralField(state, position, tbl, fields, results)
local text = state.lua
local mark = {}
@@ -1509,43 +1497,6 @@ local function checkTableLiteralField(state, position, tbl, fields, results)
end
end
-local function checkTableLiteralFieldByCall(state, position, call, defs, index, results)
- local source = findNearestTableField(state, position)
- if not source then
- return
- end
- if source.type ~= 'table'
- and (not source.parent or source.parent.type ~= 'table') then
- return
- end
- local mark = {}
- local fields = {}
- local tbl = source
- if source.type ~= 'table' then
- tbl = source.parent
- end
- if tbl.parent ~= call.args then
- return
- end
- for _, def in ipairs(defs) do
- local func = searcher.getObjectValue(def) or def
- local param = getFuncParamByCallIndex(func, index)
- if not param then
- goto CONTINUE
- end
- local defs = vm.getDefs(param, '*')
- for _, field in ipairs(defs) do
- local name = guide.getKeyName(field)
- if name and not mark[name] then
- mark[name] = true
- fields[#fields+1] = field
- end
- end
- ::CONTINUE::
- end
- checkTableLiteralField(state, position, tbl, fields, results)
-end
-
local function tryCallArg(state, position, results)
local call = findCall(state, position)
if not call then
@@ -1567,7 +1518,6 @@ local function tryCallArg(state, position, results)
for _, enum in ipairs(myResults) do
results[#results+1] = enum
end
- checkTableLiteralFieldByCall(state, position, call, defs, argIndex, results)
end
local function tryTable(state, position, results)
@@ -1585,8 +1535,7 @@ local function tryTable(state, position, results)
if source.type ~= 'table' then
tbl = source.parent
end
- local parent = tbl.parent
- local defs = vm.getDefs(parent, '*')
+ local defs = vm.getDefs(tbl, '*')
for _, field in ipairs(defs) do
local name = guide.getKeyName(field)
if name and not mark[name] then
diff --git a/script/core/noder.lua b/script/core/noder.lua
index 26f2592c..1e8ce121 100644
--- a/script/core/noder.lua
+++ b/script/core/noder.lua
@@ -852,6 +852,14 @@ local function compileCallParam(noders, call, sourceID)
end
end
end
+ if callArg.type == 'table' then
+ local paramID = sformat('%s%s%s'
+ , nodeID
+ , PARAM_INDEX
+ , firstIndex + methodIndex
+ )
+ pushForward(noders, getID(callArg), paramID)
+ end
end
end
@@ -1280,6 +1288,13 @@ compileNodeMap = util.switch()
, '...'
))
end
+ for j = i + 1, i + 10 do
+ pushForward(noders, sformat('%s%s%s'
+ , id
+ , PARAM_INDEX
+ , j
+ ), getID(arg))
+ end
end
::CONTINUE::
end
@@ -1326,6 +1341,10 @@ compileNodeMap = util.switch()
end
end
end
+ local parent = source.parent
+ if guide.isSet(parent) then
+ pushForward(noders, id, getID(parent))
+ end
end)
: case 'in'
: call(function (noders, id, source)
diff --git a/test/completion/common.lua b/test/completion/common.lua
index 3b06d572..a01b2d10 100644
--- a/test/completion/common.lua
+++ b/test/completion/common.lua
@@ -2723,3 +2723,19 @@ TEST [[
kind = define.CompletionItemKind.Class,
}
}
+
+TEST [[
+---@class A
+---@field zzzz number
+
+---@param x A
+local function f(x) end
+
+f({zzz<??>})
+]]
+{
+ [1] = {
+ label = 'zzzz',
+ kind = define.CompletionItemKind.Property,
+ }
+}
diff --git a/test/definition/luadoc.lua b/test/definition/luadoc.lua
index 58c7c8fe..8124261c 100644
--- a/test/definition/luadoc.lua
+++ b/test/definition/luadoc.lua
@@ -362,7 +362,7 @@ TEST [[
---@generic T
---@param x T
---@return fun():T
-local function f(x) end
+local function f(<!x!>) end
local v1 = f(<!{}!>)
local <?<!v2!>?> = v1()