-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from EmptyIrony/master
update: tb注解Suppress unused警告, 将tabooproject.org设为第一个下载镜像
- Loading branch information
Showing
6 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -20,7 +20,7 @@ dependencies { | |
} | ||
|
||
intellij { | ||
version.set("2023.1.5") | ||
version.set("2023.2.2") | ||
|
||
plugins.addAll( | ||
"java", | ||
|
This file contains 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
This file contains 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
40 changes: 40 additions & 0 deletions
40
src/main/kotlin/org/tabooproject/intellij/suppressor/AnnotatedUnusedSuppressor.kt
This file contains 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,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) | ||
} | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/main/kotlin/org/tabooproject/intellij/suppressor/ExpansionUnusedSuppressor.kt
This file contains 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,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() | ||
} | ||
} |
This file contains 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