Fix underflow panic when doctests are at top of file

This commit is contained in:
Paul Daniel Faria
2020-06-22 22:28:09 -04:00
parent d8842e89e9
commit 0b971625c3
3 changed files with 15 additions and 5 deletions

View File

@@ -32,7 +32,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.keyword.unsafe { color: #BC8383; font-weight: bold; } .keyword.unsafe { color: #BC8383; font-weight: bold; }
.control { font-style: italic; } .control { font-style: italic; }
</style> </style>
<pre><code><span class="keyword">struct</span> <span class="struct declaration">Foo</span> { <pre><code><span class="comment documentation">/// ```</span>
<span class="comment documentation">/// </span><span class="keyword">let</span> _ = <span class="string_literal">"early doctests should not go boom"</span>;
<span class="comment documentation">/// ```</span>
<span class="keyword">struct</span> <span class="struct declaration">Foo</span> {
<span class="field declaration">bar</span>: <span class="builtin_type">bool</span>, <span class="field declaration">bar</span>: <span class="builtin_type">bool</span>,
} }

View File

@@ -155,17 +155,21 @@ pub(super) fn highlight_doc_comment(
let mut start_offset = None; let mut start_offset = None;
let mut end_offset = None; let mut end_offset = None;
for (line_start, orig_line_start) in range_mapping.range(..h.range.end()).rev() { for (line_start, orig_line_start) in range_mapping.range(..h.range.end()).rev() {
// It's possible for orig_line_start - line_start to be negative. Add h.range.start()
// here and remove it from the end range after the loop below so that the values are
// always non-negative.
let offset = h.range.start() + orig_line_start - line_start;
if line_start <= &h.range.start() { if line_start <= &h.range.start() {
start_offset.get_or_insert(orig_line_start - line_start); start_offset.get_or_insert(offset);
break; break;
} else { } else {
end_offset.get_or_insert(orig_line_start - line_start); end_offset.get_or_insert(offset);
} }
} }
if let Some(start_offset) = start_offset { if let Some(start_offset) = start_offset {
h.range = TextRange::new( h.range = TextRange::new(
h.range.start() + start_offset, start_offset,
h.range.end() + end_offset.unwrap_or(start_offset), h.range.end() + end_offset.unwrap_or(start_offset) - h.range.start(),
); );
stack.add(h); stack.add(h);

View File

@@ -291,6 +291,9 @@ fn main() {
fn test_highlight_doctest() { fn test_highlight_doctest() {
check_highlighting( check_highlighting(
r#" r#"
/// ```
/// let _ = "early doctests should not go boom";
/// ```
struct Foo { struct Foo {
bar: bool, bar: bool,
} }