Skip to content

Commit

Permalink
feat(printer): add guarded pattern matching support
Browse files Browse the repository at this point in the history
  • Loading branch information
clementdessoude committed Nov 26, 2022
1 parent bfdc38e commit a36b2cc
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/prettier-plugin-java/src/printers/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
ParenthesisExpressionCtx,
PatternCtx,
PrimaryCtx,
PrimaryPatternCtx,
PrimaryPrefixCtx,
PrimarySuffixCtx,
PrimitiveCastExpressionCtx,
Expand Down Expand Up @@ -75,6 +76,7 @@ import {
sortAnnotationIdentifier,
sortNodes
} from "./printer-utils";
import join = builders.join;

const { ifBreak, line, softline, indentIfBreak } = builders;

Expand Down Expand Up @@ -759,7 +761,28 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
}

pattern(ctx: PatternCtx) {
return this.visitSingle(ctx);
const primaryPattern = this.visit(ctx.primaryPattern);
if (ctx.AndAnd === undefined) {
return primaryPattern;
}

const binaryExpression = this.visit(ctx.binaryExpression);
return rejectAndConcat([
primaryPattern,
" ",
ctx.AndAnd[0],
line,
binaryExpression
]);
}

primaryPattern(ctx: PrimaryPatternCtx) {
if (ctx.LBrace === undefined) {
return this.visitSingle(ctx);
}

const pattern = this.visit(ctx.pattern);
return putIntoBraces(pattern, softline, ctx.LBrace[0], ctx.RBrace![0]);
}

typePattern(ctx: TypePatternCtx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,24 @@ static String formatter(Object o) {
}
return formatted;
}

void test(Buyer other) {
return switch (other) {
case null -> true;
case Buyer b && this.bestPrice > b.bestPrice -> true;
case Buyer b && this.bestPrice > b.bestPrice -> {
return true;
}
case (Buyer b && this.bestPrice > b.bestPrice) -> true;
case Buyer b && this.bestPrice > b.bestPrice && this.bestPrice > b.bestPrice && this.bestPrice > b.bestPrice && this.bestPrice > b.bestPrice -> true;
case Buyer b && this.bestPrice > b.bestPrice && this.bestPrice > b.bestPrice && this.bestPrice > b.bestPrice && this.bestPrice > b.bestPrice -> {
return true;
}
case (Buyer b && this.bestPrice > b.bestPrice && this.bestPrice > b.bestPrice && this.bestPrice > b.bestPrice && this.bestPrice > b.bestPrice) -> true;
case (Buyer b && this.bestPrice > b.bestPrice && this.bestPrice > b.bestPrice && this.bestPrice > b.bestPrice && this.bestPrice > b.bestPrice) -> {
return true;
}
default -> false;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,44 @@ static String formatter(Object o) {
}
return formatted;
}

void test(Buyer other) {
return switch (other) {
case null -> true;
case Buyer b && this.bestPrice > b.bestPrice -> true;
case Buyer b && this.bestPrice > b.bestPrice -> {
return true;
}
case (Buyer b && this.bestPrice > b.bestPrice) -> true;
case Buyer b &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice -> true;
case Buyer b &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice -> {
return true;
}
case (
Buyer b &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice
) -> true;
case (
Buyer b &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice
) -> {
return true;
}
default -> false;
};
}
}

0 comments on commit a36b2cc

Please sign in to comment.