Skip to content

Commit

Permalink
[#3] start implementing comment formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarwell committed May 28, 2021
1 parent 1321712 commit db27099
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.codehaus.mojo.tidy.task;

import static org.codehaus.mojo.tidy.task.XMLEventReaderFactory.createEventReaderForPom;

import javax.xml.stream.Location;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
import java.util.Collections;

public class EnsureCommentsOnSingleLine implements TidyTask
{

@Override
public String tidyPom( String pom, Format format ) throws XMLStreamException
{
XMLEventReader eventReader = createEventReaderForPom( pom );
try
{
return moveTrailingComments( pom, eventReader, format );
}
finally
{
eventReader.close();
}
}

private String moveTrailingComments( String pom, XMLEventReader reader, Format format ) throws XMLStreamException
{
StringBuilder outputPom = new StringBuilder();
XMLEvent previousEvent = null;
int indent = 0;
int startPrevEl = 0;
int startCurrentEl;

while ( reader.hasNext() )
{
XMLEvent event = reader.nextEvent();
final Location location = event.getLocation();
startCurrentEl = location.getCharacterOffset();
switch ( event.getEventType() )
{
case XMLEvent.COMMENT:
final int startOfNextElement = getPosOfNextEvent( reader );

if ( previousElementInSameRow( location, previousEvent ))
{
final String whitespace = String.join( "", Collections.nCopies( indent, " " ) );
final String textToInsert =
pom.substring( startCurrentEl, startOfNextElement )
+ format.getLineSeparator()
+ whitespace;
outputPom.insert( startPrevEl, textToInsert );
}
else
{
outputPom.append( pom, startCurrentEl, startOfNextElement );
}

break;

case XMLEvent.END_DOCUMENT:
outputPom.append( pom.substring( startCurrentEl ) );
break;

case XMLEvent.START_ELEMENT:
startPrevEl = startCurrentEl;
indent = location.getColumnNumber() - 1;
// pass-through
default:
final int next = getPosOfNextEvent( reader );
previousEvent = event;

outputPom.append( pom, startCurrentEl, next );
break;
}
}

return outputPom.toString();
}

private boolean previousElementInSameRow( Location location, XMLEvent previousElement )
{
if ( previousElement == null )
{
return false;
}

return location.getLineNumber() == previousElement.getLocation().getLineNumber();
}

private boolean nextElementInSameRow( Location location, XMLEventReader reader ) throws XMLStreamException
{
final XMLEvent peek = reader.peek();

if ( peek == null )
{
return false;
}

if ( peek.getEventType() != XMLEvent.START_ELEMENT )
{
return false;
}

return peek.getLocation().getLineNumber() == location.getLineNumber();
}

private int getPosOfNextEvent( XMLEventReader reader )
throws XMLStreamException
{
return reader.peek().getLocation().getCharacterOffset();
}
}
3 changes: 2 additions & 1 deletion src/main/java/org/codehaus/mojo/tidy/task/PomTidy.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public class PomTidy
private static final FormatIdentifier FORMAT_IDENTIFIER = new FormatIdentifier();

private static final List<TidyTask> TIDY_TASKS =
asList( new EnsureXmlHeader(), new EnsureOrderAndIndent(), new EnsureSingleLineProjectStartTag(),
asList( new EnsureXmlHeader(),
new EnsureCommentsOnSingleLine(), new EnsureOrderAndIndent(), new EnsureSingleLineProjectStartTag(),
new EnsureTrailingNewLine() );

public String tidy( String pom )
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/codehaus/mojo/tidy/task/PomTidyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static Iterable<String> tests()
return asList( "add-xml-declaration", "complete-pom", "do-not-mix-tab-and-spaces", "groupid-artifactid-version",
"plugin-config-with-maven-element-names", "pom-space-indent", "pom-tab-indent",
"pom-with-comments", "pom-with-crlf", "pom-with-line-without-indent", "pom-with-profiles",
"pom-with-reporting", "project-single-line" );
"pom-with-reporting", "project-single-line", "TIDY-3_comment" );
}

@Parameter( 0 )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<groupId>org.codehaus.mojo.tidy.its</groupId>
<artifactId>pom-space-indent</artifactId>
<version>1.0-SNAPSHOT</version>

<description>Test of tidy:pom on a pom with XML comments.</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<!-- bla bla 1 -->
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-java-dsl</artifactId>
<!-- bla bla 2 -->
<version>1.1.2.RELEASE</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<groupId>org.codehaus.mojo.tidy.its</groupId>
<artifactId>pom-space-indent</artifactId>
<version>1.0-SNAPSHOT</version>

<description>Test of tidy:pom on a pom with XML comments.</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<!-- bla bla 1 -->
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-java-dsl</artifactId>
<version>1.1.2.RELEASE</version> <!-- bla bla 2 -->
</dependency>
</dependencies>

</project>

0 comments on commit db27099

Please sign in to comment.