Skip to content

Commit 22cf231

Browse files
committed
lookup element
1 parent 000d223 commit 22cf231

File tree

1 file changed

+94
-1
lines changed

1 file changed

+94
-1
lines changed

src/main/java/com/tang/intellij/lua/editor/completion/LuaLookupElement.java

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,116 @@
1616

1717
package com.tang.intellij.lua.editor.completion;
1818

19+
import com.intellij.codeInsight.completion.BasicInsertHandler;
20+
import com.intellij.codeInsight.completion.InsertHandler;
21+
import com.intellij.codeInsight.completion.InsertionContext;
1922
import com.intellij.codeInsight.lookup.LookupElement;
2023
import com.intellij.codeInsight.lookup.LookupElementBuilder;
24+
import com.intellij.codeInsight.lookup.LookupElementPresentation;
2125
import com.intellij.openapi.project.Project;
26+
import com.intellij.openapi.util.text.StringUtil;
2227
import com.tang.intellij.lua.lang.LuaIcons;
2328
import com.tang.intellij.lua.stubs.index.LuaClassIndex;
29+
import org.jetbrains.annotations.NotNull;
30+
import org.jetbrains.annotations.Nullable;
2431

32+
import javax.swing.*;
2533
import java.util.Collection;
2634

2735
/**
2836
* LuaLookupElement
2937
* Created by TangZX on 2016/12/16.
3038
*/
31-
public class LuaLookupElement {
39+
public class LuaLookupElement extends LookupElement implements Comparable<LookupElement> {
3240
public static void fillTypes(Project project, Collection<LookupElement> results) {
3341
Collection<String> collection = LuaClassIndex.getInstance().getAllKeys(project);
3442
collection.forEach(className -> {
3543
results.add(LookupElementBuilder.create(className).withIcon(LuaIcons.CLASS));
3644
});
3745
}
46+
47+
48+
protected final String myLookupString;
49+
protected final String myTypeText;
50+
protected final boolean isBold;
51+
protected final Icon myIcon;
52+
private final Icon myTypeIcon;
53+
protected final String myTailText;
54+
protected InsertHandler<LookupElement> myHandler;
55+
56+
public LuaLookupElement(@NotNull final String lookupString,
57+
@Nullable final String tailText,
58+
@Nullable final String typeText, final boolean bold,
59+
@Nullable final Icon icon,
60+
@Nullable final Icon typeIcon,
61+
@NotNull final InsertHandler<LookupElement> handler) {
62+
myLookupString = lookupString;
63+
myTailText = tailText;
64+
myTypeText = typeText;
65+
isBold = bold;
66+
myIcon = icon;
67+
myTypeIcon = typeIcon;
68+
myHandler = handler;
69+
}
70+
71+
public LuaLookupElement(@NotNull final String lookupString,
72+
@Nullable final String tailText,
73+
@Nullable final String typeText, final boolean bold,
74+
@Nullable final Icon icon,
75+
@Nullable final Icon typeIcon) {
76+
this(lookupString, tailText, typeText, bold, icon, typeIcon, new BasicInsertHandler<>());
77+
}
78+
79+
public LuaLookupElement(
80+
@NotNull final String lookupString,
81+
final boolean bold,
82+
@Nullable final Icon icon) {
83+
this(lookupString, null, null, bold, icon, null, new BasicInsertHandler<>());
84+
}
85+
86+
@NotNull
87+
public String getLookupString() {
88+
return myLookupString;
89+
}
90+
91+
@Nullable
92+
public String getTailText() {
93+
return !StringUtil.isEmpty(myTailText) ? myTailText : null;
94+
}
95+
96+
@Nullable
97+
protected String getTypeText() {
98+
return !StringUtil.isEmpty(myTypeText) ? myTypeText : null;
99+
}
100+
101+
public Icon getIcon() {
102+
return myIcon;
103+
}
104+
105+
106+
public Icon getTypeIcon() {
107+
return myTypeIcon;
108+
}
109+
110+
@Override
111+
public void handleInsert(InsertionContext context) {
112+
myHandler.handleInsert(context, this);
113+
}
114+
115+
public void setHandler(InsertHandler<LookupElement> handler) {
116+
myHandler = handler;
117+
}
118+
119+
@Override
120+
public void renderElement(LookupElementPresentation presentation) {
121+
presentation.setItemText(getLookupString());
122+
presentation.setItemTextBold(isBold);
123+
presentation.setTailText(getTailText());
124+
presentation.setTypeText(getTypeText(), getTypeIcon());
125+
presentation.setIcon(getIcon());
126+
}
127+
128+
public int compareTo(final LookupElement o) {
129+
return myLookupString.compareTo(o.getLookupString());
130+
}
38131
}

0 commit comments

Comments
 (0)