Skip to content

Commit

Permalink
πŸ› Fix breaking issue with dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
MystPi committed Oct 25, 2024
1 parent 9389cbb commit 5ddd2c8
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

- A bug where dictionaries were not being broken the right way. ([#5](https://github.com/MystPi/pprint/issues/5))

## v1.0.3 - 2024-04-26

### Changed
Expand Down
12 changes: 12 additions & 0 deletions birdie_snapshots/(erlang)_dictionaries_can_be_broken.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
version: 1.1.4
title: (erlang) dictionaries can be broken
file: ./test/pprint_test.gleam
test_name: erlang_list_breaking_test
---
Set(dict.from_list([
#(
"Some item with a lot of text in this set so that a new line is forced",
[],
),
]))
12 changes: 12 additions & 0 deletions birdie_snapshots/(javascript)_dictionaries_can_be_broken.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
version: 1.1.4
title: (javascript) dictionaries can be broken
file: ./test/pprint_test.gleam
test_name: javascript_list_breaking_test
---
Set(dict.from_list([
#(
"Some item with a lot of text in this set so that a new line is forced",
Nil,
),
]))
56 changes: 27 additions & 29 deletions src/pprint.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ fn pretty_list(items: List(Dynamic), config: Config) -> Document {
}

list.map(items, pretty_type(_, config))
|> doc.concat_join([doc.from_string(","), space])
|> wrap(doc.from_string("["), doc.from_string("]"), trailing: ",")
|> comma_list_space(doc.from_string("["), doc.from_string("]"), with: space)
}

fn pretty_dict(d: Dict(decoder.Type, decoder.Type), config: Config) -> Document {
Expand All @@ -213,27 +212,15 @@ fn pretty_dict(d: Dict(decoder.Type, decoder.Type), config: Config) -> Document
})
|> list.map(fn(field) {
// Format the dict's items into tuple literals
[
doc.from_string("#("),
pretty_type(field.0, config),
doc.from_string(", "),
pretty_type(field.1, config),
doc.from_string(")"),
]
|> doc.concat
[pretty_type(field.0, config), pretty_type(field.1, config)]
|> comma_list(doc.from_string("#("), doc.from_string(")"))
})
|> doc.concat_join([doc.from_string(","), doc.space])
|> wrap(
doc.from_string("dict.from_list(["),
doc.from_string("])"),
trailing: ",",
)
|> comma_list(doc.from_string("dict.from_list(["), doc.from_string("])"))
}

fn pretty_tuple(items: List(Dynamic), config: Config) -> Document {
list.map(items, pretty_dynamic(_, config))
|> doc.concat_join([doc.from_string(","), doc.space])
|> wrap(doc.from_string("#("), doc.from_string(")"), trailing: ",")
|> comma_list(doc.from_string("#("), doc.from_string(")"))
}

fn pretty_custom_type(
Expand Down Expand Up @@ -286,10 +273,7 @@ fn pretty_custom_type(
//
[single] -> doc.concat([open, single, close])
// However, multiple fields are indented because they would look weird otherwise.
_ ->
fields
|> doc.concat_join([doc.from_string(","), doc.space])
|> wrap(open, close, trailing: ",")
_ -> fields |> comma_list(open, close)
}
}

Expand Down Expand Up @@ -328,15 +312,29 @@ fn ansi(text: String, code: String, config: Config) -> Document {

// ---- UTILS ------------------------------------------------------------------

fn wrap(
document: Document,
fn comma_list(docs: List(Document), open: Document, close: Document) -> Document {
comma_list_space(docs, open, close, with: doc.space)
}

fn comma_list_space(
docs: List(Document),
open: Document,
close: Document,
trailing trailing: String,
with space: Document,
) -> Document {
document
|> doc.prepend_docs([open, doc.soft_break])
|> doc.nest(by: 2)
|> doc.append_docs([doc.break("", trailing), close])
let trailing = case docs {
[] -> doc.empty
_ -> doc.break("", ",")
}

[
open,
[doc.soft_break, doc.concat_join(docs, [doc.from_string(","), space])]
|> doc.concat
|> doc.nest(by: 2),
trailing,
close,
]
|> doc.concat
|> doc.group
}
22 changes: 22 additions & 0 deletions test/pprint_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import birdie
import gleam/dict
import gleam/dynamic
import gleam/option.{None}
import gleam/set
import gleeunit
import gleeunit/should
import pprint
Expand Down Expand Up @@ -94,3 +95,24 @@ pub fn erlang_labels_test() {
|> pprint.with_config(labels_config)
|> birdie.snap("(erlang) labels are not shown")
}

// https://github.com/MystPi/pprint/issues/5
fn do_dict_breaking_test() {
set.new()
|> set.insert(
"Some item with a lot of text in this set so that a new line is forced",
)
|> pprint.format
}

@target(javascript)
pub fn javascript_list_breaking_test() {
do_dict_breaking_test()
|> birdie.snap("(javascript) dictionaries can be broken")
}

@target(erlang)
pub fn erlang_list_breaking_test() {
do_dict_breaking_test()
|> birdie.snap("(erlang) dictionaries can be broken")
}

0 comments on commit 5ddd2c8

Please sign in to comment.