Skip to content

Commit 4b52013

Browse files
committed
支持回调函数补全
1 parent eb22b2a commit 4b52013

File tree

3 files changed

+117
-30
lines changed

3 files changed

+117
-30
lines changed

EmmyLua-Common/src/main/ext/com/tang/intellij/lua/editor/completion/LuaCompletionContributor.kt

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,19 @@ class LuaCompletionContributor : CompletionContributor() {
7676
//提示属性, 提示方法
7777
extend(CompletionType.BASIC, SHOW_CLASS_FIELD, ClassMemberCompletionProvider())
7878
//提示全局函数,local变量,local函数
79-
extend(CompletionType.BASIC, IN_NAME_EXPR, LocalAndGlobalCompletionProvider(LocalAndGlobalCompletionProvider.ALL))
79+
extend(
80+
CompletionType.BASIC,
81+
IN_NAME_EXPR,
82+
LocalAndGlobalCompletionProvider(LocalAndGlobalCompletionProvider.ALL)
83+
)
8084
// 表的[]索引方式提示
8185
extend(CompletionType.BASIC, IN_TABLE_STRING_INDEX, TableStringIndexCompletionProvider())
8286
// lua5.4
8387
extend(CompletionType.BASIC, ATTRIBUTE, AttributeCompletionProvider())
8488
// enum
8589
extend(CompletionType.BASIC, SHOW_ENUM, EnumCompletionProvider())
8690

91+
extend(CompletionType.BASIC, SHOW_CALLBACK, CallbackCompletionProvider())
8792
}
8893

8994
/*override fun fillCompletionVariants(parameters: CompletionParameters, result: CompletionResultSet) {
@@ -121,57 +126,63 @@ class LuaCompletionContributor : CompletionContributor() {
121126
private val IGNORE_SET = TokenSet.create(LuaTypes.STRING, LuaTypes.NUMBER, LuaTypes.CONCAT)
122127

123128
private val SHOW_CLASS_FIELD = psiElement(LuaTypes.ID)
124-
.withParent(LuaIndexExpr::class.java)
129+
.withParent(LuaIndexExpr::class.java)
125130

126131
private val IN_FUNC_NAME = psiElement(LuaTypes.ID)
127-
.withParent(LuaIndexExpr::class.java)
128-
.inside(LuaClassMethodName::class.java)
132+
.withParent(LuaIndexExpr::class.java)
133+
.inside(LuaClassMethodName::class.java)
129134
private val AFTER_FUNCTION = psiElement()
130-
.afterLeaf(psiElement(LuaTypes.FUNCTION))
135+
.afterLeaf(psiElement(LuaTypes.FUNCTION))
131136
private val IN_CLASS_METHOD_NAME = psiElement().andOr(IN_FUNC_NAME, AFTER_FUNCTION)
132137

133138
private val IN_NAME_EXPR = psiElement(LuaTypes.ID)
134-
.withParent(LuaNameExpr::class.java)
139+
.withParent(LuaNameExpr::class.java)
135140

136141
private val SHOW_OVERRIDE = psiElement()
137-
.withParent(LuaClassMethodName::class.java)
142+
.withParent(LuaClassMethodName::class.java)
138143
private val IN_CLASS_METHOD = psiElement(LuaTypes.ID)
139-
.withParent(LuaNameExpr::class.java)
140-
.inside(LuaClassMethodDef::class.java)
144+
.withParent(LuaNameExpr::class.java)
145+
.inside(LuaClassMethodDef::class.java)
141146
private val SHOW_REQUIRE_PATH = psiElement(LuaTypes.STRING)
142-
.withParent(
143-
psiElement(LuaTypes.LITERAL_EXPR).withParent(
144-
psiElement(LuaArgs::class.java).afterSibling(
145-
psiElement().with(RequireLikePatternCondition())
146-
)
147-
)
147+
.withParent(
148+
psiElement(LuaTypes.LITERAL_EXPR).withParent(
149+
psiElement(LuaArgs::class.java).afterSibling(
150+
psiElement().with(RequireLikePatternCondition())
151+
)
148152
)
153+
)
149154

150155
private val SHOW_ENUM = psiElement(LuaTypes.ID)
151-
.withParent(psiElement(LuaNameExpr::class.java)
152-
.withParent(psiElement(LuaArgs::class.java))
153-
)
156+
.withParent(
157+
psiElement(LuaNameExpr::class.java)
158+
.withParent(psiElement(LuaArgs::class.java))
159+
)
154160

161+
private val SHOW_CALLBACK = psiElement(LuaTypes.ID)
162+
.withParent(
163+
psiElement(LuaNameExpr::class.java)
164+
.withParent(psiElement(LuaArgs::class.java))
165+
)
155166

156167
private val GOTO = psiElement(LuaTypes.ID).withParent(LuaGotoStat::class.java)
157168

158169
private val IN_TABLE_FIELD = psiElement().andOr(
159-
psiElement().withParent(
160-
psiElement(LuaTypes.NAME_EXPR).withParent(LuaTableField::class.java)
161-
),
162-
psiElement(LuaTypes.ID).withParent(LuaTableField::class.java)
170+
psiElement().withParent(
171+
psiElement(LuaTypes.NAME_EXPR).withParent(LuaTableField::class.java)
172+
),
173+
psiElement(LuaTypes.ID).withParent(LuaTableField::class.java)
163174
)
164175

165176
private val IN_TABLE_STRING_INDEX = psiElement().andOr(
166177
// psiElement(LuaTypes.LITERAL_EXPR).withParent(
167178
// psiElement(LuaIndexExpr::class.java)
168179
// ),
169-
psiElement(LuaTypes.STRING)
170-
.withParent(
171-
psiElement(LuaTypes.LITERAL_EXPR).withParent(
172-
psiElement(LuaIndexExpr::class.java)
173-
)
174-
)
180+
psiElement(LuaTypes.STRING)
181+
.withParent(
182+
psiElement(LuaTypes.LITERAL_EXPR).withParent(
183+
psiElement(LuaIndexExpr::class.java)
184+
)
185+
)
175186
)
176187

177188
private val ATTRIBUTE = psiElement(LuaTypes.ID).withParent(LuaAttribute::class.java)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.tang.intellij.lua.editor.completion
2+
3+
import com.intellij.codeInsight.completion.CompletionResultSet
4+
import com.intellij.openapi.progress.ProgressManager
5+
import com.intellij.psi.PsiElement
6+
import com.intellij.util.Processor
7+
import com.tang.intellij.lua.comment.psi.LuaDocTagClass
8+
import com.tang.intellij.lua.psi.LuaCallExpr
9+
import com.tang.intellij.lua.psi.LuaTypes
10+
import com.tang.intellij.lua.psi.search.LuaShortNamesManager
11+
import com.tang.intellij.lua.search.SearchContext
12+
import com.tang.intellij.lua.ty.*
13+
import com.tang.lsp.ILuaFile
14+
import org.eclipse.lsp4j.CompletionItemKind
15+
16+
class CallbackCompletionProvider : LuaCompletionProvider() {
17+
override fun addCompletions(session: CompletionSession) {
18+
val completionParameters = session.parameters
19+
val completionResultSet = session.resultSet
20+
21+
val psi = completionParameters.position
22+
val callExpr = psi.parent.parent.parent
23+
24+
if (callExpr is LuaCallExpr) {
25+
var activeParameter = 0
26+
var nCommas = 0
27+
28+
callExpr.args.firstChild?.let { firstChild ->
29+
var child: PsiElement? = firstChild
30+
while (child != null) {
31+
if (child.node.elementType == LuaTypes.COMMA) {
32+
activeParameter++
33+
nCommas++
34+
}
35+
child = child.nextSibling
36+
}
37+
}
38+
val searchContext = SearchContext.get(callExpr.project)
39+
callExpr.guessParentType(searchContext).let { parentType ->
40+
parentType.each { ty ->
41+
if (ty is ITyFunction) {
42+
val active = ty.findPerfectSignature(nCommas + 1)
43+
ty.process(Processor { sig ->
44+
if (sig == active) {
45+
if (activeParameter < sig.params.size) {
46+
sig.params[activeParameter].let {
47+
val paramType = it.ty
48+
if (paramType is TyFunction) {
49+
addCallback(paramType, searchContext, completionResultSet)
50+
}
51+
}
52+
}
53+
54+
}
55+
56+
true
57+
})
58+
}
59+
}
60+
}
61+
}
62+
}
63+
64+
65+
private fun addCallback(
66+
luaType: TyFunction,
67+
searchContext: SearchContext,
68+
completionResultSet: CompletionResultSet
69+
) {
70+
val params = luaType.mainSignature.params
71+
val element = LuaLookupElement("function(${params.map { it.name }.joinToString(", ")}) end")
72+
element.kind = CompletionItemKind.Function
73+
completionResultSet.addElement(element)
74+
}
75+
}

EmmyLua-LS/src/main/kotlin/com/tang/vscode/documentation/DocRenderer.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,11 @@ internal fun renderFieldDef(sb: StringBuilder, def: LuaDocTagField) {
171171

172172
internal fun renderDocParam(sb: StringBuilder, child: LuaDocTagParam) {
173173
val paramNameRef = child.paramNameRef
174-
if (paramNameRef != null) {
174+
val commentString = child.commentString
175+
if (paramNameRef != null && commentString != null && commentString.text.isNotEmpty()) {
175176
sb.appendLine("@_param_ `${paramNameRef.text}`: ")
176177
renderTypeUnion(null, null, sb, child.ty)
177-
renderCommentString(" ", null, sb, child.commentString)
178+
renderCommentString(" ", null, sb, commentString)
178179
}
179180
}
180181

0 commit comments

Comments
 (0)