|
16 | 16 |
|
17 | 17 | package com.tang.intellij.lua.editor.completion;
|
18 | 18 |
|
| 19 | +import com.intellij.codeInsight.completion.BasicInsertHandler; |
| 20 | +import com.intellij.codeInsight.completion.InsertHandler; |
| 21 | +import com.intellij.codeInsight.completion.InsertionContext; |
19 | 22 | import com.intellij.codeInsight.lookup.LookupElement;
|
20 | 23 | import com.intellij.codeInsight.lookup.LookupElementBuilder;
|
| 24 | +import com.intellij.codeInsight.lookup.LookupElementPresentation; |
21 | 25 | import com.intellij.openapi.project.Project;
|
| 26 | +import com.intellij.openapi.util.text.StringUtil; |
22 | 27 | import com.tang.intellij.lua.lang.LuaIcons;
|
23 | 28 | import com.tang.intellij.lua.stubs.index.LuaClassIndex;
|
| 29 | +import org.jetbrains.annotations.NotNull; |
| 30 | +import org.jetbrains.annotations.Nullable; |
24 | 31 |
|
| 32 | +import javax.swing.*; |
25 | 33 | import java.util.Collection;
|
26 | 34 |
|
27 | 35 | /**
|
28 | 36 | * LuaLookupElement
|
29 | 37 | * Created by TangZX on 2016/12/16.
|
30 | 38 | */
|
31 |
| -public class LuaLookupElement { |
| 39 | +public class LuaLookupElement extends LookupElement implements Comparable<LookupElement> { |
32 | 40 | public static void fillTypes(Project project, Collection<LookupElement> results) {
|
33 | 41 | Collection<String> collection = LuaClassIndex.getInstance().getAllKeys(project);
|
34 | 42 | collection.forEach(className -> {
|
35 | 43 | results.add(LookupElementBuilder.create(className).withIcon(LuaIcons.CLASS));
|
36 | 44 | });
|
37 | 45 | }
|
| 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 | + } |
38 | 131 | }
|
0 commit comments