Skip to content

Commit

Permalink
Fix formatting of module trees
Browse files Browse the repository at this point in the history
after https://bugs.openjdk.java.net/browse/JDK-8255464

PiperOrigin-RevId: 378545281
  • Loading branch information
cushon authored and google-java-format Team committed Jun 10, 2021
1 parent dc55a82 commit 26adad0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,14 @@ public Void visitCompilationUnit(CompilationUnitTree node, Void unused) {
first = false;
dropEmptyDeclarations();
}
handleModule(first, node);
// set a partial format marker at EOF to make sure we can format the entire file
markForPartialFormat();
return null;
}

protected void handleModule(boolean first, CompilationUnitTree node) {}

/** Skips over extra semi-colons at the top-level, or in a class member declaration lists. */
protected void dropEmptyDeclarations() {
if (builder.peekToken().equals(Optional.of(";"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
import com.google.common.collect.ImmutableList;
import com.google.googlejavaformat.Op;
import com.google.googlejavaformat.OpsBuilder;
import com.google.googlejavaformat.OpsBuilder.BlankLineWanted;
import com.google.googlejavaformat.java.JavaInputAstVisitor;
import com.sun.source.tree.BindingPatternTree;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.InstanceOfTree;
import com.sun.source.tree.ModifiersTree;
import com.sun.source.tree.ModuleTree;
import com.sun.source.tree.SwitchExpressionTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
Expand All @@ -50,6 +53,24 @@ public Java14InputAstVisitor(OpsBuilder builder, int indentMultiplier) {
super(builder, indentMultiplier);
}

@Override
protected void handleModule(boolean first, CompilationUnitTree node) {
try {
ModuleTree module =
(ModuleTree) CompilationUnitTree.class.getMethod("getModule").invoke(node);
if (module != null) {
if (!first) {
builder.blankLineWanted(BlankLineWanted.YES);
}
markForPartialFormat();
visitModule(module, null);
builder.forcedBreak();
}
} catch (ReflectiveOperationException e) {
// Java < 17, see https://bugs.openjdk.java.net/browse/JDK-8255464
}
}

@Override
public Void visitBindingPattern(BindingPatternTree node, Void unused) {
sync(node);
Expand Down

0 comments on commit 26adad0

Please sign in to comment.