Skip to content

Commit

Permalink
GH-158 - Improve pretty printing of subqueries.
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Simons <[email protected]>
  • Loading branch information
Andy2003 and michael-simons authored Mar 5, 2021
1 parent af2bda4 commit 0250694
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import java.util.Optional;
import java.util.function.BiConsumer;

import org.neo4j.cypherdsl.core.CompoundCondition;
import org.neo4j.cypherdsl.core.Create;
import org.neo4j.cypherdsl.core.ExistentialSubquery;
import org.neo4j.cypherdsl.core.KeyValueMapEntry;
import org.neo4j.cypherdsl.core.MapExpression;
import org.neo4j.cypherdsl.core.Match;
Expand All @@ -48,7 +50,7 @@ class PrettyPrintingVisitor extends DefaultVisitor {
* In contrast to the current level in the {@link DefaultVisitor} that contains the level of elements in the tree.
*/
private int indentationLevel;
private int readingOrUpdatingClausesCount;
private boolean passedFirstReadingOrUpdatingClause;

PrettyPrintingVisitor(IndentStyle indentStyle, int indentSize) {

Expand All @@ -74,7 +76,9 @@ private void indent(int width) {
@Override
void enter(Where where) {
if (currentVisitedElements.stream().noneMatch(visitable -> visitable instanceof Return)) {
builder.append("\nWHERE ");
builder.append("\n");
indent(indentationLevel);
builder.append("WHERE ");
} else {
super.enter(where);
}
Expand All @@ -88,46 +92,42 @@ void enter(Return returning) {

@Override
void enter(With with) {
trimNewline();
if (passedFirstReadingOrUpdatingClause) {
trimNewline();
indent(indentationLevel);
}
passedFirstReadingOrUpdatingClause = true;
super.enter(with);
}

@Override
void enter(Set set) {
if (currentVisitedElements.stream().noneMatch(visitable -> visitable instanceof MergeAction)) {
trimNewline();
indent(indentationLevel);
}
super.enter(set);
}

@Override
void enter(Match match) {
if (readingOrUpdatingClausesCount > 0) {
if (passedFirstReadingOrUpdatingClause) {
trimNewline();
indent(indentationLevel);
}
passedFirstReadingOrUpdatingClause = true;
super.enter(match);
}

@Override
void leave(Match match) {
readingOrUpdatingClausesCount++;
super.leave(match);
}

@Override
void enter(Create create) {
if (readingOrUpdatingClausesCount > 0) {
if (passedFirstReadingOrUpdatingClause) {
trimNewline();
}
passedFirstReadingOrUpdatingClause = true;
super.enter(create);
}

@Override
void leave(Create create) {
readingOrUpdatingClausesCount++;
super.leave(create);
}

@Override
void enter(PropertyLookup propertyLookup) {
if (currentVisitedElements.stream().skip(1).limit(1)
Expand All @@ -147,6 +147,16 @@ void enter(KeyValueMapEntry map) {
super.enter(map);
}

void enter(CompoundCondition operator) {
indentationLevel++;
super.enter(operator);
}

void leave(CompoundCondition operator) {
indentationLevel--;
super.leave(operator);
}

@Override
void enter(Operator operator) {
Operator.Type type = operator.getType();
Expand All @@ -158,7 +168,7 @@ void enter(Operator operator) {
}
if (operator == Operator.OR || operator == Operator.AND || operator == Operator.XOR) {
trimNewline();
indent(1);
indent(indentationLevel);
}
builder.append(operator.getRepresentation());
if (type != Operator.Type.POSTFIX && operator != Operator.EXPONENTIATION) {
Expand Down Expand Up @@ -190,16 +200,26 @@ void enter(MergeAction onCreateOrMatchEvent) {

@Override
void enter(Merge merge) {
if (readingOrUpdatingClausesCount > 0) {
if (passedFirstReadingOrUpdatingClause) {
trimNewline();
indent(indentationLevel);
}
passedFirstReadingOrUpdatingClause = true;
super.enter(merge);
}

@Override
void leave(Merge merge) {
readingOrUpdatingClausesCount++;
super.leave(merge);
void enter(ExistentialSubquery subquery) {
super.enter(subquery);
indentationLevel++;
}

@Override
void leave(ExistentialSubquery subquery) {
indentationLevel--;
trimNewline();
indent(indentationLevel);
builder.append("}");
}

private void trimNewline() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3922,6 +3922,11 @@ class PrettyPrinting {
this.statement = Cypher.match(userNode)
.where(userNode.property("name").isEqualTo(Cypher.literalOf("Max")))
.and(userNode.property("lastName").isEqualTo(Cypher.literalOf("Mustermann")))
.and(Cypher
.match(userNode.relationshipTo(bikeNode, "LIKES"))
.where(bikeNode.relationshipTo(Cypher.anyNode(), "LINK"))
.or(Cypher.match(bikeNode.relationshipTo(Cypher.anyNode(), "LINK")).asCondition())
.asCondition())
.set(userNode.property("lastName").to(Cypher.parameter("newName")))
.with(userNode)
.match(bikeNode)
Expand Down Expand Up @@ -3951,7 +3956,14 @@ void prettyPrintingWithDefaultSettingShouldWork() {
.isEqualTo(
"MATCH (u:User)\n" +
"WHERE (u.name = 'Max'\n" +
" AND u.lastName = 'Mustermann')\n" +
" AND u.lastName = 'Mustermann'\n" +
" AND EXISTS {\n" +
" MATCH (u)-[:LIKES]->(b:Bike)\n" +
" WHERE ((b)-[:LINK]->()\n" +
" OR EXISTS {\n" +
" MATCH (b)-[:LINK]->()\n" +
" })\n" +
" })\n" +
"SET u.lastName = $newName\n" +
"WITH u\n" +
"MATCH (b:Bike)\n" +
Expand Down Expand Up @@ -3980,7 +3992,14 @@ void prettyPrintingWithTabsShouldWork() {
.isEqualTo(
"MATCH (u:User)\n" +
"WHERE (u.name = 'Max'\n" +
"\tAND u.lastName = 'Mustermann')\n" +
"\tAND u.lastName = 'Mustermann'\n" +
"\tAND EXISTS {\n" +
"\t\tMATCH (u)-[:LIKES]->(b:Bike)\n" +
"\t\tWHERE ((b)-[:LINK]->()\n" +
"\t\t\tOR EXISTS {\n" +
"\t\t\t\tMATCH (b)-[:LINK]->()\n" +
"\t\t\t})\n" +
"\t})\n" +
"SET u.lastName = $newName\n" +
"WITH u\n" +
"MATCH (b:Bike)\n" +
Expand Down Expand Up @@ -4027,5 +4046,24 @@ void onClauses() {
"RETURN a.prop"
);
}

@Test
void singleExistsSubquery() {
Node a = Cypher.node("A").named("a");
Node b = Cypher.node("B").named("b");

Statement mergeStatement = Cypher.match(a)
.where(Cypher.match(a.relationshipTo(b)).asCondition())
.returning(a).build();

assertThat(Renderer.getRenderer(Configuration.prettyPrinting()).render(mergeStatement))
.isEqualTo(
"MATCH (a:A)\n" +
"WHERE EXISTS {\n" +
" MATCH (a)-->(b:B)\n" +
"}\n" +
"RETURN a"
);
}
}
}

0 comments on commit 0250694

Please sign in to comment.