Skip to content

Commit

Permalink
handling nesting in pretty printing algebra
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Oct 9, 2024
1 parent a10bfe2 commit 6266863
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
30 changes: 21 additions & 9 deletions internal/pretty/pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,33 @@ func (d concat) fit(s *state, mode Mode, indentation int) {
}

func (d lines) fit(s *state, mode Mode, indentation int) {
// Lines always fits
}

func (d break_) fit(s *state, mode Mode, indentation int) {
switch mode {
case ModeBroken:

Check warning on line 70 in internal/pretty/pretty.go

View check run for this annotation

Codecov / codecov/patch

internal/pretty/pretty.go#L70

Added line #L70 was not covered by tests
// Exit immediately by emptying the queue
s.queue = NewQueue[queueItem]()

Check warning on line 72 in internal/pretty/pretty.go

View check run for this annotation

Codecov / codecov/patch

internal/pretty/pretty.go#L72

Added line #L72 was not covered by tests

case ModeUnbroken:
s.width -= len(d.Unbroken)

default:
utils.NonExhaustiveMatchPanic[any](mode)

Check warning on line 78 in internal/pretty/pretty.go

View check run for this annotation

Codecov / codecov/patch

internal/pretty/pretty.go#L77-L78

Added lines #L77 - L78 were not covered by tests
}
}

func (d nest) fit(s *state, mode Mode, indentation int) {
s.queue.PushFront(queueItem{
mode: mode,
doc: d.Document,
indentation: indentation + s.opt.nestSize,
})
}

func (d group) fit(s *state, mode Mode, indentation int) {
panic("GROUP")
panic("TODO fits group")

Check warning on line 91 in internal/pretty/pretty.go

View check run for this annotation

Codecov / codecov/patch

internal/pretty/pretty.go#L90-L91

Added lines #L90 - L91 were not covered by tests
}

// Render
Expand Down Expand Up @@ -101,7 +118,9 @@ func (d break_) render(s *state, mode Mode, indentation int) {
switch mode {
case ModeBroken:
s.builder.WriteByte('\n')
// TODO restore indentation
// Restore indentation
padding := strings.Repeat(string(s.opt.indentationSymbol), indentation)
s.builder.WriteString(padding)

case ModeUnbroken:
s.builder.WriteString(d.Unbroken)
Expand All @@ -123,13 +142,6 @@ func (d nest) render(s *state, mode Mode, indentation int) {
}

func (d group) render(s *state, mode Mode, indentation int) {
// const fit = fits(maxWidth - width, nestSize, {
// indentation,
// mode: "unbroken",
// doc,
// tail: null,
// });

stateCopy := state{
opt: s.opt,
width: s.opt.maxWidth - s.width,
Expand Down
30 changes: 30 additions & 0 deletions internal/pretty/pretty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,33 @@ func TestBreakWhenNotEnoughSpace(t *testing.T) {
out := NewPrintBuilder().WithMaxWidth(1).Print(doc)
require.Equal(t, "ab\ncd", out)
}

func TestNestedWhenUnbroken(t *testing.T) {
doc := Concat(
Text("ab"),
Nest(
Concat(
SpaceBreak(),
Text("cd"),
),
),
)

out := NewPrintBuilder().WithMaxWidth(1000000).Print(doc)
require.Equal(t, "ab cd", out)
}

func TestNestedWhenBroken(t *testing.T) {
doc := Concat(
Text("ab"),
Nest(
Concat(
SpaceBreak(),
Text("cd"),
),
),
)

out := NewPrintBuilder().WithMaxWidth(1).Print(doc)
require.Equal(t, "ab\n cd", out)
}

0 comments on commit 6266863

Please sign in to comment.