Skip to content
This repository was archived by the owner on Oct 9, 2026. It is now read-only.

Commit 214b52c

Browse files
committed
Keep a list whose elements carry comments
1 parent 2e9e0af commit 214b52c

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎src/config/prune.rs‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,32 @@ fn has_comment(table: &dyn TableLike, key: &str, item: &Item) -> bool {
111111
.key(key)
112112
.is_some_and(|key| commented(key.leaf_decor()));
113113
let item = match item {
114-
Item::Value(value) => commented(value.decor()),
114+
Item::Value(value) => value_has_comment(value),
115115
Item::Table(table) => commented(table.decor()),
116116
_ => false,
117117
};
118118
key || item
119119
}
120120

121+
/// A comment on the value or anywhere inside it: between an array's
122+
/// elements, after its last one, or on an inline table's entries.
123+
fn value_has_comment(value: &toml_edit::Value) -> bool {
124+
let note = |raw: &toml_edit::RawString| raw.as_str().is_some_and(|text| text.contains('#'));
125+
commented(value.decor())
126+
|| match value {
127+
toml_edit::Value::Array(array) => {
128+
note(array.trailing()) || array.iter().any(value_has_comment)
129+
}
130+
toml_edit::Value::InlineTable(table) => table.iter().any(|(key, value)| {
131+
table
132+
.key(key)
133+
.is_some_and(|key| commented(key.leaf_decor()))
134+
|| value_has_comment(value)
135+
}),
136+
_ => false,
137+
}
138+
}
139+
121140
fn commented(decor: &toml_edit::Decor) -> bool {
122141
[decor.prefix(), decor.suffix()]
123142
.into_iter()

‎src/config/prune/tests.rs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,15 @@ fn generate(rng: &mut Rng, settings: &[(String, Value)]) -> (DocumentMut, Vec<St
234234
table = table[segment].as_table_mut().unwrap();
235235
}
236236
let leaf = path[path.len() - 1];
237-
table.insert(leaf, toml_edit::value(edit_value(&value)));
237+
let mut value = edit_value(&value);
238+
if let Some(first) = value.as_array_mut().and_then(|array| array.get_mut(0)) {
239+
if rng.chance(20) {
240+
let comment = format!("# in list {}", comments.len());
241+
first.decor_mut().set_prefix(format!("\n {comment}\n "));
242+
comments.push(comment);
243+
}
244+
}
245+
table.insert(leaf, toml_edit::value(value));
238246
if rng.chance(10) {
239247
let comment = format!("# note {}", comments.len());
240248
table

‎src/config/store.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,22 @@ mod sparse_tests {
557557
}
558558
}
559559

560+
#[test]
561+
fn a_comment_inside_a_default_list_keeps_it() {
562+
let dir = tempfile::tempdir().unwrap();
563+
let path = dir.path().join("config.toml");
564+
std::fs::write(
565+
&path,
566+
"[plugins.bundled.hide-files]\ntags = [\n # keep generated code out\n \"generated\",\n \"vendored\",\n \"test\", # tests too\n]\n",
567+
)
568+
.unwrap();
569+
set(&path, "diff.graph_limit", "42").unwrap();
570+
let text = std::fs::read_to_string(&path).unwrap();
571+
for comment in ["# keep generated code out", "# tests too"] {
572+
assert!(text.contains(comment), "{comment} in {text}");
573+
}
574+
}
575+
560576
#[test]
561577
fn tables_left_empty_by_pruning_go_too() {
562578
let dir = tempfile::tempdir().unwrap();

0 commit comments

Comments
 (0)