Skip to content

Commit 231793d

Browse files
committed
当调用workspace并且后续不调用任何方法时给个警告
1 parent 0235632 commit 231793d

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.tabooproject.development.inspection
2+
3+
import com.intellij.codeInspection.*
4+
import com.intellij.openapi.project.Project
5+
import org.jetbrains.kotlin.descriptors.ClassDescriptor
6+
import org.jetbrains.kotlin.idea.caches.resolve.analyze
7+
import org.jetbrains.kotlin.idea.codeinsight.api.classic.inspections.AbstractKotlinInspection
8+
import org.jetbrains.kotlin.psi.*
9+
import org.jetbrains.kotlin.resolve.BindingContext
10+
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
11+
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
12+
13+
class DatabaseWorkspaceInspection: AbstractKotlinInspection() {
14+
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): KtVisitorVoid {
15+
return object : KtVisitorVoid() {
16+
override fun visitCallExpression(expression: KtCallExpression) {
17+
super.visitCallExpression(expression)
18+
val calleeExpression = expression.calleeExpression?.text
19+
if (calleeExpression == "workspace") {
20+
val qualifiedExpression = expression.parent as? KtDotQualifiedExpression
21+
val receiverExpression = qualifiedExpression?.receiverExpression
22+
val context = receiverExpression?.analyze(BodyResolveMode.PARTIAL)
23+
val type = context?.get(BindingContext.EXPRESSION_TYPE_INFO, receiverExpression)?.type
24+
val classDescriptor = type?.constructor?.declarationDescriptor as? ClassDescriptor
25+
val fqName = classDescriptor?.fqNameSafe?.asString()
26+
27+
if (fqName != "taboolib.module.database.Table") return
28+
29+
val parent = expression.parent
30+
31+
if (parent is KtDotQualifiedExpression) {
32+
val grandParent = parent.parent
33+
if (grandParent is KtBlockExpression) {
34+
val hasRunCall = grandParent.statements.any {
35+
it is KtCallExpression && it.calleeExpression != null
36+
}
37+
if (!hasRunCall) {
38+
holder.registerProblem(
39+
expression,
40+
"Calling 'workspace' without any method",
41+
ProblemHighlightType.WARNING,
42+
AddRunQuickFix()
43+
)
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}
51+
52+
class AddRunQuickFix : LocalQuickFix {
53+
override fun getName() = "Add 'run' method after workspace"
54+
override fun getFamilyName() = name
55+
56+
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
57+
val element = descriptor.psiElement
58+
val parent = element.parent
59+
if (parent is KtDotQualifiedExpression) {
60+
val factory = KtPsiFactory(project)
61+
val newExpression = factory.createExpression("${parent.text}.run()")
62+
parent.replace(newExpression)
63+
}
64+
}
65+
}
66+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,21 @@
2020

2121
<extensions defaultExtensionNs="com.intellij">
2222
<moduleBuilder id="TABOO_PROJECT_BUILDER" builderClass="org.tabooproject.development.ProjectBuilder"/>
23+
24+
<!-- Suppressor -->
2325
<lang.inspectionSuppressor language="kotlin"
2426
implementationClass="org.tabooproject.development.suppressor.AnnotatedUnusedSuppressor"/>
2527
<lang.inspectionSuppressor language="kotlin"
2628
implementationClass="org.tabooproject.development.suppressor.ExpansionUnusedSuppressor"/>
29+
30+
<localInspection displayName="Missing any method call after workspace()"
31+
language="kotlin"
32+
groupName="TabooLib"
33+
enabledByDefault="true"
34+
level="WARNING"
35+
hasStaticDescription="false"
36+
implementationClass="org.tabooproject.development.inspection.DatabaseWorkspaceInspection"/>
2737
</extensions>
2838

39+
<resource-bundle/>
2940
</idea-plugin>

0 commit comments

Comments
 (0)