Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ggj][ast] feat: add EmptyLineStatement #375

Merged
merged 4 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public interface AstNodeVisitor {

public void visit(CommentStatement commentStatement);

public void visit(EmptyLineStatement emptyLineStatement);

/** =============================== OTHER =============================== */
public void visit(MethodDefinition methodDefinition);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.generator.engine.ast;

public class EmptyLineStatement implements Statement {
private EmptyLineStatement() {}

@Override
public void accept(AstNodeVisitor visitor) {
visitor.visit(this);
}

public static EmptyLineStatement create() {
return new EmptyLineStatement();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.api.generator.engine.ast.CastExpr;
import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.CommentStatement;
import com.google.api.generator.engine.ast.EmptyLineStatement;
import com.google.api.generator.engine.ast.EnumRefExpr;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
Expand Down Expand Up @@ -316,23 +317,28 @@ public void visit(SynchronizedStatement synchronizedStatement) {

@Override
public void visit(CommentStatement commentStatement) {
// Do nothing
// Nothing to do.
}

@Override
public void visit(EmptyLineStatement emptyLineStatement) {
// Nothing to do.
}

/** =============================== COMMENT =============================== */
@Override
public void visit(LineComment lineComment) {
// Do nothing
// Nothing to do.
}

@Override
public void visit(BlockComment blockComment) {
// Do nothing
// Nothing to do.
}

@Override
public void visit(JavaDocComment javaDocComment) {
// Do nothing
// Nothing to do.
}

/** =============================== OTHER =============================== */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.api.generator.engine.ast.CastExpr;
import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.CommentStatement;
import com.google.api.generator.engine.ast.EmptyLineStatement;
import com.google.api.generator.engine.ast.EnumRefExpr;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
Expand Down Expand Up @@ -622,6 +623,11 @@ public void visit(CommentStatement commentStatement) {
commentStatement.comment().accept(this);
}

@Override
public void visit(EmptyLineStatement emptyLineStatement) {
newline();
}

/** =============================== COMMENT =============================== */
public void visit(LineComment lineComment) {
// Split comments by new line and add `//` to each line.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

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

import static com.google.common.truth.Truth.assertThat;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
Expand All @@ -25,6 +26,7 @@
import com.google.api.generator.engine.ast.CastExpr;
import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.EmptyLineStatement;
import com.google.api.generator.engine.ast.EnumRefExpr;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
Expand Down Expand Up @@ -1020,6 +1022,13 @@ public void writeLogicalOperationExprImports() {
"import com.google.api.generator.engine.ast.UnaryOperationExpr;\n\n");
}

@Test
public void writeEmptyLineStatementImports() {
EmptyLineStatement statement = EmptyLineStatement.create();
statement.accept(writerVisitor);
assertThat(writerVisitor.write()).isEmpty();
}

private static TypeNode createType(Class clazz) {
return TypeNode.withReference(ConcreteReference.withClazz(clazz));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.CommentStatement;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.EmptyLineStatement;
import com.google.api.generator.engine.ast.EnumRefExpr;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
Expand Down Expand Up @@ -2234,6 +2235,13 @@ public void writeAssignmentOperationExpr_xorAssignment() {
assertThat(writerVisitor.write()).isEqualTo("h ^= Objects.hashCode(fixedValue)");
}

@Test
public void writeEmptyLineStatement() {
EmptyLineStatement statement = EmptyLineStatement.create();
statement.accept(writerVisitor);
assertEquals(writerVisitor.write(), "\n");
}

private static String createLines(int numLines) {
return new String(new char[numLines]).replace("\0", "%s");
}
Expand Down