aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/syntax_highlighting/injection.rs
blob: d6be9708df76c6ae2d63860aaf132379edbc70a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! Syntax highlighting injections such as highlighting of documentation tests.

use std::{collections::BTreeMap, convert::TryFrom};

use hir::Semantics;
use ide_db::call_info::ActiveParameter;
use itertools::Itertools;
use syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize};

use crate::{Analysis, Highlight, HighlightModifier, HighlightTag, HighlightedRange, RootDatabase};

use super::HighlightedRangeStack;

pub(super) fn highlight_injection(
    acc: &mut HighlightedRangeStack,
    sema: &Semantics<RootDatabase>,
    literal: ast::String,
    expanded: SyntaxToken,
) -> Option<()> {
    let active_parameter = ActiveParameter::at_token(&sema, expanded)?;
    if !active_parameter.name.starts_with("ra_fixture") {
        return None;
    }
    let value = literal.value()?;
    let marker_info = MarkerInfo::new(&*value);
    let (analysis, tmp_file_id) = Analysis::from_single_file(marker_info.cleaned_text.clone());

    if let Some(range) = literal.open_quote_text_range() {
        acc.add(HighlightedRange {
            range,
            highlight: HighlightTag::StringLiteral.into(),
            binding_hash: None,
        })
    }

    for mut h in analysis.highlight(tmp_file_id).unwrap() {
        let range = marker_info.map_range_up(h.range);
        if let Some(range) = literal.map_range_up(range) {
            h.range = range;
            acc.add(h);
        }
    }

    if let Some(range) = literal.close_quote_text_range() {
        acc.add(HighlightedRange {
            range,
            highlight: HighlightTag::StringLiteral.into(),
            binding_hash: None,
        })
    }

    Some(())
}

/// Data to remove `$0` from string and map ranges
#[derive(Default, Debug)]
struct MarkerInfo {
    cleaned_text: String,
    markers: Vec<TextRange>,
}

impl MarkerInfo {
    fn new(mut text: &str) -> Self {
        let marker = "$0";

        let mut res = MarkerInfo::default();
        let mut offset: TextSize = 0.into();
        while !text.is_empty() {
            let idx = text.find(marker).unwrap_or(text.len());
            let (chunk, next) = text.split_at(idx);
            text = next;
            res.cleaned_text.push_str(chunk);
            offset += TextSize::of(chunk);

            if let Some(next) = text.strip_prefix(marker) {
                text = next;

                let marker_len = TextSize::of(marker);
                res.markers.push(TextRange::at(offset, marker_len));
                offset += marker_len;
            }
        }
        res
    }
    fn map_range_up(&self, range: TextRange) -> TextRange {
        TextRange::new(
            self.map_offset_up(range.start(), true),
            self.map_offset_up(range.end(), false),
        )
    }
    fn map_offset_up(&self, mut offset: TextSize, start: bool) -> TextSize {
        for r in &self.markers {
            if r.start() < offset || (start && r.start() == offset) {
                offset += r.len()
            }
        }
        offset
    }
}

/// Mapping from extracted documentation code to original code
type RangesMap = BTreeMap<TextSize, TextSize>;

const RUSTDOC_FENCE: &'static str = "```";
const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[
    "",
    "rust",
    "should_panic",
    "ignore",
    "no_run",
    "compile_fail",
    "edition2015",
    "edition2018",
    "edition2021",
];

/// Extracts Rust code from documentation comments as well as a mapping from
/// the extracted source code back to the original source ranges.
/// Lastly, a vector of new comment highlight ranges (spanning only the
/// comment prefix) is returned which is used in the syntax highlighting
/// injection to replace the previous (line-spanning) comment ranges.
pub(super) fn extract_doc_comments(
    node: &SyntaxNode,
) -> Option<(String, RangesMap, Vec<HighlightedRange>)> {
    // wrap the doctest into function body to get correct syntax highlighting
    let prefix = "fn doctest() {\n";
    let suffix = "}\n";
    // Mapping from extracted documentation code to original code
    let mut range_mapping: RangesMap = BTreeMap::new();
    let mut line_start = TextSize::try_from(prefix.len()).unwrap();
    let mut is_codeblock = false;
    let mut is_doctest = false;
    // Replace the original, line-spanning comment ranges by new, only comment-prefix
    // spanning comment ranges.
    let mut new_comments = Vec::new();
    let doctest = node
        .children_with_tokens()
        .filter_map(|el| el.into_token().and_then(ast::Comment::cast))
        .filter(|comment| comment.kind().doc.is_some())
        .filter(|comment| {
            if let Some(idx) = comment.text().find(RUSTDOC_FENCE) {
                is_codeblock = !is_codeblock;
                // Check whether code is rust by inspecting fence guards
                let guards = &comment.text()[idx + RUSTDOC_FENCE.len()..];
                let is_rust =
                    guards.split(',').all(|sub| RUSTDOC_FENCE_TOKENS.contains(&sub.trim()));
                is_doctest = is_codeblock && is_rust;
                false
            } else {
                is_doctest
            }
        })
        .map(|comment| {
            let prefix_len = comment.prefix().len();
            let line: &str = comment.text().as_str();
            let range = comment.syntax().text_range();

            // whitespace after comment is ignored
            let pos = if let Some(ws) = line.chars().nth(prefix_len).filter(|c| c.is_whitespace()) {
                prefix_len + ws.len_utf8()
            } else {
                prefix_len
            };

            // lines marked with `#` should be ignored in output, we skip the `#` char
            let pos = if let Some(ws) = line.chars().nth(pos).filter(|&c| c == '#') {
                pos + ws.len_utf8()
            } else {
                pos
            };

            range_mapping.insert(line_start, range.start() + TextSize::try_from(pos).unwrap());
            new_comments.push(HighlightedRange {
                range: TextRange::new(
                    range.start(),
                    range.start() + TextSize::try_from(pos).unwrap(),
                ),
                highlight: HighlightTag::Comment | HighlightModifier::Documentation,
                binding_hash: None,
            });
            line_start += range.len() - TextSize::try_from(pos).unwrap();
            line_start += TextSize::try_from('\n'.len_utf8()).unwrap();

            line[pos..].to_owned()
        })
        .join("\n");

    if doctest.is_empty() {
        return None;
    }

    let doctest = format!("{}{}{}", prefix, doctest, suffix);
    Some((doctest, range_mapping, new_comments))
}

/// Injection of syntax highlighting of doctests.
pub(super) fn highlight_doc_comment(
    text: String,
    range_mapping: RangesMap,
    new_comments: Vec<HighlightedRange>,
    stack: &mut HighlightedRangeStack,
) {
    let (analysis, tmp_file_id) = Analysis::from_single_file(text);

    stack.push();
    for mut h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap() {
        // Determine start offset and end offset in case of multi-line ranges
        let mut start_offset = None;
        let mut end_offset = None;
        for (line_start, orig_line_start) in range_mapping.range(..h.range.end()).rev() {
            // It's possible for orig_line_start - line_start to be negative. Add h.range.start()
            // here and remove it from the end range after the loop below so that the values are
            // always non-negative.
            let offset = h.range.start() + orig_line_start - line_start;
            if line_start <= &h.range.start() {
                start_offset.get_or_insert(offset);
                break;
            } else {
                end_offset.get_or_insert(offset);
            }
        }
        if let Some(start_offset) = start_offset {
            h.range = TextRange::new(
                start_offset,
                h.range.end() + end_offset.unwrap_or(start_offset) - h.range.start(),
            );

            h.highlight |= HighlightModifier::Injected;
            stack.add(h);
        }
    }

    // Inject the comment prefix highlight ranges
    stack.push();
    for comment in new_comments {
        stack.add(comment);
    }
    stack.pop_and_inject(None);
    stack.pop_and_inject(Some(Highlight::from(HighlightTag::Dummy) | HighlightModifier::Injected));
}