From c6921030f497a7955f4a6145b8b013e6870567ff Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 14 Jul 2018 10:02:56 +1000 Subject: Format via embedded eclipse formatter & add determiner --- pom.xml | 37 ++++++++++++++++++++ .../jetbrains/java/decompiler/main/Fernflower.java | 2 +- src/org/spigotmc/fernflower/EclipseFormatter.java | 40 ++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/org/spigotmc/fernflower/EclipseFormatter.java diff --git a/pom.xml b/pom.xml index 9c9b723..340282c 100644 --- a/pom.xml +++ b/pom.xml @@ -12,10 +12,31 @@ UTF-8 + + + org.spigotmc + eclipse-formatter + 3.14.0-spigotmc-SNAPSHOT + + + ./src + + net.md-5 + determiner + 0.1 + + + process-classes + + transform + + + + org.apache.maven.plugins maven-compiler-plugin @@ -38,6 +59,22 @@ + + org.apache.maven.plugins + maven-shade-plugin + 3.1.1 + + + package + + shade + + + + + true + + \ No newline at end of file diff --git a/src/org/jetbrains/java/decompiler/main/Fernflower.java b/src/org/jetbrains/java/decompiler/main/Fernflower.java index 15baac5..85d9b00 100644 --- a/src/org/jetbrains/java/decompiler/main/Fernflower.java +++ b/src/org/jetbrains/java/decompiler/main/Fernflower.java @@ -85,7 +85,7 @@ public class Fernflower implements IDecompiledData { TextBuffer buffer = new TextBuffer(ClassesProcessor.AVERAGE_CLASS_SIZE); buffer.append(DecompilerContext.getProperty(IFernflowerPreferences.BANNER).toString()); classesProcessor.writeClass(cl, buffer); - return buffer.toString(); + return org.spigotmc.fernflower.EclipseFormatter.format(buffer.toString()); // Spigot } catch (Throwable ex) { DecompilerContext.getLogger().writeMessage("Class " + cl.qualifiedName + " couldn't be fully decompiled.", ex); diff --git a/src/org/spigotmc/fernflower/EclipseFormatter.java b/src/org/spigotmc/fernflower/EclipseFormatter.java new file mode 100644 index 0000000..ecd794c --- /dev/null +++ b/src/org/spigotmc/fernflower/EclipseFormatter.java @@ -0,0 +1,40 @@ +package org.spigotmc.fernflower; + +import org.eclipse.jdt.core.formatter.CodeFormatter; +import org.eclipse.jdt.internal.core.util.SimpleDocument; +import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; +import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.text.edits.TextEdit; + +public class EclipseFormatter { + + private static final CodeFormatter formatter; + + static { + DefaultCodeFormatterOptions options = new DefaultCodeFormatterOptions(null); + options.setJavaConventionsSettings(); + + options.tab_char = DefaultCodeFormatterOptions.SPACE; + options.page_width = 0; + + options.insert_new_line_after_label = true; + options.insert_new_line_in_empty_method_body = false; + options.insert_new_line_in_empty_type_declaration = false; + + options.insert_space_before_closing_brace_in_array_initializer = false; // Compatability + // options.blank_lines_before_first_class_body_declaration = 1; // Needed later + + formatter = new DefaultCodeFormatter(options); + } + + public static String format(String contents) throws BadLocationException { + TextEdit formatted = formatter.format(CodeFormatter.K_COMPILATION_UNIT, contents, 0, contents.length(), 0, "\n"); + + IDocument doc = new SimpleDocument(contents); + formatted.apply(doc); + + return doc.get(); + } +} -- cgit v1.2.3