Skip to content

Commit

Permalink
migrate enum converter to enum bracket handler (#1453)
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlyhj committed Mar 5, 2022
1 parent 7431c4b commit 1f64bfe
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
Expand Up @@ -4,9 +4,11 @@
import com.blamejared.crafttweaker_annotation_processors.processors.document.page.comment.DocumentationComment;
import com.blamejared.crafttweaker_annotation_processors.processors.document.page.member.enum_constant.DocumentedEnumConstants;
import com.blamejared.crafttweaker_annotation_processors.processors.document.page.member.enum_constant.EnumConstant;
import com.blamejared.crafttweaker_annotations.annotations.BracketEnum;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import java.util.Locale;

/**
* @author youyihj
Expand All @@ -20,22 +22,24 @@ public EnumConstantConverter(CommentConverter commentConverter) {
this.commentConverter = commentConverter;
}

public void convertAndAddTo(Element enumElement, DocumentedEnumConstants result) {
public void convertAndAddTo(Element enumElement, DocumentedEnumConstants result, BracketEnum bracketEnum) {

enumElement.getEnclosedElements().stream()
.filter(this::isEnumConstantType)
.forEach(enumConstantElement -> this.addEnumConstantInfo(enumConstantElement, result));
.forEach(enumConstantElement -> this.addEnumConstantInfo(enumConstantElement, result, bracketEnum));
}

private boolean isEnumConstantType(Element element) {

return element.getKind() == ElementKind.ENUM_CONSTANT;
}

private void addEnumConstantInfo(Element enumConstantElement, DocumentedEnumConstants result) {

private void addEnumConstantInfo(Element enumConstantElement, DocumentedEnumConstants result, BracketEnum bracketEnum) {
String name = enumConstantElement.getSimpleName().toString();
if (bracketEnum != null) {
name = "<constant:%s:%s>".formatted(bracketEnum.value(), name.toLowerCase(Locale.ROOT));
}
final DocumentationComment description = commentConverter.convertElement(enumConstantElement, DocumentationComment.empty());
result.addConstant(new EnumConstant(enumConstantElement.getSimpleName().toString(), description));
result.addConstant(new EnumConstant(name, description, bracketEnum != null));
}

}
Expand Up @@ -19,6 +19,7 @@
import com.blamejared.crafttweaker_annotation_processors.processors.document.page.page.EnumTypePage;
import com.blamejared.crafttweaker_annotation_processors.processors.document.page.page.TypePage;
import com.blamejared.crafttweaker_annotation_processors.processors.document.page.type.AbstractTypeInfo;
import com.blamejared.crafttweaker_annotations.annotations.BracketEnum;
import org.openzen.zencode.java.ZenCodeType;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -95,7 +96,7 @@ public DocumentationPage convert(TypeElement typeElement, DocumentationPageInfo

if(isEnum(typeElement)) {
DocumentedEnumConstants enumConstants = new DocumentedEnumConstants(getName(typeElement));
enumConstantConverter.convertAndAddTo(typeElement, enumConstants);
enumConstantConverter.convertAndAddTo(typeElement, enumConstants, typeElement.getAnnotation(BracketEnum.class));
return new EnumTypePage(typePageInfo, virtualMembers, superType, implementations, staticMembers, genericParameters, enumConstants);
} else {
return new TypePage(typePageInfo, virtualMembers, superType, implementations, staticMembers, genericParameters);
Expand Down
Expand Up @@ -24,6 +24,7 @@
import com.blamejared.crafttweaker_annotation_processors.processors.document.page.page.TypePage;
import com.blamejared.crafttweaker_annotation_processors.processors.document.page.type.AbstractTypeInfo;
import com.blamejared.crafttweaker_annotation_processors.processors.document.page.type.TypePageTypeInfo;
import com.blamejared.crafttweaker_annotations.annotations.BracketEnum;
import com.blamejared.crafttweaker_annotations.annotations.NativeTypeRegistration;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -179,7 +180,7 @@ private DocumentedStaticMembers convertStaticMembers(TypeElement typeElement, Do

private void convertNativeEnumMembers(TypeElement typeElement, DocumentedEnumConstants documentedEnumConstants) {

enumConstantConverter.convertAndAddTo(getNativeType(typeElement), documentedEnumConstants);
enumConstantConverter.convertAndAddTo(getNativeType(typeElement), documentedEnumConstants, typeElement.getAnnotation(BracketEnum.class));
}

private List<DocumentedGenericParameter> convertGenericParameters(TypeElement typeElement) {
Expand Down
Expand Up @@ -41,7 +41,11 @@ public void write(PageOutputWriter writer) {
private void writeConstant(EnumConstant constant, PageOutputWriter writer) {

writeConstantComment(constant.getComment(), writer);
writer.printf("%s.%s%n", typeName.getSimpleName(), constant.getName());
if (constant.isBracket()) {
writer.println(constant.getName());
} else {
writer.println(typeName.getSimpleName() + "." + constant.getName());
}
}

private void writeConstantComment(DocumentationComment comment, PageOutputWriter writer) {
Expand Down
Expand Up @@ -8,11 +8,13 @@ public class EnumConstant implements IFillMeta {

private final String name;
private final DocumentationComment comment;
private final boolean isBracket;

public EnumConstant(String name, DocumentationComment comment) {
public EnumConstant(String name, DocumentationComment comment, boolean isBracket) {

this.name = name;
this.comment = comment;
this.isBracket = isBracket;
}

public String getName() {
Expand All @@ -24,7 +26,11 @@ public DocumentationComment getComment() {

return comment;
}


public boolean isBracket() {
return isBracket;
}

@Override
public void fillMeta(DocumentMeta meta) {

Expand Down

0 comments on commit 1f64bfe

Please sign in to comment.