]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/inject.rs
Auto merge of #93873 - Stovent:big-ints, r=m-ou-se
[rust.git] / src / tools / rust-analyzer / 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::{
8     active_parameter::ActiveParameter, base_db::FileId, defs::Definition, rust_doc::is_rust_fence,
9     SymbolKind,
10 };
11 use syntax::{
12     ast::{self, AstNode, IsString, QuoteOffsets},
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, HighlightConfig},
19     Analysis, HlMod, HlRange, HlTag, RootDatabase,
20 };
21
22 pub(super) fn ra_fixture(
23     hl: &mut Highlights,
24     sema: &Semantics<'_, RootDatabase>,
25     config: HighlightConfig,
26     literal: &ast::String,
27     expanded: &ast::String,
28 ) -> Option<()> {
29     let active_parameter = ActiveParameter::at_token(sema, expanded.syntax().clone())?;
30     if !active_parameter.ident().map_or(false, |name| name.text().starts_with("ra_fixture")) {
31         return None;
32     }
33     let value = literal.value()?;
34
35     if let Some(range) = literal.open_quote_text_range() {
36         hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
37     }
38
39     let mut inj = Injector::default();
40
41     let mut text = &*value;
42     let mut offset: TextSize = 0.into();
43
44     while !text.is_empty() {
45         let marker = "$0";
46         let idx = text.find(marker).unwrap_or(text.len());
47         let (chunk, next) = text.split_at(idx);
48         inj.add(chunk, TextRange::at(offset, TextSize::of(chunk)));
49
50         text = next;
51         offset += TextSize::of(chunk);
52
53         if let Some(next) = text.strip_prefix(marker) {
54             if let Some(range) = literal.map_range_up(TextRange::at(offset, TextSize::of(marker))) {
55                 hl.add(HlRange { range, highlight: HlTag::Keyword.into(), binding_hash: None });
56             }
57
58             text = next;
59
60             let marker_len = TextSize::of(marker);
61             offset += marker_len;
62         }
63     }
64
65     let (analysis, tmp_file_id) = Analysis::from_single_file(inj.take_text());
66
67     for mut hl_range in analysis
68         .highlight(
69             HighlightConfig { syntactic_name_ref_highlighting: false, ..config },
70             tmp_file_id,
71         )
72         .unwrap()
73     {
74         for range in inj.map_range_up(hl_range.range) {
75             if let Some(range) = literal.map_range_up(range) {
76                 hl_range.range = range;
77                 hl.add(hl_range);
78             }
79         }
80     }
81
82     if let Some(range) = literal.close_quote_text_range() {
83         hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
84     }
85
86     Some(())
87 }
88
89 const RUSTDOC_FENCE_LENGTH: usize = 3;
90 const RUSTDOC_FENCES: [&str; 2] = ["```", "~~~"];
91
92 /// Injection of syntax highlighting of doctests and intra doc links.
93 pub(super) fn doc_comment(
94     hl: &mut Highlights,
95     sema: &Semantics<'_, RootDatabase>,
96     config: HighlightConfig,
97     src_file_id: FileId,
98     node: &SyntaxNode,
99 ) {
100     let (attributes, def) = match doc_attributes(sema, node) {
101         Some(it) => it,
102         None => return,
103     };
104     let src_file_id = src_file_id.into();
105
106     // Extract intra-doc links and emit highlights for them.
107     if let Some((docs, doc_mapping)) = attributes.docs_with_rangemap(sema.db) {
108         extract_definitions_from_docs(&docs)
109             .into_iter()
110             .filter_map(|(range, link, ns)| {
111                 doc_mapping.map(range).filter(|mapping| mapping.file_id == src_file_id).and_then(
112                     |InFile { value: mapped_range, .. }| {
113                         Some(mapped_range).zip(resolve_doc_path_for_def(sema.db, def, &link, ns))
114                     },
115                 )
116             })
117             .for_each(|(range, def)| {
118                 hl.add(HlRange {
119                     range,
120                     highlight: module_def_to_hl_tag(def)
121                         | HlMod::Documentation
122                         | HlMod::Injected
123                         | HlMod::IntraDocLink,
124                     binding_hash: None,
125                 })
126             });
127     }
128
129     // Extract doc-test sources from the docs and calculate highlighting for them.
130
131     let mut inj = Injector::default();
132     inj.add_unmapped("fn doctest() {\n");
133
134     let attrs_source_map = attributes.source_map(sema.db);
135
136     let mut is_codeblock = false;
137     let mut is_doctest = false;
138
139     let mut new_comments = Vec::new();
140     let mut string;
141
142     for attr in attributes.by_key("doc").attrs() {
143         let InFile { file_id, value: src } = attrs_source_map.source_of(attr);
144         if file_id != src_file_id {
145             continue;
146         }
147         let (line, range) = match &src {
148             Either::Left(it) => {
149                 string = match find_doc_string_in_attr(attr, it) {
150                     Some(it) => it,
151                     None => continue,
152                 };
153                 let text = string.text();
154                 let text_range = string.syntax().text_range();
155                 match string.quote_offsets() {
156                     Some(QuoteOffsets { contents, .. }) => {
157                         (&text[contents - text_range.start()], contents)
158                     }
159                     None => (text, text_range),
160                 }
161             }
162             Either::Right(comment) => {
163                 let value = comment.prefix().len();
164                 let range = comment.syntax().text_range();
165                 (
166                     &comment.text()[value..],
167                     TextRange::new(range.start() + TextSize::try_from(value).unwrap(), range.end()),
168                 )
169             }
170         };
171
172         let mut range_start = range.start();
173         for line in line.split('\n') {
174             let line_len = TextSize::from(line.len() as u32);
175             let prev_range_start = {
176                 let next_range_start = range_start + line_len + TextSize::from(1);
177                 mem::replace(&mut range_start, next_range_start)
178             };
179             let mut pos = TextSize::from(0);
180
181             match RUSTDOC_FENCES.into_iter().find_map(|fence| line.find(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_LENGTH..];
186                     let is_rust = is_rust_fence(guards);
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.take_text());
216
217     if let Ok(ranges) = analysis.with_db(|db| {
218         super::highlight(
219             db,
220             HighlightConfig { syntactic_name_ref_highlighting: true, ..config },
221             tmp_file_id,
222             None,
223         )
224     }) {
225         for HlRange { range, highlight, binding_hash } in ranges {
226             for range in inj.map_range_up(range) {
227                 hl.add(HlRange { range, highlight: highlight | HlMod::Injected, binding_hash });
228             }
229         }
230     }
231
232     for range in new_comments {
233         hl.add(HlRange {
234             range,
235             highlight: HlTag::Comment | HlMod::Documentation,
236             binding_hash: None,
237         });
238     }
239 }
240
241 fn find_doc_string_in_attr(attr: &hir::Attr, it: &ast::Attr) -> Option<ast::String> {
242     match it.expr() {
243         // #[doc = lit]
244         Some(ast::Expr::Literal(lit)) => match lit.kind() {
245             ast::LiteralKind::String(it) => Some(it),
246             _ => None,
247         },
248         // #[cfg_attr(..., doc = "", ...)]
249         None => {
250             // We gotta hunt the string token manually here
251             let text = attr.string_value()?;
252             // FIXME: We just pick the first string literal that has the same text as the doc attribute
253             // This means technically we might highlight the wrong one
254             it.syntax()
255                 .descendants_with_tokens()
256                 .filter_map(NodeOrToken::into_token)
257                 .filter_map(ast::String::cast)
258                 .find(|string| {
259                     string.text().get(1..string.text().len() - 1).map_or(false, |it| it == text)
260                 })
261         }
262         _ => None,
263     }
264 }
265
266 fn module_def_to_hl_tag(def: Definition) -> HlTag {
267     let symbol = match def {
268         Definition::Module(_) => SymbolKind::Module,
269         Definition::Function(_) => SymbolKind::Function,
270         Definition::Adt(hir::Adt::Struct(_)) => SymbolKind::Struct,
271         Definition::Adt(hir::Adt::Enum(_)) => SymbolKind::Enum,
272         Definition::Adt(hir::Adt::Union(_)) => SymbolKind::Union,
273         Definition::Variant(_) => SymbolKind::Variant,
274         Definition::Const(_) => SymbolKind::Const,
275         Definition::Static(_) => SymbolKind::Static,
276         Definition::Trait(_) => SymbolKind::Trait,
277         Definition::TypeAlias(_) => SymbolKind::TypeAlias,
278         Definition::BuiltinType(_) => return HlTag::BuiltinType,
279         Definition::Macro(_) => SymbolKind::Macro,
280         Definition::Field(_) => SymbolKind::Field,
281         Definition::SelfType(_) => SymbolKind::Impl,
282         Definition::Local(_) => SymbolKind::Local,
283         Definition::GenericParam(gp) => match gp {
284             hir::GenericParam::TypeParam(_) => SymbolKind::TypeParam,
285             hir::GenericParam::ConstParam(_) => SymbolKind::ConstParam,
286             hir::GenericParam::LifetimeParam(_) => SymbolKind::LifetimeParam,
287         },
288         Definition::Label(_) => SymbolKind::Label,
289         Definition::BuiltinAttr(_) => SymbolKind::BuiltinAttr,
290         Definition::ToolModule(_) => SymbolKind::ToolModule,
291         Definition::DeriveHelper(_) => SymbolKind::DeriveHelper,
292     };
293     HlTag::Symbol(symbol)
294 }