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