aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src
diff options
context:
space:
mode:
authorPaul Daniel Faria <[email protected]>2020-06-23 03:28:09 +0100
committerPaul Daniel Faria <[email protected]>2020-06-23 03:29:08 +0100
commit0b971625c389c1638957b010a35c7bb1a6bd69b9 (patch)
treec6efdc103468ee72475c7f9b10902f15df6522fd /crates/ra_ide/src
parentd8842e89e9053b62c081d2995cbf43b8fd5c51b2 (diff)
Fix underflow panic when doctests are at top of file
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r--crates/ra_ide/src/snapshots/highlight_doctest.html5
-rw-r--r--crates/ra_ide/src/syntax_highlighting/injection.rs12
-rw-r--r--crates/ra_ide/src/syntax_highlighting/tests.rs3
3 files changed, 15 insertions, 5 deletions
diff --git a/crates/ra_ide/src/snapshots/highlight_doctest.html b/crates/ra_ide/src/snapshots/highlight_doctest.html
index 63199cdbe..ac546806e 100644
--- a/crates/ra_ide/src/snapshots/highlight_doctest.html
+++ b/crates/ra_ide/src/snapshots/highlight_doctest.html
@@ -32,7 +32,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
32.keyword.unsafe { color: #BC8383; font-weight: bold; } 32.keyword.unsafe { color: #BC8383; font-weight: bold; }
33.control { font-style: italic; } 33.control { font-style: italic; }
34</style> 34</style>
35<pre><code><span class="keyword">struct</span> <span class="struct declaration">Foo</span> { 35<pre><code><span class="comment documentation">/// ```</span>
36<span class="comment documentation">/// </span><span class="keyword">let</span> _ = <span class="string_literal">"early doctests should not go boom"</span>;
37<span class="comment documentation">/// ```</span>
38<span class="keyword">struct</span> <span class="struct declaration">Foo</span> {
36 <span class="field declaration">bar</span>: <span class="builtin_type">bool</span>, 39 <span class="field declaration">bar</span>: <span class="builtin_type">bool</span>,
37} 40}
38 41
diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs
index 415f24a6d..9d82b4009 100644
--- a/crates/ra_ide/src/syntax_highlighting/injection.rs
+++ b/crates/ra_ide/src/syntax_highlighting/injection.rs
@@ -155,17 +155,21 @@ pub(super) fn highlight_doc_comment(
155 let mut start_offset = None; 155 let mut start_offset = None;
156 let mut end_offset = None; 156 let mut end_offset = None;
157 for (line_start, orig_line_start) in range_mapping.range(..h.range.end()).rev() { 157 for (line_start, orig_line_start) in range_mapping.range(..h.range.end()).rev() {
158 // It's possible for orig_line_start - line_start to be negative. Add h.range.start()
159 // here and remove it from the end range after the loop below so that the values are
160 // always non-negative.
161 let offset = h.range.start() + orig_line_start - line_start;
158 if line_start <= &h.range.start() { 162 if line_start <= &h.range.start() {
159 start_offset.get_or_insert(orig_line_start - line_start); 163 start_offset.get_or_insert(offset);
160 break; 164 break;
161 } else { 165 } else {
162 end_offset.get_or_insert(orig_line_start - line_start); 166 end_offset.get_or_insert(offset);
163 } 167 }
164 } 168 }
165 if let Some(start_offset) = start_offset { 169 if let Some(start_offset) = start_offset {
166 h.range = TextRange::new( 170 h.range = TextRange::new(
167 h.range.start() + start_offset, 171 start_offset,
168 h.range.end() + end_offset.unwrap_or(start_offset), 172 h.range.end() + end_offset.unwrap_or(start_offset) - h.range.start(),
169 ); 173 );
170 174
171 stack.add(h); 175 stack.add(h);
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs
index 93a276ffe..b1f48f03b 100644
--- a/crates/ra_ide/src/syntax_highlighting/tests.rs
+++ b/crates/ra_ide/src/syntax_highlighting/tests.rs
@@ -291,6 +291,9 @@ fn main() {
291fn test_highlight_doctest() { 291fn test_highlight_doctest() {
292 check_highlighting( 292 check_highlighting(
293 r#" 293 r#"
294/// ```
295/// let _ = "early doctests should not go boom";
296/// ```
294struct Foo { 297struct Foo {
295 bar: bool, 298 bar: bool,
296} 299}