Skip to content

Commit 3cea014

Browse files
authored
[ggj][ast] feat: add EmptyLineStatement (#375)
* fix: refactor requestBuilder into separate method in ServiceClientClassComposer * feat: add EmptyLineStatement
1 parent c789364 commit 3cea014

6 files changed

Lines changed: 63 additions & 4 deletions

File tree

src/main/java/com/google/api/generator/engine/ast/AstNodeVisitor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public interface AstNodeVisitor {
8787

8888
public void visit(CommentStatement commentStatement);
8989

90+
public void visit(EmptyLineStatement emptyLineStatement);
91+
9092
/** =============================== OTHER =============================== */
9193
public void visit(MethodDefinition methodDefinition);
9294

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.api.generator.engine.ast;
16+
17+
public class EmptyLineStatement implements Statement {
18+
private EmptyLineStatement() {}
19+
20+
@Override
21+
public void accept(AstNodeVisitor visitor) {
22+
visitor.visit(this);
23+
}
24+
25+
public static EmptyLineStatement create() {
26+
return new EmptyLineStatement();
27+
}
28+
}

src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.api.generator.engine.ast.CastExpr;
2626
import com.google.api.generator.engine.ast.ClassDefinition;
2727
import com.google.api.generator.engine.ast.CommentStatement;
28+
import com.google.api.generator.engine.ast.EmptyLineStatement;
2829
import com.google.api.generator.engine.ast.EnumRefExpr;
2930
import com.google.api.generator.engine.ast.Expr;
3031
import com.google.api.generator.engine.ast.ExprStatement;
@@ -316,23 +317,28 @@ public void visit(SynchronizedStatement synchronizedStatement) {
316317

317318
@Override
318319
public void visit(CommentStatement commentStatement) {
319-
// Do nothing
320+
// Nothing to do.
321+
}
322+
323+
@Override
324+
public void visit(EmptyLineStatement emptyLineStatement) {
325+
// Nothing to do.
320326
}
321327

322328
/** =============================== COMMENT =============================== */
323329
@Override
324330
public void visit(LineComment lineComment) {
325-
// Do nothing
331+
// Nothing to do.
326332
}
327333

328334
@Override
329335
public void visit(BlockComment blockComment) {
330-
// Do nothing
336+
// Nothing to do.
331337
}
332338

333339
@Override
334340
public void visit(JavaDocComment javaDocComment) {
335-
// Do nothing
341+
// Nothing to do.
336342
}
337343

338344
/** =============================== OTHER =============================== */

src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.api.generator.engine.ast.CastExpr;
2626
import com.google.api.generator.engine.ast.ClassDefinition;
2727
import com.google.api.generator.engine.ast.CommentStatement;
28+
import com.google.api.generator.engine.ast.EmptyLineStatement;
2829
import com.google.api.generator.engine.ast.EnumRefExpr;
2930
import com.google.api.generator.engine.ast.Expr;
3031
import com.google.api.generator.engine.ast.ExprStatement;
@@ -622,6 +623,11 @@ public void visit(CommentStatement commentStatement) {
622623
commentStatement.comment().accept(this);
623624
}
624625

626+
@Override
627+
public void visit(EmptyLineStatement emptyLineStatement) {
628+
newline();
629+
}
630+
625631
/** =============================== COMMENT =============================== */
626632
public void visit(LineComment lineComment) {
627633
// Split comments by new line and add `//` to each line.

src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.api.generator.engine.writer;
1616

17+
import static com.google.common.truth.Truth.assertThat;
1718
import static junit.framework.Assert.assertEquals;
1819
import static junit.framework.Assert.assertFalse;
1920
import static junit.framework.Assert.assertTrue;
@@ -25,6 +26,7 @@
2526
import com.google.api.generator.engine.ast.CastExpr;
2627
import com.google.api.generator.engine.ast.ClassDefinition;
2728
import com.google.api.generator.engine.ast.ConcreteReference;
29+
import com.google.api.generator.engine.ast.EmptyLineStatement;
2830
import com.google.api.generator.engine.ast.EnumRefExpr;
2931
import com.google.api.generator.engine.ast.Expr;
3032
import com.google.api.generator.engine.ast.ExprStatement;
@@ -1020,6 +1022,13 @@ public void writeLogicalOperationExprImports() {
10201022
"import com.google.api.generator.engine.ast.UnaryOperationExpr;\n\n");
10211023
}
10221024

1025+
@Test
1026+
public void writeEmptyLineStatementImports() {
1027+
EmptyLineStatement statement = EmptyLineStatement.create();
1028+
statement.accept(writerVisitor);
1029+
assertThat(writerVisitor.write()).isEmpty();
1030+
}
1031+
10231032
private static TypeNode createType(Class clazz) {
10241033
return TypeNode.withReference(ConcreteReference.withClazz(clazz));
10251034
}

src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.api.generator.engine.ast.ClassDefinition;
2929
import com.google.api.generator.engine.ast.CommentStatement;
3030
import com.google.api.generator.engine.ast.ConcreteReference;
31+
import com.google.api.generator.engine.ast.EmptyLineStatement;
3132
import com.google.api.generator.engine.ast.EnumRefExpr;
3233
import com.google.api.generator.engine.ast.Expr;
3334
import com.google.api.generator.engine.ast.ExprStatement;
@@ -2234,6 +2235,13 @@ public void writeAssignmentOperationExpr_xorAssignment() {
22342235
assertThat(writerVisitor.write()).isEqualTo("h ^= Objects.hashCode(fixedValue)");
22352236
}
22362237

2238+
@Test
2239+
public void writeEmptyLineStatement() {
2240+
EmptyLineStatement statement = EmptyLineStatement.create();
2241+
statement.accept(writerVisitor);
2242+
assertEquals(writerVisitor.write(), "\n");
2243+
}
2244+
22372245
private static String createLines(int numLines) {
22382246
return new String(new char[numLines]).replace("\0", "%s");
22392247
}

0 commit comments

Comments
 (0)