Open nrabulinski opened 8 months ago
I tested it on multiple version and it seems like the bug has been here since forever, but I couldn't find any issue which would reference it
This is another way of accomplishing the end result as in #267.
For others searching for a workaround: I set the comment to the table value instead of the key. Here I'm using the visitor while adding doc strings to the serialized toml.
impl VisitMut for Formatter {
fn visit_table_like_kv_mut(&mut self, mut key: KeyMut<'_>, node: &mut Item) {
if node.is_inline_table() {
let item = std::mem::replace(node, Item::None);
if let Ok(table) = item.into_table() {
*node = Item::Table(table);
}
}
...
if let Ok(doc) = doc {
let mut comment = String::from("\n");
for line in doc.lines() {
let line = format!("# {line}\n");
comment.push_str(&line);
}
match node {
Item::Table(table) => {
// Set the comment to the table instead of key
let decor = table.decor_mut();
decor.set_prefix(comment);
}
_ => {
let decor = key.leaf_decor_mut();
decor.set_prefix(comment);
}
}
}
...
}
}
This produces output
# Comment
[section]
Expected output:
Actual output: