Skip to content

Commit 39ac98c

Browse files
authored
Merge pull request #5 from EmptyIrony/master
update: tb注解Suppress unused警告, 将tabooproject.org设为第一个下载镜像
2 parents 0fa9db9 + 2f98904 commit 39ac98c

File tree

6 files changed

+140
-2
lines changed

6 files changed

+140
-2
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ dependencies {
2020
}
2121

2222
intellij {
23-
version.set("2023.1.5")
23+
version.set("2023.2.2")
2424

2525
plugins.addAll(
2626
"java",

src/main/kotlin/org/tabooproject/intellij/Utils.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.tabooproject.intellij
22

3+
import com.intellij.psi.*
34
import okhttp3.OkHttpClient
45
import okhttp3.Request
6+
import org.jetbrains.kotlin.psi.KtAnnotated
57
import java.net.InetSocketAddress
68
import java.net.Proxy
79
import java.nio.file.Files
@@ -43,4 +45,29 @@ fun createOkHttpClientWithSystemProxy(block: OkHttpClient.Builder.() -> Unit = {
4345

4446
fun getRequest(url: String): Request {
4547
return Request.Builder().url(url).build()
48+
}
49+
50+
fun PsiElement.findContainingAnnotated(): KtAnnotated? = findParent(resolveReferences = false) { it is KtAnnotated }
51+
52+
private inline fun <reified T : PsiElement> PsiElement.findParent(
53+
resolveReferences: Boolean,
54+
stop: (PsiElement) -> Boolean,
55+
): T? {
56+
var el: PsiElement = this
57+
58+
while (true) {
59+
if (resolveReferences && el is PsiReference) {
60+
el = el.resolve() ?: return null
61+
}
62+
63+
if (el is T) {
64+
return el
65+
}
66+
67+
if (el is PsiFile || el is PsiDirectory || stop(el)) {
68+
return null
69+
}
70+
71+
el = el.parent ?: return null
72+
}
4673
}

src/main/kotlin/org/tabooproject/intellij/step/ConfigurationPropertiesStep.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ data class Module(
5252

5353

5454
val TEMPLATE_DOWNLOAD_MIRROR = mapOf(
55+
"tabooproject.org" to "https://template.tabooproject.org",
5556
"github.com" to "https://github.com/TabooLib/taboolib-sdk/archive/refs/heads/idea-template.zip",
56-
"tabooproject.org" to "https://template.tabooproject.org"
5757
)
5858

5959
data class ConfigurationProperty(
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.tabooproject.intellij.suppressor
2+
3+
import com.intellij.codeInspection.InspectionSuppressor
4+
import com.intellij.codeInspection.SuppressQuickFix
5+
import com.intellij.psi.PsiElement
6+
import org.jetbrains.kotlin.psi.KtAnnotated
7+
import org.tabooproject.intellij.findContainingAnnotated
8+
9+
private val ANNOTATIONS = hashSetOf(
10+
"SubscribeEvent",
11+
"Schedule",
12+
"Awake",
13+
"CommandBody",
14+
"CommandHeader",
15+
"KetherParser",
16+
"KetherProperty"
17+
)
18+
19+
private const val INSPECTION = "unused"
20+
21+
class AnnotatedUnusedSuppressor: InspectionSuppressor {
22+
override fun isSuppressedFor(element: PsiElement, toolId: String): Boolean {
23+
if (toolId != INSPECTION) {
24+
return false
25+
}
26+
27+
return element.findContainingAnnotated()?.hasSuppressUnusedAnnotation() ?: false
28+
}
29+
30+
override fun getSuppressActions(element: PsiElement?, toolId: String): Array<SuppressQuickFix> =
31+
SuppressQuickFix.EMPTY_ARRAY
32+
33+
private fun KtAnnotated.hasSuppressUnusedAnnotation(): Boolean {
34+
val annotationEntries = annotationEntries
35+
return annotationEntries.any {
36+
ANNOTATIONS.contains(it.shortName?.asString() ?: return false)
37+
}
38+
}
39+
40+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.tabooproject.intellij.suppressor
2+
3+
import com.intellij.codeInspection.InspectionSuppressor
4+
import com.intellij.codeInspection.SuppressQuickFix
5+
import com.intellij.psi.PsiElement
6+
import com.intellij.psi.util.PsiTreeUtil
7+
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
8+
import org.jetbrains.kotlin.psi.KtClassOrObject
9+
import org.jetbrains.kotlin.psi.KtTypeReference
10+
import org.jetbrains.kotlin.resolve.BindingContext
11+
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
12+
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
13+
14+
private const val INSPECTION = "unused"
15+
16+
private val classes = listOf(
17+
"taboolib.platform.compat.PlaceholderExpansion",
18+
"taboolib.common.platform.Plugin",
19+
)
20+
21+
class ExpansionUnusedSuppressor: InspectionSuppressor {
22+
override fun isSuppressedFor(element: PsiElement, toolId: String): Boolean {
23+
if (toolId != INSPECTION) {
24+
return false
25+
}
26+
27+
return classes.any { className ->
28+
checkIfClassImplementsOrExtends(element, className)
29+
}
30+
}
31+
32+
override fun getSuppressActions(element: PsiElement?, toolId: String): Array<SuppressQuickFix> =
33+
SuppressQuickFix.EMPTY_ARRAY
34+
35+
private fun checkIfClassImplementsOrExtends(element: PsiElement, className: String): Boolean {
36+
val ktClass = PsiTreeUtil.getParentOfType(element, KtClassOrObject::class.java) ?: return false
37+
val context = ktClass.getResolutionFacade().analyze(ktClass, BodyResolveMode.FULL)
38+
return ktClass.implementsInterface(className, context) || ktClass.isSubclassOf(className, context)
39+
}
40+
41+
private fun KtClassOrObject.isSubclassOf(className: String, context: BindingContext): Boolean {
42+
if (fqName?.asString() == className) return true
43+
44+
val superTypes = this.superTypeListEntries
45+
return superTypes.any { typeEntry ->
46+
val typeReference = typeEntry.typeReference
47+
val typeFqName = typeReference?.getFqName(context)
48+
typeFqName == className
49+
}
50+
}
51+
52+
private fun KtClassOrObject.implementsInterface(interfaceName: String, context: BindingContext): Boolean {
53+
if (isSubclassOf(interfaceName, context)) return true
54+
55+
val superTypes = this.superTypeListEntries
56+
return superTypes.any { typeEntry ->
57+
val typeReference = typeEntry.typeReference
58+
val typeFqName = typeReference?.getFqName(context)
59+
typeFqName == interfaceName
60+
}
61+
}
62+
63+
private fun KtTypeReference.getFqName(context: BindingContext): String? {
64+
val type = context[BindingContext.TYPE, this]
65+
return type?.constructor?.declarationDescriptor?.fqNameSafe?.asString()
66+
}
67+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
<extensions defaultExtensionNs="com.intellij">
2121
<moduleBuilder id="TABOO_PROJECT_BUILDER" builderClass="org.tabooproject.intellij.ProjectBuilder"/>
22+
<lang.inspectionSuppressor language="kotlin"
23+
implementationClass="org.tabooproject.intellij.suppressor.AnnotatedUnusedSuppressor"/>
24+
<lang.inspectionSuppressor language="kotlin"
25+
implementationClass="org.tabooproject.intellij.suppressor.ExpansionUnusedSuppressor"/>
2226
</extensions>
2327

2428
</idea-plugin>

0 commit comments

Comments
 (0)