Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: getDocComment uses the same implementation as pretty-printer #2775

Merged
merged 3 commits into from
Nov 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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