Skip to content

Commit 6516394

Browse files
author
Copilot
committed
fix: whitespace after the box operator, causing [][Next] to be reformatted to [] [Next]
PrefixExprConstruct.java used z[0].getImage().length() > 1 to decide spacing, but getImage() returns SANY's internal node name (N_GenPrefixOp) — always long — so it always added a space. Changed to Character.isLetter(z[0].getHumanReadableImage().charAt(0)) — uses the actual operator text ([], DOMAIN, etc.) and checks if it starts with a letter (keyword → space, symbol → no space)
1 parent 7adbe44 commit 6516394

File tree

6 files changed

+70
-12
lines changed

6 files changed

+70
-12
lines changed

src/main/java/me/fponzi/tlaplusformatter/constructs/impl/PrefixExprConstruct.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public Doc buildDoc(TreeNode node, ConstructContext context, int indentSize) {
2727
var z = node.zero();
2828
var genInfixOp = context.buildChild(z[0]);
2929
var val = context.buildChild(z[1]);
30-
if (z[0].getImage().length() > 1) {
30+
if (Character.isLetter(z[0].getHumanReadableImage().charAt(0))) {
3131
// e.g., DOMAIN, SUBSET etc.
3232
return genInfixOp.appendSpace(val);
3333
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package me.fponzi.tlaplusformatter.constructs.impl;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static me.fponzi.tlaplusformatter.Utils.assertUnchanged;
6+
7+
public class PrefixExprConstructTest {
8+
9+
@Test
10+
void testBoxOperatorNoSpaceWithActionExpr() {
11+
var s = "----- MODULE Test -----\n" +
12+
"VARIABLE x\n" +
13+
"Next == x' = x\n" +
14+
"Spec == [][Next]_x\n" +
15+
"====";
16+
assertUnchanged(s);
17+
}
18+
19+
@Test
20+
void testBoxOperatorNoSpaceWithIdentifier() {
21+
var s = "----- MODULE Test -----\n" +
22+
"VARIABLE x\n" +
23+
"Inv == x = 0\n" +
24+
"Prop == []Inv\n" +
25+
"====";
26+
assertUnchanged(s);
27+
}
28+
29+
@Test
30+
void testDiamondOperatorNoSpace() {
31+
var s = "----- MODULE Test -----\n" +
32+
"VARIABLE x\n" +
33+
"Inv == x = 0\n" +
34+
"Prop == <>Inv\n" +
35+
"====";
36+
assertUnchanged(s);
37+
}
38+
39+
@Test
40+
void testDomainHasSpace() {
41+
var s = "----- MODULE Test -----\n" +
42+
"VARIABLE f\n" +
43+
"op == DOMAIN f\n" +
44+
"====";
45+
assertUnchanged(s);
46+
}
47+
48+
@Test
49+
void testSubsetHasSpace() {
50+
var s = "----- MODULE Test -----\n" +
51+
"EXTENDS Naturals\n" +
52+
"VARIABLE x\n" +
53+
"op == SUBSET x\n" +
54+
"====";
55+
assertUnchanged(s);
56+
}
57+
58+
}

src/test/resources/outputs/AllConstructs.tla

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Power(U) == SUBSET U
5151
BigUnion(A) == UNION A
5252

5353
CaseExample(u) == CASE u \in Nat -> u + 1
54-
[] u \in Int \ Nat -> - u
54+
[] u \in Int \ Nat -> -u
5555
[] OTHER-> 0
5656

5757
IfElseExample(b, e1, e2) == IF b THEN e1 ELSE e2
@@ -112,7 +112,7 @@ Init == /\ x = 0
112112

113113
IncX == x' = x + 1
114114
DecX == x' = x - 1
115-
ToggleQ == q' = ~ q
115+
ToggleQ == q' = ~q
116116
AssignY == \E n \in 0 .. N: y' = n
117117
UpdateZ == \E e \in S: z' = z \cup { e }
118118
BumpFAny == \E i \in DOMAIN f: f' = [f EXCEPT ![i] = @ + 1]
@@ -130,13 +130,13 @@ Next == \/ ( IncX /\ UNCHANGED << y, z, f, r, s, q >> )
130130
\/ ( AppendS /\ UNCHANGED << x, y, z, f, r, q >> )
131131
\/ Stutter
132132

133-
Spec == Init /\ [] [Next]_vars /\ WF_vars(IncX) /\ SF_vars(AssignY)
133+
Spec == Init /\ [][Next]_vars /\ WF_vars(IncX) /\ SF_vars(AssignY)
134134

135-
Invariance == [] TypeInv
135+
Invariance == []TypeInv
136136
LeadsToExample == ( x < N ) ~> ( x = N )
137137

138138
EnablementExample == /\ ENABLED IncX
139-
/\ ~ ENABLED DecX
139+
/\ ~ENABLED DecX
140140

141141
---- MODULE SimpleImported ----
142142
EXTENDS Naturals, Sequences

src/test/resources/outputs/HourClock.tla

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ EXTENDS Naturals, TLC
55
VARIABLE hr
66
HCini == hr \in ( 1 .. 12 )
77
HCnxt == hr' = IF hr # 12 THEN hr + 1 ELSE 1
8-
HC == HCini /\ [] [HCnxt]_hr
8+
HC == HCini /\ [][HCnxt]_hr
99
--------------------------------------------------------------
10-
THEOREM HC => [] HCini
10+
THEOREM HC => []HCini
1111
==============================================================
1212
This is post text
1313
Has multiple lines in it.

src/test/resources/outputs/Slush.tla

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ Init ==
286286

287287
QueryReplyLoop(self) ==
288288
/\ pc[self] = "QueryReplyLoop"
289-
/\ IF ~ Terminate
289+
/\ IF ~Terminate
290290
THEN /\ pc' = [pc EXCEPT ![self] = "WaitForQueryMessageOrTermination"]
291291
ELSE /\ pc' = [pc EXCEPT ![self] = "Done"]
292292
/\ UNCHANGED << pick, message, sampleSet, loopVariant >>
@@ -424,9 +424,9 @@ Next ==
424424
( \E self \in SlushLoopProcess: SlushLoop(self) ) \/
425425
Terminating
426426

427-
Spec == Init /\ [] [Next]_vars
427+
Spec == Init /\ [][Next]_vars
428428

429-
Termination == <> ( \A self \in ProcSet: pc[self] = "Done" )
429+
Termination == <>( \A self \in ProcSet: pc[self] = "Done" )
430430

431431

432432
\* END TRANSLATION

src/test/resources/outputs/Stones.tla

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Partitions(seq, wt) ==
7575
(* of the sequence seq sum to wt. *)
7676
(***************************************************************************)
7777
Weighs(seq, wt) ==
78-
\E coef \in [1 .. N -> - 1 .. 1]:
78+
\E coef \in [1 .. N -> -1 .. 1]:
7979
SeqSum([i \in 1 .. N |-> coef[i] * seq[i]]) = wt
8080

8181
(***************************************************************************)

0 commit comments

Comments
 (0)