-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathplugin.groovy
44 lines (38 loc) · 1.73 KB
/
plugin.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiLiteralExpression
import static liveplugin.PluginUtil.registerInspection
import static liveplugin.PluginUtil.show
// depends-on-plugin com.intellij.java
registerInspection(pluginDisposable, new HelloWorldInspection())
if (!isIdeStartup) {
show("Loaded hello world inspection<br/>It replaces \"hello\" string literal in Java code with \"Hello world\"")
}
class HelloWorldInspection extends AbstractBaseJavaLocalInspectionTool {
@Override PsiElementVisitor buildVisitor(ProblemsHolder holder, boolean isOnTheFly) {
new JavaElementVisitor() {
@Override void visitLiteralExpression(PsiLiteralExpression expression) {
super.visitLiteralExpression(expression)
if (expression.type.equalsToText("java.lang.String") && expression.value == "hello") {
holder.registerProblem(expression, "Found hello word", new HelloWorldQuickFix())
}
}
}
}
@Override String getDisplayName() { 'Replace "hello" with "Hello world"' }
@Override String getShortName() { "HelloWorldInspection" }
@Override String getGroupDisplayName() { "Live plugin" }
@Override boolean isEnabledByDefault() { true }
}
class HelloWorldQuickFix implements LocalQuickFix {
@Override void applyFix(Project project, ProblemDescriptor descriptor) {
def factory = JavaPsiFacade.getInstance(project).elementFactory
def stringLiteral = factory.createExpressionFromText('"Hello World"', null)
descriptor.psiElement.replace(stringLiteral)
}
@Override String getName() { 'Replace with "Hello World"' }
@Override String getFamilyName() { name }
}