Skip to content

Commit 8f3fd3d

Browse files
Add support for primitives in "jump to definition" feature
1 parent 1cd17ad commit 8f3fd3d

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/librustdoc/html/highlight.rs

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//!
66
//! Use the `render_with_highlighting` to highlight some rust code.
77
8+
use crate::clean::PrimitiveType;
89
use crate::html::escape::Escape;
910
use crate::html::render::Context;
1011

@@ -584,6 +585,13 @@ fn string<T: Display>(
584585
.ok()
585586
.map(|(url, _, _)| url)
586587
}
588+
LinkFromSrc::Primitive(prim) => format::href_with_root_path(
589+
PrimitiveType::primitive_locations(context.tcx())[&prim],
590+
context,
591+
Some(context_info.root_path),
592+
)
593+
.ok()
594+
.map(|(url, _, _)| url),
587595
}
588596
})
589597
{

src/librustdoc/html/render/span_map.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::clean;
1+
use crate::clean::{self, PrimitiveType};
22
use crate::html::sources;
33

44
use rustc_data_structures::fx::FxHashMap;
@@ -22,6 +22,7 @@ use std::path::{Path, PathBuf};
2222
crate enum LinkFromSrc {
2323
Local(clean::Span),
2424
External(DefId),
25+
Primitive(PrimitiveType),
2526
}
2627

2728
/// This function will do at most two things:
@@ -73,17 +74,20 @@ impl<'tcx> SpanMapVisitor<'tcx> {
7374
Some(def_id)
7475
}
7576
Res::Local(_) => None,
77+
Res::PrimTy(p) => {
78+
// FIXME: Doesn't handle "path-like" primitives like arrays or tuples.
79+
let span = path_span.unwrap_or(path.span);
80+
self.matches.insert(span, LinkFromSrc::Primitive(PrimitiveType::from(p)));
81+
return;
82+
}
7683
Res::Err => return,
7784
_ => return,
7885
};
7986
if let Some(span) = self.tcx.hir().res_span(path.res) {
80-
self.matches.insert(
81-
path_span.unwrap_or_else(|| path.span),
82-
LinkFromSrc::Local(clean::Span::new(span)),
83-
);
84-
} else if let Some(def_id) = info {
8587
self.matches
86-
.insert(path_span.unwrap_or_else(|| path.span), LinkFromSrc::External(def_id));
88+
.insert(path_span.unwrap_or(path.span), LinkFromSrc::Local(clean::Span::new(span)));
89+
} else if let Some(def_id) = info {
90+
self.matches.insert(path_span.unwrap_or(path.span), LinkFromSrc::External(def_id));
8791
}
8892
}
8993
}

0 commit comments

Comments
 (0)