Skip to content

Commit b35440f

Browse files
#16: Trailing comments formatting issue
Fixed #16
1 parent 4492859 commit b35440f

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

src/main/java/io/protostuff/jetbrains/plugin/formatter/ProtoFileBlock.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import java.util.List;
1616

1717
import static io.protostuff.jetbrains.plugin.formatter.BlockFactory.createBlock;
18+
import static io.protostuff.jetbrains.plugin.formatter.StatementBlock.COMMENT;
19+
import static io.protostuff.jetbrains.plugin.formatter.StatementBlock.LINE_COMMENT;
20+
import static io.protostuff.jetbrains.plugin.formatter.StatementBlock.SPACE_OR_NEW_LINE;
1821

1922
/**
2023
* @author Kostiantyn Shchepanovskyi
@@ -65,6 +68,16 @@ public Spacing getSpacing(@Nullable Block child1, @NotNull Block child2) {
6568
if (child1 == null) {
6669
return StatementBlock.NONE;
6770
}
71+
if (child2 instanceof ASTBlock) {
72+
ASTBlock block = (ASTBlock) child2;
73+
IElementType elementType = block.getNode().getElementType();
74+
75+
// Do not move trailing comments to new line.
76+
if (LINE_COMMENT.equals(elementType)
77+
|| COMMENT.equals(elementType)) {
78+
return SPACE_OR_NEW_LINE;
79+
}
80+
}
6881
return StatementBlock.NEW_LINE;
6982
}
7083

src/test/java/io/protostuff/jetbrains/plugin/formatter/FormatterTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ protected String getTestDataPath() {
2121
return "src/test/resources/formatting";
2222
}
2323

24+
private void run(String test) {
25+
run(test, commonCodeStyleSettings -> {});
26+
}
27+
2428
private void run(String test, Consumer<CommonCodeStyleSettings> settings) {
2529
myFixture.configureByFiles(test + "/Source.proto");
2630
CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(getProject());
@@ -36,7 +40,7 @@ protected void run() throws Throwable {
3640
}
3741

3842
public void testDefaultFormatter() {
39-
run("default", settings -> {});
43+
run("default");
4044
}
4145

4246
public void testTab() {
@@ -53,10 +57,7 @@ public void testCustomIndent() {
5357
});
5458
}
5559

56-
// public void testDisableSpaceAroundAssignmentOperator() {
57-
// run("disable_space_around_assignment_operator", settings -> {
58-
// settings.SPACE_AROUND_ASSIGNMENT_OPERATORS = false;
59-
// });
60-
// }
61-
60+
public void testTrailingComments() {
61+
run("trailing_line_comments");
62+
}
6263
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
syntax = "proto2"; // trailing comments should stay one the same line
2+
3+
message TestMessage {
4+
optional int32 x = 1; // trailing comments should stay one the same line
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
syntax = "proto2";// trailing comments should stay one the same line
2+
3+
message TestMessage {
4+
optional int32 x = 1;// trailing comments should stay one the same line
5+
}

0 commit comments

Comments
 (0)