Skip to content

Commit

Permalink
Rollup merge of #112894 - GuillaumeGomez:gui-fields-display, r=notriddle
Browse files Browse the repository at this point in the history
Fix union fields display

![Screenshot from 2023-06-21 16-47-24](https://github.com/rust-lang/rust/assets/3050060/833b0fe6-7fb6-4371-86c3-d82fa0c3fe49)

So two bugs in this screenshot: no whitespace between field name and type name, both fields are on the same line. Both problems come from issues in the templates because all whitespace are removed if a askama "command" follows.

r? `@notriddle`
  • Loading branch information
GuillaumeGomez authored Jun 21, 2023
2 parents d938be1 + 805edb0 commit f5470af
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 11 deletions.
5 changes: 2 additions & 3 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,10 @@ fn document<'a, 'cx: 'a>(
display_fn(move |f| {
document_item_info(cx, item, parent).render_into(f).unwrap();
if parent.is_none() {
write!(f, "{}", document_full_collapsible(item, cx, heading_offset))?;
write!(f, "{}", document_full_collapsible(item, cx, heading_offset))
} else {
write!(f, "{}", document_full(item, cx, heading_offset))?;
write!(f, "{}", document_full(item, cx, heading_offset))
}
Ok(())
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/templates/item_info.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% if !items.is_empty() %}
<span class="item-info"> {# #}
<span class="item-info">
{% for item in items %}
{{item|safe}} {# #}
{% endfor %}
Expand Down
11 changes: 6 additions & 5 deletions src/librustdoc/html/templates/item_union.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
</code></pre>
{{ self.document() | safe }}
{% if self.fields_iter().peek().is_some() %}
<h2 id="fields" class="fields small-section-header">
Fields<a href="#fields" class="anchor">§</a>
<h2 id="fields" class="fields small-section-header"> {# #}
Fields<a href="#fields" class="anchor">§</a> {# #}
</h2>
{% for (field, ty) in self.fields_iter() %}
{% let name = field.name.expect("union field name") %}
<span id="structfield.{{ name }}" class="{{ ItemType::StructField }} small-section-header">
<a href="#structfield.{{ name }}" class="anchor field">§</a>
<code>{{ name }}: {{ self.print_ty(ty) | safe }}</code>
<span id="structfield.{{ name }}" {#+ #}
class="{{ ItemType::StructField +}} small-section-header"> {# #}
<a href="#structfield.{{ name }}" class="anchor field">§</a> {# #}
<code>{{ name }}: {{+ self.print_ty(ty) | safe }}</code> {# #}
</span>
{% if let Some(stability_class) = self.stability_field(field) %}
<span class="stab {{ stability_class }}"></span>
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/templates/print_item.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="main-heading"> {# #}
<h1> {# #}
<h1>
{{typ}}
{# The breadcrumbs of the item path, like std::string #}
{% for component in path_components %}
Expand All @@ -12,7 +12,7 @@ <h1> {# #}
alt="Copy item path"> {# #}
</button> {# #}
</h1> {# #}
<span class="out-of-band"> {# #}
<span class="out-of-band">
{% if !stability_since_raw.is_empty() %}
{{ stability_since_raw|safe +}} · {#+ #}
{% endif %}
Expand Down
18 changes: 18 additions & 0 deletions tests/rustdoc-gui/fields.goml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// This test checks that fields are displayed as expected (one by line).
go-to: "file://" + |DOC_PATH| + "/test_docs/fields/struct.Struct.html"
store-position: ("#structfield\.a", {"y": a_y})
store-position: ("#structfield\.b", {"y": b_y})
assert: |a_y| < |b_y|

go-to: "file://" + |DOC_PATH| + "/test_docs/fields/union.Union.html"
store-position: ("#structfield\.a", {"y": a_y})
store-position: ("#structfield\.b", {"y": b_y})
assert: |a_y| < |b_y|

go-to: "file://" + |DOC_PATH| + "/test_docs/fields/enum.Enum.html"
store-position: ("#variant\.A\.field\.a", {"y": a_y})
store-position: ("#variant\.A\.field\.b", {"y": b_y})
assert: |a_y| < |b_y|
store-position: ("#variant\.B\.field\.a", {"y": a_y})
store-position: ("#variant\.B\.field\.b", {"y": b_y})
assert: |a_y| < |b_y|
21 changes: 21 additions & 0 deletions tests/rustdoc-gui/src/test_docs/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,24 @@ pub mod search_results {
}

}

pub mod fields {
pub struct Struct {
pub a: u8,
pub b: u32,
}
pub union Union {
pub a: u8,
pub b: u32,
}
pub enum Enum {
A {
a: u8,
b: u32,
},
B {
a: u8,
b: u32,
},
}
}
11 changes: 11 additions & 0 deletions tests/rustdoc/union-fields-html.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![crate_name = "foo"]

// @has 'foo/union.Union.html'
// Checking that there is a whitespace after `:`.
// @has - '//*[@id="structfield.a"]/code' 'a: u8'
// @has - '//*[@id="structfield.b"]/code' 'b: u32'
pub union Union {
pub a: u8,
/// tadam
pub b: u32,
}

0 comments on commit f5470af

Please sign in to comment.