|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.util.completion; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.completion.CompletionResultSet; |
| 4 | +import com.intellij.codeInsight.completion.CompletionUtil; |
| 5 | +import com.intellij.codeInsight.completion.InsertHandler; |
| 6 | +import com.intellij.codeInsight.completion.InsertionContext; |
| 7 | +import com.intellij.codeInsight.lookup.LookupElement; |
| 8 | +import com.intellij.openapi.actionSystem.ActionManager; |
| 9 | +import com.intellij.openapi.keymap.KeymapUtil; |
| 10 | +import com.intellij.psi.PsiElement; |
| 11 | +import com.jetbrains.php.PhpBundle; |
| 12 | +import com.jetbrains.php.PhpIndex; |
| 13 | +import com.jetbrains.php.completion.PhpCompletionUtil; |
| 14 | +import com.jetbrains.php.completion.PhpLookupElement; |
| 15 | +import com.jetbrains.php.completion.PhpVariantsUtil; |
| 16 | +import com.jetbrains.php.completion.insert.PhpInsertHandlerUtil; |
| 17 | +import com.jetbrains.php.lang.psi.elements.Field; |
| 18 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 19 | +import com.jetbrains.php.lang.psi.elements.PhpNamedElement; |
| 20 | +import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionProvider; |
| 21 | +import fr.adrienbrault.idea.symfony2plugin.codeInsight.GotoCompletionProviderLookupArguments; |
| 22 | +import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; |
| 23 | +import org.jetbrains.annotations.NotNull; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.List; |
| 29 | +import java.util.stream.Collectors; |
| 30 | + |
| 31 | +public class PhpConstGotoCompletionProvider extends GotoCompletionProvider { |
| 32 | + |
| 33 | + private static final String[] SPECIAL_STUB_CONSTANTS = new String[]{"true", "false", "null"}; |
| 34 | + private static final String SCOPE_OPERATOR = "::"; |
| 35 | + |
| 36 | + public PhpConstGotoCompletionProvider(@NotNull PsiElement element) { |
| 37 | + super(element); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void getLookupElements(@NotNull GotoCompletionProviderLookupArguments arguments) { |
| 42 | + PhpIndex phpIndex = PhpIndex.getInstance(this.getProject()); |
| 43 | + CompletionResultSet resultSet = arguments.getResultSet(); |
| 44 | + |
| 45 | + final String prefix = getElement().getText().replace(CompletionUtil.DUMMY_IDENTIFIER_TRIMMED, ""); |
| 46 | + |
| 47 | + // Class constants: !php/const Foo::<caret> |
| 48 | + if (prefix.contains(SCOPE_OPERATOR)) { |
| 49 | + String classFQN = prefix.substring(0, getElement().getText().indexOf(SCOPE_OPERATOR)); |
| 50 | + PhpClass phpClass = PhpElementsUtil.getClassInterface(this.getProject(), classFQN); |
| 51 | + if (phpClass != null) { |
| 52 | + // reset the prefix matcher, starting after :: |
| 53 | + resultSet = resultSet.withPrefixMatcher(prefix.substring(prefix.indexOf(SCOPE_OPERATOR) + 2)); |
| 54 | + resultSet.addAllElements(PhpVariantsUtil.getLookupItems(phpClass.getFields().stream().filter(Field::isConstant).collect(Collectors.toList()), false, null)); |
| 55 | + } |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + Collection<LookupElement> elements = new ArrayList<>(); |
| 60 | + |
| 61 | + // Global constants: !php/const BAR |
| 62 | + for (String constantName : phpIndex.getAllConstantNames(resultSet.getPrefixMatcher())) { |
| 63 | + if (Arrays.asList(SPECIAL_STUB_CONSTANTS).contains(constantName)) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + elements.addAll(PhpVariantsUtil.getLookupItems(phpIndex.getConstantsByName(constantName), false, null)); |
| 67 | + } |
| 68 | + |
| 69 | + if (arguments.getParameters().getInvocationCount() <= 1) { |
| 70 | + String completionShortcut = KeymapUtil.getFirstKeyboardShortcutText(ActionManager.getInstance().getAction("CodeCompletion")); |
| 71 | + resultSet.addLookupAdvertisement(PhpBundle.message("completion.press.again.to.see.more.variants", completionShortcut)); |
| 72 | + |
| 73 | + // Classes and interfaces: !php/const Foo |
| 74 | + for (String className : phpIndex.getAllClassNames(resultSet.getPrefixMatcher())) { |
| 75 | + addAllClasses(elements, phpIndex.getClassesByName(className)); |
| 76 | + addAllClasses(elements, phpIndex.getInterfacesByName(className)); |
| 77 | + } |
| 78 | + } else { |
| 79 | + // Constants from all classes and interfaces: !php/const Foo::BAR |
| 80 | + for (String className : phpIndex.getAllClassNames(null)) { |
| 81 | + addAllClassConstants(elements, phpIndex.getClassesByName(className)); |
| 82 | + addAllClassConstants(elements, phpIndex.getInterfacesByName(className)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + arguments.addAllElements(elements); |
| 87 | + } |
| 88 | + |
| 89 | + private void addAllClasses(Collection<LookupElement> elements, Collection<PhpClass> classes) { |
| 90 | + for (PhpClass phpClass : classes) { |
| 91 | + // Filter by classes only with constants (including inherited constants) |
| 92 | + if (PhpElementsUtil.hasClassConstantFields(phpClass)) { |
| 93 | + elements.add(wrapClassInsertHandler(new MyPhpLookupElement(phpClass))); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private void addAllClassConstants(Collection<LookupElement> elements, Collection<PhpClass> classes) { |
| 99 | + for (PhpClass phpClass : classes) { |
| 100 | + // All class constants |
| 101 | + List<Field> fields = Arrays.stream(phpClass.getOwnFields()).filter(Field::isConstant).collect(Collectors.toList()); |
| 102 | + for (PhpNamedElement field : fields) { |
| 103 | + // Foo::BAR |
| 104 | + String lookupString = phpClass.getName() + SCOPE_OPERATOR + field.getName(); |
| 105 | + elements.add(wrapClassConstInsertHandler(new MyPhpLookupElement(field, lookupString))); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private static MyPhpLookupElement wrapClassInsertHandler(MyPhpLookupElement lookupElement) { |
| 111 | + return lookupElement.withInsertHandler(PhpClassWithScopeOperatorInsertHandler.getInstance()); |
| 112 | + } |
| 113 | + |
| 114 | + private static MyPhpLookupElement wrapClassConstInsertHandler(MyPhpLookupElement lookupElement) { |
| 115 | + return lookupElement.withInsertHandler(PhpReferenceTrimBackslashInsertHandler.getInstance()); |
| 116 | + } |
| 117 | + |
| 118 | + private static class MyPhpLookupElement extends PhpLookupElement { |
| 119 | + |
| 120 | + MyPhpLookupElement(@NotNull PhpNamedElement namedElement) { |
| 121 | + super(namedElement); |
| 122 | + } |
| 123 | + |
| 124 | + MyPhpLookupElement(@NotNull PhpNamedElement namedElement, @NotNull String lookupString) { |
| 125 | + super(namedElement); |
| 126 | + this.lookupString = lookupString; |
| 127 | + } |
| 128 | + |
| 129 | + MyPhpLookupElement withInsertHandler(InsertHandler insertHandler) { |
| 130 | + this.handler = insertHandler; |
| 131 | + return this; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public static class PhpClassWithScopeOperatorInsertHandler extends PhpReferenceTrimBackslashInsertHandler { |
| 136 | + private static final PhpClassWithScopeOperatorInsertHandler instance = new PhpClassWithScopeOperatorInsertHandler(); |
| 137 | + |
| 138 | + public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) { |
| 139 | + super.handleInsert(context, lookupElement); |
| 140 | + |
| 141 | + if (context.getCompletionChar() == ':') { |
| 142 | + context.setAddCompletionChar(false); |
| 143 | + } |
| 144 | + |
| 145 | + if (!PhpInsertHandlerUtil.isStringAtCaret(context.getEditor(), SCOPE_OPERATOR)) { |
| 146 | + PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), SCOPE_OPERATOR); |
| 147 | + } |
| 148 | + |
| 149 | + PhpCompletionUtil.showCompletion(context); |
| 150 | + } |
| 151 | + |
| 152 | + public static PhpClassWithScopeOperatorInsertHandler getInstance() { |
| 153 | + return instance; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments