Skip to content

Commit 99a8746

Browse files
Avoid duplicate [tool.uv] header in TOML examples (#8545)
## Summary For example, in: ```toml [tool.uv] [[tool.uv.index]] name = "pytorch" url = "https://download.pytorch.org/whl/cu121" ``` We can just omit `[tool.uv]`.
1 parent ec71fb1 commit 99a8746

File tree

2 files changed

+48
-51
lines changed

2 files changed

+48
-51
lines changed

crates/uv-dev/src/generate_options_reference.rs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,12 @@ fn emit_field(output: &mut String, name: &str, field: &OptionField, parents: &[S
268268
} => {
269269
output.push_str(&format_code(
270270
"pyproject.toml",
271-
&format_header(field.scope, parents, ConfigurationFile::PyprojectToml),
271+
&format_header(
272+
field.scope,
273+
field.example,
274+
parents,
275+
ConfigurationFile::PyprojectToml,
276+
),
272277
field.example,
273278
));
274279
}
@@ -278,12 +283,22 @@ fn emit_field(output: &mut String, name: &str, field: &OptionField, parents: &[S
278283
} => {
279284
output.push_str(&format_tab(
280285
"pyproject.toml",
281-
&format_header(field.scope, parents, ConfigurationFile::PyprojectToml),
286+
&format_header(
287+
field.scope,
288+
field.example,
289+
parents,
290+
ConfigurationFile::PyprojectToml,
291+
),
282292
field.example,
283293
));
284294
output.push_str(&format_tab(
285295
"uv.toml",
286-
&format_header(field.scope, parents, ConfigurationFile::UvToml),
296+
&format_header(
297+
field.scope,
298+
field.example,
299+
parents,
300+
ConfigurationFile::UvToml,
301+
),
287302
field.example,
288303
));
289304
}
@@ -293,12 +308,20 @@ fn emit_field(output: &mut String, name: &str, field: &OptionField, parents: &[S
293308
}
294309

295310
fn format_tab(tab_name: &str, header: &str, content: &str) -> String {
296-
format!(
297-
"=== \"{}\"\n\n ```toml\n {}\n{}\n ```\n",
298-
tab_name,
299-
header,
300-
textwrap::indent(content, " ")
301-
)
311+
if header.is_empty() {
312+
format!(
313+
"=== \"{}\"\n\n ```toml\n{}\n ```\n",
314+
tab_name,
315+
textwrap::indent(content, " ")
316+
)
317+
} else {
318+
format!(
319+
"=== \"{}\"\n\n ```toml\n {}\n{}\n ```\n",
320+
tab_name,
321+
header,
322+
textwrap::indent(content, " ")
323+
)
324+
}
302325
}
303326

304327
fn format_code(file_name: &str, header: &str, content: &str) -> String {
@@ -308,7 +331,12 @@ fn format_code(file_name: &str, header: &str, content: &str) -> String {
308331
/// Format the TOML header for the example usage for a given option.
309332
///
310333
/// For example: `[tool.uv.pip]`.
311-
fn format_header(scope: Option<&str>, parents: &[Set], configuration: ConfigurationFile) -> String {
334+
fn format_header(
335+
scope: Option<&str>,
336+
example: &str,
337+
parents: &[Set],
338+
configuration: ConfigurationFile,
339+
) -> String {
312340
let tool_parent = match configuration {
313341
ConfigurationFile::PyprojectToml => Some("tool.uv"),
314342
ConfigurationFile::UvToml => None,
@@ -320,6 +348,15 @@ fn format_header(scope: Option<&str>, parents: &[Set], configuration: Configurat
320348
.chain(scope)
321349
.join(".");
322350

351+
// Ex) `[[tool.uv.index]]`
352+
if example.starts_with(&format!("[[{header}")) {
353+
return String::new();
354+
}
355+
// Ex) `[tool.uv.sources]`
356+
if example.starts_with(&format!("[{header}")) {
357+
return String::new();
358+
}
359+
323360
if header.is_empty() {
324361
String::new()
325362
} else {

0 commit comments

Comments
 (0)