Skip to content

Commit 157a660

Browse files
committed
support basic lombok builder method.
1 parent b2ac7d2 commit 157a660

File tree

7 files changed

+138
-13
lines changed

7 files changed

+138
-13
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ intellij {
4242
// username 'AmailP'
4343
// pluginId '7415'
4444
// }
45-
plugins 'Kotlin', 'org.nik.presentation-assistant:1.0.1'
46-
45+
plugins 'Kotlin', 'org.nik.presentation-assistant:1.0.1','Lombook Plugin:0.31-2018.3'
46+
4747
sandboxDirectory 'sandbox'
4848
}
4949

@@ -55,4 +55,4 @@ dependencies {
5555
}
5656

5757
group 'com.bruce.intellijplugin'
58-
version '2.5'
58+
version '2.6'

src/main/java/com/bruce/intellijplugin/generatesetter/CommonConstants.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
* @author bruce ge
1919
*/
2020
public class CommonConstants {
21-
public static final String GENERATE_SETTER_METHOD = "Generate all setter with default value";
21+
public static final String GENERATE_SETTER_METHOD = "Generate builder chain call";
2222
public static final String GENERATE_SETTER_METHOD_NO_DEAULT_VALUE = "Generate all setter no default value";
2323
public static final String ASSERTALLPROPS = "Assert all getters";
2424
public static final String GENERATE_CONVERTER_FROM_METHOD = "Generate setter getter converter";
25+
26+
public static final String BUILDER_METHOD_NAME = "builder";
2527
}

src/main/java/com/bruce/intellijplugin/generatesetter/GenerateAllHandlerAdapter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ public boolean isFromMethod() {
3939
public String formatLine(String line) {
4040
return line;
4141
}
42+
43+
@Override
44+
public boolean forBuilder() {
45+
return false;
46+
}
4247
}

src/main/java/com/bruce/intellijplugin/generatesetter/actions/GenerateAllHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ public interface GenerateAllHandler {
2525
boolean isFromMethod();
2626

2727
String formatLine(String line);
28+
29+
30+
boolean forBuilder();
2831
}

src/main/java/com/bruce/intellijplugin/generatesetter/actions/GenerateAllSetterBase.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public abstract class GenerateAllSetterBase extends PsiElementBaseIntentionActio
4646
public static final String GET = "get";
4747
private static final String SET_SETTER_PREFIX = "set";
4848
private static final String WITH_SETTER_PREFIX = "with";
49+
public static final String STATIC = "static";
4950
private final GenerateAllHandler generateAllHandler;
5051

5152
public GenerateAllSetterBase(GenerateAllHandler generateAllHandler) {
@@ -251,8 +252,8 @@ private String generateStringForParam(String generateName,
251252
method.getName().startsWith(SET_SETTER_PREFIX)
252253
? SET_SETTER_PREFIX
253254
: method.getName().startsWith(WITH_SETTER_PREFIX)
254-
? WITH_SETTER_PREFIX
255-
: null;
255+
? WITH_SETTER_PREFIX
256+
: null;
256257
if (setterMethodNamePrefix != null) {
257258
String fieldToLower = method.getName().substring(setterMethodNamePrefix.length())
258259
.toLowerCase();
@@ -527,6 +528,19 @@ private String findNextNotNull(PsiTypeElement psiType, String defaultName) {
527528
public boolean isAvailable(@NotNull Project project, Editor editor,
528529
@NotNull PsiElement element) {
529530
boolean setter = generateAllHandler.isSetter();
531+
if (generateAllHandler.forBuilder()) {
532+
PsiClass localVarialbeContainingClass = getLocalVarialbeContainingClass(element);
533+
if (localVarialbeContainingClass == null) {
534+
return false;
535+
}
536+
PsiMethod[] methods = localVarialbeContainingClass.getMethods();
537+
for (PsiMethod method : methods) {
538+
if (method.getName().equals(CommonConstants.BUILDER_METHOD_NAME)&&method.hasModifierProperty(STATIC)) {
539+
return true;
540+
}
541+
}
542+
return false;
543+
}
530544
Boolean validAsLocalVariableWithSetterOrGetterMethod = isValidAsLocalVariableWithSetterOrGetterMethod(element, setter);
531545
if (validAsLocalVariableWithSetterOrGetterMethod) {
532546
return validAsLocalVariableWithSetterOrGetterMethod;
@@ -563,21 +577,29 @@ private Boolean isValidAsMethodWithSetterMethod(@NotNull PsiElement element) {
563577

564578
@NotNull
565579
private Boolean isValidAsLocalVariableWithSetterOrGetterMethod(@NotNull PsiElement element, boolean setter) {
580+
PsiClass psiClass = getLocalVarialbeContainingClass(element);
581+
if (psiClass == null) {
582+
return false;
583+
}
584+
if (setter) {
585+
return PsiClassUtils.checkClassHasValidSetMethod(psiClass);
586+
} else {
587+
return PsiClassUtils.checkClasHasValidGetMethod(psiClass);
588+
}
589+
}
590+
591+
public static PsiClass getLocalVarialbeContainingClass(@NotNull PsiElement element) {
566592
PsiElement psiParent = PsiTreeUtil.getParentOfType(element,
567593
PsiLocalVariable.class);
568594
if (psiParent == null) {
569-
return false;
595+
return null;
570596
}
571597
PsiLocalVariable psiLocal = (PsiLocalVariable) psiParent;
572598
if (!(psiLocal.getParent() instanceof PsiDeclarationStatement)) {
573-
return false;
599+
return null;
574600
}
575601
PsiClass psiClass = PsiTypesUtil.getPsiClass(psiLocal.getType());
576-
if (setter) {
577-
return PsiClassUtils.checkClassHasValidSetMethod(psiClass);
578-
} else {
579-
return PsiClassUtils.checkClasHasValidGetMethod(psiClass);
580-
}
602+
return psiClass;
581603
}
582604

583605
private static boolean notObjectClass(PsiClass psiClass) {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2017-2019, bruce.ge.
3+
* This program is free software; you can redistribute it and/or
4+
* modify it under the terms of the GNU General Public License
5+
* as published by the Free Software Foundation; version 2 of
6+
* the License.
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
* You should have received a copy of the GNU General Public License
12+
* along with this program;
13+
*/
14+
15+
package com.bruce.intellijplugin.generatesetter.actions;
16+
17+
import com.bruce.intellijplugin.generatesetter.CommonConstants;
18+
import com.bruce.intellijplugin.generatesetter.GenerateAllHandlerAdapter;
19+
import com.bruce.intellijplugin.generatesetter.utils.PsiDocumentUtils;
20+
import com.intellij.openapi.command.WriteCommandAction;
21+
import com.intellij.openapi.editor.Document;
22+
import com.intellij.openapi.editor.Editor;
23+
import com.intellij.openapi.project.Project;
24+
import com.intellij.psi.PsiClass;
25+
import com.intellij.psi.PsiDocumentManager;
26+
import com.intellij.psi.PsiElement;
27+
import com.intellij.psi.PsiMethod;
28+
import com.intellij.psi.PsiType;
29+
import com.intellij.psi.util.PsiTypesUtil;
30+
import com.intellij.util.IncorrectOperationException;
31+
import org.jetbrains.annotations.NotNull;
32+
33+
/**
34+
* @author bruce ge 2020/9/23
35+
*/
36+
public class GenerateWithBuilderAction extends GenerateAllSetterBase {
37+
public GenerateWithBuilderAction() {
38+
super(new GenerateAllHandlerAdapter() {
39+
@Override
40+
public boolean forBuilder() {
41+
return true;
42+
}
43+
});
44+
}
45+
46+
47+
@Override
48+
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
49+
PsiClass localVarialbeContainingClass = GenerateAllSetterBase.getLocalVarialbeContainingClass(element);
50+
for (PsiMethod method : localVarialbeContainingClass.getMethods()) {
51+
if (method.getName().equals(CommonConstants.BUILDER_METHOD_NAME)) {
52+
PsiType returnType = method.getReturnType();
53+
PsiClass psiClass = PsiTypesUtil.getPsiClass(returnType);
54+
StringBuilder builder = new StringBuilder(localVarialbeContainingClass.getQualifiedName() + ".builder()");
55+
for (PsiMethod psiClassMethod : psiClass.getMethods()) {
56+
if (!psiClassMethod.isConstructor() && !psiClassMethod.getName().equals("toString")
57+
&& !psiClassMethod.getName().equals("build")) {
58+
builder.append("." + psiClassMethod.getName() + "()");
59+
}
60+
}
61+
builder.append(".build();");
62+
63+
// insert into the element.
64+
Document document = PsiDocumentManager.getInstance(project).getDocument(element.getContainingFile());
65+
66+
WriteCommandAction.runWriteCommandAction(project, new Runnable() {
67+
@Override
68+
public void run() {
69+
document.insertString(element.getTextRange().getEndOffset() + 2, builder.toString());
70+
PsiDocumentManager.getInstance(project).commitDocument(document);
71+
}
72+
});
73+
74+
}
75+
}
76+
}
77+
78+
79+
@NotNull
80+
@Override
81+
public String getText() {
82+
return CommonConstants.GENERATE_SETTER_METHOD;
83+
}
84+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
]]></description>
2424

2525
<change-notes><![CDATA[
26+
<strong>2.6</strong>
27+
<ul>
28+
<li>[NEW]support lombok builder using with(Thanks https://github.com/axesipov)</li>
29+
</ul>
2630
<strong>2.5</strong>
2731
<ul>
2832
<li>[NEW]compatible for 2020 version</li>
@@ -106,6 +110,11 @@
106110
<className>com.bruce.intellijplugin.generatesetter.actions.GenerateAllSetterAction</className>
107111
</intentionAction>
108112

113+
114+
<intentionAction>
115+
<className>com.bruce.intellijplugin.generatesetter.actions.GenerateWithBuilderAction</className>
116+
</intentionAction>
117+
109118
<intentionAction>
110119
<className>com.bruce.intellijplugin.generatesetter.actions.GenerateAllSetterNoDefaultValueAction</className>
111120
</intentionAction>

0 commit comments

Comments
 (0)