Skip to content

Commit

Permalink
refactor: getDocComment uses the same implementation as pretty-printer (
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus authored and pvojtechovsky committed Nov 25, 2018
1 parent 7b56f7f commit 1bda786
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 47 deletions.
85 changes: 48 additions & 37 deletions src/main/java/spoon/reflect/visitor/CommentHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtJavaDoc;
import spoon.reflect.code.CtJavaDocTag;
import spoon.support.Internal;

import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Pattern;

/**
* Computes source code representation of the Comment literal
*/
class CommentHelper {
@Internal
public class CommentHelper {

/**
* RegExp which matches all possible line separators
Expand All @@ -37,15 +40,19 @@ class CommentHelper {
private CommentHelper() {
}

static void printComment(PrinterHelper printer, CtComment comment) {
List<CtJavaDocTag> tags = null;
if (comment instanceof CtJavaDoc) {
tags = ((CtJavaDoc) comment).getTags();
}
printComment(printer, comment.getCommentType(), comment.getContent(), tags);
/** returns a pretty-printed version of a comment, with prefix, suffix, and intermediate prefix for block and Javadoc */
public static String printComment(CtComment comment) {
PrinterHelper ph = new PrinterHelper(comment.getFactory().getEnvironment());
// now we only use one single method to print all tags
printCommentContent(ph, comment, s -> { return s; });
return ph.toString();
}

static void printComment(PrinterHelper printer, CtComment.CommentType commentType, String content, Collection<CtJavaDocTag> javaDocTags) {

static void printComment(PrinterHelper printer, CtComment comment) {
CtComment.CommentType commentType = comment.getCommentType();
String content = comment.getContent();
// prefix
switch (commentType) {
case FILE:
printer.write(DefaultJavaPrettyPrinter.JAVADOC_START).writeln();
Expand All @@ -60,34 +67,16 @@ static void printComment(PrinterHelper printer, CtComment.CommentType commentTyp
printer.write(DefaultJavaPrettyPrinter.BLOCK_COMMENT_START);
break;
}
// content
switch (commentType) {
case INLINE:
printer.write(content);
break;
default:
String[] lines = LINE_SEPARATORS_RE.split(content);
for (String com : lines) {
if (commentType == CtComment.CommentType.BLOCK) {
printer.write(com);
if (lines.length > 1) {
printer.writeln();
}
} else {
if (!com.isEmpty()) {
printer.write(DefaultJavaPrettyPrinter.COMMENT_STAR + com).writeln();
} else {
printer.write(" *" /* no trailing space */ + com).writeln();
}
}
}
if (javaDocTags != null && javaDocTags.isEmpty() == false) {
printer.write(" *").writeln();
for (CtJavaDocTag docTag : javaDocTags) {
printJavaDocTag(printer, docTag);
}
}
break;
// per line suffix
printCommentContent(printer, comment, s -> { return (" * " + s).replaceAll(" *$", ""); });
}
// suffix
switch (commentType) {
case BLOCK:
printer.write(DefaultJavaPrettyPrinter.BLOCK_COMMENT_END);
Expand All @@ -101,9 +90,34 @@ static void printComment(PrinterHelper printer, CtComment.CommentType commentTyp
}
}

static void printJavaDocTag(PrinterHelper printer, CtJavaDocTag docTag) {
printer.write(DefaultJavaPrettyPrinter.COMMENT_STAR);
printer.write(CtJavaDocTag.JAVADOC_TAG_PREFIX);
static void printCommentContent(PrinterHelper printer, CtComment comment, Function<String, String> transfo) {
CtComment.CommentType commentType = comment.getCommentType();
String content = comment.getContent();
String[] lines = LINE_SEPARATORS_RE.split(content);
for (String com : lines) {
if (commentType == CtComment.CommentType.BLOCK) {
printer.write(com);
if (lines.length > 1) {
printer.write(CtComment.LINE_SEPARATOR);
}
} else {
printer.write(transfo.apply(com)).writeln(); // removing spaces at the end of the space
}
}
if (comment instanceof CtJavaDoc) {
List<CtJavaDocTag> tags = null;
Collection<CtJavaDocTag> javaDocTags = ((CtJavaDoc) comment).getTags();
if (javaDocTags != null && javaDocTags.isEmpty() == false) {
printer.write(transfo.apply("")).writeln();
for (CtJavaDocTag docTag : javaDocTags) {
printJavaDocTag(printer, docTag, transfo);
}
}
}
}

static void printJavaDocTag(PrinterHelper printer, CtJavaDocTag docTag, Function<String, String> transfo) {
printer.write(transfo.apply(CtJavaDocTag.JAVADOC_TAG_PREFIX));
printer.write(docTag.getType().name().toLowerCase());
printer.write(" ");
if (docTag.getType().hasParam()) {
Expand All @@ -113,11 +127,8 @@ static void printJavaDocTag(PrinterHelper printer, CtJavaDocTag docTag) {
String[] tagLines = LINE_SEPARATORS_RE.split(docTag.getContent());
for (int i = 0; i < tagLines.length; i++) {
String com = tagLines[i];
if (i > 0 || docTag.getType().hasParam()) {
printer.write(DefaultJavaPrettyPrinter.COMMENT_STAR);
}
if (docTag.getType().hasParam()) {
printer.write("\t\t");
printer.write(transfo.apply("\t\t"));
}
printer.write(com.trim()).writeln();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ public void visitCtJavaDocTag(CtJavaDocTag docTag) {
* E.g. from CtJavaDocTag#toString
* Write directly to PrinterHelper, because java doc tag is not a java token. Normally it is part of COMMENT token.
*/
CommentHelper.printJavaDocTag(printer.getPrinterHelper(), docTag);
CommentHelper.printJavaDocTag(printer.getPrinterHelper(), docTag, x -> { return x; });
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import spoon.reflect.ModelElementContainerDefaultCapacities;
import spoon.reflect.annotations.MetamodelPropertyField;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtJavaDoc;
import spoon.reflect.code.CtJavaDocTag;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtAnnotation;
Expand Down Expand Up @@ -75,6 +73,8 @@
import java.util.Map;
import java.util.Set;

import static spoon.reflect.visitor.CommentHelper.printComment;

/**
* Contains the default implementation of most CtElement methods.
*
Expand Down Expand Up @@ -186,12 +186,7 @@ public List<CtAnnotation<? extends Annotation>> getAnnotations() {
public String getDocComment() {
for (CtComment ctComment : comments) {
if (ctComment.getCommentType() == CtComment.CommentType.JAVADOC) {
StringBuilder result = new StringBuilder();
result.append(ctComment.getContent() + System.lineSeparator());
for (CtJavaDocTag tag: ((CtJavaDoc) ctComment).getTags()) {
result.append(tag.toString()); // the tag already contains a new line
}
return result.toString();
return printComment(ctComment);
}
}
return "";
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/javadoc/JavaDocTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void testJavaDocReprint() {

// contract: getDocComment returns the comment content together with the tag content
CtMethod<?> method = aClass.getMethodsByName("create").get(0);
assertEquals("Creates an annotation type." + System.lineSeparator()
assertEquals("Creates an annotation type." + System.lineSeparator() + System.lineSeparator()
+ "@param owner" + System.lineSeparator()
+ "\t\tthe package of the annotation type" + System.lineSeparator()
+ "@param simpleName" + System.lineSeparator()
Expand Down

0 comments on commit 1bda786

Please sign in to comment.