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 20, 2017
1 parent 4b32c66 commit 14473c3
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.Javadoc;
import com.github.javaparser.javadoc.JavadocBlockTag; import com.github.javaparser.javadoc.JavadocBlockTag;
import com.github.javaparser.javadoc.description.JavadocDescription; import com.github.javaparser.javadoc.description.JavadocDescription;
import com.github.javaparser.utils.Utils;


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


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


/** /**
* Any kind of utility. * Any kind of utility.
Expand All @@ -34,6 +34,8 @@
public class Utils { public class Utils {
public static final String EOL = System.getProperty("line.separator"); 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) { public static <T> List<T> ensureNotNull(List<T> list) {
return list == null ? new ArrayList<>() : list; return list == null ? new ArrayList<>() : list;
} }
Expand Down
Expand Up @@ -91,4 +91,17 @@ public void inlineTagsAreParsable() {
assertTrue(javadoc.contains("{@link TOVersion}")); 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 14473c3

Please sign in to comment.