Skip to content

Commit

Permalink
feat: optimize trailing comments of block collection
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Jul 5, 2024
1 parent 0ac9bc2 commit 0134882
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pretty_yaml/tests/fmt/comment/collection.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ a: # a.trailingComment

d:
- 123
# sequence
# sequence

e:
- 123
Expand Down
2 changes: 1 addition & 1 deletion pretty_yaml/tests/fmt/comment/end-comment.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: pretty_yaml/tests/fmt.rs
---
parent:
one: 1
# two: 2
# two: 2

a:
b:
Expand Down
6 changes: 3 additions & 3 deletions pretty_yaml/tests/fmt/comment/issue-9130.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ source: pretty_yaml/tests/fmt.rs
- foo: 0
bar: 1

# baz: 2
# baz: 2
- quux: 3

- foo: 0
bar: 1

# baz: 2
# baz: 2

# baz: 3
# baz: 3
- quux: 3
6 changes: 3 additions & 3 deletions pretty_yaml/tests/fmt/comment/map-2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ foo1:
foo2:
- foo2

# - foo2
# - foo2

# - foo2
# - foo2
# - foo2
# - foo2
4 changes: 2 additions & 2 deletions pretty_yaml/tests/fmt/comment/map-3.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ foo1:
foo2:
- foo2

# first line
# next line
# first line
# next line
8 changes: 4 additions & 4 deletions pretty_yaml/tests/fmt/comment/map.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ foo2:
- foo: item11
bar: item11

# - foo: item22
# bar: item22
# - foo: item22
# bar: item22

# - foo: item33
# bar: item33
# - foo: item33
# bar: item33
4 changes: 2 additions & 2 deletions pretty_yaml/tests/fmt/comment/object.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ obj:
# before
key: value

# after
# after

# after
# after
2 changes: 1 addition & 1 deletion pretty_yaml/tests/fmt/comment/sequence-2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ source: pretty_yaml/tests/fmt.rs
# before-after.comment

- none:
# none.comment
# none.comment
- before(2 line)

# before.comment
Expand Down
4 changes: 2 additions & 2 deletions pretty_yaml/tests/fmt/comment/sequence.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ source: pretty_yaml/tests/fmt.rs

- - a

# - b
# - b

# - c
# - c
2 changes: 1 addition & 1 deletion pretty_yaml/tests/fmt/root/example.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ omap:
- aardvark: African pig-like ant eater. Ugly.
- anteater: South-American ant eater. Two species.
- anaconda: South-American constrictor snake. Scaly.
# Etc.
# Etc.
# Flow style
Numbers: !!omap [one: 1, two: 2, three: 3]

Expand Down
25 changes: 23 additions & 2 deletions yaml_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,16 +738,21 @@ fn block_sequence(input: &mut Input) -> GreenResult {
(
block_sequence_entry,
repeat(0.., (cmts_or_ws1.verify_indent(), block_sequence_entry)),
block_collection_trailing_trivias(input.state.indent),
),
)
.parse_next(input)
.map(|(first, rest): (_, Vec<_>)| {
.map(|(first, rest, trailing_trivias): (_, Vec<_>, _)| {
let mut children = Vec::with_capacity(1 + rest.len());
children.push(first);
for (mut trivias, entry) in rest {
children.append(&mut trivias);
children.push(entry);
}
trailing_trivias.into_iter().for_each(|(ws, comment)| {
children.push(ws);
children.push(comment);
});
node(BLOCK_SEQ, children)
})
}
Expand Down Expand Up @@ -819,16 +824,21 @@ fn block_map(input: &mut Input) -> GreenResult {
alt((block_map_implicit_entry, block_map_explicit_entry)),
),
),
block_collection_trailing_trivias(indent),
),
)
.parse_next(input)
.map(|(first, rest): (_, Vec<_>)| {
.map(|(first, rest, trailing_trivias): (_, Vec<_>, _)| {
let mut children = Vec::with_capacity(1 + rest.len());
children.push(first);
for (mut trivias, entry) in rest {
children.append(&mut trivias);
children.push(entry);
}
trailing_trivias.into_iter().for_each(|(ws, comment)| {
children.push(ws);
children.push(comment);
});
node(BLOCK_MAP, children)
})
}
Expand Down Expand Up @@ -938,6 +948,17 @@ fn block_map_implicit_key(input: &mut Input) -> GreenResult {
.map(|child| node(BLOCK_MAP_KEY, [child]))
}

fn block_collection_trailing_trivias<'s>(
indent: usize,
) -> impl Parser<Input<'s>, Vec<(GreenElement, GreenElement)>, ContextError> {
// 1. We don't use `verify_indent` or `state.prev_indent` because they were changed after
// the last entry. Comments and whitespaces will be parsed after each entry but they're
// discarded because there're no entries any more, but state is still changed.
// 2. Top-level comments shouldn't be included in block collection.
let verify = verify_state(move |state| state.indent == indent && !state.document_top);
repeat(0.., (terminated(ws, verify), comment))
}

fn block(input: &mut Input) -> GreenResult {
let mut bf_ctx = |input: &mut Input| -> PResult<_> { Ok(input.state.bf_ctx.clone()) };

Expand Down

0 comments on commit 0134882

Please sign in to comment.