Skip to content

Commit

Permalink
Merge pull request #5 from EmptyIrony/master
Browse files Browse the repository at this point in the history
update: tb注解Suppress unused警告, 将tabooproject.org设为第一个下载镜像
  • Loading branch information
CziSKY authored Aug 12, 2024
2 parents 0fa9db9 + 2f98904 commit 39ac98c
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies {
}

intellij {
version.set("2023.1.5")
version.set("2023.2.2")

plugins.addAll(
"java",
Expand Down
27 changes: 27 additions & 0 deletions src/main/kotlin/org/tabooproject/intellij/Utils.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 <reified T : PsiElement> 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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.KtAnnotated
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> =
SuppressQuickFix.EMPTY_ARRAY

private fun KtAnnotated.hasSuppressUnusedAnnotation(): Boolean {
val annotationEntries = annotationEntries
return annotationEntries.any {
ANNOTATIONS.contains(it.shortName?.asString() ?: return false)
}
}

}
Original file line number Diff line number Diff line change
@@ -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> =
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()
}
}
4 changes: 4 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

<extensions defaultExtensionNs="com.intellij">
<moduleBuilder id="TABOO_PROJECT_BUILDER" builderClass="org.tabooproject.intellij.ProjectBuilder"/>
<lang.inspectionSuppressor language="kotlin"
implementationClass="org.tabooproject.intellij.suppressor.AnnotatedUnusedSuppressor"/>
<lang.inspectionSuppressor language="kotlin"
implementationClass="org.tabooproject.intellij.suppressor.ExpansionUnusedSuppressor"/>
</extensions>

</idea-plugin>

0 comments on commit 39ac98c

Please sign in to comment.