Skip to content

Commit

Permalink
Extract display name and description from JavaDoc (#51)
Browse files Browse the repository at this point in the history
* Extract display name and description from JavaDoc

* Sanitize display name as much as possible too

* Replace any `{@code }` or `{@link }` tag with backticks

* Non greedy match

* Add or remove dots as per convention

* Correctly handle first sentence across two lines.

* Prevent empty description
  • Loading branch information
timtebeek authored Dec 17, 2023
1 parent c758a24 commit 626b74f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.parser.Tokens;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.TreeMaker;
Expand Down Expand Up @@ -399,6 +400,32 @@ private String recipeDescriptor(JCTree.JCClassDecl classDecl, String defaultDisp
String displayName = defaultDisplayName;
String description = defaultDescription;
Set<String> tags = new LinkedHashSet<>();

// Extract from JavaDoc
Tokens.Comment comment = cu.docComments.getComment(classDecl);
if (comment != null && comment.getText() != null && !comment.getText().isEmpty()) {
String commentText = comment.getText()
.replaceAll("\\{@\\S+\\s+(.*?)}", "`$1`")
.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\b", "\\b")
.replace("\t", "\\t")
.replace("\f", "\\f")
.replace("\r", "\\r");
String[] lines = commentText.split("\\.\\R+", 2);
displayName = lines[0].trim().replace("\n", "");
if (displayName.endsWith(".")) {
displayName = displayName.substring(0, displayName.length() - 1);
}
if (lines.length > 1 && !lines[1].trim().isEmpty()) {
description = lines[1].trim().replace("\n", "\\n");
if (!description.endsWith(".")) {
description += '.';
}
}
}

// Extract from the RecipeDescriptor annotation
for (JCTree.JCAnnotation annotation : classDecl.getModifiers().getAnnotations()) {
if (annotation.type.toString().equals("org.openrewrite.java.template.RecipeDescriptor")) {
for (JCTree.JCExpression argExpr : annotation.getArguments()) {
Expand Down Expand Up @@ -777,7 +804,7 @@ private boolean resolve(Context context, JCCompilationUnit cu) {

/**
* @param message The message to print
* @param symbol The symbol to attach the message to; printed as clickable link to file
* @param symbol The symbol to attach the message to; printed as clickable link to file
*/
private void printNoteOnce(String message, Symbol.ClassSymbol symbol) {
if (printedMessages.add(message)) {
Expand Down
10 changes: 10 additions & 0 deletions src/test/resources/refaster/UseStringIsEmpty.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;

/**
* Replace `s.length() > 0`
* with `!s.isEmpty()`.
*
* Second line that should show up in description only.
* May contain " and ' and \" and \\" and \n.
* Or even references to {@link String}.
* Or unicode 🐛.
*/
// XXX: Comment that should not show up in display name or description
public class UseStringIsEmpty {
@BeforeTemplate
boolean before(String s) {
Expand Down
7 changes: 5 additions & 2 deletions src/test/resources/refaster/UseStringIsEmptyRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,20 @@
import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*;


/**
* OpenRewrite recipe created for Refaster template `UseStringIsEmpty`.
*/
@NonNullApi
public class UseStringIsEmptyRecipe extends Recipe {

@Override
public String getDisplayName() {
return "Refaster template `UseStringIsEmpty`";
return "Replace `s.length() > 0` with `!s.isEmpty()`";
}

@Override
public String getDescription() {
return "Recipe created for the following Refaster template:\n```java\npublic class UseStringIsEmpty {\n \n @BeforeTemplate()\n boolean before(String s) {\n return s.length() > 0;\n }\n \n @AfterTemplate()\n boolean after(String s) {\n return !s.isEmpty();\n }\n}\n```\n.";
return "Second line that should show up in description only.\n May contain \" and ' and \\\" and \\\\\" and \\n.\n Or even references to `String`.\n Or unicode 🐛.";
}

@Override
Expand Down

0 comments on commit 626b74f

Please sign in to comment.