Skip to content

Commit

Permalink
Allow formatting relative to case statement, fix parsing of variable …
Browse files Browse the repository at this point in the history
…declarations with array dimensions
  • Loading branch information
Jon Schneider committed Feb 18, 2020
1 parent d985bba commit f294ba3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static com.netflix.rewrite.tree.Formatting.EMPTY;
import static com.netflix.rewrite.tree.Formatting.format;
import static com.netflix.rewrite.tree.Formatting.*;
import static com.netflix.rewrite.tree.Tr.AssignOp.Operator.*;
import static com.netflix.rewrite.tree.Type.Primitive.Method;
import static com.netflix.rewrite.tree.TypeUtils.asClass;
Expand Down Expand Up @@ -1166,12 +1165,19 @@ private Tr.VariableDecls visitVariables(List<VariableTree> nodes, Formatting fmt

var namedVarPrefix = sourceBefore(n.getName().toString());
JCVariableDecl vd = (JCVariableDecl) n;

var dimensionsAfterName = dimensions.get();
if(!dimensionsAfterName.isEmpty()) {
formatLastSuffix(dimensionsAfterName, vd.init != null ? sourceBefore("=") : "");
}

var name = Tr.Ident.build(randomId(), n.getName().toString(), type(node),
format("", (vd.init != null) ? sourceBefore("=") : ""));
format("", (dimensionsAfterName.isEmpty() && vd.init != null) ? sourceBefore("=") : ""));

vars.add(
new Tr.VariableDecls.NamedVar(randomId(),
name,
dimensions.get(),
dimensionsAfterName,
convertOrNull(vd.init),
type(n),
i == nodes.size() - 1 ? format(namedVarPrefix) : format(namedVarPrefix, sourceBefore(","))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2787,6 +2787,7 @@ public static class NamedVar extends Tr {
@With
Ident name;

@With
List<Dimension> dimensionsAfterName;

@With
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,16 @@ public Result findIndent(int enclosingIndent, Tree... trees) {
return new Result(enclosingIndent, indentToUse, indentedWithSpaces);
}

public Formatting format(Tr.Block<?> relativeToEnclosing) {
Result indentation = findIndent(relativeToEnclosing.getIndent(), relativeToEnclosing.getStatements().toArray(Tree[]::new));
public Formatting format(Tree relativeToEnclosing) {
Tree[] siblings = new Tree[0];
if(relativeToEnclosing instanceof Tr.Block) {
siblings = ((Tr.Block<?>) relativeToEnclosing).getStatements().toArray(Tree[]::new);
}
else if(relativeToEnclosing instanceof Tr.Case) {
siblings = ((Tr.Case) relativeToEnclosing).getStatements().toArray(Tree[]::new);
}

Result indentation = findIndent(enclosingIndent(relativeToEnclosing), siblings);
return Formatting.format(indentation.getPrefix());
}

Expand Down

0 comments on commit f294ba3

Please sign in to comment.