Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: Remove 'need_backline' field from DocFragment #92095

Merged
merged 2 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 22 additions & 28 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,6 @@ crate struct DocFragment {
crate parent_module: Option<DefId>,
crate doc: Symbol,
crate kind: DocFragmentKind,
crate need_backline: bool,
crate indent: usize,
}

Expand All @@ -930,28 +929,25 @@ crate enum DocFragmentKind {
RawDoc,
}

// The goal of this function is to apply the `DocFragment` transformations that are required when
// transforming into the final markdown. So the transformations in here are:
//
// * Applying the computed indent to each lines in each doc fragment (a `DocFragment` can contain
// multiple lines in case of `#[doc = ""]`).
// * Adding backlines between `DocFragment`s and adding an extra one if required (stored in the
// `need_backline` field).
/// The goal of this function is to apply the `DocFragment` transformation that is required when
/// transforming into the final Markdown, which is applying the computed indent to each line in
/// each doc fragment (a `DocFragment` can contain multiple lines in case of `#[doc = ""]`).
///
/// Note: remove the trailing newline where appropriate
fn add_doc_fragment(out: &mut String, frag: &DocFragment) {
let s = frag.doc.as_str();
let mut iter = s.lines().peekable();
let mut iter = s.lines();
if s == "" {
out.push('\n');
return;
}
while let Some(line) = iter.next() {
if line.chars().any(|c| !c.is_whitespace()) {
assert!(line.len() >= frag.indent);
out.push_str(&line[frag.indent..]);
} else {
out.push_str(line);
}
if iter.peek().is_some() {
out.push('\n');
}
}
if frag.need_backline {
out.push('\n');
}
}
Expand All @@ -963,6 +959,7 @@ crate fn collapse_doc_fragments(doc_strings: &[DocFragment]) -> String {
for frag in doc_strings {
add_doc_fragment(&mut acc, frag);
}
acc.pop();
acc
}

Expand Down Expand Up @@ -1028,7 +1025,6 @@ impl Attributes {
additional_attrs: Option<(&[ast::Attribute], DefId)>,
) -> Attributes {
let mut doc_strings: Vec<DocFragment> = vec![];

let clean_attr = |(attr, parent_module): (&ast::Attribute, Option<DefId>)| {
if let Some(value) = attr.doc_str() {
trace!("got doc_str={:?}", value);
Expand All @@ -1039,18 +1035,8 @@ impl Attributes {
DocFragmentKind::RawDoc
};

let frag = DocFragment {
span: attr.span,
doc: value,
kind,
parent_module,
need_backline: false,
indent: 0,
};

if let Some(prev) = doc_strings.last_mut() {
prev.need_backline = true;
}
let frag =
DocFragment { span: attr.span, doc: value, kind, parent_module, indent: 0 };

doc_strings.push(frag);

Expand Down Expand Up @@ -1086,6 +1072,7 @@ impl Attributes {
}
add_doc_fragment(&mut out, new_frag);
}
out.pop();
if out.is_empty() { None } else { Some(out) }
}

Expand All @@ -1094,10 +1081,17 @@ impl Attributes {
/// The module can be different if this is a re-export with added documentation.
crate fn collapsed_doc_value_by_module_level(&self) -> FxHashMap<Option<DefId>, String> {
let mut ret = FxHashMap::default();
if self.doc_strings.len() == 0 {
return ret;
}
let last_index = self.doc_strings.len() - 1;

for new_frag in self.doc_strings.iter() {
for (i, new_frag) in self.doc_strings.iter().enumerate() {
let out = ret.entry(new_frag.parent_module).or_default();
add_doc_fragment(out, new_frag);
if i == last_index {
out.pop();
}
}
ret
}
Expand Down
1 change: 0 additions & 1 deletion src/librustdoc/passes/unindent_comments/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ fn create_doc_fragment(s: &str) -> Vec<DocFragment> {
parent_module: None,
doc: Symbol::intern(s),
kind: DocFragmentKind::SugaredDoc,
need_backline: false,
indent: 0,
}]
}
Expand Down