Skip to content

Commit

Permalink
formatter api
Browse files Browse the repository at this point in the history
  • Loading branch information
ZZZank committed May 6, 2024
1 parent 189b565 commit 684cc6a
Show file tree
Hide file tree
Showing 24 changed files with 92 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/probejs/compiler/EventCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private static void writeWildcardEvents(BufferedWriter writer) throws IOExceptio
"",
"E.g. `player.data_from_server.reload`, `ftbquests.completed.123456`"
)
.format(0, 0)
.formatLines(0, 0)
);
lines.add(
"declare function onEvent(name: `${string}.${string}`, handler: (event: Internal.EventJS) => void): void;"
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/probejs/compiler/TypingCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static void compileGlobal(Set<Class<?>> globalClasses) throws IOException

NameResolver.ResolvedName name = NameResolver.getResolvedName(clazz.getName());
if (name.getNamespace().isEmpty()) {
for (String line : formatter.format(0, 4)) {
for (String line : formatter.formatLines(0, 4)) {
writer.write(line);
writer.write("\n");
}
Expand All @@ -96,7 +96,7 @@ public static void compileGlobal(Set<Class<?>> globalClasses) throws IOException
val path = entry.getKey();
val formatters = entry.getValue();
val namespace = new FormatterNamespace(path, formatters);
for (val line : namespace.format(0, 4)) {
for (val line : namespace.formatLines(0, 4)) {
writer.write(line);
writer.write('\n');
}
Expand All @@ -113,17 +113,17 @@ public static void compileGlobal(Set<Class<?>> globalClasses) throws IOException
"Document",
DocManager.classAdditions.values().stream().map(l -> l.get(0)).collect(Collectors.toList())
)
.format(0, 4)) {
.formatLines(0, 4)) {
writer.write(line);
writer.write("\n");
}
//namespace::Type
for (String line : new FormatterNamespace("Type", DocManager.typeDocuments).format(0, 4)) {
for (String line : new FormatterNamespace("Type", DocManager.typeDocuments).formatLines(0, 4)) {
writer.write(line);
writer.write("\n");
}
//no namespace
for (String line : new FormatterRaw(DocManager.rawTSDoc).format(0, 4)) {
for (String line : new FormatterRaw(DocManager.rawTSDoc).formatLines(0, 4)) {
writer.write(line);
writer.write("\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.probejs.ProbeJS;
import com.probejs.formatter.api.IFormatter;
import com.probejs.formatter.api.MultiFormatter;
import com.probejs.util.PUtil;
import java.util.*;
import java.util.function.Consumer;
Expand All @@ -13,7 +14,7 @@
import net.minecraft.client.resources.language.LanguageManager;
import net.minecraft.locale.Language;

public class FormatterLang implements IFormatter {
public class FormatterLang implements MultiFormatter {

public static final LanguageInfo DEFAULT_LANGUAGE = new LanguageInfo("en_us", "US", "English", false);
private static final Set<String> ALL_KEYS = new HashSet<>();
Expand All @@ -33,7 +34,7 @@ public static Set<String> getAllKeys() {
}

@Override
public List<String> format(int indent, int stepIndent) {
public List<String> formatLines(int indent, int stepIndent) {
if (!(Language.getInstance() instanceof ClientLanguage)) {
return new ArrayList<>(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void compile(BufferedWriter writer) throws IOException {
//more?

for (final String line : new FormatterNamespace("platform", new FormatterRaw(lines, false))
.format(0, 4)) {
.formatLines(0, 4)) {
writer.write(line);
writer.write('\n');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static List<String> format(int indent, int stepIndent) {
namespecedFmtr.add(new FormatterRaw(lines, false));
}

return new FormatterNamespace("stub.probejs", namespecedFmtr).format(indent, stepIndent);
return new FormatterNamespace("stub.probejs", namespecedFmtr).formatLines(indent, stepIndent);
}

public static void compile(BufferedWriter writer) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void compile(BufferedWriter writer) throws IOException {
formatters.add(new FormatterNamespace(namespace, new FormatterRaw(lines, false)));
});
val namespaced = new FormatterNamespace("Registry", formatters);
for (val line : namespaced.format(0, 4)) {
for (val line : namespaced.formatLines(0, 4)) {
writer.write(line);
writer.write('\n');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static List<String> format(int indent, int stepIndent) {
)
.collect(Collectors.toList());
return new FormatterNamespace("Tag", new FormatterRaw(lines, false))
.format(indent, stepIndent);
.formatLines(indent, stepIndent);
}

public static void compile(BufferedWriter writer) throws IOException {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/probejs/document/DocumentClass.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.probejs.document;

import com.probejs.document.comment.CommentUtil;
import com.probejs.formatter.api.IFormatter;
import com.probejs.formatter.api.MultiFormatter;
import com.probejs.util.PUtil;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

public class DocumentClass extends DocumentProperty implements IConcrete, IFormatter {
public class DocumentClass extends DocumentProperty implements IConcrete, MultiFormatter {

@Setter
@Getter
Expand Down Expand Up @@ -48,7 +48,7 @@ public void merge(DocumentClass other) {
}

@Override
public List<String> format(int indent, int stepIndent) {
public List<String> formatLines(int indent, int stepIndent) {
List<String> lines = new ArrayList<>();
StringBuilder firstLine = new StringBuilder(PUtil.indent(indent))
.append("class ")
Expand All @@ -61,8 +61,8 @@ public List<String> format(int indent, int stepIndent) {
firstLine.append("implements ").append(String.join(", ", this.interfaces)).append(' ');
}
lines.add(firstLine.append('{').toString());
this.fieldDocs.forEach(f -> lines.addAll(f.format(indent + stepIndent, stepIndent)));
this.methodDocs.forEach(m -> lines.addAll(m.format(indent + stepIndent, stepIndent)));
this.fieldDocs.forEach(f -> lines.addAll(f.formatLines(indent + stepIndent, stepIndent)));
this.methodDocs.forEach(m -> lines.addAll(m.formatLines(indent + stepIndent, stepIndent)));
lines.add(PUtil.indent(indent) + "}");
return lines;
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/probejs/document/DocumentComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import com.probejs.document.comment.CommentHandler;
import com.probejs.document.comment.CommentUtil;
import com.probejs.formatter.api.IFormatter;
import com.probejs.formatter.api.MultiFormatter;
import com.probejs.util.PUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

public class DocumentComment implements IDecorative, IFormatter {
public class DocumentComment implements IDecorative, MultiFormatter {

private final List<String> documentText;
private final HashMap<Class<? extends AbstractComment>, List<AbstractComment>> abstractComments = new HashMap<>();
Expand Down Expand Up @@ -59,7 +60,7 @@ public List<String> getDocumentText() {
}

@Override
public List<String> format(int indent, int stepIndent) {
public List<String> formatLines(int indent, int stepIndent) {
return getDocumentText().stream().map(PUtil.indent(indent)::concat).collect(Collectors.toList());
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/probejs/document/DocumentField.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.probejs.document.parser.processor.IDocumentProvider;
import com.probejs.document.type.IDocType;
import com.probejs.document.type.DocTypeResolver;
import com.probejs.formatter.api.IFormatter;
import com.probejs.formatter.api.MultiFormatter;
import com.probejs.util.PUtil;
import com.probejs.util.Pair;
import com.probejs.util.StringUtil;
Expand All @@ -12,7 +12,7 @@
import java.util.ArrayList;
import java.util.List;

public class DocumentField extends DocumentProperty implements IDocumentProvider<DocumentField>, IFormatter {
public class DocumentField extends DocumentProperty implements IDocumentProvider<DocumentField>, MultiFormatter {

private final boolean isFinal;
private final boolean isStatic;
Expand Down Expand Up @@ -61,10 +61,10 @@ public DocumentField provide() {
}

@Override
public List<String> format(int indent, int stepIndent) {
public List<String> formatLines(int indent, int stepIndent) {
List<String> formatted = new ArrayList<>();
if (comment != null) {
formatted.addAll(comment.format(indent, stepIndent));
formatted.addAll(comment.formatLines(indent, stepIndent));
}
formatted.add(
String.format(
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/probejs/document/DocumentMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.probejs.document.parser.processor.IDocumentProvider;
import com.probejs.document.type.IDocType;
import com.probejs.document.type.DocTypeResolver;
import com.probejs.formatter.api.IFormatter;
import com.probejs.formatter.api.MultiFormatter;
import com.probejs.info.clazz.MethodInfo;
import com.probejs.util.PUtil;
import com.probejs.util.StringUtil;
Expand All @@ -14,7 +14,7 @@

public class DocumentMethod
extends DocumentProperty
implements IDocumentProvider<DocumentMethod>, IFormatter {
implements IDocumentProvider<DocumentMethod>, MultiFormatter {

@Override
public DocumentMethod provide() {
Expand All @@ -30,9 +30,9 @@ public String getMethodBody() {
}

@Override
public List<String> format(int indent, int stepIndent) {
public List<String> formatLines(int indent, int stepIndent) {
List<String> formatted = new ArrayList<>();
if (comment != null) formatted.addAll(comment.format(indent, stepIndent));
if (comment != null) formatted.addAll(comment.formatLines(indent, stepIndent));
String paramStr = getParams()
.stream()
.map(documentParam ->
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/probejs/document/DocumentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.probejs.document.parser.processor.IDocumentProvider;
import com.probejs.document.type.IDocType;
import com.probejs.document.type.DocTypeResolver;
import com.probejs.formatter.api.IFormatter;
import com.probejs.formatter.api.MultiFormatter;
import com.probejs.util.PUtil;
import com.probejs.util.Pair;
import com.probejs.util.StringUtil;
Expand All @@ -15,7 +15,7 @@
import java.util.List;

@Getter
public class DocumentType implements IDocumentProvider<DocumentType>, IFormatter, IConcrete {
public class DocumentType implements IDocumentProvider<DocumentType>, MultiFormatter, IConcrete {

//type <name> = <type>;

Expand Down Expand Up @@ -43,7 +43,7 @@ public DocumentType provide() {
}

@Override
public List<String> format(int indent, int stepIndent) {
public List<String> formatLines(int indent, int stepIndent) {
if (!CommentUtil.isLoaded(comment) || CommentUtil.isHidden(comment)) {
return new ArrayList<>(0);
}
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/probejs/formatter/FormatterClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.probejs.document.comment.CommentUtil;
import com.probejs.document.type.IDocType;
import com.probejs.formatter.api.DocumentReceiver;
import com.probejs.formatter.api.IFormatter;
import com.probejs.formatter.api.MultiFormatter;
import com.probejs.formatter.resolver.NameResolver;
import com.probejs.info.clazz.ClassInfo;
import com.probejs.info.clazz.FieldInfo;
Expand All @@ -27,7 +27,7 @@
import java.util.*;
import java.util.stream.Collectors;

public class FormatterClass extends DocumentReceiver<DocumentClass> implements IFormatter {
public class FormatterClass extends DocumentReceiver<DocumentClass> implements MultiFormatter {

private final ClassInfo classInfo;
private final Map<String, FormatterField> fieldFormatters;
Expand Down Expand Up @@ -78,14 +78,14 @@ public static String formatParameterized(IType info) {
}

@Override
public List<String> format(int indent, int stepIndent) {
public List<String> formatLines(int indent, int stepIndent) {
List<String> lines = new ArrayList<>();
DocumentComment comment = document == null ? null : document.getComment();
if (comment != null) {
if (CommentUtil.isHidden(comment)) {
return lines;
}
lines.addAll(comment.format(indent, stepIndent));
lines.addAll(comment.formatLines(indent, stepIndent));
}

List<String> assignableTypes = DocManager.typesAssignable
Expand Down Expand Up @@ -180,7 +180,7 @@ public List<String> format(int indent, int stepIndent) {
//not static interface in namespace `Internal`
!(classInfo.isInterface() && fmtrMethod.getInfo().isStatic() && internal)
)
.forEach(fmtrMethod -> lines.addAll(fmtrMethod.format(indent + stepIndent, stepIndent)));
.forEach(fmtrMethod -> lines.addAll(fmtrMethod.formatLines(indent + stepIndent, stepIndent)));
//fields
fieldFormatters
.entrySet()
Expand All @@ -189,7 +189,7 @@ public List<String> format(int indent, int stepIndent) {
.filter(f -> !(classInfo.isInterface() && f.getValue().getFieldInfo().isStatic() && internal))
.forEach(f -> {
f.getValue().setFromInterface(classInfo.isInterface());
lines.addAll(f.getValue().format(indent + stepIndent, stepIndent));
lines.addAll(f.getValue().formatLines(indent + stepIndent, stepIndent));
});

// beans
Expand Down Expand Up @@ -254,21 +254,21 @@ public List<String> format(int indent, int stepIndent) {
lines.addAll(
new FormatterComments("Internal constructor, not callable unless via `java()`.")
.setStyle(FormatterComments.CommentStyle.J_DOC)
.format(indent + stepIndent, stepIndent)
.formatLines(indent + stepIndent, stepIndent)
);
}
classInfo
.getConstructors()
.stream()
.map(FormatterConstructor::new)
.forEach(f -> lines.addAll(f.format(indent + stepIndent, stepIndent)));
.forEach(f -> lines.addAll(f.formatLines(indent + stepIndent, stepIndent)));
}
// additions
for (DocumentField fieldDoc : fieldAdditions) {
lines.addAll(fieldDoc.format(indent + stepIndent, stepIndent));
lines.addAll(fieldDoc.formatLines(indent + stepIndent, stepIndent));
}
for (DocumentMethod methodDoc : methodAdditions) {
lines.addAll(methodDoc.format(indent + stepIndent, stepIndent));
lines.addAll(methodDoc.formatLines(indent + stepIndent, stepIndent));
}
//end
lines.add(PUtil.indent(indent) + "}");
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/probejs/formatter/FormatterComments.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.probejs.formatter;

import com.probejs.formatter.api.IFormatter;
import com.probejs.formatter.api.MultiFormatter;
import com.probejs.util.PUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -12,7 +12,7 @@
import java.util.Arrays;
import java.util.List;

public class FormatterComments implements IFormatter {
public class FormatterComments implements MultiFormatter {

@AllArgsConstructor
@Getter
Expand Down Expand Up @@ -68,7 +68,7 @@ public FormatterComments trim() {
}

@Override
public List<String> format(int indent, int stepIndent) {
public List<String> formatLines(int indent, int stepIndent) {
val idnt = PUtil.indent(indent);
val lines = new ArrayList<String>(2 + this.raw.size());
if (this.style.begin != null) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/probejs/formatter/FormatterConstructor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.probejs.formatter;

import com.probejs.formatter.api.IFormatter;
import com.probejs.formatter.api.MultiFormatter;
import com.probejs.formatter.resolver.NameResolver;
import com.probejs.info.clazz.ClassInfo;
import com.probejs.info.clazz.ConstructorInfo;
Expand All @@ -12,7 +12,7 @@
import java.util.List;
import java.util.stream.Collectors;

public class FormatterConstructor implements IFormatter {
public class FormatterConstructor implements MultiFormatter {

private final ConstructorInfo constructor;

Expand Down Expand Up @@ -58,7 +58,7 @@ private String formatParams() {
}

@Override
public List<String> format(int indent, int stepIndent) {
public List<String> formatLines(int indent, int stepIndent) {
List<String> lines = new ArrayList<>();
lines.add(String.format("%sconstructor(%s);", PUtil.indent(indent), formatParams()));
return lines;
Expand Down
Loading

0 comments on commit 684cc6a

Please sign in to comment.