Skip to content

Commit

Permalink
Filter empty lines between block tags
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbosch committed Jan 30, 2017
1 parent f255e9e commit c8ce8fa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
Expand Up @@ -25,6 +25,7 @@
import com.github.javaparser.javadoc.Javadoc;
import com.github.javaparser.javadoc.JavadocBlockTag;
import com.github.javaparser.javadoc.description.JavadocDescription;
import com.github.javaparser.utils.Utils;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -44,20 +45,21 @@ public static Javadoc parse(JavadocComment comment) {

public static Javadoc parse(String commentContent) {
List<String> cleanLines = cleanLines(commentContent);
int index = -1;
for (int i = 0; i < cleanLines.size() && index == -1; i++) {
if (isABlockLine(cleanLines.get(i))) {
index = i;
}
}
int indexOfFirstBlockTag = cleanLines.stream()
.filter(JavadocParser::isABlockLine)
.map(cleanLines::indexOf)
.findFirst()
.orElse(-1);
List<String> blockLines;
String descriptionText;
if (index == -1) {
if (indexOfFirstBlockTag == -1) {
descriptionText = trimRight(String.join("\n", cleanLines));
blockLines = Collections.emptyList();
} else {
descriptionText = trimRight(String.join("\n", cleanLines.subList(0, index)));
blockLines = cleanLines.subList(index, cleanLines.size());
descriptionText = trimRight(String.join("\n", cleanLines.subList(0, indexOfFirstBlockTag)));
blockLines = cleanLines.subList(indexOfFirstBlockTag, cleanLines.size()).stream()
.filter(Utils.STRING_NOT_EMPTY)
.collect(Collectors.toList());
}
Javadoc document = new Javadoc(JavadocDescription.parseText(descriptionText));
blockLines.forEach(l -> document.addBlockTag(parseBlockTag(l)));
Expand Down
Expand Up @@ -24,7 +24,7 @@
import java.io.IOException;
import java.io.Reader;
import java.util.*;
import java.util.stream.Collectors;
import java.util.function.Predicate;

/**
* Any kind of utility.
Expand All @@ -34,6 +34,8 @@
public class Utils {
public static final String EOL = System.getProperty("line.separator");

public static final Predicate<String> STRING_NOT_EMPTY = s -> !s.isEmpty();

public static <T> List<T> ensureNotNull(List<T> list) {
return list == null ? new ArrayList<>() : list;
}
Expand Down
Expand Up @@ -91,4 +91,17 @@ public void inlineTagsAreParsable() {
assertTrue(javadoc.contains("{@link TOVersion}"));
}

@Test
public void emptyLinesBetweenBlockTagsGetsFiltered() {
String comment = " * The type of the Object to be mapped.\n" +
" * This interface maps the given Objects to existing ones in the database and\n" +
" * saves them.\n" +
" * \n" +
" * @author censored\n" +
" * \n" +
" * @param <T>\n";
Javadoc javadoc = JavaParser.parseJavadoc(comment);
assertEquals(javadoc.getBlockTags().size(), 2);
}

}

0 comments on commit c8ce8fa

Please sign in to comment.