From 4cf1a61739fe92449d6d9d485681fda657c97a7e Mon Sep 17 00:00:00 2001 From: EmptyIrony Date: Tue, 13 Aug 2024 00:14:09 +0800 Subject: [PATCH 1/2] =?UTF-8?q?update:=20=E5=AF=B9tb=E6=B3=A8=E8=A7=A3Supp?= =?UTF-8?q?ress=20unused=E8=AD=A6=E5=91=8A=20=E5=B0=86tabooproject.org?= =?UTF-8?q?=E8=AE=BE=E4=B8=BA=E7=AC=AC=E4=B8=80=E4=B8=AA=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E9=95=9C=E5=83=8F(github=E4=BC=9A=E5=A4=B1=E8=B4=A5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 2 +- .../kotlin/org/tabooproject/intellij/Utils.kt | 27 ++++++++++++ .../component/AnnotatedUnusedSuppressor.kt | 41 +++++++++++++++++++ .../step/ConfigurationPropertiesStep.kt | 2 +- src/main/resources/META-INF/plugin.xml | 2 + 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/org/tabooproject/intellij/component/AnnotatedUnusedSuppressor.kt diff --git a/build.gradle.kts b/build.gradle.kts index 3b588fb..c90e4f4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -20,7 +20,7 @@ dependencies { } intellij { - version.set("2023.1.5") + version.set("2023.2.2") plugins.addAll( "java", diff --git a/src/main/kotlin/org/tabooproject/intellij/Utils.kt b/src/main/kotlin/org/tabooproject/intellij/Utils.kt index d0a4d57..05ce187 100644 --- a/src/main/kotlin/org/tabooproject/intellij/Utils.kt +++ b/src/main/kotlin/org/tabooproject/intellij/Utils.kt @@ -1,7 +1,9 @@ package org.tabooproject.intellij +import com.intellij.psi.* import okhttp3.OkHttpClient import okhttp3.Request +import org.jetbrains.kotlin.psi.KtAnnotated import java.net.InetSocketAddress import java.net.Proxy import java.nio.file.Files @@ -43,4 +45,29 @@ fun createOkHttpClientWithSystemProxy(block: OkHttpClient.Builder.() -> Unit = { fun getRequest(url: String): Request { return Request.Builder().url(url).build() +} + +fun PsiElement.findContainingAnnotated(): KtAnnotated? = findParent(resolveReferences = false) { it is KtAnnotated } + +private inline fun PsiElement.findParent( + resolveReferences: Boolean, + stop: (PsiElement) -> Boolean, +): T? { + var el: PsiElement = this + + while (true) { + if (resolveReferences && el is PsiReference) { + el = el.resolve() ?: return null + } + + if (el is T) { + return el + } + + if (el is PsiFile || el is PsiDirectory || stop(el)) { + return null + } + + el = el.parent ?: return null + } } \ No newline at end of file diff --git a/src/main/kotlin/org/tabooproject/intellij/component/AnnotatedUnusedSuppressor.kt b/src/main/kotlin/org/tabooproject/intellij/component/AnnotatedUnusedSuppressor.kt new file mode 100644 index 0000000..470d53d --- /dev/null +++ b/src/main/kotlin/org/tabooproject/intellij/component/AnnotatedUnusedSuppressor.kt @@ -0,0 +1,41 @@ +package org.tabooproject.intellij.component + +import com.intellij.codeInspection.InspectionSuppressor +import com.intellij.codeInspection.SuppressQuickFix +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.psi.* +import org.tabooproject.intellij.findContainingAnnotated + + +private val ANNOTATIONS = hashSetOf( + "SubscribeEvent", + "Schedule", + "Awake", + "CommandBody", + "CommandHeader", + "KetherParser", + "KetherProperty" +) + +private const val INSPECTION = "unused" + +class AnnotatedUnusedSuppressor: InspectionSuppressor { + override fun isSuppressedFor(element: PsiElement, toolId: String): Boolean { + if (toolId != INSPECTION) { + return false + } + + return element.findContainingAnnotated()?.hasSuppressUnusedAnnotation() ?: false + } + + override fun getSuppressActions(element: PsiElement?, toolId: String): Array = + SuppressQuickFix.EMPTY_ARRAY + + private fun KtAnnotated.hasSuppressUnusedAnnotation(): Boolean { + val annotationEntries = annotationEntries + return annotationEntries.any { + ANNOTATIONS.contains(it.shortName?.asString() ?: return false) + } + } + +} \ No newline at end of file diff --git a/src/main/kotlin/org/tabooproject/intellij/step/ConfigurationPropertiesStep.kt b/src/main/kotlin/org/tabooproject/intellij/step/ConfigurationPropertiesStep.kt index 5386f78..b198a42 100644 --- a/src/main/kotlin/org/tabooproject/intellij/step/ConfigurationPropertiesStep.kt +++ b/src/main/kotlin/org/tabooproject/intellij/step/ConfigurationPropertiesStep.kt @@ -52,8 +52,8 @@ data class Module( val TEMPLATE_DOWNLOAD_MIRROR = mapOf( + "tabooproject.org" to "https://template.tabooproject.org", "github.com" to "https://github.com/TabooLib/taboolib-sdk/archive/refs/heads/idea-template.zip", - "tabooproject.org" to "https://template.tabooproject.org" ) data class ConfigurationProperty( diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 480bdce..4e9b044 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -19,6 +19,8 @@ + \ No newline at end of file From 2f98904be35807aec1b8ea37fb4d5f0a9dd260d5 Mon Sep 17 00:00:00 2001 From: EmptyIrony Date: Tue, 13 Aug 2024 00:44:41 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AF=B9=E9=83=A8=E5=88=86=E7=B1=BB?= =?UTF-8?q?=E7=9A=84=E7=BB=A7=E6=89=BF=E5=92=8C=E5=AE=9E=E7=8E=B0=E5=8E=BB?= =?UTF-8?q?=E9=99=A4=E4=BA=86unused=EF=BC=8C=E6=8C=AA=E4=BA=86=E4=B8=8B?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AnnotatedUnusedSuppressor.kt | 5 +- .../suppressor/ExpansionUnusedSuppressor.kt | 67 +++++++++++++++++++ src/main/resources/META-INF/plugin.xml | 4 +- 3 files changed, 72 insertions(+), 4 deletions(-) rename src/main/kotlin/org/tabooproject/intellij/{component => suppressor}/AnnotatedUnusedSuppressor.kt (92%) create mode 100644 src/main/kotlin/org/tabooproject/intellij/suppressor/ExpansionUnusedSuppressor.kt diff --git a/src/main/kotlin/org/tabooproject/intellij/component/AnnotatedUnusedSuppressor.kt b/src/main/kotlin/org/tabooproject/intellij/suppressor/AnnotatedUnusedSuppressor.kt similarity index 92% rename from src/main/kotlin/org/tabooproject/intellij/component/AnnotatedUnusedSuppressor.kt rename to src/main/kotlin/org/tabooproject/intellij/suppressor/AnnotatedUnusedSuppressor.kt index 470d53d..0fa5469 100644 --- a/src/main/kotlin/org/tabooproject/intellij/component/AnnotatedUnusedSuppressor.kt +++ b/src/main/kotlin/org/tabooproject/intellij/suppressor/AnnotatedUnusedSuppressor.kt @@ -1,12 +1,11 @@ -package org.tabooproject.intellij.component +package org.tabooproject.intellij.suppressor import com.intellij.codeInspection.InspectionSuppressor import com.intellij.codeInspection.SuppressQuickFix import com.intellij.psi.PsiElement -import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.KtAnnotated import org.tabooproject.intellij.findContainingAnnotated - private val ANNOTATIONS = hashSetOf( "SubscribeEvent", "Schedule", diff --git a/src/main/kotlin/org/tabooproject/intellij/suppressor/ExpansionUnusedSuppressor.kt b/src/main/kotlin/org/tabooproject/intellij/suppressor/ExpansionUnusedSuppressor.kt new file mode 100644 index 0000000..d1cf12c --- /dev/null +++ b/src/main/kotlin/org/tabooproject/intellij/suppressor/ExpansionUnusedSuppressor.kt @@ -0,0 +1,67 @@ +package org.tabooproject.intellij.suppressor + +import com.intellij.codeInspection.InspectionSuppressor +import com.intellij.codeInspection.SuppressQuickFix +import com.intellij.psi.PsiElement +import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade +import org.jetbrains.kotlin.psi.KtClassOrObject +import org.jetbrains.kotlin.psi.KtTypeReference +import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode + +private const val INSPECTION = "unused" + +private val classes = listOf( + "taboolib.platform.compat.PlaceholderExpansion", + "taboolib.common.platform.Plugin", +) + +class ExpansionUnusedSuppressor: InspectionSuppressor { + override fun isSuppressedFor(element: PsiElement, toolId: String): Boolean { + if (toolId != INSPECTION) { + return false + } + + return classes.any { className -> + checkIfClassImplementsOrExtends(element, className) + } + } + + override fun getSuppressActions(element: PsiElement?, toolId: String): Array = + SuppressQuickFix.EMPTY_ARRAY + + private fun checkIfClassImplementsOrExtends(element: PsiElement, className: String): Boolean { + val ktClass = PsiTreeUtil.getParentOfType(element, KtClassOrObject::class.java) ?: return false + val context = ktClass.getResolutionFacade().analyze(ktClass, BodyResolveMode.FULL) + return ktClass.implementsInterface(className, context) || ktClass.isSubclassOf(className, context) + } + + private fun KtClassOrObject.isSubclassOf(className: String, context: BindingContext): Boolean { + if (fqName?.asString() == className) return true + + val superTypes = this.superTypeListEntries + return superTypes.any { typeEntry -> + val typeReference = typeEntry.typeReference + val typeFqName = typeReference?.getFqName(context) + typeFqName == className + } + } + + private fun KtClassOrObject.implementsInterface(interfaceName: String, context: BindingContext): Boolean { + if (isSubclassOf(interfaceName, context)) return true + + val superTypes = this.superTypeListEntries + return superTypes.any { typeEntry -> + val typeReference = typeEntry.typeReference + val typeFqName = typeReference?.getFqName(context) + typeFqName == interfaceName + } + } + + private fun KtTypeReference.getFqName(context: BindingContext): String? { + val type = context[BindingContext.TYPE, this] + return type?.constructor?.declarationDescriptor?.fqNameSafe?.asString() + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 4e9b044..9b4c977 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -20,7 +20,9 @@ + implementationClass="org.tabooproject.intellij.suppressor.AnnotatedUnusedSuppressor"/> + \ No newline at end of file