Skip to content

Commit 8ff91cf

Browse files
committed
Inspect markdown code fences to determine whether to apply syntax highlighting
1 parent 5d7974e commit 8ff91cf

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

crates/ra_ide/src/snapshots/highlight_doctest.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@
7373
<span class="comment">///</span>
7474
<span class="comment">/// ```</span>
7575
<span class="comment">///</span>
76-
<span class="comment">/// ```</span>
76+
<span class="comment">/// ```rust,no_run</span>
7777
<span class="comment">/// </span><span class="keyword">let</span> <span class="variable declaration">foobar</span> = <span class="struct">Foo</span>::<span class="function">new</span>().<span class="function">bar</span>();
7878
<span class="comment">/// ```</span>
79+
<span class="comment">///</span>
80+
<span class="comment">/// ```sh</span>
81+
<span class="comment">/// echo 1</span>
82+
<span class="comment">/// ```</span>
7983
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration">foo</span>(&<span class="self_keyword">self</span>) -&gt; <span class="builtin_type">bool</span> {
8084
<span class="bool_literal">true</span>
8185
}

crates/ra_ide/src/syntax_highlighting/injection.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ pub(super) fn highlight_injection(
5353
/// Mapping from extracted documentation code to original code
5454
type RangesMap = BTreeMap<TextSize, TextSize>;
5555

56+
const RUSTDOC_FENCE: &'static str = "```";
57+
const RUSTDOC_FENCE_TOKENS: &[&'static str] =
58+
&["", "rust", "should_panic", "ignore", "no_run", "compile_fail", "edition2015", "edition2018"];
59+
5660
/// Extracts Rust code from documentation comments as well as a mapping from
5761
/// the extracted source code back to the original source ranges.
5862
/// Lastly, a vector of new comment highlight ranges (spanning only the
@@ -67,6 +71,7 @@ pub(super) fn extract_doc_comments(
6771
// Mapping from extracted documentation code to original code
6872
let mut range_mapping: RangesMap = BTreeMap::new();
6973
let mut line_start = TextSize::try_from(prefix.len()).unwrap();
74+
let mut is_codeblock = false;
7075
let mut is_doctest = false;
7176
// Replace the original, line-spanning comment ranges by new, only comment-prefix
7277
// spanning comment ranges.
@@ -76,8 +81,13 @@ pub(super) fn extract_doc_comments(
7681
.filter_map(|el| el.into_token().and_then(ast::Comment::cast))
7782
.filter(|comment| comment.kind().doc.is_some())
7883
.filter(|comment| {
79-
if comment.text().contains("```") {
80-
is_doctest = !is_doctest;
84+
if let Some(idx) = comment.text().find(RUSTDOC_FENCE) {
85+
is_codeblock = !is_codeblock;
86+
// Check whether code is rust by inspecting fence guards
87+
let guards = &comment.text()[idx + RUSTDOC_FENCE.len()..];
88+
let is_rust =
89+
guards.split(',').all(|sub| RUSTDOC_FENCE_TOKENS.contains(&sub.trim()));
90+
is_doctest = is_codeblock && is_rust;
8191
false
8292
} else {
8393
is_doctest

crates/ra_ide/src/syntax_highlighting/tests.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,13 @@ impl Foo {
329329
///
330330
/// ```
331331
///
332-
/// ```
332+
/// ```rust,no_run
333333
/// let foobar = Foo::new().bar();
334334
/// ```
335+
///
336+
/// ```sh
337+
/// echo 1
338+
/// ```
335339
pub fn foo(&self) -> bool {
336340
true
337341
}

0 commit comments

Comments
 (0)