Skip to content

Commit 2d54b47

Browse files
authored
Do not indent new lines (#127)
1 parent 774e6d1 commit 2d54b47

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

src/main/java/io/fabric8/maven/LineBreakProcessor.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,46 @@
1313
* Inserts a line break on every major section
1414
*/
1515
class LineBreakProcessor extends AbstractXMLOutputProcessor {
16+
17+
private String buffer;
18+
1619
@Override
1720
protected void printElement(Writer out, FormatStack fstack, NamespaceStack nstack, Element element) throws IOException {
21+
Element nextSiblingElement = findNextSiblingElement(element);
22+
boolean shouldBreakLine = false;
23+
if (nextSiblingElement != null) {
24+
shouldBreakLine = shouldBreakLine(nextSiblingElement);
25+
}
26+
super.printElement(out, fstack, nstack, element);
27+
buffer = (shouldBreakLine) ? fstack.getLineSeparator() : null;
28+
}
29+
30+
private Element findNextSiblingElement(Element element) {
31+
Element parent = element.getParentElement();
32+
if (parent != null) {
33+
List<Element> children = parent.getChildren();
34+
int idx = children.indexOf(element);
35+
if (idx < children.size() - 1) {
36+
return children.get(idx + 1);
37+
}
38+
}
39+
return null;
40+
}
41+
42+
@Override
43+
protected void textRaw(Writer out, String str) throws IOException {
44+
if (buffer != null) {
45+
out.write(buffer);
46+
buffer = null;
47+
}
48+
super.textRaw(out, str);
49+
}
50+
51+
private static boolean shouldBreakLine(Element element) {
1852
Element rootElement = element.getDocument().getRootElement();
1953
// Test if it's a major section
2054
int idxElement = rootElement.indexOf(element);
2155
List<Element> children = element.getChildren();
22-
boolean addBreak = (idxElement > -1 && !children.isEmpty());
23-
if (addBreak) {
24-
textRaw(out, fstack.getLineSeparator());
25-
textRaw(out, fstack.getLevelIndent());
26-
}
27-
super.printElement(out, fstack, nstack, element);
56+
return (idxElement > -1 && !children.isEmpty());
2857
}
2958
}

src/test/approvals/io/fabric8/maven/MavenTest.should_write_existing_model_formatted.approved.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4-
4+
55
<parent>
66
<groupId>io.quarkiverse.githubapp</groupId>
77
<artifactId>quarkus-github-app-parent</artifactId>
@@ -11,7 +11,7 @@
1111
<artifactId>quarkus-github-app</artifactId>
1212
<name>Quarkus - GitHub App - Runtime</name>
1313
<description>Automate GitHub tasks with a GitHub App</description>
14-
14+
1515
<dependencies>
1616
<dependency>
1717
<groupId>io.quarkiverse.githubapp</groupId>
@@ -59,7 +59,7 @@
5959
<artifactId>graal-sdk</artifactId>
6060
</dependency>
6161
</dependencies>
62-
62+
6363
<build>
6464
<plugins>
6565
<plugin>

src/test/approvals/io/fabric8/maven/XMLFormatTest.should_ident_and_insert_line_break_on_major_sections.approved.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4-
4+
55
<parent>
66
<groupId>io.quarkiverse.githubapp</groupId>
77
<artifactId>quarkus-github-app-parent</artifactId>
@@ -11,7 +11,7 @@
1111
<artifactId>quarkus-github-app</artifactId>
1212
<name>Quarkus - GitHub App - Runtime</name>
1313
<description>Automate GitHub tasks with a GitHub App</description>
14-
14+
1515
<dependencies>
1616
<dependency>
1717
<groupId>io.quarkiverse.githubapp</groupId>
@@ -59,7 +59,7 @@
5959
<artifactId>graal-sdk</artifactId>
6060
</dependency>
6161
</dependencies>
62-
62+
6363
<build>
6464
<plugins>
6565
<plugin>

0 commit comments

Comments
 (0)