Always provide a range

Co-authored-by: Joshua Nelson <joshua@yottadb.com>
This commit is contained in:
Dániel Buga 2020-12-31 10:11:50 +01:00
parent 2b70da1661
commit cb4317de00
2 changed files with 33 additions and 38 deletions

View File

@ -1127,7 +1127,7 @@ crate fn plain_text_summary(md: &str) -> String {
s s
} }
crate fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> { crate fn markdown_links(md: &str) -> Vec<(String, Range<usize>)> {
if md.is_empty() { if md.is_empty() {
return vec![]; return vec![];
} }
@ -1135,7 +1135,7 @@ crate fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
let mut links = vec![]; let mut links = vec![];
let mut shortcut_links = vec![]; let mut shortcut_links = vec![];
let locate = |s: &str| unsafe { let locate = |s: &str, fallback: Range<usize>| unsafe {
let s_start = s.as_ptr(); let s_start = s.as_ptr();
let s_end = s_start.add(s.len()); let s_end = s_start.add(s.len());
let md_start = md.as_ptr(); let md_start = md.as_ptr();
@ -1143,16 +1143,16 @@ crate fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
if md_start <= s_start && s_end <= md_end { if md_start <= s_start && s_end <= md_end {
let start = s_start.offset_from(md_start) as usize; let start = s_start.offset_from(md_start) as usize;
let end = s_end.offset_from(md_start) as usize; let end = s_end.offset_from(md_start) as usize;
Some(start..end) start..end
} else { } else {
None fallback
} }
}; };
let mut push = |link: BrokenLink<'_>| { let mut push = |link: BrokenLink<'_>| {
// FIXME: use `link.span` instead of `locate` // FIXME: use `link.span` instead of `locate`
// (doing it now includes the `[]` as well as the text) // (doing it now includes the `[]` as well as the text)
shortcut_links.push((link.reference.to_owned(), locate(link.reference))); shortcut_links.push((link.reference.to_owned(), locate(link.reference, link.span)));
None None
}; };
let p = Parser::new_with_broken_link_callback(md, opts(), Some(&mut push)).into_offset_iter(); let p = Parser::new_with_broken_link_callback(md, opts(), Some(&mut push)).into_offset_iter();
@ -1166,8 +1166,8 @@ crate fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
if let Event::Start(Tag::Link(_, dest, _)) = ev.0 { if let Event::Start(Tag::Link(_, dest, _)) = ev.0 {
debug!("found link: {}", dest); debug!("found link: {}", dest);
links.push(match dest { links.push(match dest {
CowStr::Borrowed(s) => (s.to_owned(), locate(s)), CowStr::Borrowed(s) => (s.to_owned(), locate(s, ev.1)),
s @ (CowStr::Boxed(..) | CowStr::Inlined(..)) => (s.into_string(), None), s @ (CowStr::Boxed(..) | CowStr::Inlined(..)) => (s.into_string(), ev.1),
}); });
} }
} }

View File

@ -245,7 +245,7 @@ struct DiagnosticInfo<'a> {
item: &'a Item, item: &'a Item,
dox: &'a str, dox: &'a str,
ori_link: &'a str, ori_link: &'a str,
link_range: Option<Range<usize>>, link_range: Range<usize>,
} }
#[derive(Clone, Debug, Hash)] #[derive(Clone, Debug, Hash)]
@ -982,7 +982,7 @@ impl LinkCollector<'_, '_> {
parent_node: Option<DefId>, parent_node: Option<DefId>,
krate: CrateNum, krate: CrateNum,
ori_link: String, ori_link: String,
link_range: Option<Range<usize>>, link_range: Range<usize>,
) -> Option<ItemLink> { ) -> Option<ItemLink> {
trace!("considering link '{}'", ori_link); trace!("considering link '{}'", ori_link);
@ -1628,7 +1628,7 @@ fn report_diagnostic(
msg: &str, msg: &str,
item: &Item, item: &Item,
dox: &str, dox: &str,
link_range: &Option<Range<usize>>, link_range: &Range<usize>,
decorate: impl FnOnce(&mut DiagnosticBuilder<'_>, Option<rustc_span::Span>), decorate: impl FnOnce(&mut DiagnosticBuilder<'_>, Option<rustc_span::Span>),
) { ) {
let hir_id = match cx.as_local_hir_id(item.def_id) { let hir_id = match cx.as_local_hir_id(item.def_id) {
@ -1646,31 +1646,27 @@ fn report_diagnostic(
cx.tcx.struct_span_lint_hir(lint, hir_id, sp, |lint| { cx.tcx.struct_span_lint_hir(lint, hir_id, sp, |lint| {
let mut diag = lint.build(msg); let mut diag = lint.build(msg);
let span = link_range let span = super::source_span_for_markdown_range(cx, dox, link_range, attrs);
.as_ref()
.and_then(|range| super::source_span_for_markdown_range(cx, dox, range, attrs));
if let Some(link_range) = link_range { if let Some(sp) = span {
if let Some(sp) = span { diag.set_span(sp);
diag.set_span(sp); } else {
} else { // blah blah blah\nblah\nblah [blah] blah blah\nblah blah
// blah blah blah\nblah\nblah [blah] blah blah\nblah blah // ^ ~~~~
// ^ ~~~~ // | link_range
// | link_range // last_new_line_offset
// last_new_line_offset let last_new_line_offset = dox[..link_range.start].rfind('\n').map_or(0, |n| n + 1);
let last_new_line_offset = dox[..link_range.start].rfind('\n').map_or(0, |n| n + 1); let line = dox[last_new_line_offset..].lines().next().unwrap_or("");
let line = dox[last_new_line_offset..].lines().next().unwrap_or("");
// Print the line containing the `link_range` and manually mark it with '^'s. // Print the line containing the `link_range` and manually mark it with '^'s.
diag.note(&format!( diag.note(&format!(
"the link appears in this line:\n\n{line}\n\ "the link appears in this line:\n\n{line}\n\
{indicator: <before$}{indicator:^<found$}", {indicator: <before$}{indicator:^<found$}",
line = line, line = line,
indicator = "", indicator = "",
before = link_range.start - last_new_line_offset, before = link_range.start - last_new_line_offset,
found = link_range.len(), found = link_range.len(),
)); ));
}
} }
decorate(&mut diag, span); decorate(&mut diag, span);
@ -1690,7 +1686,7 @@ fn resolution_failure(
path_str: &str, path_str: &str,
disambiguator: Option<Disambiguator>, disambiguator: Option<Disambiguator>,
dox: &str, dox: &str,
link_range: Option<Range<usize>>, link_range: Range<usize>,
kinds: SmallVec<[ResolutionFailure<'_>; 3]>, kinds: SmallVec<[ResolutionFailure<'_>; 3]>,
) { ) {
let tcx = collector.cx.tcx; let tcx = collector.cx.tcx;
@ -1914,7 +1910,7 @@ fn anchor_failure(
item: &Item, item: &Item,
path_str: &str, path_str: &str,
dox: &str, dox: &str,
link_range: Option<Range<usize>>, link_range: Range<usize>,
failure: AnchorFailure, failure: AnchorFailure,
) { ) {
let msg = match failure { let msg = match failure {
@ -1939,7 +1935,7 @@ fn ambiguity_error(
item: &Item, item: &Item,
path_str: &str, path_str: &str,
dox: &str, dox: &str,
link_range: Option<Range<usize>>, link_range: Range<usize>,
candidates: Vec<Res>, candidates: Vec<Res>,
) { ) {
let mut msg = format!("`{}` is ", path_str); let mut msg = format!("`{}` is ", path_str);
@ -1988,13 +1984,12 @@ fn suggest_disambiguator(
path_str: &str, path_str: &str,
dox: &str, dox: &str,
sp: Option<rustc_span::Span>, sp: Option<rustc_span::Span>,
link_range: &Option<Range<usize>>, link_range: &Range<usize>,
) { ) {
let suggestion = disambiguator.suggestion(); let suggestion = disambiguator.suggestion();
let help = format!("to link to the {}, {}", disambiguator.descr(), suggestion.descr()); let help = format!("to link to the {}, {}", disambiguator.descr(), suggestion.descr());
if let Some(sp) = sp { if let Some(sp) = sp {
let link_range = link_range.as_ref().expect("must have a link range if we have a span");
let msg = if dox.bytes().nth(link_range.start) == Some(b'`') { let msg = if dox.bytes().nth(link_range.start) == Some(b'`') {
format!("`{}`", suggestion.as_help(path_str)) format!("`{}`", suggestion.as_help(path_str))
} else { } else {
@ -2013,7 +2008,7 @@ fn privacy_error(
item: &Item, item: &Item,
path_str: &str, path_str: &str,
dox: &str, dox: &str,
link_range: Option<Range<usize>>, link_range: Range<usize>,
) { ) {
let sym; let sym;
let item_name = match item.name { let item_name = match item.name {