]> git.lizzy.rs Git - rust.git/blob - crates/ide/src/syntax_highlighting/inject.rs
Merge #8097
[rust.git] / crates / ide / src / syntax_highlighting / inject.rs
1 //! "Recursive" Syntax highlighting for code in doctests and fixtures.
2
3 use std::{mem, ops::Range};
4
5 use either::Either;
6 use hir::{HasAttrs, InFile, Semantics};
7 use ide_db::{call_info::ActiveParameter, defs::Definition, SymbolKind};
8 use syntax::{
9     ast::{self, AstNode, AttrsOwner, DocCommentsOwner},
10     match_ast, AstToken, NodeOrToken, SyntaxNode, SyntaxToken, TextRange, TextSize,
11 };
12
13 use crate::{
14     doc_links::extract_definitions_from_markdown, Analysis, HlMod, HlRange, HlTag, RootDatabase,
15 };
16
17 use super::{highlights::Highlights, injector::Injector};
18
19 pub(super) fn ra_fixture(
20     hl: &mut Highlights,
21     sema: &Semantics<RootDatabase>,
22     literal: ast::String,
23     expanded: SyntaxToken,
24 ) -> Option<()> {
25     let active_parameter = ActiveParameter::at_token(&sema, expanded)?;
26     if !active_parameter.name.starts_with("ra_fixture") {
27         return None;
28     }
29     let value = literal.value()?;
30
31     if let Some(range) = literal.open_quote_text_range() {
32         hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
33     }
34
35     let mut inj = Injector::default();
36
37     let mut text = &*value;
38     let mut offset: TextSize = 0.into();
39
40     while !text.is_empty() {
41         let marker = "$0";
42         let idx = text.find(marker).unwrap_or(text.len());
43         let (chunk, next) = text.split_at(idx);
44         inj.add(chunk, TextRange::at(offset, TextSize::of(chunk)));
45
46         text = next;
47         offset += TextSize::of(chunk);
48
49         if let Some(next) = text.strip_prefix(marker) {
50             if let Some(range) = literal.map_range_up(TextRange::at(offset, TextSize::of(marker))) {
51                 hl.add(HlRange { range, highlight: HlTag::Keyword.into(), binding_hash: None });
52             }
53
54             text = next;
55
56             let marker_len = TextSize::of(marker);
57             offset += marker_len;
58         }
59     }
60
61     let (analysis, tmp_file_id) = Analysis::from_single_file(inj.text().to_string());
62
63     for mut hl_range in analysis.highlight(tmp_file_id).unwrap() {
64         for range in inj.map_range_up(hl_range.range) {
65             if let Some(range) = literal.map_range_up(range) {
66                 hl_range.range = range;
67                 hl.add(hl_range);
68             }
69         }
70     }
71
72     if let Some(range) = literal.close_quote_text_range() {
73         hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
74     }
75
76     Some(())
77 }
78
79 const RUSTDOC_FENCE: &'static str = "```";
80 const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[
81     "",
82     "rust",
83     "should_panic",
84     "ignore",
85     "no_run",
86     "compile_fail",
87     "edition2015",
88     "edition2018",
89     "edition2021",
90 ];
91
92 // Basically an owned dyn AttrsOwner without extra Boxing
93 struct AttrsOwnerNode {
94     node: SyntaxNode,
95 }
96
97 impl AttrsOwnerNode {
98     fn new<N: DocCommentsOwner>(node: N) -> Self {
99         AttrsOwnerNode { node: node.syntax().clone() }
100     }
101 }
102
103 impl AttrsOwner for AttrsOwnerNode {}
104 impl AstNode for AttrsOwnerNode {
105     fn can_cast(_: syntax::SyntaxKind) -> bool
106     where
107         Self: Sized,
108     {
109         false
110     }
111     fn cast(_: SyntaxNode) -> Option<Self>
112     where
113         Self: Sized,
114     {
115         None
116     }
117     fn syntax(&self) -> &SyntaxNode {
118         &self.node
119     }
120 }
121
122 fn doc_attributes<'node>(
123     sema: &Semantics<RootDatabase>,
124     node: &'node SyntaxNode,
125 ) -> Option<(AttrsOwnerNode, hir::Attrs, Definition)> {
126     match_ast! {
127         match node {
128             ast::SourceFile(it)  => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Module(def)))),
129             ast::Module(it)      => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Module(def)))),
130             ast::Fn(it)          => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Function(def)))),
131             ast::Struct(it)      => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Struct(def))))),
132             ast::Union(it)       => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Union(def))))),
133             ast::Enum(it)        => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(def))))),
134             ast::Variant(it)     => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Variant(def)))),
135             ast::Trait(it)       => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Trait(def)))),
136             ast::Static(it)      => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Static(def)))),
137             ast::Const(it)       => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Const(def)))),
138             ast::TypeAlias(it)   => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::TypeAlias(def)))),
139             ast::Impl(it)        => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db), Definition::SelfType(def))),
140             ast::RecordField(it) => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db), Definition::Field(def))),
141             ast::TupleField(it)  => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db), Definition::Field(def))),
142             ast::MacroRules(it)  => sema.to_def(&it).map(|def| (AttrsOwnerNode::new(it), def.attrs(sema.db), Definition::Macro(def))),
143             // ast::MacroDef(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
144             // ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
145             _ => return None
146         }
147     }
148 }
149
150 /// Injection of syntax highlighting of doctests.
151 pub(super) fn doc_comment(
152     hl: &mut Highlights,
153     sema: &Semantics<RootDatabase>,
154     node: InFile<&SyntaxNode>,
155 ) {
156     let (owner, attributes, def) = match doc_attributes(sema, node.value) {
157         Some(it) => it,
158         None => return,
159     };
160
161     let mut inj = Injector::default();
162     inj.add_unmapped("fn doctest() {\n");
163
164     let attrs_source_map = match def {
165         Definition::ModuleDef(hir::ModuleDef::Module(module)) => {
166             attributes.source_map_for_module(sema.db, module.into())
167         }
168         _ => attributes.source_map(node.with_value(&owner)),
169     };
170
171     let mut is_codeblock = false;
172     let mut is_doctest = false;
173
174     // Replace the original, line-spanning comment ranges by new, only comment-prefix
175     // spanning comment ranges.
176     let mut new_comments = Vec::new();
177     let mut intra_doc_links = Vec::new();
178     let mut string;
179     for attr in attributes.by_key("doc").attrs() {
180         let InFile { file_id, value: src } = attrs_source_map.source_of(&attr);
181         if file_id != node.file_id {
182             continue;
183         }
184         let (line, range, prefix) = match &src {
185             Either::Left(it) => {
186                 string = match find_doc_string_in_attr(attr, it) {
187                     Some(it) => it,
188                     None => continue,
189                 };
190                 let text_range = string.syntax().text_range();
191                 let text_range = TextRange::new(
192                     text_range.start() + TextSize::from(1),
193                     text_range.end() - TextSize::from(1),
194                 );
195                 let text = string.text();
196                 (&text[1..text.len() - 1], text_range, "")
197             }
198             Either::Right(comment) => {
199                 (comment.text(), comment.syntax().text_range(), comment.prefix())
200             }
201         };
202
203         let mut pos = TextSize::from(prefix.len() as u32);
204         let mut range_start = range.start();
205         for line in line.split('\n') {
206             let line_len = TextSize::from(line.len() as u32);
207             let prev_range_start = {
208                 let next_range_start = range_start + line_len + TextSize::from(1);
209                 mem::replace(&mut range_start, next_range_start)
210             };
211             // only first line has the prefix so take it away for future iterations
212             let mut pos = mem::take(&mut pos);
213
214             match line.find(RUSTDOC_FENCE) {
215                 Some(idx) => {
216                     is_codeblock = !is_codeblock;
217                     // Check whether code is rust by inspecting fence guards
218                     let guards = &line[idx + RUSTDOC_FENCE.len()..];
219                     let is_rust =
220                         guards.split(',').all(|sub| RUSTDOC_FENCE_TOKENS.contains(&sub.trim()));
221                     is_doctest = is_codeblock && is_rust;
222                     continue;
223                 }
224                 None if !is_doctest => {
225                     intra_doc_links.extend(
226                         extract_definitions_from_markdown(line)
227                             .into_iter()
228                             .filter_map(|(link, ns, range)| {
229                                 validate_intra_doc_link(sema.db, &def, &link, ns).zip(Some(range))
230                             })
231                             .map(|(def, Range { start, end })| {
232                                 (
233                                     def,
234                                     TextRange::at(
235                                         prev_range_start + TextSize::from(start as u32),
236                                         TextSize::from((end - start) as u32),
237                                     ),
238                                 )
239                             }),
240                     );
241                     continue;
242                 }
243                 None => (),
244             }
245
246             // whitespace after comment is ignored
247             if let Some(ws) = line[pos.into()..].chars().next().filter(|c| c.is_whitespace()) {
248                 pos += TextSize::of(ws);
249             }
250             // lines marked with `#` should be ignored in output, we skip the `#` char
251             if line[pos.into()..].starts_with('#') {
252                 pos += TextSize::of('#');
253             }
254
255             new_comments.push(TextRange::at(prev_range_start, pos));
256             inj.add(&line[pos.into()..], TextRange::new(pos, line_len) + prev_range_start);
257             inj.add_unmapped("\n");
258         }
259     }
260
261     for (def, range) in intra_doc_links {
262         hl.add(HlRange {
263             range,
264             highlight: module_def_to_hl_tag(def)
265                 | HlMod::Documentation
266                 | HlMod::Injected
267                 | HlMod::IntraDocLink,
268             binding_hash: None,
269         });
270     }
271
272     if new_comments.is_empty() {
273         return; // no need to run an analysis on an empty file
274     }
275
276     inj.add_unmapped("\n}");
277
278     let (analysis, tmp_file_id) = Analysis::from_single_file(inj.text().to_string());
279
280     for HlRange { range, highlight, binding_hash } in
281         analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap()
282     {
283         for range in inj.map_range_up(range) {
284             hl.add(HlRange { range, highlight: highlight | HlMod::Injected, binding_hash });
285         }
286     }
287
288     for range in new_comments {
289         hl.add(HlRange {
290             range,
291             highlight: HlTag::Comment | HlMod::Documentation,
292             binding_hash: None,
293         });
294     }
295 }
296
297 fn find_doc_string_in_attr(attr: &hir::Attr, it: &ast::Attr) -> Option<ast::String> {
298     match it.expr() {
299         // #[doc = lit]
300         Some(ast::Expr::Literal(lit)) => match lit.kind() {
301             ast::LiteralKind::String(it) => Some(it),
302             _ => None,
303         },
304         // #[cfg_attr(..., doc = "", ...)]
305         None => {
306             // We gotta hunt the string token manually here
307             let text = attr.string_value()?;
308             // FIXME: We just pick the first string literal that has the same text as the doc attribute
309             // This means technically we might highlight the wrong one
310             it.syntax()
311                 .descendants_with_tokens()
312                 .filter_map(NodeOrToken::into_token)
313                 .filter_map(ast::String::cast)
314                 .find(|string| {
315                     string.text().get(1..string.text().len() - 1).map_or(false, |it| it == text)
316                 })
317         }
318         _ => return None,
319     }
320 }
321
322 fn validate_intra_doc_link(
323     db: &RootDatabase,
324     def: &Definition,
325     link: &str,
326     ns: Option<hir::Namespace>,
327 ) -> Option<hir::ModuleDef> {
328     match def {
329         Definition::ModuleDef(def) => match def {
330             hir::ModuleDef::Module(it) => it.resolve_doc_path(db, &link, ns),
331             hir::ModuleDef::Function(it) => it.resolve_doc_path(db, &link, ns),
332             hir::ModuleDef::Adt(it) => it.resolve_doc_path(db, &link, ns),
333             hir::ModuleDef::Variant(it) => it.resolve_doc_path(db, &link, ns),
334             hir::ModuleDef::Const(it) => it.resolve_doc_path(db, &link, ns),
335             hir::ModuleDef::Static(it) => it.resolve_doc_path(db, &link, ns),
336             hir::ModuleDef::Trait(it) => it.resolve_doc_path(db, &link, ns),
337             hir::ModuleDef::TypeAlias(it) => it.resolve_doc_path(db, &link, ns),
338             hir::ModuleDef::BuiltinType(_) => None,
339         },
340         Definition::Macro(it) => it.resolve_doc_path(db, &link, ns),
341         Definition::Field(it) => it.resolve_doc_path(db, &link, ns),
342         Definition::SelfType(_)
343         | Definition::Local(_)
344         | Definition::GenericParam(_)
345         | Definition::Label(_) => None,
346     }
347 }
348
349 fn module_def_to_hl_tag(def: hir::ModuleDef) -> HlTag {
350     let symbol = match def {
351         hir::ModuleDef::Module(_) => SymbolKind::Module,
352         hir::ModuleDef::Function(_) => SymbolKind::Function,
353         hir::ModuleDef::Adt(hir::Adt::Struct(_)) => SymbolKind::Struct,
354         hir::ModuleDef::Adt(hir::Adt::Enum(_)) => SymbolKind::Enum,
355         hir::ModuleDef::Adt(hir::Adt::Union(_)) => SymbolKind::Union,
356         hir::ModuleDef::Variant(_) => SymbolKind::Variant,
357         hir::ModuleDef::Const(_) => SymbolKind::Const,
358         hir::ModuleDef::Static(_) => SymbolKind::Static,
359         hir::ModuleDef::Trait(_) => SymbolKind::Trait,
360         hir::ModuleDef::TypeAlias(_) => SymbolKind::TypeAlias,
361         hir::ModuleDef::BuiltinType(_) => return HlTag::BuiltinType,
362     };
363     HlTag::Symbol(symbol)
364 }