summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-09-29 14:44:37 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-09-29 14:44:37 +0800
commita7a38de065f5ab570c805c367e0260849ee7ab3d (patch)
tree115b33af5b7f91c2d88be24eab99fdbf3f6f43d7
parentb3df71ad1ae706a081411666e1a54973c2490db0 (diff)
downloadlua-language-server-a7a38de065f5ab570c805c367e0260849ee7ab3d.zip
resolve #587
-rw-r--r--changelog.md18
-rw-r--r--script/core/completion.lua49
-rw-r--r--test/completion/common.lua27
3 files changed, 92 insertions, 2 deletions
diff --git a/changelog.md b/changelog.md
index 6dd5f965..002cee3b 100644
--- a/changelog.md
+++ b/changelog.md
@@ -10,6 +10,24 @@
+ `different-requires`
* `NEW` supports `---@CustomClass<string, number>`
* `NEW` supports `$/cancelRequest`
+* `NEW` supports `EventEmitter`
+ ```lua
+ --- @class Emit
+ --- @field on fun(eventName: string, cb: function)
+ --- @field on fun(eventName: '"died"', cb: fun(i: integer))
+ --- @field on fun(eventName: '"won"', cb: fun(s: string))
+ local emit = {}
+
+ emit:on(--[[support autocomplete fr "died" and "won"]])
+
+ emit:on("died", function (i)
+ -- should be i: integer
+ end)
+
+ emit:on('won', function (s)
+ -- should be s: string
+ end)
+ ```
* `CHG` hover: improve showing multi defines
* `CHG` hover: improve showing multi comments at enums
* `CHG` hint: `Lua.hint.paramName` now supports `Disable`, `Literal` and `All`
diff --git a/script/core/completion.lua b/script/core/completion.lua
index d27a32c5..a45c7a08 100644
--- a/script/core/completion.lua
+++ b/script/core/completion.lua
@@ -19,6 +19,7 @@ local lang = require 'language'
local lookBackward = require 'core.look-backward'
local guide = require 'parser.guide'
local infer = require 'core.infer'
+local noder = require 'core.noder'
local DiagnosticModes = {
'disable-next-line',
@@ -1315,7 +1316,7 @@ local function pushCallEnumsAndFuncs(defs)
return results
end
-local function getCallEnumsAndFuncs(source, index, oop)
+local function getCallEnumsAndFuncs(source, index, oop, call)
if source.type == 'function' and source.bindDocs then
if not source.args then
return
@@ -1361,6 +1362,50 @@ local function getCallEnumsAndFuncs(source, index, oop)
return pushCallEnumsAndFuncs(vm.getDefs(arg.extends))
end
end
+ if source.type == 'doc.field.name' then
+ local currentIndex = index
+ if oop then
+ currentIndex = index - 1
+ end
+ local class = source.parent.class
+ if not class then
+ return
+ end
+ local results = {}
+ if currentIndex == 1 then
+ for _, doc in ipairs(class.fields) do
+ if doc.field ~= source
+ and doc.field[1] == source[1] then
+ local eventName = noder.getFieldEventName(doc)
+ if eventName then
+ results[#results+1] = {
+ label = ('%q'):format(eventName),
+ description = doc.comment,
+ kind = define.CompletionItemKind.EnumMember,
+ }
+ end
+ end
+ end
+ elseif currentIndex == 2 then
+ local myEventName = call.args[index - 1][1]
+ for _, doc in ipairs(class.fields) do
+ if doc.field ~= source
+ and doc.field[1] == source[1] then
+ local eventName = noder.getFieldEventName(doc)
+ if eventName and eventName == myEventName then
+ local docFunc = doc.extends.types[1].args[2].extends.types[1]
+ results[#results+1] = {
+ label = infer.viewDocFunction(docFunc),
+ description = doc.comment,
+ kind = define.CompletionItemKind.Function,
+ insertText = buildInsertDocFunction(docFunc),
+ }
+ end
+ end
+ end
+ end
+ return results
+ end
end
local function findCall(state, text, position)
@@ -1494,7 +1539,7 @@ local function tryCallArg(state, text, position, results)
local defs = vm.getDefs(call.node)
for _, def in ipairs(defs) do
def = searcher.getObjectValue(def) or def
- local enums = getCallEnumsAndFuncs(def, argIndex, oop)
+ local enums = getCallEnumsAndFuncs(def, argIndex, oop, call)
if enums then
mergeEnums(myResults, enums, arg)
end
diff --git a/test/completion/common.lua b/test/completion/common.lua
index c5740e15..62569d01 100644
--- a/test/completion/common.lua
+++ b/test/completion/common.lua
@@ -2632,3 +2632,30 @@ class2:<??>
{
[1] = EXISTS,
}
+
+TEST [[
+--- @class Emit
+--- @field on fun(eventName: string, cb: function)
+--- @field on fun(eventName: '"died"', cb: fun(i: integer))
+--- @field on fun(eventName: '"won"', cb: fun(s: string))
+local emit = {}
+
+emit:on('<??>')
+]]
+(EXISTS)
+
+TEST [[
+--- @class Emit
+--- @field on fun(eventName: string, cb: function)
+--- @field on fun(eventName: '"died"', cb: fun(i: integer))
+--- @field on fun(eventName: '"won"', cb: fun(s: string))
+local emit = {}
+
+emit:on('won', <??>)
+]]
+{
+ [1] = {
+ label = 'fun(s: string)',
+ kind = define.CompletionItemKind.Function,
+ }
+}