Skip to content

Commit 3b9bde4

Browse files
committed
completion: support delegate
1 parent 6592c2c commit 3b9bde4

File tree

4 files changed

+66
-17
lines changed

4 files changed

+66
-17
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.6.0
44
* `NEW` completion: auto require local modules
5+
* `NEW` completion: support delegate
56
* `NEW` hover: show function by keyword `function`
67
* `NEW` code action: swap params
78
* `CHG` standalone: unbind the relative path between binaries and scripts

script/core/completion.lua

+18-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local vm = require 'vm'
66
local getLabel = require 'core.hover.label'
77
local getName = require 'core.hover.name'
88
local getArg = require 'core.hover.arg'
9+
local getReturn = require 'core.hover.return'
910
local getDesc = require 'core.hover.description'
1011
local getHover = require 'core.hover'
1112
local config = require 'config'
@@ -939,8 +940,9 @@ local function mergeEnums(a, b, source)
939940
mark[label] = true
940941
local result = {
941942
label = label,
942-
kind = define.CompletionItemKind.EnumMember,
943+
kind = enum.kind,
943944
description = enum.description,
945+
insertText = enum.insertText,
944946
textEdit = source and {
945947
start = source.start,
946948
finish = source.finish,
@@ -1140,6 +1142,17 @@ local function trySymbol(ast, text, offset, results)
11401142
end
11411143
end
11421144

1145+
local function buildInsertDocFunction(doc)
1146+
local args = {}
1147+
for i, arg in ipairs(doc.args) do
1148+
args[i] = ('${%d:%s}'):format(i, arg.name[1])
1149+
end
1150+
return ([[
1151+
function (%s)
1152+
$0
1153+
end]]):format(table.concat(args, ', '))
1154+
end
1155+
11431156
local function getCallEnums(source, index)
11441157
if source.type == 'function' and source.bindDocs then
11451158
if not source.args then
@@ -1167,12 +1180,14 @@ local function getCallEnums(source, index)
11671180
kind = define.CompletionItemKind.EnumMember,
11681181
}
11691182
end
1170-
for _, unit in ipairs(vm.getDocTypes(doc.extends)) do
1183+
for _, unit in ipairs(vm.getDocTypeUnits(doc.extends)) do
11711184
if unit.type == 'doc.type.function' then
1185+
local text = files.getText(guide.getUri(unit))
11721186
enums[#enums+1] = {
1173-
label = guide.getDocTypeUnitName(nil, unit),
1187+
label = text:sub(unit.start, unit.finish),
11741188
description = doc.comment,
11751189
kind = define.CompletionItemKind.Function,
1190+
insertText = buildInsertDocFunction(unit),
11761191
}
11771192
end
11781193
end

script/vm/getDocs.lua

+29
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,35 @@ function vm.getDocEnums(doc, mark, results)
7474
return results
7575
end
7676

77+
function vm.getDocTypeUnits(doc, mark, results)
78+
mark = mark or {}
79+
if mark[doc] then
80+
return nil
81+
end
82+
mark[doc] = true
83+
results = results or {}
84+
for _, enum in ipairs(doc.enums) do
85+
results[#results+1] = enum
86+
end
87+
for _, resume in ipairs(doc.resumes) do
88+
results[#results+1] = resume
89+
end
90+
for _, unit in ipairs(doc.types) do
91+
if unit.type == 'doc.type.name' then
92+
for _, other in ipairs(vm.getDocTypes(unit[1])) do
93+
if other.type == 'doc.alias.name' then
94+
vm.getDocTypeUnits(other.parent.extends, mark, results)
95+
elseif other.type == 'doc.class.name' then
96+
results[#results+1] = other
97+
end
98+
end
99+
else
100+
results[#results+1] = unit
101+
end
102+
end
103+
return results
104+
end
105+
77106
function vm.getDocTypes(name)
78107
local cache = vm.getCache('getDocTypes')[name]
79108
if cache ~= nil then

test/completion/init.lua

+18-14
Original file line numberDiff line numberDiff line change
@@ -1953,17 +1953,21 @@ vvv$
19531953
},
19541954
}
19551955

1956-
--TEST [[
1957-
-----@param callback fun(x: number, y: number):string
1958-
--local function f(callback)
1959-
--
1960-
--f($)
1961-
--]]
1962-
--{
1963-
-- {
1964-
-- label = 'fun(x: number, y: number):string',
1965-
-- detail = 'function',
1966-
-- kind = define.CompletionItemKind.Function,
1967-
-- description = EXISTS,
1968-
-- },
1969-
--}
1956+
Cared['insertText'] = true
1957+
TEST [[
1958+
---@param callback fun(x: number, y: number):string
1959+
local function f(callback) end
1960+
1961+
f($)
1962+
]]
1963+
{
1964+
{
1965+
label = 'fun(x: number, y: number):string',
1966+
kind = define.CompletionItemKind.Function,
1967+
insertText = [[
1968+
function (${1:x}, ${2:y})
1969+
$0
1970+
end]],
1971+
},
1972+
}
1973+
Cared['insertText'] = nil

0 commit comments

Comments
 (0)