Skip to content

Commit dffc9c0

Browse files
Move extra arguments for highlight URL generation into a new ContextInfo struct for better readability
1 parent e8869cb commit dffc9c0

File tree

5 files changed

+42
-40
lines changed

5 files changed

+42
-40
lines changed

src/librustdoc/html/highlight.rs

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ use rustc_span::symbol::Symbol;
1919
use super::format::{self, Buffer};
2020
use super::render::{LightSpan, LinkFromSrc};
2121

22+
/// This type is needed in case we want to render links on items to allow to go to their definition.
23+
crate struct ContextInfo<'a, 'b, 'c> {
24+
crate context: &'a Context<'b>,
25+
/// This represents the "lo" bytes of the current file we're rendering. To get a [`Span`] from
26+
/// it, you just need to add add your current byte position in the string and its length (to get
27+
/// the "hi" part).
28+
///
29+
/// This is used to create a [`LightSpan`] which is then used as an index in the `span_map` in
30+
/// order to retrieve the definition's [`Span`] (which is used to generate the URL).
31+
crate file_span_lo: u32,
32+
/// This field is used to know "how far" from the top of the directory we are to link to either
33+
/// documentation pages or other source pages.
34+
crate root_path: &'c str,
35+
}
36+
2237
/// Highlights `src`, returning the HTML output.
2338
crate fn render_with_highlighting(
2439
src: &str,
@@ -28,9 +43,7 @@ crate fn render_with_highlighting(
2843
tooltip: Option<(Option<Edition>, &str)>,
2944
edition: Edition,
3045
extra_content: Option<Buffer>,
31-
file_span_lo: u32,
32-
context: Option<&Context<'_>>,
33-
root_path: &str,
46+
context_info: Option<ContextInfo<'_, '_, '_>>,
3447
) {
3548
debug!("highlighting: ================\n{}\n==============", src);
3649
if let Some((edition_info, class)) = tooltip {
@@ -47,7 +60,7 @@ crate fn render_with_highlighting(
4760
}
4861

4962
write_header(out, class, extra_content);
50-
write_code(out, &src, edition, file_span_lo, context, root_path);
63+
write_code(out, &src, edition, context_info);
5164
write_footer(out, playground_button);
5265
}
5366

@@ -69,37 +82,28 @@ fn write_header(out: &mut Buffer, class: Option<&str>, extra_content: Option<Buf
6982
///
7083
/// Some explanations on the last arguments:
7184
///
72-
/// In case we are rendering a code block and not a source code file, `file_span_lo` value doesn't
73-
/// matter and `context` will be `None`. To put it more simply: if `context` is `None`, the code
74-
/// won't try to generate links to an ident definition.
85+
/// In case we are rendering a code block and not a source code file, `context_info` will be `None`.
86+
/// To put it more simply: if `context_info` is `None`, the code won't try to generate links to an
87+
/// item definition.
7588
///
7689
/// More explanations about spans and how we use them here are provided in the
7790
/// [`LightSpan::new_in_file`] function documentation about how it works.
78-
///
79-
/// As for `root_path`, it's used to know "how far" from the top of the directory we are to link
80-
/// to either documentation pages or other source pages.
81-
///
82-
/// Same as `file_span_lo`: its value doesn't matter in case you are not rendering a source code
83-
/// file.
8491
fn write_code(
8592
out: &mut Buffer,
8693
src: &str,
8794
edition: Edition,
88-
file_span_lo: u32,
89-
context: Option<&Context<'_>>,
90-
root_path: &str,
95+
context_info: Option<ContextInfo<'_, '_, '_>>,
9196
) {
9297
// This replace allows to fix how the code source with DOS backline characters is displayed.
9398
let src = src.replace("\r\n", "\n");
94-
Classifier::new(&src, edition, file_span_lo).highlight(&mut |highlight| {
95-
match highlight {
96-
Highlight::Token { text, class } => {
97-
string(out, Escape(text), class, context, root_path)
98-
}
99-
Highlight::EnterSpan { class } => enter_span(out, class),
100-
Highlight::ExitSpan => exit_span(out),
101-
};
102-
});
99+
Classifier::new(&src, edition, context_info.as_ref().map(|c| c.file_span_lo).unwrap_or(0))
100+
.highlight(&mut |highlight| {
101+
match highlight {
102+
Highlight::Token { text, class } => string(out, Escape(text), class, &context_info),
103+
Highlight::EnterSpan { class } => enter_span(out, class),
104+
Highlight::ExitSpan => exit_span(out),
105+
};
106+
});
103107
}
104108

105109
fn write_footer(out: &mut Buffer, playground_button: Option<&str>) {
@@ -540,8 +544,7 @@ fn string<T: Display>(
540544
out: &mut Buffer,
541545
text: T,
542546
klass: Option<Class>,
543-
context: Option<&Context<'_>>,
544-
root_path: &str,
547+
context_info: &Option<ContextInfo<'_, '_, '_>>,
545548
) {
546549
let klass = match klass {
547550
None => return write!(out, "{}", text),
@@ -570,14 +573,19 @@ fn string<T: Display>(
570573
path
571574
});
572575
}
573-
if let Some(context) = context {
574-
if let Some(href) =
575-
context.shared.span_correspondance_map.get(&def_span).and_then(|href| {
576+
if let Some(context_info) = context_info {
577+
if let Some(href) = context_info
578+
.context
579+
.shared
580+
.span_correspondance_map
581+
.get(&def_span)
582+
.and_then(|href| {
583+
let context = context_info.context;
576584
match href {
577585
LinkFromSrc::Local(span) => {
578586
context
579587
.href_from_span(clean::Span::wrap_raw(*span))
580-
.map(|s| format!("{}{}", root_path, s))
588+
.map(|s| format!("{}{}", context_info.root_path, s))
581589
}
582590
LinkFromSrc::External(def_id) => {
583591
format::href(*def_id, context).map(|(url, _, _)| url)

src/librustdoc/html/highlight/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn test_html_highlighting() {
2222
let src = include_str!("fixtures/sample.rs");
2323
let html = {
2424
let mut out = Buffer::new();
25-
write_code(&mut out, src, Edition::Edition2018, 0, None, "");
25+
write_code(&mut out, src, Edition::Edition2018, None);
2626
format!("{}<pre><code>{}</code></pre>\n", STYLE, out.into_inner())
2727
};
2828
expect_file!["fixtures/sample.html"].assert_eq(&html);
@@ -36,7 +36,7 @@ fn test_dos_backline() {
3636
println!(\"foo\");\r\n\
3737
}\r\n";
3838
let mut html = Buffer::new();
39-
write_code(&mut html, src, Edition::Edition2018, 0, None, "");
39+
write_code(&mut html, src, Edition::Edition2018, None);
4040
expect_file!["fixtures/dos_line.html"].assert_eq(&html.into_inner());
4141
});
4242
}

src/librustdoc/html/markdown.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
330330
tooltip,
331331
edition,
332332
None,
333-
0,
334333
None,
335-
"",
336334
);
337335
Some(Event::Html(s.into_inner().into()))
338336
}

src/librustdoc/html/render/print_item.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,9 +1081,7 @@ fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Mac
10811081
None,
10821082
it.span(cx.tcx()).inner().edition(),
10831083
None,
1084-
0,
10851084
None,
1086-
"",
10871085
);
10881086
});
10891087
document(w, cx, it, None)

src/librustdoc/html/sources.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,6 @@ fn print_src(
275275
None,
276276
edition,
277277
Some(line_numbers),
278-
file_span_lo,
279-
Some(context),
280-
root_path,
278+
Some(highlight::ContextInfo { context, file_span_lo, root_path }),
281279
);
282280
}

0 commit comments

Comments
 (0)