implements PLN-0137 (4/7) document-formatting
Advertise full-document formatting only. The formatter reprints the token stream and changes indentation by four spaces where brace or parenthesis nesting changes. Doc text-block interiors stay as written, comments stay on their current line, and declarations are not reordered. Range formatting and on-type formatting stay off.
This commit is contained in:
+53
@@ -0,0 +1,53 @@
|
||||
package p.studio.compiler.pbs;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PbsDocumentFormatterTest {
|
||||
|
||||
@Test
|
||||
void indentsOnlyWhereBraceOrParenNestingChanges() {
|
||||
final String source = """
|
||||
fn length(value: int) -> int {
|
||||
return value;
|
||||
}
|
||||
fn other() -> int {
|
||||
return 1;
|
||||
}
|
||||
""";
|
||||
|
||||
final String formatted = PbsDocumentFormatter.format(source);
|
||||
|
||||
assertEquals("""
|
||||
fn length(value: int) -> int {
|
||||
return value;
|
||||
}
|
||||
fn other() -> int {
|
||||
return 1;
|
||||
}
|
||||
""", formatted);
|
||||
assertTrue(formatted.indexOf("fn length") < formatted.indexOf("fn other"));
|
||||
assertFalse(formatted.contains("\nreturn value;\nreturn 1;"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void leavesDocTextBlockInteriorAndTrailingCommentUntouched() {
|
||||
final String source = """
|
||||
[Doc(markdown = \"\"\"
|
||||
hello
|
||||
\"\"\")]
|
||||
fn length() -> int { // keep
|
||||
return 1;
|
||||
}
|
||||
""";
|
||||
|
||||
final String formatted = PbsDocumentFormatter.format(source);
|
||||
|
||||
assertTrue(formatted.contains(" hello\n"));
|
||||
assertTrue(formatted.contains("fn length() -> int { // keep"));
|
||||
assertTrue(formatted.contains("\n return 1;\n"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user