diff options
Diffstat (limited to 'crates/ide/src/syntax_highlighting/inject.rs')
-rw-r--r-- | crates/ide/src/syntax_highlighting/inject.rs | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs new file mode 100644 index 000000000..281461493 --- /dev/null +++ b/crates/ide/src/syntax_highlighting/inject.rs | |||
@@ -0,0 +1,158 @@ | |||
1 | //! "Recursive" Syntax highlighting for code in doctests and fixtures. | ||
2 | |||
3 | use hir::Semantics; | ||
4 | use ide_db::call_info::ActiveParameter; | ||
5 | use syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize}; | ||
6 | |||
7 | use crate::{Analysis, HlMod, HlRange, HlTag, RootDatabase}; | ||
8 | |||
9 | use super::{highlights::Highlights, injector::Injector}; | ||
10 | |||
11 | pub(super) fn ra_fixture( | ||
12 | hl: &mut Highlights, | ||
13 | sema: &Semantics<RootDatabase>, | ||
14 | literal: ast::String, | ||
15 | expanded: SyntaxToken, | ||
16 | ) -> Option<()> { | ||
17 | let active_parameter = ActiveParameter::at_token(&sema, expanded)?; | ||
18 | if !active_parameter.name.starts_with("ra_fixture") { | ||
19 | return None; | ||
20 | } | ||
21 | let value = literal.value()?; | ||
22 | |||
23 | if let Some(range) = literal.open_quote_text_range() { | ||
24 | hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None }) | ||
25 | } | ||
26 | |||
27 | let mut inj = Injector::default(); | ||
28 | |||
29 | let mut text = &*value; | ||
30 | let mut offset: TextSize = 0.into(); | ||
31 | |||
32 | while !text.is_empty() { | ||
33 | let marker = "$0"; | ||
34 | let idx = text.find(marker).unwrap_or(text.len()); | ||
35 | let (chunk, next) = text.split_at(idx); | ||
36 | inj.add(chunk, TextRange::at(offset, TextSize::of(chunk))); | ||
37 | |||
38 | text = next; | ||
39 | offset += TextSize::of(chunk); | ||
40 | |||
41 | if let Some(next) = text.strip_prefix(marker) { | ||
42 | if let Some(range) = literal.map_range_up(TextRange::at(offset, TextSize::of(marker))) { | ||
43 | hl.add(HlRange { range, highlight: HlTag::Keyword.into(), binding_hash: None }); | ||
44 | } | ||
45 | |||
46 | text = next; | ||
47 | |||
48 | let marker_len = TextSize::of(marker); | ||
49 | offset += marker_len; | ||
50 | } | ||
51 | } | ||
52 | |||
53 | let (analysis, tmp_file_id) = Analysis::from_single_file(inj.text().to_string()); | ||
54 | |||
55 | for mut hl_range in analysis.highlight(tmp_file_id).unwrap() { | ||
56 | for range in inj.map_range_up(hl_range.range) { | ||
57 | if let Some(range) = literal.map_range_up(range) { | ||
58 | hl_range.range = range; | ||
59 | hl.add(hl_range.clone()); | ||
60 | } | ||
61 | } | ||
62 | } | ||
63 | |||
64 | if let Some(range) = literal.close_quote_text_range() { | ||
65 | hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None }) | ||
66 | } | ||
67 | |||
68 | Some(()) | ||
69 | } | ||
70 | |||
71 | const RUSTDOC_FENCE: &'static str = "```"; | ||
72 | const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[ | ||
73 | "", | ||
74 | "rust", | ||
75 | "should_panic", | ||
76 | "ignore", | ||
77 | "no_run", | ||
78 | "compile_fail", | ||
79 | "edition2015", | ||
80 | "edition2018", | ||
81 | "edition2021", | ||
82 | ]; | ||
83 | |||
84 | /// Injection of syntax highlighting of doctests. | ||
85 | pub(super) fn doc_comment(hl: &mut Highlights, node: &SyntaxNode) { | ||
86 | let doc_comments = node | ||
87 | .children_with_tokens() | ||
88 | .filter_map(|it| it.into_token().and_then(ast::Comment::cast)) | ||
89 | .filter(|it| it.kind().doc.is_some()); | ||
90 | |||
91 | if !doc_comments.clone().any(|it| it.text().contains(RUSTDOC_FENCE)) { | ||
92 | return; | ||
93 | } | ||
94 | |||
95 | let mut inj = Injector::default(); | ||
96 | inj.add_unmapped("fn doctest() {\n"); | ||
97 | |||
98 | let mut is_codeblock = false; | ||
99 | let mut is_doctest = false; | ||
100 | |||
101 | // Replace the original, line-spanning comment ranges by new, only comment-prefix | ||
102 | // spanning comment ranges. | ||
103 | let mut new_comments = Vec::new(); | ||
104 | for comment in doc_comments { | ||
105 | match comment.text().find(RUSTDOC_FENCE) { | ||
106 | Some(idx) => { | ||
107 | is_codeblock = !is_codeblock; | ||
108 | // Check whether code is rust by inspecting fence guards | ||
109 | let guards = &comment.text()[idx + RUSTDOC_FENCE.len()..]; | ||
110 | let is_rust = | ||
111 | guards.split(',').all(|sub| RUSTDOC_FENCE_TOKENS.contains(&sub.trim())); | ||
112 | is_doctest = is_codeblock && is_rust; | ||
113 | continue; | ||
114 | } | ||
115 | None if !is_doctest => continue, | ||
116 | None => (), | ||
117 | } | ||
118 | |||
119 | let line: &str = comment.text().as_str(); | ||
120 | let range = comment.syntax().text_range(); | ||
121 | |||
122 | let mut pos = TextSize::of(comment.prefix()); | ||
123 | // whitespace after comment is ignored | ||
124 | if let Some(ws) = line[pos.into()..].chars().next().filter(|c| c.is_whitespace()) { | ||
125 | pos += TextSize::of(ws); | ||
126 | } | ||
127 | // lines marked with `#` should be ignored in output, we skip the `#` char | ||
128 | if let Some(ws) = line[pos.into()..].chars().next().filter(|&c| c == '#') { | ||
129 | pos += TextSize::of(ws); | ||
130 | } | ||
131 | |||
132 | new_comments.push(TextRange::at(range.start(), pos)); | ||
133 | |||
134 | inj.add(&line[pos.into()..], TextRange::new(range.start() + pos, range.end())); | ||
135 | inj.add_unmapped("\n"); | ||
136 | } | ||
137 | inj.add_unmapped("\n}"); | ||
138 | |||
139 | let (analysis, tmp_file_id) = Analysis::from_single_file(inj.text().to_string()); | ||
140 | |||
141 | for h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap() { | ||
142 | for r in inj.map_range_up(h.range) { | ||
143 | hl.add(HlRange { | ||
144 | range: r, | ||
145 | highlight: h.highlight | HlMod::Injected, | ||
146 | binding_hash: h.binding_hash, | ||
147 | }); | ||
148 | } | ||
149 | } | ||
150 | |||
151 | for range in new_comments { | ||
152 | hl.add(HlRange { | ||
153 | range, | ||
154 | highlight: HlTag::Comment | HlMod::Documentation, | ||
155 | binding_hash: None, | ||
156 | }); | ||
157 | } | ||
158 | } | ||