|
| 1 | +/* |
| 2 | + * Minecraft Development for IntelliJ |
| 3 | + * |
| 4 | + * https://mcdev.io/ |
| 5 | + * |
| 6 | + * Copyright (C) 2025 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.mixin.expression.reference |
| 22 | + |
| 23 | +import com.demonwav.mcdev.platform.mixin.expression.gen.psi.MEDeclaration |
| 24 | +import com.demonwav.mcdev.platform.mixin.expression.gen.psi.MEName |
| 25 | +import com.intellij.lang.injection.InjectedLanguageManager |
| 26 | +import com.intellij.openapi.application.QueryExecutorBase |
| 27 | +import com.intellij.psi.PsiElement |
| 28 | +import com.intellij.psi.PsiReference |
| 29 | +import com.intellij.psi.search.LocalSearchScope |
| 30 | +import com.intellij.psi.search.RequestResultProcessor |
| 31 | +import com.intellij.psi.search.UsageSearchContext |
| 32 | +import com.intellij.psi.search.searches.ReferencesSearch |
| 33 | +import com.intellij.psi.util.parentOfType |
| 34 | +import com.intellij.psi.util.startOffset |
| 35 | +import com.intellij.util.Processor |
| 36 | + |
| 37 | +/** |
| 38 | + * Custom searcher for ME definition references, as ReferencesSearch doesn't seem to work for injected files |
| 39 | + */ |
| 40 | +class MEDefinitionReferencesSearcher : QueryExecutorBase<PsiReference, ReferencesSearch.SearchParameters>(true) { |
| 41 | + override fun processQuery( |
| 42 | + params: ReferencesSearch.SearchParameters, |
| 43 | + consumer: Processor<in PsiReference> |
| 44 | + ) { |
| 45 | + val declaration = params.elementToSearch as? MEDeclaration ?: return |
| 46 | + val declName = declaration.name ?: return |
| 47 | + val injectedLanguageManager = InjectedLanguageManager.getInstance(params.project) |
| 48 | + val hostFile = injectedLanguageManager.getInjectionHost(declaration)?.containingFile ?: return |
| 49 | + params.optimizer.searchWord( |
| 50 | + declName, |
| 51 | + LocalSearchScope(hostFile), |
| 52 | + UsageSearchContext.IN_STRINGS, |
| 53 | + true, |
| 54 | + declaration, |
| 55 | + object : RequestResultProcessor() { |
| 56 | + override fun processTextOccurrence( |
| 57 | + element: PsiElement, |
| 58 | + offsetInElement: Int, |
| 59 | + consumer: Processor<in PsiReference> |
| 60 | + ): Boolean { |
| 61 | + val injectedElement = injectedLanguageManager.findInjectedElementAt(hostFile, element.startOffset + offsetInElement) |
| 62 | + ?: return true |
| 63 | + val meName = injectedElement.parentOfType<MEName>(true) ?: return true |
| 64 | + val reference = meName.reference ?: return true |
| 65 | + if (!reference.isReferenceTo(declaration)) { |
| 66 | + return true |
| 67 | + } |
| 68 | + return consumer.process(reference) |
| 69 | + } |
| 70 | + } |
| 71 | + ) |
| 72 | + } |
| 73 | +} |
0 commit comments