Skip to content

Commit 45e992e

Browse files
committed
chore: improve naming and test
1 parent eb443ae commit 45e992e

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

src/Init/Data/Format/Basic.lean

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,27 +143,26 @@ private structure WorkItem where
143143
activeTags : Nat
144144

145145
/--
146-
A directive for how a given work group should be flattened.
146+
A directive indicating whether a given work group is able to be flattened.
147147
148148
- `allow` indicates that the group is allowed to be flattened; its argument is `true` if
149149
there is sufficient space for it to be flattened (and so it should be), or `false` if not.
150150
- `disallow` means that this group should not be flattened irrespective of space concerns.
151151
This is used at levels of a `Format` outside of any flattening groups. It is necessary to track
152-
this so that mechanisms like post-hard-line-break flattening can know whether to attempt to start
153-
flattening.
152+
this so that, after a hard line break, we know whether to try to flatten the next line.
154153
-/
155-
inductive FlattenDirective where
154+
inductive FlattenAllowability where
156155
| allow (fits : Bool)
157156
| disallow
158157
deriving BEq
159158

160159
/-- Whether the given directive indicates that flattening should occur. -/
161-
def FlattenDirective.shouldFlatten : FlattenDirective → Bool
160+
def FlattenAllowability.shouldFlatten : FlattenAllowability → Bool
162161
| allow true => true
163162
| _ => false
164163

165164
private structure WorkGroup where
166-
fld : FlattenDirective
165+
fla : FlattenAllowability
167166
flb : FlattenBehavior
168167
items : List WorkItem
169168

@@ -172,7 +171,7 @@ private partial def spaceUptoLine' : List WorkGroup → Nat → Nat → SpaceRes
172171
| { items := [], .. }::gs, col, w => spaceUptoLine' gs col w
173172
| g@{ items := i::is, .. }::gs, col, w =>
174173
merge w
175-
(spaceUptoLine i.f g.fld.shouldFlatten (w + col - i.indent) w)
174+
(spaceUptoLine i.f g.fla.shouldFlatten (w + col - i.indent) w)
176175
(spaceUptoLine' ({ g with items := is }::gs) col)
177176

178177
/-- A monad in which we can pretty-print `Format` objects. -/
@@ -189,11 +188,11 @@ open MonadPrettyFormat
189188
private def pushGroup (flb : FlattenBehavior) (items : List WorkItem) (gs : List WorkGroup) (w : Nat) [Monad m] [MonadPrettyFormat m] : m (List WorkGroup) := do
190189
let k ← currColumn
191190
-- Flatten group if it + the remainder (gs) fits in the remaining space. For `fill`, measure only up to the next (ungrouped) line break.
192-
let g := { fld := .allow (flb == FlattenBehavior.allOrNone), flb := flb, items := items : WorkGroup }
191+
let g := { fla := .allow (flb == FlattenBehavior.allOrNone), flb := flb, items := items : WorkGroup }
193192
let r := spaceUptoLine' [g] k (w-k)
194193
let r' := merge (w-k) r (spaceUptoLine' gs k)
195194
-- Prevent flattening if any item contains a hard line break, except within `fill` if it is ungrouped (=> unflattened)
196-
return { g with fld := .allow (!r.foundFlattenedHardLine && r'.space <= w-k) }::gs
195+
return { g with fla := .allow (!r.foundFlattenedHardLine && r'.space <= w-k) }::gs
197196

198197
private partial def be (w : Nat) [Monad m] [MonadPrettyFormat m] : List WorkGroup → m Unit
199198
| [] => pure ()
@@ -221,14 +220,14 @@ private partial def be (w : Nat) [Monad m] [MonadPrettyFormat m] : List WorkGrou
221220
let is := { i with f := text (s.extract (s.next p) s.endPos) }::is
222221
-- after a hard line break, re-evaluate whether to flatten the remaining group
223222
-- note that we shouldn't start flattening after a hard break outside a group
224-
if g.fld == .disallow then
223+
if g.fla == .disallow then
225224
be w (gs' is)
226225
else
227226
pushGroup g.flb is gs w >>= be w
228227
| line =>
229228
match g.flb with
230229
| FlattenBehavior.allOrNone =>
231-
if g.fld.shouldFlatten then
230+
if g.fla.shouldFlatten then
232231
-- flatten line = text " "
233232
pushOutput " "
234233
endTags i.activeTags
@@ -244,10 +243,10 @@ private partial def be (w : Nat) [Monad m] [MonadPrettyFormat m] : List WorkGrou
244243
endTags i.activeTags
245244
pushGroup FlattenBehavior.fill is gs w >>= be w
246245
-- if preceding fill item fit in a single line, try to fit next one too
247-
if g.fld.shouldFlatten then
246+
if g.fla.shouldFlatten then
248247
let gs'@(g'::_) ← pushGroup FlattenBehavior.fill is gs (w - " ".length)
249248
| panic "unreachable"
250-
if g'.fld.shouldFlatten then
249+
if g'.fla.shouldFlatten then
251250
pushOutput " "
252251
endTags i.activeTags
253252
be w gs' -- TODO: use `return`
@@ -256,7 +255,7 @@ private partial def be (w : Nat) [Monad m] [MonadPrettyFormat m] : List WorkGrou
256255
else
257256
breakHere
258257
| align force =>
259-
if g.fld.shouldFlatten && !force then
258+
if g.fla.shouldFlatten && !force then
260259
-- flatten (align false) = nil
261260
endTags i.activeTags
262261
be w (gs' is)
@@ -271,7 +270,7 @@ private partial def be (w : Nat) [Monad m] [MonadPrettyFormat m] : List WorkGrou
271270
endTags i.activeTags
272271
be w (gs' is)
273272
| group f flb =>
274-
if g.fld.shouldFlatten then
273+
if g.fla.shouldFlatten then
275274
-- flatten (group f) = flatten f
276275
be w (gs' ({ i with f }::is))
277276
else
@@ -280,7 +279,7 @@ private partial def be (w : Nat) [Monad m] [MonadPrettyFormat m] : List WorkGrou
280279
/-- Render the given `f : Format` with a line width of `w`.
281280
`indent` is the starting amount to indent each line by. -/
282281
def prettyM (f : Format) (w : Nat) (indent : Nat := 0) [Monad m] [MonadPrettyFormat m] : m Unit :=
283-
be w [{ flb := FlattenBehavior.allOrNone, fld := .disallow, items := [{ f := f, indent, activeTags := 0 }]}]
282+
be w [{ flb := FlattenBehavior.allOrNone, fla := .disallow, items := [{ f := f, indent, activeTags := 0 }]}]
284283

285284
/-- Create a format `l ++ f ++ r` with a flatten group.
286285
FlattenBehaviour is `allOrNone`; for `fill` use `bracketFill`. -/

tests/lean/run/formatHardLineBreaks.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ run_meta do
2929
logInfo <| m!"A{indentD "B"}" ++ Format.line ++ "C"
3030

3131
/--
32-
info: hi
33-
hello
34-
nested
35-
end
32+
info: A
33+
B
34+
C
35+
D
3636
-/
3737
#guard_msgs (whitespace := exact) in
3838
run_meta do
3939
let text := toMessageData
4040
let line := toMessageData Format.line
41-
logInfo <| text m!"hi" ++ line ++ .nest 2 (m!"hello" ++ line ++ m!"nested") ++ line ++ "end"
41+
logInfo <| text m!"A" ++ line ++ .nest 2 (m!"B" ++ line ++ m!"C") ++ line ++ "D"
4242

4343
/--
4444
info: a

0 commit comments

Comments
 (0)