Skip to main content

ide/syntax_highlighting/
inject.rs

1//! "Recursive" Syntax highlighting for code in doctests and fixtures.
2
3use hir::{EditionedFileId, HirFileId, InFile, Semantics, db::HirDatabase};
4use ide_db::{
5    SymbolKind, defs::Definition, documentation::Documentation, range_mapper::RangeMapper,
6    rust_doc::is_rust_fence,
7};
8use syntax::{
9    SyntaxNode, TextRange, TextSize,
10    ast::{self, IsString},
11};
12use triomphe::Arc;
13
14use crate::{
15    Analysis, HlMod, HlRange, HlTag, RootDatabase,
16    doc_links::{doc_attributes, extract_definitions_from_docs, resolve_doc_path_for_def},
17    syntax_highlighting::{HighlightConfig, highlights::Highlights},
18};
19
20pub(super) fn ra_fixture(
21    hl: &mut Highlights,
22    sema: &Semantics<'_, RootDatabase>,
23    config: &HighlightConfig<'_>,
24    literal: &ast::String,
25    expanded: &ast::String,
26) -> Option<()> {
27    let (analysis, fixture_analysis) = Analysis::from_ra_fixture_with_on_cursor(
28        sema,
29        literal.clone(),
30        expanded,
31        &config.ra_fixture,
32        &mut |range| {
33            hl.add(HlRange {
34                range,
35                highlight: HlTag::Keyword | HlMod::Injected,
36                binding_hash: None,
37            });
38        },
39    )?;
40
41    if let Some(range) = literal.open_quote_text_range() {
42        hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
43    }
44
45    for tmp_file_id in fixture_analysis.files() {
46        for mut hl_range in analysis
47            .highlight(
48                HighlightConfig {
49                    syntactic_name_ref_highlighting: false,
50                    comments: true,
51                    punctuation: true,
52                    operator: true,
53                    strings: true,
54                    specialize_punctuation: config.specialize_punctuation,
55                    specialize_operator: config.operator,
56                    inject_doc_comment: config.inject_doc_comment,
57                    macro_bang: config.macro_bang,
58                    // What if there is a fixture inside a fixture? It's fixtures all the way down.
59                    // (In fact, we have a fixture inside a fixture in our test suite!)
60                    ra_fixture: config.ra_fixture,
61                },
62                tmp_file_id,
63            )
64            .unwrap()
65        {
66            for range in fixture_analysis.map_range_up(tmp_file_id, hl_range.range) {
67                hl_range.range = range;
68                hl_range.highlight |= HlMod::Injected;
69                hl.add(hl_range);
70            }
71        }
72    }
73
74    if let Some(range) = literal.close_quote_text_range() {
75        hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
76    }
77
78    Some(())
79}
80
81const RUSTDOC_FENCE_LENGTH: usize = 3;
82const RUSTDOC_FENCES: [&str; 2] = ["```", "~~~"];
83
84/// Injection of syntax highlighting of doctests and intra doc links.
85pub(super) fn doc_comment(
86    hl: &mut Highlights,
87    sema: &Semantics<'_, RootDatabase>,
88    config: &HighlightConfig<'_>,
89    src_file_id: EditionedFileId,
90    node: &SyntaxNode,
91) {
92    let (attributes, def) = match doc_attributes(sema, node) {
93        Some(it) => it,
94        None => return,
95    };
96    let vfs_file_id = src_file_id.file_id(sema.db);
97    let src_file_id: HirFileId = src_file_id.into();
98    let Some(docs) = attributes.hir_docs(sema.db) else { return };
99
100    // Extract intra-doc links and emit highlights for them.
101    extract_definitions_from_docs(&Documentation::new_borrowed(docs.docs()))
102        .into_iter()
103        .filter_map(|(range, link, ns)| {
104            docs.find_ast_range(range)
105                .filter(|(mapping, _)| mapping.file_id == src_file_id)
106                .and_then(|(InFile { value: mapped_range, .. }, is_inner)| {
107                    Some(mapped_range)
108                        .zip(resolve_doc_path_for_def(sema.db, def, &link, ns, is_inner))
109                })
110        })
111        .for_each(|(range, def)| {
112            hl.add(HlRange {
113                range,
114                highlight: module_def_to_hl_tag(sema.db, def)
115                    | HlMod::Documentation
116                    | HlMod::Injected
117                    | HlMod::IntraDocLink,
118                binding_hash: None,
119            })
120        });
121
122    // Extract doc-test sources from the docs and calculate highlighting for them.
123
124    let mut inj = RangeMapper::default();
125    inj.add_unmapped("fn doctest() {\n");
126
127    let mut is_codeblock = false;
128    let mut is_doctest = false;
129
130    let mut has_doctests = false;
131
132    let mut docs_offset = TextSize::new(0);
133    for mut line in docs.docs().split('\n') {
134        let mut line_docs_offset = docs_offset;
135        docs_offset += TextSize::of(line) + TextSize::of("\n");
136
137        match RUSTDOC_FENCES.into_iter().find_map(|fence| line.find(fence)) {
138            Some(idx) => {
139                is_codeblock = !is_codeblock;
140                // Check whether code is rust by inspecting fence guards
141                let guards = &line[idx + RUSTDOC_FENCE_LENGTH..];
142                let is_rust = is_rust_fence(guards);
143                is_doctest = is_codeblock && is_rust;
144                continue;
145            }
146            None if !is_doctest => continue,
147            None => (),
148        }
149
150        // lines marked with `#` should be ignored in output, we skip the `#` char
151        if line.starts_with('#') {
152            line_docs_offset += TextSize::of("#");
153            line = &line["#".len()..];
154        }
155
156        let Some((InFile { file_id, value: mapped_range }, _)) =
157            docs.find_ast_range(TextRange::at(line_docs_offset, TextSize::of(line)))
158        else {
159            continue;
160        };
161        if file_id != src_file_id {
162            continue;
163        }
164
165        has_doctests = true;
166        inj.add(line, mapped_range);
167        inj.add_unmapped("\n");
168    }
169
170    if !has_doctests {
171        return; // no need to run an analysis on an empty file
172    }
173
174    inj.add_unmapped("\n}");
175
176    let proc_macro_cwd = {
177        match sema.first_crate(vfs_file_id) {
178            Some(krate) => krate.base().data(sema.db).proc_macro_cwd.clone(),
179            None => {
180                // Arbitrarily pick /, since from_single_file treats this file as /main.rs anyway.
181                Arc::new(ide_db::base_db::AbsPathBuf::try_from("/").unwrap())
182            }
183        }
184    };
185    let (analysis, tmp_file_id) = Analysis::from_single_file(inj.take_text(), proc_macro_cwd);
186
187    if let Ok(ranges) = analysis.with_db(|db| {
188        super::highlight(
189            db,
190            &HighlightConfig {
191                syntactic_name_ref_highlighting: true,
192                comments: true,
193                punctuation: true,
194                operator: true,
195                strings: true,
196                specialize_punctuation: config.specialize_punctuation,
197                specialize_operator: config.operator,
198                inject_doc_comment: config.inject_doc_comment,
199                macro_bang: config.macro_bang,
200                ra_fixture: config.ra_fixture,
201            },
202            tmp_file_id,
203            None,
204        )
205    }) {
206        for HlRange { range, highlight, binding_hash } in ranges {
207            for range in inj.map_range_up(range) {
208                hl.add(HlRange { range, highlight: highlight | HlMod::Injected, binding_hash });
209            }
210        }
211    }
212}
213
214fn module_def_to_hl_tag(db: &dyn HirDatabase, def: Definition) -> HlTag {
215    let symbol = match def {
216        Definition::Crate(_) | Definition::ExternCrateDecl(_) => SymbolKind::CrateRoot,
217        Definition::Module(m) if m.is_crate_root(db) => SymbolKind::CrateRoot,
218        Definition::Module(_) => SymbolKind::Module,
219        Definition::Function(_) => SymbolKind::Function,
220        Definition::Adt(hir::Adt::Struct(_)) => SymbolKind::Struct,
221        Definition::Adt(hir::Adt::Enum(_)) => SymbolKind::Enum,
222        Definition::Adt(hir::Adt::Union(_)) => SymbolKind::Union,
223        Definition::EnumVariant(_) => SymbolKind::Variant,
224        Definition::Const(_) => SymbolKind::Const,
225        Definition::Static(_) => SymbolKind::Static,
226        Definition::Trait(_) => SymbolKind::Trait,
227        Definition::TypeAlias(_) => SymbolKind::TypeAlias,
228        Definition::BuiltinLifetime(_) => SymbolKind::LifetimeParam,
229        Definition::BuiltinType(_) => return HlTag::BuiltinType,
230        Definition::Macro(_) => SymbolKind::Macro,
231        Definition::Field(_) | Definition::TupleField(_) => SymbolKind::Field,
232        Definition::SelfType(_) => SymbolKind::Impl,
233        Definition::Local(_) => SymbolKind::Local,
234        Definition::GenericParam(gp) => match gp {
235            hir::GenericParam::TypeParam(_) => SymbolKind::TypeParam,
236            hir::GenericParam::ConstParam(_) => SymbolKind::ConstParam,
237            hir::GenericParam::LifetimeParam(_) => SymbolKind::LifetimeParam,
238        },
239        Definition::Label(_) => SymbolKind::Label,
240        Definition::BuiltinAttr(_) => SymbolKind::BuiltinAttr,
241        Definition::ToolModule(_) => SymbolKind::ToolModule,
242        Definition::DeriveHelper(_) => SymbolKind::DeriveHelper,
243        Definition::InlineAsmRegOrRegClass(_) => SymbolKind::InlineAsmRegOrRegClass,
244        Definition::InlineAsmOperand(_) => SymbolKind::Local,
245    };
246    HlTag::Symbol(symbol)
247}