Skip to content

Commit

Permalink
Add class javadoc injection (#617)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoJoDeveloping authored and LexManos committed Oct 15, 2019
1 parent 1b7bd5e commit 09e1d53
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ private JavadocAdder() { /* no constructing */ }
* Converts a raw javadoc string into a nicely formatted, indented, and wrapped string.
* @param indent the indent to be inserted before every line.
* @param javadoc The javadoc string to be processed
* @param isMethod If this javadoc is for a method or a field
* @param multiline If this javadoc is mutlilined (for a field, it isn't) even if there is only one line in the doc
* @return A fully formatted javadoc comment string complete with comment characters and newlines.
*/
public static String buildJavadoc(String indent, String javadoc, boolean isMethod)
public static String buildJavadoc(String indent, String javadoc, boolean multiline)
{
StringBuilder builder = new StringBuilder();

Expand All @@ -51,7 +51,7 @@ public static String buildJavadoc(String indent, String javadoc, boolean isMetho
list.addAll(wrapText(line, 120 - (indent.length() + 3)));
}

if (list.size() > 1 || isMethod)
if (list.size() > 1 || multiline)
{
builder.append(indent);
builder.append("/**");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
Expand All @@ -41,12 +43,16 @@
import de.siegmar.fastcsv.reader.CsvContainer;
import de.siegmar.fastcsv.reader.CsvReader;
import de.siegmar.fastcsv.reader.CsvRow;
import org.apache.commons.lang3.tuple.Pair;

public class McpNames {
private static final String NEWLINE = System.getProperty("line.separator");
private static final Pattern SRG_FINDER = Pattern.compile("func_[0-9]+_[a-zA-Z_]+|field_[0-9]+_[a-zA-Z_]+|p_[\\w]+_\\d+_\\b");
private static final Pattern METHOD_JAVADOC_PATTERN = Pattern.compile("^(?<indent>(?: {3})+|\\t+)(?!return)(?:\\w+\\s+)*(?<generic><[\\w\\W]*>\\s+)?(?<return>\\w+[\\w$.]*(?:<[\\w\\W]*>)?[\\[\\]]*)\\s+(?<name>func_[0-9]+_[a-zA-Z_]+)\\(");
private static final Pattern FIELD_JAVADOC_PATTERN = Pattern.compile("^(?<indent>(?: {3})+|\\t+)(?!return)(?:\\w+\\s+)*(?:\\w+[\\w$.]*(?:<[\\w\\W]*>)?[\\[\\]]*)\\s+(?<name>field_[0-9]+_[a-zA-Z_]+) *(?:=|;)");
private static final Pattern CLASS_JAVADOC_PATTERN = Pattern.compile("^(?<indent>(?: )*|\\t*)([\\w|@]*\\s)*(class|interface|@interface|enum) (?<name>[\\w]+)");
private static final Pattern CLOSING_CURLY_BRACE = Pattern.compile("^(?<indent>(?: )*|\\t*)}");
private static final Pattern PACKAGE_DECL = Pattern.compile("^[\\s]*package(\\s)*(?<name>[\\w|.]+);$");

public static McpNames load(File data) throws IOException {
Map<String, String> names = new HashMap<>();
Expand Down Expand Up @@ -84,9 +90,15 @@ private McpNames(String hash, Map<String, String> names, Map<String, String> doc

public String rename(InputStream stream, boolean javadocs) throws IOException {
List<String> lines = new ArrayList<>();
Deque<Pair<String, Integer>> innerClasses = new LinkedList<>(); //pair of inner class name & indentation
String _package = ""; //default package
for (String line : CharStreams.readLines(new InputStreamReader(stream))) {
Matcher m = PACKAGE_DECL.matcher(line);
if(m.find())
_package = m.group("name") + ".";

if (javadocs)
injectJavadoc(lines, line);
injectJavadoc(lines, line, _package, innerClasses);
lines.add(replaceInLine(line));
}
return Joiner.on(NEWLINE).join(lines);
Expand All @@ -99,13 +111,12 @@ public String rename(String entry) {
/**
* Injects a javadoc into the given list of lines, if the given line is a
* method or field declaration.
*
* @param lines The current file content (to be modified by this method)
* @param line The line that was just read (will not be in the list)
* @param methodFunc A function that takes a method SRG id and returns its javadoc
* @param fieldFunc A function that takes a field SRG id and returns its javadoc
* @param _package the name of the package this file is declared to be in, in com.example format;
* @param innerClasses current position in inner class
*/
private void injectJavadoc(List<String> lines, String line) {
private void injectJavadoc(List<String> lines, String line, String _package, Deque<Pair<String, Integer>> innerClasses) {
// methods
Matcher matcher = METHOD_JAVADOC_PATTERN.matcher(line);
if (matcher.find()) {
Expand All @@ -123,6 +134,36 @@ private void injectJavadoc(List<String> lines, String line) {
String javadoc = docs.get(matcher.group("name"));
if (!Strings.isNullOrEmpty(javadoc))
insertAboveAnnotations(lines, JavadocAdder.buildJavadoc(matcher.group("indent"), javadoc, false));

return;
}

//classes
matcher = CLASS_JAVADOC_PATTERN.matcher(line);
if(matcher.find()) {
//we maintain a stack of the current (inner) class in com.example.ClassName$Inner format (along with indentation)
//if the stack is not empty we are entering a new inner class
String currentClass = (innerClasses.isEmpty() ? _package : innerClasses.peek().getLeft() + "$") + matcher.group("name");
innerClasses.push(Pair.of(currentClass, matcher.group("indent").length()));
String javadoc = docs.get(currentClass);
if (!Strings.isNullOrEmpty(javadoc)) {
insertAboveAnnotations(lines, JavadocAdder.buildJavadoc(matcher.group("indent"), javadoc, true));
}

return;
}

//detect curly braces for inner class stacking/end identification
matcher = CLOSING_CURLY_BRACE.matcher(line);
if(matcher.find()){
if(!innerClasses.isEmpty()) {
int len = matcher.group("indent").length();
if (len == innerClasses.peek().getRight()) {
innerClasses.pop();
} else if (len < innerClasses.peek().getRight()) {
throw new IllegalArgumentException("Failed to properly track class blocks around class " + innerClasses.peek().getLeft() + ":" + (lines.size() + 1));
}
}
}
}

Expand Down

0 comments on commit 09e1d53

Please sign in to comment.