Skip to content

Commit

Permalink
Merge pull request #184 from hazendaz/main
Browse files Browse the repository at this point in the history
Resolve comment formatter issues
  • Loading branch information
hazendaz authored Apr 11, 2024
2 parents f00445d + b04de4d commit 84a2d88
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 72 deletions.
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ updates:
maven:
patterns:
- "*"
ignore:
- dependency-name: org.slf4j:slf4j-api
version:
- ">= 2.0.0"
- dependency-name: org.slf4j:slf4j-simple
version:
- ">= 2.0.0"
- package-ecosystem: github-actions
directory: "/"
schedule:
Expand Down
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ SPDX-License-Identifier: EPL-2.0
</ignoredUnusedDeclaredDependencies>
</configuration>
</plugin>
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<configuration>
<licenseSets>
<licenseSet>
<excludes combine.children="append">
<exclude>**/*-output.xml</exclude>
<exclude>**/*-expected.xml</exclude>
</excludes>
</licenseSet>
</licenseSets>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,45 @@
package net.revelc.code.formatter.xml.lib;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CommentFormatter {

private static final Pattern ORIGINAL_INDENT_PATTERN = Pattern.compile("^(?<indent>\\s*)-->");
private static final Logger logger = LoggerFactory.getLogger(CommentFormatter.class);

public String format(String tagText, String indent, String lineDelimiter) {
public String format(String tagText, String indent, String lineDelimiter, FormattingPreferences prefs) {
String[] lines = tagText.split(lineDelimiter, -1);
String originalIndent = resolveOriginalIndent(lines);

List<String> newLines = new ArrayList<>();
for (String line : lines) {
newLines.add(indent + removeOriginalIndent(line, originalIndent));
if (logger.isDebugEnabled()) {
logger.debug("input: {}\n", Arrays.toString(lines));
}

return String.join(lineDelimiter, newLines);
}

private String resolveOriginalIndent(String[] lines) {
// only multi-line comments need replace original indentation.
if (lines.length < 2) {
return null;
// Caller sets initial indents to method (no indent before <!-- and indent before -->)
List<String> newLines = new ArrayList<>();
for (String line : lines) {
// do not trim leading space on multi-line comments (just add one indent)
if (lines.length < 2) {
newLines.add(indent + line);
} else if (line.trim().equals("<!--") || line.trim().equals("-->") || line.contains("<!--")) {
// add one indent for begin comment / end comment / line starting comment with text
// allowing last line with comment to be indented twice on else
newLines.add(indent + line.stripLeading());
} else if (indent.equals("") && !line.stripLeading().equals("")) {
// If indent is empty and line is not empty, use canonicalIndent
newLines.add(prefs.getCanonicalIndent() + line.stripLeading());
} else {
// Add two intent for others
newLines.add(indent + indent + line.stripLeading());
}
}

for (int i = lines.length - 1; i >= 0; i--) {
String line = lines[i];
logger.debug("output: {}\n", newLines);

if (line.trim().endsWith("-->")) {
Matcher m = ORIGINAL_INDENT_PATTERN.matcher(line);
if (m.matches()) {
return m.group("indent");
}
return null;
}
}
return null;
// Returned data to caller will be properly positioned by caller
return String.join(lineDelimiter, newLines);
}

private static String removeOriginalIndent(String line, String indent) {
if (indent != null && line.startsWith(indent)) {
return line.substring(indent.length());
}
return line;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ private void copyNode(Reader reader, FormatState state) throws IOException {
} else if (tag instanceof CommentReader) {
StringBuilder indentBuilder = new StringBuilder(30);
indent(state.depth, indentBuilder);
state.out.append(
new CommentFormatter().format(tag.getTagText(), indentBuilder.toString(), fDefaultLineDelimiter));
state.out.append(new CommentFormatter().format(tag.getTagText(), indentBuilder.toString(),
fDefaultLineDelimiter, prefs));
} else {
String tagText = tag.getTagText();
if (!prefs.getDeleteBlankLines()
Expand Down Expand Up @@ -452,9 +452,7 @@ protected String readTag() throws IOException {
char c = (char) intChar;

node.append(c);
// TODO logic incorrectly assumes that " is quote character
// when it could also be '
if (c == '"') {
if (c == '"' || c == '\'') {
insideQuote = !insideQuote;
}
if (c == '>' && !insideQuote) {
Expand All @@ -468,6 +466,7 @@ protected String readTag() throws IOException {
private static ErrorHandler errorHandler = new ErrorHandler() {
@Override
public void warning(SAXParseException e) throws SAXException {
// Do nothing on warning
}

@Override
Expand Down
20 changes: 10 additions & 10 deletions src/test/resources/default-output.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
This program and the accompanying materials are made
available under the terms of the Eclipse Public License 2.0
which is available at https://www.eclipse.org/legal/epl-2.0/
This program and the accompanying materials are made
available under the terms of the Eclipse Public License 2.0
which is available at https://www.eclipse.org/legal/epl-2.0/
SPDX-License-Identifier: EPL-2.0
SPDX-License-Identifier: EPL-2.0
-->
<mule xmlns:api-platform-gw="http://www.mulesoft.org/schema/mule/api-platform-gw"
Expand Down Expand Up @@ -34,20 +34,20 @@ http://www.mulesoft.org/schema/mule/api-platform-gw http://www.mulesoft.org/sche
<!--Here's a comment surrounded wrapped by empty lines to make sure they are kept-->

<!--
Here's a comment block.
Make sure they have correct indentation after format.
Here's a comment block.
Make sure they have correct indentation after format.
-->

<!--
Here's a comment block with leading spaces.
Make sure they have correct indentation and keep the leading spaces after format.
Here's a comment block with leading spaces.
Make sure they have correct indentation and keep the leading spaces after format.
-->

<!--Here's a comment block with unaligned block start/end.
Make sure they have correct indentation after format.-->
Make sure they have correct indentation after format.-->

<api-platform-gw:api apiName="${api-v2.name}" version="${api-v2.version}" flowRef="api-v2-main" create="true"
apikitRef="api-v2-config" doc:name="API Autodiscovery" />
apikitRef='api-v2-config' doc:name="API Autodiscovery" />
<flow name="api-v2-main">
<http:listener config-ref="api-httpsListenerConfig" path="/api/v2/*" doc:name="HTTP" />
<set-variable variableName="traceId"
Expand Down
20 changes: 10 additions & 10 deletions src/test/resources/multi-lined-attrs-output.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
This program and the accompanying materials are made
available under the terms of the Eclipse Public License 2.0
which is available at https://www.eclipse.org/legal/epl-2.0/
This program and the accompanying materials are made
available under the terms of the Eclipse Public License 2.0
which is available at https://www.eclipse.org/legal/epl-2.0/
SPDX-License-Identifier: EPL-2.0
SPDX-License-Identifier: EPL-2.0
-->
<mule
Expand Down Expand Up @@ -57,24 +57,24 @@ http://www.mulesoft.org/schema/mule/api-platform-gw http://www.mulesoft.org/sche
<!--Here's a comment surrounded wrapped by empty lines to make sure they are kept-->

<!--
Here's a comment block.
Make sure they have correct indentation after format.
Here's a comment block.
Make sure they have correct indentation after format.
-->

<!--
Here's a comment block with leading spaces.
Make sure they have correct indentation and keep the leading spaces after format.
Here's a comment block with leading spaces.
Make sure they have correct indentation and keep the leading spaces after format.
-->

<!--Here's a comment block with unaligned block start/end.
Make sure they have correct indentation after format.-->
Make sure they have correct indentation after format.-->

<api-platform-gw:api
apiName="${api-v2.name}"
version="${api-v2.version}"
flowRef="api-v2-main"
create="true"
apikitRef="api-v2-config"
apikitRef='api-v2-config'
doc:name="API Autodiscovery" />
<flow
name="api-v2-main">
Expand Down
20 changes: 10 additions & 10 deletions src/test/resources/no-wrap-tags-output.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
This program and the accompanying materials are made
available under the terms of the Eclipse Public License 2.0
which is available at https://www.eclipse.org/legal/epl-2.0/
This program and the accompanying materials are made
available under the terms of the Eclipse Public License 2.0
which is available at https://www.eclipse.org/legal/epl-2.0/
SPDX-License-Identifier: EPL-2.0
SPDX-License-Identifier: EPL-2.0
-->
<mule xmlns:api-platform-gw="http://www.mulesoft.org/schema/mule/api-platform-gw" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:apikit="http://www.mulesoft.org/schema/mule/apikit" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
Expand All @@ -24,19 +24,19 @@ http://www.mulesoft.org/schema/mule/api-platform-gw http://www.mulesoft.org/sche
<!--Here's a comment surrounded wrapped by empty lines to make sure they are kept-->

<!--
Here's a comment block.
Make sure they have correct indentation after format.
Here's a comment block.
Make sure they have correct indentation after format.
-->

<!--
Here's a comment block with leading spaces.
Make sure they have correct indentation and keep the leading spaces after format.
Here's a comment block with leading spaces.
Make sure they have correct indentation and keep the leading spaces after format.
-->

<!--Here's a comment block with unaligned block start/end.
Make sure they have correct indentation after format.-->
Make sure they have correct indentation after format.-->

<api-platform-gw:api apiName="${api-v2.name}" version="${api-v2.version}" flowRef="api-v2-main" create="true" apikitRef="api-v2-config" doc:name="API Autodiscovery" />
<api-platform-gw:api apiName="${api-v2.name}" version="${api-v2.version}" flowRef="api-v2-main" create="true" apikitRef='api-v2-config' doc:name="API Autodiscovery" />
<flow name="api-v2-main">
<http:listener config-ref="api-httpsListenerConfig" path="/api/v2/*" doc:name="HTTP" />
<set-variable variableName="traceId" value="#[message.inboundProperties.'correlationId' != null ? message.inboundProperties.'uber-trace-id' : java.util.UUID.randomUUID().toString()]" doc:name="Variable - traceId" mimeType="application/java" />
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/test-input.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ http://www.mulesoft.org/schema/mule/api-platform-gw http://www.mulesoft.org/sche
<!--Here's a comment block with unaligned block start/end.
Make sure they have correct indentation after format.-->

<api-platform-gw:api apiName="${api-v2.name}" version="${api-v2.version}" flowRef="api-v2-main" create="true" apikitRef="api-v2-config" doc:name="API Autodiscovery"/>
<api-platform-gw:api apiName="${api-v2.name}" version="${api-v2.version}" flowRef="api-v2-main" create="true" apikitRef='api-v2-config' doc:name="API Autodiscovery"/>
<flow name="api-v2-main">
<http:listener config-ref="api-httpsListenerConfig" path="/api/v2/*" doc:name="HTTP" />
<set-variable variableName="traceId" value="#[message.inboundProperties.'correlationId' != null ? message.inboundProperties.'uber-trace-id' : java.util.UUID.randomUUID().toString()]" doc:name="Variable - traceId" mimeType="application/java"/>
Expand Down
8 changes: 4 additions & 4 deletions src/test/resources/test-space-expected.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
This program and the accompanying materials are made
available under the terms of the Eclipse Public License 2.0
which is available at https://www.eclipse.org/legal/epl-2.0/
This program and the accompanying materials are made
available under the terms of the Eclipse Public License 2.0
which is available at https://www.eclipse.org/legal/epl-2.0/
SPDX-License-Identifier: EPL-2.0
SPDX-License-Identifier: EPL-2.0
-->
<root>
Expand Down

0 comments on commit 84a2d88

Please sign in to comment.