]> git.proxmox.com Git - rustc.git/blob - src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/inject.rs
New upstream version 1.74.1+dfsg1
[rustc.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,
9 documentation::docs_with_rangemap, rust_doc::is_rust_fence, 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 {
56 range,
57 highlight: HlTag::Keyword | HlMod::Injected,
58 binding_hash: None,
59 });
60 }
61
62 text = next;
63
64 let marker_len = TextSize::of(marker);
65 offset += marker_len;
66 }
67 }
68
69 let (analysis, tmp_file_id) = Analysis::from_single_file(inj.take_text());
70
71 for mut hl_range in analysis
72 .highlight(
73 HighlightConfig {
74 syntactic_name_ref_highlighting: false,
75 punctuation: true,
76 operator: true,
77 strings: true,
78 specialize_punctuation: config.specialize_punctuation,
79 specialize_operator: config.operator,
80 inject_doc_comment: config.inject_doc_comment,
81 macro_bang: config.macro_bang,
82 },
83 tmp_file_id,
84 )
85 .unwrap()
86 {
87 for range in inj.map_range_up(hl_range.range) {
88 if let Some(range) = literal.map_range_up(range) {
89 hl_range.range = range;
90 hl_range.highlight |= HlMod::Injected;
91 hl.add(hl_range);
92 }
93 }
94 }
95
96 if let Some(range) = literal.close_quote_text_range() {
97 hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
98 }
99
100 Some(())
101 }
102
103 const RUSTDOC_FENCE_LENGTH: usize = 3;
104 const RUSTDOC_FENCES: [&str; 2] = ["```", "~~~"];
105
106 /// Injection of syntax highlighting of doctests and intra doc links.
107 pub(super) fn doc_comment(
108 hl: &mut Highlights,
109 sema: &Semantics<'_, RootDatabase>,
110 config: HighlightConfig,
111 src_file_id: FileId,
112 node: &SyntaxNode,
113 ) {
114 let (attributes, def) = match doc_attributes(sema, node) {
115 Some(it) => it,
116 None => return,
117 };
118 let src_file_id = src_file_id.into();
119
120 // Extract intra-doc links and emit highlights for them.
121 if let Some((docs, doc_mapping)) = docs_with_rangemap(sema.db, &attributes) {
122 extract_definitions_from_docs(&docs)
123 .into_iter()
124 .filter_map(|(range, link, ns)| {
125 doc_mapping.map(range).filter(|mapping| mapping.file_id == src_file_id).and_then(
126 |InFile { value: mapped_range, .. }| {
127 Some(mapped_range).zip(resolve_doc_path_for_def(sema.db, def, &link, ns))
128 },
129 )
130 })
131 .for_each(|(range, def)| {
132 hl.add(HlRange {
133 range,
134 highlight: module_def_to_hl_tag(def)
135 | HlMod::Documentation
136 | HlMod::Injected
137 | HlMod::IntraDocLink,
138 binding_hash: None,
139 })
140 });
141 }
142
143 // Extract doc-test sources from the docs and calculate highlighting for them.
144
145 let mut inj = Injector::default();
146 inj.add_unmapped("fn doctest() {\n");
147
148 let attrs_source_map = attributes.source_map(sema.db);
149
150 let mut is_codeblock = false;
151 let mut is_doctest = false;
152
153 let mut new_comments = Vec::new();
154 let mut string;
155
156 for attr in attributes.by_key("doc").attrs() {
157 let InFile { file_id, value: src } = attrs_source_map.source_of(attr);
158 if file_id != src_file_id {
159 continue;
160 }
161 let (line, range) = match &src {
162 Either::Left(it) => {
163 string = match find_doc_string_in_attr(attr, it) {
164 Some(it) => it,
165 None => continue,
166 };
167 let text = string.text();
168 let text_range = string.syntax().text_range();
169 match string.quote_offsets() {
170 Some(QuoteOffsets { contents, .. }) => {
171 (&text[contents - text_range.start()], contents)
172 }
173 None => (text, text_range),
174 }
175 }
176 Either::Right(comment) => {
177 let value = comment.prefix().len();
178 let range = comment.syntax().text_range();
179 (
180 &comment.text()[value..],
181 TextRange::new(range.start() + TextSize::try_from(value).unwrap(), range.end()),
182 )
183 }
184 };
185
186 let mut range_start = range.start();
187 for line in line.split('\n') {
188 let line_len = TextSize::from(line.len() as u32);
189 let prev_range_start = {
190 let next_range_start = range_start + line_len + TextSize::from(1);
191 mem::replace(&mut range_start, next_range_start)
192 };
193 let mut pos = TextSize::from(0);
194
195 match RUSTDOC_FENCES.into_iter().find_map(|fence| line.find(fence)) {
196 Some(idx) => {
197 is_codeblock = !is_codeblock;
198 // Check whether code is rust by inspecting fence guards
199 let guards = &line[idx + RUSTDOC_FENCE_LENGTH..];
200 let is_rust = is_rust_fence(guards);
201 is_doctest = is_codeblock && is_rust;
202 continue;
203 }
204 None if !is_doctest => continue,
205 None => (),
206 }
207
208 // whitespace after comment is ignored
209 if let Some(ws) = line[pos.into()..].chars().next().filter(|c| c.is_whitespace()) {
210 pos += TextSize::of(ws);
211 }
212 // lines marked with `#` should be ignored in output, we skip the `#` char
213 if line[pos.into()..].starts_with('#') {
214 pos += TextSize::of('#');
215 }
216
217 new_comments.push(TextRange::at(prev_range_start, pos));
218 inj.add(&line[pos.into()..], TextRange::new(pos, line_len) + prev_range_start);
219 inj.add_unmapped("\n");
220 }
221 }
222
223 if new_comments.is_empty() {
224 return; // no need to run an analysis on an empty file
225 }
226
227 inj.add_unmapped("\n}");
228
229 let (analysis, tmp_file_id) = Analysis::from_single_file(inj.take_text());
230
231 if let Ok(ranges) = analysis.with_db(|db| {
232 super::highlight(
233 db,
234 HighlightConfig {
235 syntactic_name_ref_highlighting: true,
236 punctuation: true,
237 operator: true,
238 strings: true,
239 specialize_punctuation: config.specialize_punctuation,
240 specialize_operator: config.operator,
241 inject_doc_comment: config.inject_doc_comment,
242 macro_bang: config.macro_bang,
243 },
244 tmp_file_id,
245 None,
246 )
247 }) {
248 for HlRange { range, highlight, binding_hash } in ranges {
249 for range in inj.map_range_up(range) {
250 hl.add(HlRange { range, highlight: highlight | HlMod::Injected, binding_hash });
251 }
252 }
253 }
254
255 for range in new_comments {
256 hl.add(HlRange {
257 range,
258 highlight: HlTag::Comment | HlMod::Documentation,
259 binding_hash: None,
260 });
261 }
262 }
263
264 fn find_doc_string_in_attr(attr: &hir::Attr, it: &ast::Attr) -> Option<ast::String> {
265 match it.expr() {
266 // #[doc = lit]
267 Some(ast::Expr::Literal(lit)) => match lit.kind() {
268 ast::LiteralKind::String(it) => Some(it),
269 _ => None,
270 },
271 // #[cfg_attr(..., doc = "", ...)]
272 None => {
273 // We gotta hunt the string token manually here
274 let text = attr.string_value()?;
275 // FIXME: We just pick the first string literal that has the same text as the doc attribute
276 // This means technically we might highlight the wrong one
277 it.syntax()
278 .descendants_with_tokens()
279 .filter_map(NodeOrToken::into_token)
280 .filter_map(ast::String::cast)
281 .find(|string| {
282 string.text().get(1..string.text().len() - 1).map_or(false, |it| it == text)
283 })
284 }
285 _ => None,
286 }
287 }
288
289 fn module_def_to_hl_tag(def: Definition) -> HlTag {
290 let symbol = match def {
291 Definition::Module(_) | Definition::ExternCrateDecl(_) => SymbolKind::Module,
292 Definition::Function(_) => SymbolKind::Function,
293 Definition::Adt(hir::Adt::Struct(_)) => SymbolKind::Struct,
294 Definition::Adt(hir::Adt::Enum(_)) => SymbolKind::Enum,
295 Definition::Adt(hir::Adt::Union(_)) => SymbolKind::Union,
296 Definition::Variant(_) => SymbolKind::Variant,
297 Definition::Const(_) => SymbolKind::Const,
298 Definition::Static(_) => SymbolKind::Static,
299 Definition::Trait(_) => SymbolKind::Trait,
300 Definition::TraitAlias(_) => SymbolKind::TraitAlias,
301 Definition::TypeAlias(_) => SymbolKind::TypeAlias,
302 Definition::BuiltinType(_) => return HlTag::BuiltinType,
303 Definition::Macro(_) => SymbolKind::Macro,
304 Definition::Field(_) => SymbolKind::Field,
305 Definition::SelfType(_) => SymbolKind::Impl,
306 Definition::Local(_) => SymbolKind::Local,
307 Definition::GenericParam(gp) => match gp {
308 hir::GenericParam::TypeParam(_) => SymbolKind::TypeParam,
309 hir::GenericParam::ConstParam(_) => SymbolKind::ConstParam,
310 hir::GenericParam::LifetimeParam(_) => SymbolKind::LifetimeParam,
311 },
312 Definition::Label(_) => SymbolKind::Label,
313 Definition::BuiltinAttr(_) => SymbolKind::BuiltinAttr,
314 Definition::ToolModule(_) => SymbolKind::ToolModule,
315 Definition::DeriveHelper(_) => SymbolKind::DeriveHelper,
316 };
317 HlTag::Symbol(symbol)
318 }