@@ -268,7 +268,12 @@ fn emit_field(output: &mut String, name: &str, field: &OptionField, parents: &[S
268
268
} => {
269
269
output. push_str ( & format_code (
270
270
"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
+ ) ,
272
277
field. example ,
273
278
) ) ;
274
279
}
@@ -278,12 +283,22 @@ fn emit_field(output: &mut String, name: &str, field: &OptionField, parents: &[S
278
283
} => {
279
284
output. push_str ( & format_tab (
280
285
"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
+ ) ,
282
292
field. example ,
283
293
) ) ;
284
294
output. push_str ( & format_tab (
285
295
"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
+ ) ,
287
302
field. example ,
288
303
) ) ;
289
304
}
@@ -293,12 +308,20 @@ fn emit_field(output: &mut String, name: &str, field: &OptionField, parents: &[S
293
308
}
294
309
295
310
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
+ }
302
325
}
303
326
304
327
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 {
308
331
/// Format the TOML header for the example usage for a given option.
309
332
///
310
333
/// 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 {
312
340
let tool_parent = match configuration {
313
341
ConfigurationFile :: PyprojectToml => Some ( "tool.uv" ) ,
314
342
ConfigurationFile :: UvToml => None ,
@@ -320,6 +348,15 @@ fn format_header(scope: Option<&str>, parents: &[Set], configuration: Configurat
320
348
. chain ( scope)
321
349
. join ( "." ) ;
322
350
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
+
323
360
if header. is_empty ( ) {
324
361
String :: new ( )
325
362
} else {
0 commit comments