@@ -53,6 +53,10 @@ pub(super) fn highlight_injection(
53
53
/// Mapping from extracted documentation code to original code
54
54
type RangesMap = BTreeMap < TextSize , TextSize > ;
55
55
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
+
56
60
/// Extracts Rust code from documentation comments as well as a mapping from
57
61
/// the extracted source code back to the original source ranges.
58
62
/// Lastly, a vector of new comment highlight ranges (spanning only the
@@ -67,6 +71,7 @@ pub(super) fn extract_doc_comments(
67
71
// Mapping from extracted documentation code to original code
68
72
let mut range_mapping: RangesMap = BTreeMap :: new ( ) ;
69
73
let mut line_start = TextSize :: try_from ( prefix. len ( ) ) . unwrap ( ) ;
74
+ let mut is_codeblock = false ;
70
75
let mut is_doctest = false ;
71
76
// Replace the original, line-spanning comment ranges by new, only comment-prefix
72
77
// spanning comment ranges.
@@ -76,8 +81,13 @@ pub(super) fn extract_doc_comments(
76
81
. filter_map ( |el| el. into_token ( ) . and_then ( ast:: Comment :: cast) )
77
82
. filter ( |comment| comment. kind ( ) . doc . is_some ( ) )
78
83
. 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;
81
91
false
82
92
} else {
83
93
is_doctest
0 commit comments