@@ -53,6 +53,10 @@ pub(super) fn highlight_injection(
5353/// Mapping from extracted documentation code to original code
5454type 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
0 commit comments