]> git.lizzy.rs Git - rust.git/blob - crates/ide/src/syntax_highlighting/inject.rs
Don't require all doc fences to be valid for identifying rust code
[rust.git] / crates / ide / src / syntax_highlighting / inject.rs
1 //! "Recursive" Syntax highlighting for code in doctests and fixtures.
2
3 use std::mem;
4
5 use either::Either;
6 use hir::{InFile, Semantics};
7 use ide_db::{call_info::ActiveParameter, SymbolKind};
8 use syntax::{
9     ast::{self, AstNode},
10     AstToken, NodeOrToken, SyntaxNode, SyntaxToken, TextRange, TextSize,
11 };
12
13 use crate::{
14     doc_links::{doc_attributes, extract_definitions_from_markdown, resolve_doc_path_for_def},
15     Analysis, HlMod, HlRange, HlTag, RootDatabase,
16 };
17
18 use super::{highlights::Highlights, injector::Injector};
19
20 pub(super) fn ra_fixture(
21     hl: &mut Highlights,
22     sema: &Semantics<RootDatabase>,
23     literal: ast::String,
24     expanded: SyntaxToken,
25 ) -> Option<()> {
26     let active_parameter = ActiveParameter::at_token(&sema, expanded)?;
27     if !active_parameter.ident().map_or(false, |name| name.text().starts_with("ra_fixture")) {
28         return None;
29     }
30     let value = literal.value()?;
31
32     if let Some(range) = literal.open_quote_text_range() {
33         hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
34     }
35
36     let mut inj = Injector::default();
37
38     let mut text = &*value;
39     let mut offset: TextSize = 0.into();
40
41     while !text.is_empty() {
42         let marker = "$0";
43         let idx = text.find(marker).unwrap_or(text.len());
44         let (chunk, next) = text.split_at(idx);
45         inj.add(chunk, TextRange::at(offset, TextSize::of(chunk)));
46
47         text = next;
48         offset += TextSize::of(chunk);
49
50         if let Some(next) = text.strip_prefix(marker) {
51             if let Some(range) = literal.map_range_up(TextRange::at(offset, TextSize::of(marker))) {
52                 hl.add(HlRange { range, highlight: HlTag::Keyword.into(), binding_hash: None });
53             }
54
55             text = next;
56
57             let marker_len = TextSize::of(marker);
58             offset += marker_len;
59         }
60     }
61
62     let (analysis, tmp_file_id) = Analysis::from_single_file(inj.text().to_string());
63
64     for mut hl_range in analysis.highlight(tmp_file_id).unwrap() {
65         for range in inj.map_range_up(hl_range.range) {
66             if let Some(range) = literal.map_range_up(range) {
67                 hl_range.range = range;
68                 hl.add(hl_range);
69             }
70         }
71     }
72
73     if let Some(range) = literal.close_quote_text_range() {
74         hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
75     }
76
77     Some(())
78 }
79
80 const RUSTDOC_FENCE: &'static str = "```";
81 const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[
82     "",
83     "rust",
84     "should_panic",
85     "ignore",
86     "no_run",
87     "compile_fail",
88     "allow_fail",
89     "test_harness",
90     "edition2015",
91     "edition2018",
92     "edition2021",
93 ];
94
95 fn is_rustdoc_fence_token(token: &str) -> bool {
96     if RUSTDOC_FENCE_TOKENS.contains(&token) {
97         return true;
98     }
99     token.starts_with('E') && token.len() == 5 && token[1..].parse::<u32>().is_ok()
100 }
101
102 /// Injection of syntax highlighting of doctests.
103 pub(super) fn doc_comment(
104     hl: &mut Highlights,
105     sema: &Semantics<RootDatabase>,
106     node: InFile<&SyntaxNode>,
107 ) {
108     let (attributes, def) = match doc_attributes(sema, node.value) {
109         Some(it) => it,
110         None => return,
111     };
112
113     let mut inj = Injector::default();
114     inj.add_unmapped("fn doctest() {\n");
115
116     let attrs_source_map = attributes.source_map(sema.db);
117
118     let mut is_codeblock = false;
119     let mut is_doctest = false;
120
121     // Replace the original, line-spanning comment ranges by new, only comment-prefix
122     // spanning comment ranges.
123     let mut new_comments = Vec::new();
124     let mut string;
125
126     if let Some((docs, doc_mapping)) = attributes.docs_with_rangemap(sema.db) {
127         extract_definitions_from_markdown(docs.as_str())
128             .into_iter()
129             .filter_map(|(range, link, ns)| {
130                 let def = resolve_doc_path_for_def(sema.db, def, &link, ns)?;
131                 let InFile { file_id, value: range } = doc_mapping.map(range)?;
132                 (file_id == node.file_id).then(|| (range, def))
133             })
134             .for_each(|(range, def)| {
135                 hl.add(HlRange {
136                     range,
137                     highlight: module_def_to_hl_tag(def)
138                         | HlMod::Documentation
139                         | HlMod::Injected
140                         | HlMod::IntraDocLink,
141                     binding_hash: None,
142                 })
143             });
144     }
145
146     for attr in attributes.by_key("doc").attrs() {
147         let InFile { file_id, value: src } = attrs_source_map.source_of(&attr);
148         if file_id != node.file_id {
149             continue;
150         }
151         let (line, range, prefix) = match &src {
152             Either::Left(it) => {
153                 string = match find_doc_string_in_attr(attr, it) {
154                     Some(it) => it,
155                     None => continue,
156                 };
157                 let text_range = string.syntax().text_range();
158                 let text_range = TextRange::new(
159                     text_range.start() + TextSize::from(1),
160                     text_range.end() - TextSize::from(1),
161                 );
162                 let text = string.text();
163                 (&text[1..text.len() - 1], text_range, "")
164             }
165             Either::Right(comment) => {
166                 (comment.text(), comment.syntax().text_range(), comment.prefix())
167             }
168         };
169
170         let mut pos = TextSize::from(prefix.len() as u32);
171         let mut range_start = range.start();
172         for line in line.split('\n') {
173             let line_len = TextSize::from(line.len() as u32);
174             let prev_range_start = {
175                 let next_range_start = range_start + line_len + TextSize::from(1);
176                 mem::replace(&mut range_start, next_range_start)
177             };
178             // only first line has the prefix so take it away for future iterations
179             let mut pos = mem::take(&mut pos);
180
181             match line.find(RUSTDOC_FENCE) {
182                 Some(idx) => {
183                     is_codeblock = !is_codeblock;
184                     // Check whether code is rust by inspecting fence guards
185                     let guards = &line[idx + RUSTDOC_FENCE.len()..];
186                     let is_rust = guards.split(',').any(|sub| is_rustdoc_fence_token(sub.trim()));
187                     is_doctest = is_codeblock && is_rust;
188                     continue;
189                 }
190                 None if !is_doctest => continue,
191                 None => (),
192             }
193
194             // whitespace after comment is ignored
195             if let Some(ws) = line[pos.into()..].chars().next().filter(|c| c.is_whitespace()) {
196                 pos += TextSize::of(ws);
197             }
198             // lines marked with `#` should be ignored in output, we skip the `#` char
199             if line[pos.into()..].starts_with('#') {
200                 pos += TextSize::of('#');
201             }
202
203             new_comments.push(TextRange::at(prev_range_start, pos));
204             inj.add(&line[pos.into()..], TextRange::new(pos, line_len) + prev_range_start);
205             inj.add_unmapped("\n");
206         }
207     }
208
209     if new_comments.is_empty() {
210         return; // no need to run an analysis on an empty file
211     }
212
213     inj.add_unmapped("\n}");
214
215     let (analysis, tmp_file_id) = Analysis::from_single_file(inj.text().to_string());
216
217     for HlRange { range, highlight, binding_hash } in
218         analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap()
219     {
220         for range in inj.map_range_up(range) {
221             hl.add(HlRange { range, highlight: highlight | HlMod::Injected, binding_hash });
222         }
223     }
224
225     for range in new_comments {
226         hl.add(HlRange {
227             range,
228             highlight: HlTag::Comment | HlMod::Documentation,
229             binding_hash: None,
230         });
231     }
232 }
233
234 fn find_doc_string_in_attr(attr: &hir::Attr, it: &ast::Attr) -> Option<ast::String> {
235     match it.expr() {
236         // #[doc = lit]
237         Some(ast::Expr::Literal(lit)) => match lit.kind() {
238             ast::LiteralKind::String(it) => Some(it),
239             _ => None,
240         },
241         // #[cfg_attr(..., doc = "", ...)]
242         None => {
243             // We gotta hunt the string token manually here
244             let text = attr.string_value()?;
245             // FIXME: We just pick the first string literal that has the same text as the doc attribute
246             // This means technically we might highlight the wrong one
247             it.syntax()
248                 .descendants_with_tokens()
249                 .filter_map(NodeOrToken::into_token)
250                 .filter_map(ast::String::cast)
251                 .find(|string| {
252                     string.text().get(1..string.text().len() - 1).map_or(false, |it| it == text)
253                 })
254         }
255         _ => return None,
256     }
257 }
258
259 fn module_def_to_hl_tag(def: hir::ModuleDef) -> HlTag {
260     let symbol = match def {
261         hir::ModuleDef::Module(_) => SymbolKind::Module,
262         hir::ModuleDef::Function(_) => SymbolKind::Function,
263         hir::ModuleDef::Adt(hir::Adt::Struct(_)) => SymbolKind::Struct,
264         hir::ModuleDef::Adt(hir::Adt::Enum(_)) => SymbolKind::Enum,
265         hir::ModuleDef::Adt(hir::Adt::Union(_)) => SymbolKind::Union,
266         hir::ModuleDef::Variant(_) => SymbolKind::Variant,
267         hir::ModuleDef::Const(_) => SymbolKind::Const,
268         hir::ModuleDef::Static(_) => SymbolKind::Static,
269         hir::ModuleDef::Trait(_) => SymbolKind::Trait,
270         hir::ModuleDef::TypeAlias(_) => SymbolKind::TypeAlias,
271         hir::ModuleDef::BuiltinType(_) => return HlTag::BuiltinType,
272     };
273     HlTag::Symbol(symbol)
274 }