Skip to content

Commit

Permalink
GH-2 Add prettier to convert indentation based source to brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Jul 24, 2020
1 parent 25ffe14 commit 1b98d6e
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/main/java/net/dzikoysk/cdn/CdnConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public final class CdnConfiguration {

private final Map<Class<?>, Function<Object, String>> serializers = new HashMap<>();
private final Map<Class<?>, Function<String, Object>> deserializers = new HashMap<>();
private boolean indentationEnabled;

{
serializer(String.class, Object::toString);
Expand All @@ -31,6 +32,15 @@ public <T> CdnConfiguration deserializer(Class<T> type, Function<String, T> dese
return this;
}

public CdnConfiguration enableIndentationFormatting() {
this.indentationEnabled = true;
return this;
}

public boolean isIndentationEnabled() {
return indentationEnabled;
}

public Map<Class<?>, Function<String, Object>> getDeserializers() {
return deserializers;
}
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/net/dzikoysk/cdn/CdnPrettier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package net.dzikoysk.cdn;

import org.panda_lang.utilities.commons.StringUtils;

final class CdnPrettier {

private final String[] lines;
private final StringBuilder converted = new StringBuilder();
private int previousIndentation = 0;

CdnPrettier(String source) {
this.lines = StringUtils.split(source.replace(System.lineSeparator(), CdnConstants.LINE_SEPARATOR), CdnConstants.LINE_SEPARATOR);
}

String tryToConvertIndentationInADumbWay() {
for (String line : lines) {
String indentation = StringUtils.extractParagraph(line);
line = line.trim();
close(indentation.length());

if (line.endsWith(":")) {
converted.append(indentation)
.append(line)
.append(" {")
.append(CdnConstants.LINE_SEPARATOR);
}
else {
converted.append(indentation)
.append(line)
.append(CdnConstants.LINE_SEPARATOR);
}

previousIndentation = indentation.length();
}

close(0);
return converted.toString();
}

private void close(int toIndentation) {
while (previousIndentation > toIndentation) {
previousIndentation = previousIndentation - 2;

converted.append(StringUtils.buildSpace(previousIndentation))
.append("}")
.append(CdnConstants.LINE_SEPARATOR);
}
}

}
4 changes: 4 additions & 0 deletions src/main/java/net/dzikoysk/cdn/CdnReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public CdnReader(CDN cdn) {
}

public Configuration read(String source) {
if (cdn.getConfiguration().isIndentationEnabled()) {
source = new CdnPrettier(source).tryToConvertIndentationInADumbWay();
}

String normalizedSource = StringUtils.replace(source.trim(), System.lineSeparator(), CdnConstants.LINE_SEPARATOR);

List<String> lines = Arrays.stream(normalizedSource.split(CdnConstants.LINE_SEPARATOR))
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/dzikoysk/cdn/CdnWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public String render(ConfigurationElement<?> element) {
private void render(StringBuilder content, int level, ConfigurationElement<?> element) {
String indentation = StringUtils.buildSpace(level * 2);


for (String comment : element.getDescription()) {
content.append(indentation)
.append(comment)
Expand Down
35 changes: 35 additions & 0 deletions src/test/groovy/net/dzikoysk/cdn/CdnPrettierTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.dzikoysk.cdn


import org.junit.jupiter.api.Test

import static org.junit.jupiter.api.Assertions.assertEquals

class CdnPrettierTest {

@Test
void 'should convert indentation to brackets' () {
def prettier = new CdnPrettier("""
key: value
section:
subSection:
subKey: value
# comment
key: value
""".stripIndent().trim())

assertEquals("""\
key: value
section: {
subSection: {
subKey: value
}
# comment
key: value
}
""".stripIndent(), prettier.tryToConvertIndentationInADumbWay())
}

}
6 changes: 3 additions & 3 deletions src/test/groovy/net/dzikoysk/cdn/TestConfiguration.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import net.dzikoysk.cdn.entity.SectionLink
class TestConfiguration {

@Description("# Root entry description")
public String rootEntry = "default value";
public String rootEntry = "default value"

@Description("")
@Description("// Section description")
@SectionLink
public SectionConfiguration section = new SectionConfiguration();
public SectionConfiguration section = new SectionConfiguration()

static class SectionConfiguration {

@Description("# Random value")
public Integer subEntry = 7;
public Integer subEntry = 7

}

Expand Down

0 comments on commit 1b98d6e

Please sign in to comment.