|
| 1 | +/* |
| 2 | + * Minecraft Development for IntelliJ |
| 3 | + * |
| 4 | + * https://mcdev.io/ |
| 5 | + * |
| 6 | + * Copyright (C) 2026 minecraft-dev |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published |
| 10 | + * by the Free Software Foundation, version 3.0 only. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.demonwav.mcdev.platform.bukkit.completion |
| 22 | + |
| 23 | +import com.demonwav.mcdev.platform.bukkit.util.BukkitConstants |
| 24 | +import com.demonwav.mcdev.util.findContainingClass |
| 25 | +import com.demonwav.mcdev.util.findContainingMethod |
| 26 | +import com.intellij.codeInsight.completion.CompletionParameters |
| 27 | +import com.intellij.codeInsight.completion.CompletionProvider |
| 28 | +import com.intellij.codeInsight.completion.CompletionResultSet |
| 29 | +import com.intellij.codeInsight.completion.PrioritizedLookupElement |
| 30 | +import com.intellij.codeInsight.lookup.LookupElementBuilder |
| 31 | +import com.intellij.icons.AllIcons |
| 32 | +import com.intellij.psi.PsiModifier |
| 33 | +import com.intellij.psi.impl.JavaPsiFacadeEx |
| 34 | +import com.intellij.psi.search.GlobalSearchScope |
| 35 | +import com.intellij.psi.search.searches.ClassInheritorsSearch |
| 36 | +import com.intellij.util.ProcessingContext |
| 37 | + |
| 38 | +class BukkitEventHandlerCompletionProvider : CompletionProvider<CompletionParameters>() { |
| 39 | + |
| 40 | + override fun addCompletions( |
| 41 | + completionParameters: CompletionParameters, |
| 42 | + processingContext: ProcessingContext, |
| 43 | + completionResultSet: CompletionResultSet |
| 44 | + ) { |
| 45 | + val prefix = completionResultSet.prefixMatcher.prefix |
| 46 | + if (!prefix.startsWith("on") || prefix.length == 2) { |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + val position = completionParameters.position |
| 51 | + val containingClass = position.findContainingClass() ?: return |
| 52 | + val project = position.project |
| 53 | + val facade = JavaPsiFacadeEx.getInstance(project) |
| 54 | + |
| 55 | + val eventListenerClass = facade.findClass(BukkitConstants.LISTENER_CLASS, GlobalSearchScope.allScope(project)) ?: return |
| 56 | + if (!containingClass.isInheritor(eventListenerClass, true)) { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + if (position.findContainingMethod() != null) { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + val scope = GlobalSearchScope.allScope(project) |
| 65 | + val eventBaseClass = facade.findClass(BukkitConstants.EVENT_CLASS, scope) ?: return |
| 66 | + val eventNameFilter = prefix.substring(2).lowercase() |
| 67 | + |
| 68 | + ClassInheritorsSearch.search(eventBaseClass, scope, true) |
| 69 | + .forEach { psiClass -> |
| 70 | + if (psiClass.isInterface || psiClass.hasModifierProperty(PsiModifier.ABSTRACT)) { |
| 71 | + return@forEach |
| 72 | + } |
| 73 | + |
| 74 | + val eventSimpleName = psiClass.name ?: return@forEach |
| 75 | + if (eventNameFilter.isNotEmpty() && !eventSimpleName.lowercase().startsWith(eventNameFilter)) { |
| 76 | + return@forEach |
| 77 | + } |
| 78 | + |
| 79 | + val lookupString = "on$eventSimpleName" |
| 80 | + val qualifiedName = psiClass.qualifiedName |
| 81 | + |
| 82 | + val element = LookupElementBuilder |
| 83 | + .create(lookupString) |
| 84 | + .withPresentableText("$lookupString()") |
| 85 | + .withTailText(" - $qualifiedName", true) |
| 86 | + .withTypeText("@EventHandler") |
| 87 | + .withIcon(AllIcons.Nodes.Method) |
| 88 | + .withBaseLookupString(lookupString) |
| 89 | + .withBoldness(true) |
| 90 | + .withInsertHandler(BukkitEventHandlerInsertHandler(psiClass)) |
| 91 | + |
| 92 | + completionResultSet.addElement( |
| 93 | + PrioritizedLookupElement.withPriority(element, 100.0) |
| 94 | + ) |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments