1 //! "Recursive" Syntax highlighting for code in doctests and fixtures.
6 use hir::{InFile, Semantics};
8 active_parameter::ActiveParameter, base_db::FileId, defs::Definition, rust_doc::is_rust_fence,
12 ast::{self, AstNode, IsString, QuoteOffsets},
13 AstToken, NodeOrToken, SyntaxNode, TextRange, TextSize,
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,
22 pub(super) fn ra_fixture(
24 sema: &Semantics<'_, RootDatabase>,
25 config: HighlightConfig,
26 literal: &ast::String,
27 expanded: &ast::String,
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")) {
33 let value = literal.value()?;
35 if let Some(range) = literal.open_quote_text_range() {
36 hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
39 let mut inj = Injector::default();
41 let mut text = &*value;
42 let mut offset: TextSize = 0.into();
44 while !text.is_empty() {
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)));
51 offset += TextSize::of(chunk);
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 });
60 let marker_len = TextSize::of(marker);
65 let (analysis, tmp_file_id) = Analysis::from_single_file(inj.take_text());
67 for mut hl_range in analysis
69 HighlightConfig { syntactic_name_ref_highlighting: false, ..config },
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;
82 if let Some(range) = literal.close_quote_text_range() {
83 hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
89 const RUSTDOC_FENCE_LENGTH: usize = 3;
90 const RUSTDOC_FENCES: [&str; 2] = ["```", "~~~"];
92 /// Injection of syntax highlighting of doctests and intra doc links.
93 pub(super) fn doc_comment(
95 sema: &Semantics<'_, RootDatabase>,
96 config: HighlightConfig,
100 let (attributes, def) = match doc_attributes(sema, node) {
104 let src_file_id = src_file_id.into();
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)
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))
117 .for_each(|(range, def)| {
120 highlight: module_def_to_hl_tag(def)
121 | HlMod::Documentation
123 | HlMod::IntraDocLink,
129 // Extract doc-test sources from the docs and calculate highlighting for them.
131 let mut inj = Injector::default();
132 inj.add_unmapped("fn doctest() {\n");
134 let attrs_source_map = attributes.source_map(sema.db);
136 let mut is_codeblock = false;
137 let mut is_doctest = false;
139 let mut new_comments = Vec::new();
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 {
147 let (line, range) = match &src {
148 Either::Left(it) => {
149 string = match find_doc_string_in_attr(attr, it) {
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)
159 None => (text, text_range),
162 Either::Right(comment) => {
163 let value = comment.prefix().len();
164 let range = comment.syntax().text_range();
166 &comment.text()[value..],
167 TextRange::new(range.start() + TextSize::try_from(value).unwrap(), range.end()),
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)
179 let mut pos = TextSize::from(0);
181 match RUSTDOC_FENCES.into_iter().find_map(|fence| line.find(fence)) {
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;
190 None if !is_doctest => continue,
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);
198 // lines marked with `#` should be ignored in output, we skip the `#` char
199 if line[pos.into()..].starts_with('#') {
200 pos += TextSize::of('#');
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");
209 if new_comments.is_empty() {
210 return; // no need to run an analysis on an empty file
213 inj.add_unmapped("\n}");
215 let (analysis, tmp_file_id) = Analysis::from_single_file(inj.take_text());
217 if let Ok(ranges) = analysis.with_db(|db| {
220 HighlightConfig { syntactic_name_ref_highlighting: true, ..config },
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 });
232 for range in new_comments {
235 highlight: HlTag::Comment | HlMod::Documentation,
241 fn find_doc_string_in_attr(attr: &hir::Attr, it: &ast::Attr) -> Option<ast::String> {
244 Some(ast::Expr::Literal(lit)) => match lit.kind() {
245 ast::LiteralKind::String(it) => Some(it),
248 // #[cfg_attr(..., doc = "", ...)]
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
255 .descendants_with_tokens()
256 .filter_map(NodeOrToken::into_token)
257 .filter_map(ast::String::cast)
259 string.text().get(1..string.text().len() - 1).map_or(false, |it| it == text)
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,
288 Definition::Label(_) => SymbolKind::Label,
289 Definition::BuiltinAttr(_) => SymbolKind::BuiltinAttr,
290 Definition::ToolModule(_) => SymbolKind::ToolModule,
291 Definition::DeriveHelper(_) => SymbolKind::DeriveHelper,
293 HlTag::Symbol(symbol)