Skip to content

Commit

Permalink
fix bug && add test
Browse files Browse the repository at this point in the history
  • Loading branch information
alingse committed Dec 9, 2024
1 parent 5b36417 commit 3c36819
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/src/main/java/thriftlabs/thriftfmt/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Option {
private boolean alignByField;

public Option() {
this(DEFAULT_INDENT, true, true, true, true, false);
this(DEFAULT_INDENT, true, true, true, false, false);
}

public Option(int indent, boolean patchRequired, boolean patchSeparator, boolean keepComment, boolean alignByAssign,
Expand Down
9 changes: 5 additions & 4 deletions lib/src/main/java/thriftlabs/thriftfmt/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,16 @@ public static void walkNode(ParseTree root, Consumer<ParseTree> callback) {
stack.add(root);

while (!stack.isEmpty()) {
ParseTree node = stack.removeFirst(); // 移除并获取第一个元素
ParseTree node = stack.removeFirst();
if (node == null) {
break;
}

callback.accept(root); // 调用回调函数
List<ParseTree> children = getNodeChildren(node); // 获取子节点
callback.accept(node);

List<ParseTree> children = getNodeChildren(node);
for (ParseTree child : children) {
stack.add(child); // 添加子节点到栈中
stack.add(child);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

import java.io.IOException;
Expand Down Expand Up @@ -69,4 +70,25 @@ public void testFormatterFile(String fileName) {
var formatter2 = new ThriftFormatter(result, option);
formatter2.format();
}

@Test
public void testWithComplexThrift() {
String origin = "struct A {\n" +
" 1: i64 n,\n" +
" 2: string text\n" +
" 3: boolean flag = true\n" +
"}";
var result = Thrift.parse(origin);
assertTrue(result.isSuccess());
var opt = new Option();
var formatter = new ThriftFormatter(result, opt);
var newContent = formatter.format();
var expect = "struct A {\n" +
" 1: required i64 n,\n" +
" 2: required string text,\n" +
" 3: required boolean flag = true,\n" +
"}";
assertEquals(newContent, expect);
}

}

0 comments on commit 3c36819

Please sign in to comment.