Skip to content

Commit 16890cf

Browse files
committed
优化代码补全的表现
1 parent b55a01f commit 16890cf

File tree

3 files changed

+73
-39
lines changed

3 files changed

+73
-39
lines changed

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

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package com.tang.intellij.lua.editor.completion
44

55
import com.intellij.codeInsight.lookup.LookupElement
66
import com.intellij.psi.tree.IElementType
7+
import com.tang.intellij.lua.lang.LuaIcons
78
import com.tang.intellij.lua.psi.LuaClassField
89
import com.tang.intellij.lua.psi.LuaClassMember
910
import com.tang.intellij.lua.psi.LuaParamInfo
@@ -20,51 +21,68 @@ object LookupElementFactory {
2021

2122
fun createGuessableLookupElement(name: String, psi: LuaPsiElement, ty: ITy, icon: Icon?): LookupElement {
2223
val element = LuaLookupElement(name)
23-
element.kind = CompletionItemKind.Value
24+
element.kind = when (icon) {
25+
LuaIcons.LOCAL_VAR -> {
26+
CompletionItemKind.Variable
27+
}
28+
LuaIcons.PARAMETER -> {
29+
CompletionItemKind.TypeParameter
30+
}
31+
else -> {
32+
CompletionItemKind.Value
33+
}
34+
}
35+
element.additionDetailDescription = ty.displayName
2436
return element
2537
}
2638

27-
fun createFunctionLookupElement(name: String,
28-
psi: LuaPsiElement,
29-
signature: IFunSignature,
30-
bold: Boolean,
31-
ty: ITyFunction,
32-
icon: Icon?): LookupElement {
39+
fun createFunctionLookupElement(
40+
name: String,
41+
psi: LuaPsiElement,
42+
signature: IFunSignature,
43+
bold: Boolean,
44+
ty: ITyFunction,
45+
icon: Icon?
46+
): LookupElement {
3347
val item = buildSignatureCompletionItem(name, signature, false)
3448
item.kind = CompletionItemKind.Function
3549

3650
return item
3751
}
3852

39-
fun createMethodLookupElement(clazzName: String,
40-
lookupString: String,
41-
classMember: LuaClassMember,
42-
signature: IFunSignature,
43-
bold: Boolean,
44-
isColonStyle: Boolean,
45-
fnTy: ITyFunction,
46-
icon: Icon?): LuaLookupElement {
53+
fun createMethodLookupElement(
54+
clazzName: String,
55+
lookupString: String,
56+
classMember: LuaClassMember,
57+
signature: IFunSignature,
58+
bold: Boolean,
59+
isColonStyle: Boolean,
60+
fnTy: ITyFunction,
61+
icon: Icon?
62+
): LuaLookupElement {
4763
val item = buildSignatureCompletionItem(lookupString, signature, isColonStyle)
4864
item.kind = CompletionItemKind.Method
4965
item.itemText = "[$clazzName]"
5066
val file = classMember.containingFile?.virtualFile as? ILuaFile
5167
if (file != null) {
5268
item.data = "${file.uri}|${classMember.textOffset}"
5369
}
54-
if(classMember.isDeprecated){
70+
if (classMember.isDeprecated) {
5571
item.deprecated = true
5672
}
5773

5874
return item
5975
}
6076

61-
fun createShouldBeMethodLookupElement(clazzName: String,
62-
lookupString: String,
63-
classMember: LuaClassMember,
64-
signature: IFunSignature,
65-
bold: Boolean,
66-
fnTy: ITyFunction,
67-
icon: Icon?): LuaLookupElement {
77+
fun createShouldBeMethodLookupElement(
78+
clazzName: String,
79+
lookupString: String,
80+
classMember: LuaClassMember,
81+
signature: IFunSignature,
82+
bold: Boolean,
83+
fnTy: ITyFunction,
84+
icon: Icon?
85+
): LuaLookupElement {
6886
val item = buildSignatureCompletionItem(lookupString, signature, true)
6987
item.lookupString = ":${item.lookupString}"
7088
item.kind = CompletionItemKind.Method
@@ -73,32 +91,39 @@ object LookupElementFactory {
7391
if (file != null) {
7492
item.data = "${file.uri}|${classMember.textOffset}"
7593
}
76-
if(classMember.isDeprecated){
94+
if (classMember.isDeprecated) {
7795
item.deprecated = true
7896
}
7997

8098
return item
8199
}
82100

83-
fun createFieldLookupElement(clazzName: String,
84-
name: String,
85-
field: LuaClassField,
86-
ty: ITy?,
87-
bold: Boolean): LuaLookupElement {
101+
fun createFieldLookupElement(
102+
clazzName: String,
103+
name: String,
104+
field: LuaClassField,
105+
ty: ITy?,
106+
bold: Boolean
107+
): LuaLookupElement {
88108
val element = LuaLookupElement(name)
89109
element.kind = CompletionItemKind.Field
110+
element.additionDetailDescription = ty?.displayName
90111
val file = field.containingFile?.virtualFile as? ILuaFile
91112
if (file != null) {
92113
element.data = "${file.uri}|${field.textOffset}"
93114
}
94-
if(field.isDeprecated){
115+
if (field.isDeprecated) {
95116
element.deprecated = true
96117
}
97118

98119
return element
99120
}
100121

101-
private fun buildSignatureCompletionItem(name: String, signature: IFunSignature, isColonStyle: Boolean): LuaLookupElement {
122+
private fun buildSignatureCompletionItem(
123+
name: String,
124+
signature: IFunSignature,
125+
isColonStyle: Boolean
126+
): LuaLookupElement {
102127
var pIndex = 0
103128
val params = mutableListOf<LuaParamInfo>()
104129
if (isColonStyle) { //a:b()
@@ -112,22 +137,22 @@ object LookupElementFactory {
112137
}
113138
params.addAll(signature.params)
114139

115-
val lookupString = buildString {
116-
append(name)
140+
val item = LuaLookupElement(name)
141+
item.additionDetail = buildString {
117142
append("(")
118143
val strings = mutableListOf<String>()
119144
for (i in pIndex until params.size) {
120145
val p = params[i]
121146
strings.add(p.name)
122147
}
123-
if(signature.hasVarargs()){
148+
if (signature.hasVarargs()) {
124149
strings.add("...")
125150
}
126151
append(strings.joinToString(","))
127152
append(")")
128153
}
154+
item.additionDetailDescription = signature.returnTy.displayName
129155

130-
val item = LuaLookupElement(lookupString)
131156
if (pIndex >= params.size) {
132157
item.insertText = "$name()"
133158
} else {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class LuaLookupElement(label: String) : LookupElement(label) {
1111
var textEdit: TextEdit? = null
1212
var deprecated = false
1313
var isEnumMember = false
14+
var additionDetailDescription: String? = null
1415
var additionDetail: String? = null
1516
}
1617

@@ -34,9 +35,10 @@ val LookupElement.asCompletionItem: CompletionItem
3435
if (deprecated) {
3536
completionItem.tags = listOf(CompletionItemTag.Deprecated)
3637
}
37-
if(additionDetail != null){
38+
if(additionDetailDescription != null || additionDetail != null){
3839
val labelDetail = CompletionItemLabelDetails()
39-
labelDetail.description = additionDetail
40+
labelDetail.description = additionDetailDescription
41+
labelDetail.detail = additionDetail
4042
completionItem.labelDetails = labelDetail
4143
}
4244

EmmyLua-Common/src/main/java/com/tang/intellij/lua/editor/completion/ClassMemberCompletionProvider.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,10 @@ open class ClassMemberCompletionProvider : LuaCompletionProvider() {
242242
val name = classMember.name
243243
if (name != null) {
244244
fnTy.process(Processor {
245+
val context = SearchContext.get(classMember.project)
245246
val firstParam = it.getFirstParam(thisType, isColonStyle)
246247
val firstParamIsSelf = if (firstParam != null) {
247-
callType.subTypeOf(firstParam.ty, SearchContext.get(classMember.project), true)
248+
callType.subTypeOf(firstParam.ty, context, true)
248249
} else {
249250
false
250251
}
@@ -268,7 +269,13 @@ open class ClassMemberCompletionProvider : LuaCompletionProvider() {
268269
val ele = handlerProcessor?.process(element, classMember, fnTy) ?: element
269270
completionResultSet.addElement(ele)
270271
// correction completion
271-
if (!isColonStyle && firstParamIsSelf) {
272+
if (!isColonStyle && firstParamIsSelf
273+
&& callType != Ty.STRING
274+
// workaround now
275+
&& callType is TyClass
276+
&& callType !is TySerializedClass
277+
&& callType !is TyTable
278+
) {
272279
val colonElement = LookupElementFactory.createShouldBeMethodLookupElement(
273280
clazzName,
274281
lookupString,

0 commit comments

Comments
 (0)