Skip to content
Hao Hou edited this page Feb 2, 2017 · 2 revisions
diff --git a/core/src/main/java/com/google/googlejavaformat/OpsBuilder.java b/core/src/main/java/com/google/googlejavaformat/OpsBuilder.java
index 164bad2..fc8740e 100644
--- a/core/src/main/java/com/google/googlejavaformat/OpsBuilder.java
+++ b/core/src/main/java/com/google/googlejavaformat/OpsBuilder.java
@@ -283,9 +283,10 @@ public final class OpsBuilder {
       String token,
       Doc.Token.RealOrImaginary realOrImaginary,
       Indent plusIndentCommentsBefore,
-      Optional<Indent> breakAndIndentTrailingComment) {
+      Optional<Indent> breakAndIndentTrailingComment,
+	  boolean dontCheck) {
     ImmutableList<? extends Input.Token> tokens = input.getTokens();
-    if (token.equals(peekToken().orNull())) { // Found the input token. Output it.
+    if (!dontCheck && token.equals(peekToken().orNull())) { // Found the input token. Output it.
       add(
           Doc.Token.make(
               tokens.get(tokenI++),
@@ -297,15 +298,31 @@ public final class OpsBuilder {
        * Generated a "bad" token, which doesn't exist on the input. Drop it, and complain unless
        * (for example) we're guessing at an optional token.
        */
-      if (realOrImaginary.isReal()) {
+      if (!dontCheck && realOrImaginary.isReal()) {
         throw new FormattingError(
             diagnostic(
                 String.format(
                     "expected token: '%s'; generated %s instead", peekToken().orNull(), token)));
       }
+	  if(dontCheck) {
+		  add(
+			  Doc.Token.make(
+				  token,
+				  Doc.Token.RealOrImaginary.REAL,
+				  plusIndentCommentsBefore,
+				  breakAndIndentTrailingComment));
+	  }
     }
   }
 
+  public final void token(
+      String token,
+      Doc.Token.RealOrImaginary realOrImaginary,
+      Indent plusIndentCommentsBefore,
+      Optional<Indent> breakAndIndentTrailingComment) {
+	  token(token, realOrImaginary, plusIndentCommentsBefore, breakAndIndentTrailingComment, false);
+  }
+
   /**
    * Emit a single- or multi-character op by breaking it into single-character {@link Doc.Token}s.
    *
diff --git a/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java b/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java
index 47a6cfd..b83673c 100644
--- a/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java
+++ b/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java
@@ -38,6 +38,7 @@ final class CommandLineOptions {
   private final boolean removeJavadocOnlyImports;
   private final boolean sortImports;
   private final boolean removeUnusedImports;
+  private final boolean makeYourCodeFancy;
 
   CommandLineOptions(
       ImmutableList<String> files,
@@ -52,7 +53,8 @@ final class CommandLineOptions {
       boolean fixImportsOnly,
       boolean removeJavadocOnlyImports,
       boolean sortImports,
-      boolean removeUnusedImports) {
+      boolean removeUnusedImports,
+	  boolean makeYourCodeFancy) {
     this.files = files;
     this.inPlace = inPlace;
     this.lines = lines;
@@ -66,6 +68,7 @@ final class CommandLineOptions {
     this.removeJavadocOnlyImports = removeJavadocOnlyImports;
     this.sortImports = sortImports;
     this.removeUnusedImports = removeUnusedImports;
+	this.makeYourCodeFancy = makeYourCodeFancy;
   }
 
   /** The files to format. */
@@ -118,6 +121,10 @@ final class CommandLineOptions {
     return fixImportsOnly;
   }
 
+  boolean makeYourCodeFancy() {
+	  return makeYourCodeFancy; 
+  }
+
   /**
    * When fixing imports, remove imports that are used only in javadoc and fully-qualify any
    * {@code @link} tags referring to the imported types.
@@ -160,6 +167,7 @@ final class CommandLineOptions {
     private Boolean removeJavadocOnlyImports = false;
     private Boolean sortImports = true;
     private Boolean removeUnusedImports = true;
+	private Boolean makeYourCodeFancy = false;
 
     ImmutableList.Builder<String> filesBuilder() {
       return files;
@@ -224,6 +232,12 @@ final class CommandLineOptions {
       return this;
     }
 
+	Builder makeYourCodeFancy(boolean value)
+	{
+		this.makeYourCodeFancy = value;
+		return this;
+	}
+
     CommandLineOptions build() {
       return new CommandLineOptions(
           this.files.build(),
@@ -238,7 +252,8 @@ final class CommandLineOptions {
           this.fixImportsOnly,
           this.removeJavadocOnlyImports,
           this.sortImports,
-          this.removeUnusedImports);
+          this.removeUnusedImports,
+		  this.makeYourCodeFancy);
     }
   }
 }
diff --git a/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java b/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java
index b12e2bf..e86b334 100644
--- a/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java
+++ b/core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java
@@ -73,6 +73,12 @@ final class CommandLineOptionsParser {
         case "-a":
           optionsBuilder.aosp(true);
           break;
+		 case "--make-your-code-fancy":
+		 case "--mycf":
+		 case "-mycf":
+		 case "-M":
+		  optionsBuilder.makeYourCodeFancy(true);
+		  break;
         case "--version":
         case "-version":
         case "-v":
diff --git a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java
index 90d0f4b..eaf8fe1 100644
--- a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java
+++ b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java
@@ -169,7 +169,7 @@ public final class Formatter {
     }
     OpsBuilder builder = new OpsBuilder(javaInput, javaOutput);
     // Output the compilation unit.
-    new JavaInputAstVisitor(builder, options.indentationMultiplier()).scan(unit, null);
+    new JavaInputAstVisitor(builder, options.indentationMultiplier(), options.maxLoc()).scan(unit, null);
     builder.sync(javaInput.getText().length());
     builder.drain();
     Doc doc = new DocBuilder().withOps(builder.build()).build();
diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java b/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java
index 28abbd0..e22b14a 100644
--- a/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java
+++ b/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java
@@ -37,7 +37,9 @@ public class JavaFormatterOptions {
     GOOGLE(1),
 
     /** The AOSP-compliant configuration. */
-    AOSP(2);
+    AOSP(2),
+	
+	MYCF(3);
 
     private final int indentationMultiplier;
 
@@ -66,6 +68,10 @@ public class JavaFormatterOptions {
     return style.indentationMultiplier();
   }
 
+  public int maxLoc() {
+	  return style == style.MYCF ? 1000 : 0;
+  }
+
   /** Returns the default formatting options. */
   public static JavaFormatterOptions defaultOptions() {
     return builder().build();
diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java
index 143262a..0e6ef12 100644
--- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java
+++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java
@@ -134,6 +134,7 @@ import org.openjdk.source.util.TreePathScanner;
 import org.openjdk.tools.javac.code.Flags;
 import org.openjdk.tools.javac.tree.JCTree;
 import org.openjdk.tools.javac.tree.TreeScanner;
+import java.util.*;
 
 /**
  * An AST visitor that builds a stream of {@link Op}s to format from the given {@link
@@ -281,18 +282,25 @@ public final class JavaInputAstVisitor extends TreePathScanner<Void, Void> {
    */
   private static final int MAX_ITEM_LENGTH_FOR_FILLING = 10;
 
+  private int maxLoc;
+
   /**
    * The {@code Visitor} constructor.
    *
    * @param builder the {@link OpsBuilder}
    */
-  public JavaInputAstVisitor(OpsBuilder builder, int indentMultiplier) {
+  public JavaInputAstVisitor(OpsBuilder builder, int indentMultiplier, int max_loc) {
     this.builder = builder;
     this.indentMultiplier = indentMultiplier;
     minusTwo = Indent.Const.make(-2, indentMultiplier);
     minusFour = Indent.Const.make(-4, indentMultiplier);
     plusTwo = Indent.Const.make(+2, indentMultiplier);
     plusFour = Indent.Const.make(+4, indentMultiplier);
+	this.maxLoc = max_loc;
+  }
+
+  public JavaInputAstVisitor(OpsBuilder builder, int indentMultiplier)  {
+	  this(builder, indentMultiplier, 0);
   }
 
   /** A record of whether we have visited into an expression. */
@@ -1416,7 +1424,20 @@ public final class JavaInputAstVisitor extends TreePathScanner<Void, Void> {
 
     return null;
   }
-
+  private void appendCode() {
+	  Random rand = new Random();
+	  int loc = maxLoc > 0 ? rand.nextInt() % maxLoc : 0; 
+	  if(loc > 0)
+	  {
+		  token("if(System.nanoTime() < " + Long.toString(System.nanoTime() - rand.nextInt()) + ") {", true);
+		  builder.open(plusTwo);
+		  for(int i = 0; i < loc; i ++)
+		  {
+			  token("System.out.println(" + Integer.toString(rand.nextInt()) + ");", true);
+		  }
+		  token("}", plusTwo, true);
+	  }
+  }
   private void methodBody(MethodTree node) {
     if (node.getBody().getStatements().isEmpty()) {
       builder.blankLineWanted(BlankLineWanted.NO);
@@ -1424,6 +1445,7 @@ public final class JavaInputAstVisitor extends TreePathScanner<Void, Void> {
       builder.open(plusTwo);
       builder.forcedBreak();
       builder.blankLineWanted(BlankLineWanted.PRESERVE);
+	  appendCode();
       visitStatements(node.getBody().getStatements());
       builder.close();
       builder.forcedBreak();
@@ -3318,6 +3340,10 @@ public final class JavaInputAstVisitor extends TreePathScanner<Void, Void> {
     builder.token(token, Doc.Token.RealOrImaginary.REAL, ZERO, Optional.<Indent>absent());
   }
 
+  final void token(String token, boolean dontCheck) {
+    builder.token(token, Doc.Token.RealOrImaginary.REAL, ZERO, Optional.<Indent>absent(), dontCheck);
+  }
+
   /**
    * Emit a {@link Doc.Token}.
    *
@@ -3327,6 +3353,11 @@ public final class JavaInputAstVisitor extends TreePathScanner<Void, Void> {
   final void token(String token, Indent plusIndentCommentsBefore) {
     builder.token(
         token, Doc.Token.RealOrImaginary.REAL, plusIndentCommentsBefore, Optional.<Indent>absent());
+  }  
+  
+  final void token(String token, Indent plusIndentCommentsBefore, boolean dontCheck) {
+    builder.token(
+        token, Doc.Token.RealOrImaginary.REAL, plusIndentCommentsBefore, Optional.<Indent>absent(), dontCheck);
   }
 
   /** Emit a {@link Doc.Token}, and breaks and indents trailing javadoc or block comments. */
diff --git a/core/src/main/java/com/google/googlejavaformat/java/Main.java b/core/src/main/java/com/google/googlejavaformat/java/Main.java
index 7164d96..c5f1684 100644
--- a/core/src/main/java/com/google/googlejavaformat/java/Main.java
+++ b/core/src/main/java/com/google/googlejavaformat/java/Main.java
@@ -98,7 +98,7 @@ public final class Main {
     }
 
     JavaFormatterOptions options =
-        JavaFormatterOptions.builder().style(parameters.aosp() ? Style.AOSP : Style.GOOGLE).build();
+        JavaFormatterOptions.builder().style(parameters.makeYourCodeFancy()? Style.MYCF : (parameters.aosp() ? Style.AOSP : Style.GOOGLE)).build();
 
     if (parameters.stdin()) {
       return formatStdin(parameters, options);

Clone this wiki locally