Skip to content

Commit

Permalink
Fix issues formatting paragraphs in doc comments and its tests. Also …
Browse files Browse the repository at this point in the history
…fix missing handling of deprecation on enum fields.

Signed-off-by: jasperpotts <jasperpotts@users.noreply.github.com>
  • Loading branch information
jasperpotts committed Jan 10, 2025
1 parent 270006e commit 6621751
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static String cleanJavaDocComment(String fieldComment) {
return cleanDocStr(fieldComment
.replaceAll("/\\*\\*[\n\r\s\t]*\\*[\t\s]*|[\n\r\s\t]*\\*/","") // remove java doc
.replaceAll("\n\s+\\*\s+","\n") // remove indenting and *
.replaceAll("\n\s+\\*\s*\n","\n<p>\n") // remove indenting and *
.replaceAll("\n\s+\\*\s*\n","\n\n") // remove indenting and *
.replaceAll("/\\*\\*","") // remove indenting and /** at beginning of comment.
.trim() // Remove leading and trailing spaces.
);
Expand All @@ -162,12 +162,15 @@ public static String cleanDocStr(String docStr) {
.replaceAll(" < ", " &lt; ") // escape loose less than
.replaceAll(" > ", " &gt; ") // escape loose less than
.replaceAll(" & ", " &amp; ") //
.replaceAll("<p>((.|\n)*?)</p>", "%%%%%$1%%%%") // replace closed paragraphs temporarily
.replaceAll("<p>((\s|\n)*?)($|<[^>]+>)","$1$2$3") // remove <p> at end of paragraph
.replaceAll("<p>((.|\n)*?)(<p>|$|<[^>]+>)","<p>$1</p>$2$3") // clean up loose paragraphs
.replaceAll("<p>([^<]*?)</p>", "%%%%%$1%%%%") // replace closed paragraphs temporarily
.replaceAll("<p>((\\s|\\n)*?)($|<[^>]+>)","$1$2$3") // remove <p> at end of paragraph
.replaceAll("<p>((.|\\n)*?)([\\s\\n]*)(%%%%%|<p>|\\n@\\w+ |$|<[^>]+>)","<p>$1</p>$3$4") // clean up loose paragraphs
// Do second pass as we can miss some <p> that were caught as closers in first pass
.replaceAll("<p>([^<]*?)</p>", "%%%%%$1%%%%") // replace closed paragraphs temporarily
.replaceAll("<p>((.|\\n)*?)([\\s\\n]*)(%%%%%|<p>|\\n@\\w+ |$|<[^>]+>)","<p>$1</p>$3$4") // clean up loose paragraphs
// restore completed paragraphs
.replaceAll("%%%%%", "<p>") // replace back to paragraphs
.replaceAll("%%%%", "</p>") // replace back to paragraphs

;
}

Expand Down Expand Up @@ -482,18 +485,8 @@ else if (f.repeated()) {
@NonNull
private static String getPrimitiveWrapperEqualsGeneration(String generatedCodeSoFar, Field f) {
switch (f.messageType()) {
case "StringValue" ->
generatedCodeSoFar += (
"""
if (this.$fieldName == null && thatObj.$fieldName != null) {
return false;
}
if (this.$fieldName != null && !$fieldName.equals(thatObj.$fieldName)) {
return false;
}
""").replace("$fieldName", f.nameCamelFirstLower());
case "BoolValue" ->

case "StringValue", "BoolValue", "Int32Value", "UInt32Value", "Int64Value", "UInt64Value", "FloatValue",
"DoubleValue", "BytesValue" ->
generatedCodeSoFar += (
"""
if (this.$fieldName == null && thatObj.$fieldName != null) {
Expand All @@ -503,27 +496,7 @@ private static String getPrimitiveWrapperEqualsGeneration(String generatedCodeSo
return false;
}
""").replace("$fieldName", f.nameCamelFirstLower());
case "Int32Value", "UInt32Value", "Int64Value", "UInt64Value", "FloatValue", "DoubleValue" ->
generatedCodeSoFar += (
"""
if (this.$fieldName == null && thatObj.$fieldName != null) {
return false;
}
if (this.$fieldName != null && !$fieldName.equals(thatObj.$fieldName)) {
return false;
}
""").replace("$fieldName", f.nameCamelFirstLower());
case "BytesValue" ->
generatedCodeSoFar += (
"""
if (this.$fieldName == null && thatObj.$fieldName != null) {
return false;
}
if (this.$fieldName != null && !$fieldName.equals(thatObj.$fieldName)) {
return false;
}
""").replace("$fieldName", f.nameCamelFirstLower());
default -> throw new UnsupportedOperationException("Unhandled optional message type:" + f.messageType());
default -> throw new UnsupportedOperationException("Unhandled optional message type:" + f.messageType());
}
return generatedCodeSoFar;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,18 @@ public static void generateEnumFile(Protobuf3Parser.EnumDefContext enumDef, File
.replaceAll("/n\s*/n","/n") // remove empty lines
);
maxIndex = Math.max(maxIndex, enumNumber);
enumValues.put(enumNumber, new EnumValue(enumValueName, false,enumValueJavaDoc));
// extract if the enum is marks as deprecated
boolean deprecatedEnumValue = false;
if(item.enumField().enumValueOptions() != null && item.enumField().enumValueOptions().enumValueOption() != null) {
for(var option:item.enumField().enumValueOptions().enumValueOption()) {
if ("deprecated".equals(option.optionName().getText())) {
deprecatedEnumValue = true;
} else {
System.err.println("Unhandled Option: "+option.getText());
}
}
}
enumValues.put(enumNumber, new EnumValue(enumValueName, deprecatedEnumValue,enumValueJavaDoc));
} else if (item.optionStatement() != null){
if ("deprecated".equals(item.optionStatement().optionName().getText())) {
deprecated = "@Deprecated ";
Expand Down Expand Up @@ -102,10 +113,16 @@ static String createEnum(String javaDocComment, String deprecated, String enumNa
for (int i = 0; i <= maxIndex; i++) {
final EnumValue enumValue = enumValues.get(i);
if (enumValue != null) {
final String cleanedEnumComment =
final String cleanedEnumComment = enumValue.javaDoc.contains("\n") ?
"""
/**
* $enumJavadoc
*/
"""
.replace("$enumJavadoc",
enumValue.javaDoc.replaceAll("\n\s*","\n * ")) :
"""
/**$enumJavadoc
*/
/** $enumJavadoc */
"""
.replace("$enumJavadoc", enumValue.javaDoc);
final String deprecatedText = enumValue.deprecated ? "@Deprecated\n" : "";
Expand Down
Loading

0 comments on commit 6621751

Please sign in to comment.