-
Notifications
You must be signed in to change notification settings - Fork 227
Add completion contributor for Bukkit event handlers #2613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Earthcomputer
merged 6 commits into
minecraft-dev:dev
from
Leguan16:feat/bukkit-event-handler-completion-comtributor
May 2, 2026
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e7f4d43
Add completion contributor for Bukkit event handlers
Leguan16 f8e9e4f
cleanup code
Leguan16 a6fff04
add license header
Leguan16 a13919e
Save template as liveTemplate
Strokkur424 998afd2
add license headers
Leguan16 f7361d1
don't stop processing completions & update year in license header
Leguan16 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/platform/bukkit/completion/BukkitEventHandlerCompletionContributor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.demonwav.mcdev.platform.bukkit.completion | ||
|
|
||
| import com.intellij.codeInsight.completion.CompletionContributor | ||
| import com.intellij.codeInsight.completion.CompletionType | ||
| import com.intellij.patterns.PlatformPatterns | ||
| import com.intellij.psi.PsiClass | ||
|
|
||
| class BukkitEventHandlerCompletionContributor : CompletionContributor() { | ||
| init { | ||
| extend( | ||
| CompletionType.BASIC, | ||
| PlatformPatterns.psiElement().inside(PsiClass::class.java), | ||
| BukkitEventHandlerCompletionProvider() | ||
| ) | ||
| } | ||
| } |
80 changes: 80 additions & 0 deletions
80
src/main/kotlin/platform/bukkit/completion/BukkitEventHandlerCompletionProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package com.demonwav.mcdev.platform.bukkit.completion | ||
|
|
||
| import com.intellij.codeInsight.completion.CompletionParameters | ||
| import com.intellij.codeInsight.completion.CompletionProvider | ||
| import com.intellij.codeInsight.completion.CompletionResultSet | ||
| import com.intellij.codeInsight.completion.PrioritizedLookupElement | ||
| import com.intellij.codeInsight.lookup.LookupElementBuilder | ||
| import com.intellij.icons.AllIcons | ||
| import com.intellij.openapi.project.Project | ||
| import com.intellij.psi.PsiClass | ||
| import com.intellij.psi.PsiMethod | ||
| import com.intellij.psi.PsiModifier | ||
| import com.intellij.psi.impl.JavaPsiFacadeEx | ||
| import com.intellij.psi.search.GlobalSearchScope | ||
| import com.intellij.psi.search.searches.ClassInheritorsSearch | ||
| import com.intellij.psi.util.PsiTreeUtil | ||
| import com.intellij.util.ProcessingContext | ||
| import org.jetbrains.annotations.NotNull | ||
|
|
||
| class BukkitEventHandlerCompletionProvider : CompletionProvider<CompletionParameters>() { | ||
| companion object { | ||
| const val EVENT_LISTENER = "org.bukkit.event.Listener" | ||
| private const val BUKKIT_EVENT_FQN = "org.bukkit.event.Event" | ||
| } | ||
|
|
||
| override fun addCompletions( | ||
| @NotNull completionParameters: CompletionParameters, | ||
| @NotNull processingContext: ProcessingContext, | ||
| @NotNull completionResultSet: CompletionResultSet | ||
| ) { | ||
| val prefix = completionResultSet.prefixMatcher.prefix | ||
| if (!prefix.startsWith("on") || prefix.length == 2) return | ||
|
Leguan16 marked this conversation as resolved.
Outdated
|
||
|
|
||
| val position = completionParameters.position | ||
| val containingClass = PsiTreeUtil.getParentOfType(position, PsiClass::class.java) ?: return | ||
|
Leguan16 marked this conversation as resolved.
Outdated
|
||
| val project: Project = position.project | ||
|
Leguan16 marked this conversation as resolved.
Outdated
|
||
| val facade = JavaPsiFacadeEx.getInstanceEx(project) | ||
|
Leguan16 marked this conversation as resolved.
Outdated
|
||
|
|
||
| val eventListenerClass = facade.findClass(EVENT_LISTENER, GlobalSearchScope.allScope(project)) ?: return | ||
| if (!containingClass.isInheritor(eventListenerClass, true)) return | ||
| if (PsiTreeUtil.getParentOfType(position, PsiMethod::class.java) != null) return | ||
|
Leguan16 marked this conversation as resolved.
Outdated
|
||
|
|
||
| val scope = GlobalSearchScope.allScope(project) | ||
| val eventBaseClass = facade.findClass(BUKKIT_EVENT_FQN, scope) ?: return | ||
|
|
||
| val eventNameFilter = prefix.substring(2).lowercase() | ||
|
|
||
| ClassInheritorsSearch.search(eventBaseClass, scope, true) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks expensive. However it's not so easy to cache effectively because the easiest way to do that is using the PSI modification count as the cache key, and since you're in a completion provider that is changing on every keystroke. Leave it as it is for now. |
||
| .forEach { psiClass -> | ||
| if (psiClass.isInterface || psiClass.hasModifierProperty(PsiModifier.ABSTRACT)) { | ||
| return@forEach | ||
| } | ||
|
|
||
| val eventSimpleName = psiClass.name ?: return@forEach | ||
| if (eventNameFilter.isNotEmpty() && !eventSimpleName.lowercase().startsWith(eventNameFilter)) { | ||
| return@forEach | ||
| } | ||
|
|
||
| val lookupString = "on$eventSimpleName" | ||
| val methodName = lookupString.replace("Event", "") | ||
|
Leguan16 marked this conversation as resolved.
Outdated
|
||
| val qualifiedName = psiClass.qualifiedName | ||
|
|
||
| val element = LookupElementBuilder | ||
| .create(lookupString) | ||
| .withPresentableText("$lookupString()") | ||
| .withTailText(" - $qualifiedName", true) | ||
| .withTypeText("@EventHandler") | ||
| .withIcon(AllIcons.Nodes.Method) | ||
| .withBaseLookupString(lookupString) | ||
| .withBoldness(true) | ||
| .withInsertHandler(BukkitEventHandlerInsertHandler(methodName, qualifiedName)) | ||
|
|
||
| completionResultSet.addElement( | ||
| PrioritizedLookupElement.withPriority(element, 100.0) | ||
| ) | ||
| } | ||
|
|
||
| completionResultSet.stopHere() | ||
|
Leguan16 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
src/main/kotlin/platform/bukkit/completion/BukkitEventHandlerInsertHandler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.demonwav.mcdev.platform.bukkit.completion | ||
|
|
||
| import com.intellij.codeInsight.completion.InsertHandler | ||
| import com.intellij.codeInsight.completion.InsertionContext | ||
| import com.intellij.codeInsight.lookup.LookupElement | ||
| import com.intellij.codeInsight.template.TemplateManager | ||
| import com.intellij.codeInsight.template.impl.TextExpression | ||
| import com.intellij.openapi.util.NlsSafe | ||
| import org.jetbrains.annotations.NotNull | ||
|
|
||
| class BukkitEventHandlerInsertHandler( | ||
| private val methodName: String, | ||
| private val qualifiedName: @NlsSafe String? | ||
| ) : InsertHandler<LookupElement> { | ||
|
|
||
| companion object { | ||
| private const val EVENT_HANDLER_FQN = "org.bukkit.event.EventHandler" | ||
| } | ||
|
|
||
| override fun handleInsert(@NotNull insertionContext: InsertionContext, @NotNull lookupElement: LookupElement) { | ||
|
Leguan16 marked this conversation as resolved.
Outdated
|
||
| val project = insertionContext.project | ||
| val editor = insertionContext.editor | ||
|
|
||
| val templateManager = TemplateManager.getInstance(project) | ||
| val template = templateManager.createTemplate("", "") | ||
| template.isToReformat = true | ||
|
|
||
| template.addTextSegment("@${EVENT_HANDLER_FQN}\n") | ||
| template.addTextSegment("public void ") | ||
|
Leguan16 marked this conversation as resolved.
Outdated
|
||
| template.addVariable("METHOD_NAME", TextExpression(methodName), true) | ||
| template.addTextSegment("($qualifiedName ") | ||
| template.addVariable("EVENT_PARAM", TextExpression("event"), true) | ||
| template.addTextSegment(") { \n") | ||
| template.addEndVariable() | ||
| template.addTextSegment("\n}") | ||
|
|
||
| insertionContext.getDocument().deleteString( | ||
| insertionContext.getStartOffset(), | ||
| insertionContext.getTailOffset() | ||
| ); | ||
|
Leguan16 marked this conversation as resolved.
Outdated
|
||
|
|
||
| templateManager.startTemplate(editor, template) | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.