Skip to content

Commit 298fbcb

Browse files
authored
Fix: Format for comprehension expressions (#3916)
1 parent bd2d4ec commit 298fbcb

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

Source/DafnyCore/AST/Expressions/ComprehensionExpr.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ public virtual bool SetIndent(int indentBefore, TokenNewIndentCollector formatte
473473
indentBefore = formatter.ReduceBlockiness ? indentBefore : formatter.GetNewTokenVisualIndent(token, indentBefore);
474474
assignOpIndent = formatter.ReduceBlockiness ? indentBefore + formatter.SpaceTab : indentBefore;
475475
formatter.SetOpeningIndentedRegion(token, indentBefore);
476+
476477
break;
477478
}
478479
case ":=":

Source/DafnyCore/AST/Expressions/Expressions.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Numerics;
66
using System.Linq;
77
using System.Diagnostics;
8+
using System.Reflection.Emit;
89
using System.Security.AccessControl;
910
using Microsoft.Boogie;
1011

@@ -2274,7 +2275,11 @@ public bool SetIndent(int indentBefore, TokenNewIndentCollector formatter) {
22742275
formatter.SetIndentations(ownedTokens[0], formatter.binOpIndent, formatter.binOpIndent, formatter.binOpArgIndent);
22752276
} else {
22762277
var startToken = this.StartToken;
2277-
var newIndent = formatter.GetNewTokenVisualIndent(startToken, formatter.GetIndentInlineOrAbove(startToken));
2278+
//"," in a comprehension is an "&&", except that it should not try to keep a visual indentation between components.
2279+
var newIndent =
2280+
ownedTokens[0].val == "," ?
2281+
formatter.GetIndentInlineOrAbove(startToken)
2282+
: formatter.GetNewTokenVisualIndent(startToken, formatter.GetIndentInlineOrAbove(startToken));
22782283
formatter.SetIndentations(ownedTokens[0], newIndent, newIndent, newIndent);
22792284
}
22802285
}
@@ -2339,6 +2344,33 @@ public bool SetIndent(int indentBefore, TokenNewIndentCollector formatter) {
23392344
formatter.Visit(E1, item2Indent);
23402345
formatter.SetIndentations(EndToken, below: indent);
23412346
return false;
2347+
} else if (Op is Opcode.In or Opcode.NotIn) {
2348+
var itemIndent = formatter.GetNewTokenVisualIndent(
2349+
E0.StartToken, indent);
2350+
var item2Indent = itemIndent + formatter.SpaceTab;
2351+
formatter.Visit(E0, itemIndent);
2352+
foreach (var token in this.OwnedTokens) {
2353+
switch (token.val) {
2354+
case "<": {
2355+
if (token.Prev.line != token.line) {
2356+
itemIndent = formatter.GetNewTokenVisualIndent(token, indent);
2357+
}
2358+
2359+
break;
2360+
}
2361+
case "in":
2362+
case "-": {
2363+
if (TokenNewIndentCollector.IsFollowedByNewline(token)) {
2364+
formatter.SetOpeningIndentedRegion(token, itemIndent);
2365+
} else {
2366+
formatter.SetAlign(itemIndent, token, out item2Indent, out _);
2367+
}
2368+
break;
2369+
}
2370+
}
2371+
}
2372+
formatter.Visit(E1, item2Indent);
2373+
return false;
23422374
} else {
23432375
foreach (var token in OwnedTokens) {
23442376
formatter.SetIndentations(token, indent, indent, indent);

Source/DafnyPipeline.Test/FormatterIssues.cs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,83 @@ namespace DafnyPipeline.Test;
44

55
[Collection("Singleton Test Collection - FormatterForTopLevelDeclarations")]
66
public class FormatterIssues : FormatterBaseTest {
7+
[Fact]
8+
public void GitIssue3912FormatterCollectionArrow() {
9+
FormatterWorksFor(@"
10+
const i :=
11+
a in
12+
B + // Previously it was not indented
13+
C +
14+
D &&
15+
b in
16+
B +
17+
C
18+
");
19+
}
20+
21+
[Fact]
22+
public void GitIssue3912FormatterCollectionArrowA() {
23+
FormatterWorksFor(@"
24+
const newline :=
25+
set
26+
i <-
27+
PartA +
28+
PartB +
29+
PartC,
30+
j <-
31+
PartD
32+
::
33+
f(i,j)
34+
35+
const sameline :=
36+
set i <-
37+
PartA +
38+
PartB +
39+
PartC,
40+
j <-
41+
PartD
42+
::
43+
f(i,j)
44+
45+
");
46+
}
47+
48+
[Fact]
49+
public void GitIssue3912FormatterCollectionArrowB() {
50+
FormatterWorksFor(@"
51+
const newlineOp :=
52+
set
53+
i
54+
<-
55+
PartA +
56+
PartB +
57+
PartC,
58+
j
59+
<-
60+
PartD
61+
::
62+
f(i,j)
63+
64+
");
65+
}
66+
67+
[Fact]
68+
public void GitIssue3912FormatterCollectionArrowC() {
69+
FormatterWorksFor(@"
70+
const sameLineOp :=
71+
set i
72+
<-
73+
PartA +
74+
PartB +
75+
PartC,
76+
j
77+
<- PartD +
78+
PartE
79+
::
80+
f(i,j)
81+
");
82+
}
83+
784
[Fact]
885
public void GitIssue3960FormattingIssueForallStatements() {
986
FormatterWorksFor(@"
@@ -50,4 +127,4 @@ public void FormatterWorksForEmptyDocument() {
50127
FormatterWorksFor(@"
51128
", null, true);
52129
}
53-
}
130+
}

docs/dev/news/3912.fix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Format for comprehension expressions

0 commit comments

Comments
 (0)