Skip to content

Commit

Permalink
feat(fmt): optimize whitespaces after colon in block map (close #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Jul 6, 2024
1 parent be071a8 commit 38638e8
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 34 deletions.
40 changes: 20 additions & 20 deletions pretty_yaml/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,25 +835,6 @@ where
.next_token()
.filter(|token| token.kind() == SyntaxKind::WHITESPACE)
{
if value.syntax().kind() == SyntaxKind::FLOW_MAP_VALUE {
value_docs.push(Doc::space());
} else if token.text().contains(['\n', '\r'])
|| value
.syntax()
.children()
.find(|child| child.kind() == SyntaxKind::BLOCK)
// for the case that there's no properties
// so the block map/seq comes as first child
.and_then(|block| block.first_child())
.is_some_and(|child| {
matches!(child.kind(), SyntaxKind::BLOCK_MAP | SyntaxKind::BLOCK_SEQ)
})
{
value_docs.push(Doc::hard_line());
has_line_break = true;
} else {
value_docs.push(Doc::space());
}
let last_ws_index = value
.syntax()
.prev_sibling_or_token()
Expand All @@ -863,7 +844,7 @@ where
if let Some(index) = last_ws_index {
let mut has_comment = false;
let mut trivia_docs = format_trivias(
token
colon
.siblings_with_tokens(Direction::Next)
.filter(|token| token.index() != index),
&mut has_comment,
Expand All @@ -875,6 +856,25 @@ where
has_line_break = true;
}
}
if has_line_break {
} else if value.syntax().kind() == SyntaxKind::FLOW_MAP_VALUE {
value_docs.push(Doc::space());
} else if token.text().contains(['\n', '\r'])
|| value
.syntax()
.children()
.find(|child| child.kind() == SyntaxKind::BLOCK)
// for the case that there's no properties
// so the block seq comes as first child
.and_then(|block| block.first_child())
.is_some_and(|child| child.kind() == SyntaxKind::BLOCK_SEQ)
&& !has_question_mark
{
value_docs.push(Doc::hard_line());
has_line_break = true;
} else {
value_docs.push(Doc::space());
}
} else if !has_trivias_before_colon {
docs.push(Doc::space());
}
Expand Down
3 changes: 1 addition & 2 deletions pretty_yaml/tests/fmt/mapping/explicit-key.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ source: pretty_yaml/tests/fmt.rs
key1: value
key2: longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: value
solongitshouldbreakbutitcannot_longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong:
# Comment
solongitshouldbreakbutitcannot_longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: # Comment
foo: bar
? multiline
scalar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
source: pretty_yaml/tests/fmt.rs
---
---
hr:
# 1998 hr ranking
hr: # 1998 hr ranking
- Mark McGwire
- Sammy Sosa
rbi:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ source: pretty_yaml/tests/fmt.rs
? explicit key # Empty value
? |
block key
:
- one # Explicit compact
: - one # Explicit compact
- two # block value
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ source: pretty_yaml/tests/fmt.rs
---
- sun: yellow
- ? earth: blue
:
moon: white
: moon: white
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ complex4:
:
complex5:
? - a
:
- b
: - b
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ c: #lala
d
? # lala
- seq1
:
# lala
: # lala
- #lala
seq2
e: &node # lala
Expand Down
3 changes: 1 addition & 2 deletions pretty_yaml/tests/fmt/spec/various-trailing-comments.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ c: #lala
d
? # lala
- seq1
:
# lala
: # lala
- #lala
seq2
e:
Expand Down

0 comments on commit 38638e8

Please sign in to comment.