Skip to content

Commit

Permalink
added editor fold tag
Browse files Browse the repository at this point in the history
  • Loading branch information
DattatreyaReddy committed Nov 22, 2024
1 parent 526ff66 commit 2deaa99
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "com.padya"
version = "1.4"
version = "1.5"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElementFactory;
import com.padya.stepbuilder.element.ElementGenerator;
import com.padya.stepbuilder.model.Pojo;
Expand Down Expand Up @@ -32,9 +33,22 @@ public StepBuilderPattern build(Pojo pojo) {
return stepBuilderPattern()
.withBuilderClass(builderClass(pojo))
.withStepInterfaces(stepInterfaces(pojo))
.withFoldStartComment(startFoldComment())
.withFoldEndComment(endFoldComment())
.build();
}

private PsiComment endFoldComment() {
return psiElementFactory.createCommentFromText(
elementGenerator.endFoldComment(), null);
}

private PsiComment startFoldComment() {
return psiElementFactory.createCommentFromText(
elementGenerator.startFoldComment(), null
);
}

private PsiClass builderClass(Pojo pojo) {
PsiClass builderClass = generateClass(elementGenerator.builderClass(pojo));
for (Property property : pojo.getProperties()) {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/padya/stepbuilder/StepBuilderGeneratorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiParserFacade;
import com.intellij.psi.codeStyle.CodeStyleManager;
import java.util.List;
import com.padya.stepbuilder.element.ElementGenerator;
Expand Down Expand Up @@ -51,6 +52,25 @@ private void includeInPojo(StepBuilderPattern stepBuilderPattern, PsiClass psiCl
reformat(inner);
psiClass.add(inner);
}
if (psiClass.getAllInnerClasses().length == 0) {
return;
}
List<PsiClass> innerClasses = List.of(psiClass.getAllInnerClasses());
if (!innerClasses.isEmpty()) {
PsiClass firstInnerClass = innerClasses.get(0);
PsiClass lastInnerClass = innerClasses.get(innerClasses.size() - 1);

PsiParserFacade parserFacade = PsiParserFacade.getInstance(psiClass.getProject());
PsiElement newLine = parserFacade.createWhiteSpaceFromText("\n\n\t");

// Fold start
psiClass.addBefore(newLine, firstInnerClass);
psiClass.addBefore(stepBuilderPattern.getFoldStartComment(), firstInnerClass);

// Fold End
psiClass.addAfter(stepBuilderPattern.getFoldEndComment(), lastInnerClass);
psiClass.addAfter(newLine, lastInnerClass);
}
}

private void reformat(PsiClass psiClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public enum DialogFactories {
private final DialogFactory dialogFactory;

DialogFactories(DialogFactory dialogFactory) {

this.dialogFactory = dialogFactory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
import static org.apache.commons.lang3.StringUtils.uncapitalize;

public class ElementGenerator {
public String startFoldComment() {
return "// <editor-fold defaultstate=\"collapsed\" desc=\"Step-Builder\">";
}

public String endFoldComment() {
return "// </editor-fold>";
}

public String builderClass(Pojo pojo) {
String steps = on(", ").join(transform(pojo.getProperties(), this::stepName));
if (pojo.getProperties().isEmpty()) {
Expand Down
65 changes: 55 additions & 10 deletions src/main/java/com/padya/stepbuilder/model/StepBuilderPattern.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package com.padya.stepbuilder.model;

import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiComment;
import java.util.List;

public class StepBuilderPattern {
private final PsiClass builderClass;

private final List<PsiClass> stepInterfaces;

private StepBuilderPattern(PsiClass builderClass, List<PsiClass> stepInterfaces) {
private final PsiComment foldStartComment;

private final PsiComment foldEndComment;

public StepBuilderPattern(PsiClass builderClass, List<PsiClass> stepInterfaces,
PsiComment foldStartComment, PsiComment foldEndComment) {
this.builderClass = builderClass;
this.stepInterfaces = stepInterfaces;
this.foldStartComment = foldStartComment;
this.foldEndComment = foldEndComment;
}

public PsiClass getBuilderClass() {
Expand All @@ -21,31 +29,54 @@ public List<PsiClass> getStepInterfaces() {
return stepInterfaces;
}

@Override
public String toString() {
public PsiComment getFoldStartComment() {
return foldStartComment;
}

public PsiComment getFoldEndComment() {
return foldEndComment;
}

@Override public String toString() {
return "StepBuilderPattern{" +
"builderClass=" + builderClass +
", stepInterfaces=" + stepInterfaces +
", foldStartComment=" + foldStartComment +
", foldEndComment=" + foldEndComment +
'}';
}

public interface BuilderClassStep {
public static interface BuilderClassStep {
StepInterfacesStep withBuilderClass(PsiClass builderClass);
}

public interface StepInterfacesStep {
BuildStep withStepInterfaces(List<PsiClass> stepInterfaces);
public static interface StepInterfacesStep {
FoldStartCommentStep withStepInterfaces(List<PsiClass> stepInterfaces);
}

public static interface FoldStartCommentStep {
FoldEndCommentStep withFoldStartComment(PsiComment foldStartComment);
}

public static interface FoldEndCommentStep {
BuildStep withFoldEndComment(PsiComment foldEndComment);
}

public interface BuildStep {
public static interface BuildStep {
StepBuilderPattern build();
}

public static class Builder implements BuilderClassStep, StepInterfacesStep, BuildStep {
public static class Builder
implements BuilderClassStep, StepInterfacesStep, FoldStartCommentStep, FoldEndCommentStep,
BuildStep {
private PsiClass builderClass;

private List<PsiClass> stepInterfaces;

private PsiComment foldStartComment;

private PsiComment foldEndComment;

private Builder() {
}

Expand All @@ -60,16 +91,30 @@ public StepInterfacesStep withBuilderClass(PsiClass builderClass) {
}

@Override
public BuildStep withStepInterfaces(List<PsiClass> stepInterfaces) {
public FoldStartCommentStep withStepInterfaces(List<PsiClass> stepInterfaces) {
this.stepInterfaces = stepInterfaces;
return this;
}

@Override
public FoldEndCommentStep withFoldStartComment(PsiComment foldStartComment) {
this.foldStartComment = foldStartComment;
return this;
}

@Override
public BuildStep withFoldEndComment(PsiComment foldEndComment) {
this.foldEndComment = foldEndComment;
return this;
}

@Override
public StepBuilderPattern build() {
return new StepBuilderPattern(
this.builderClass,
this.stepInterfaces
this.stepInterfaces,
this.foldStartComment,
this.foldEndComment
);
}
}
Expand Down

0 comments on commit 2deaa99

Please sign in to comment.