Unnamed repository; edit this file 'description' to name the repository.
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
use std::borrow::Cow;
use std::path::Path;
use std::sync::Arc;

use crate::tree_sitter::query::{Capture, Pattern, QueryStr, UserPredicate};
use crate::tree_sitter::{query, Grammar, Query, QueryMatch, SyntaxTreeNode};
use arc_swap::ArcSwap;
use helix_stdx::rope::{self, RopeSliceExt};
use once_cell::sync::Lazy;
use regex::Regex;
use ropey::RopeSlice;

use crate::byte_range_to_str;
use crate::highlighter::Highlight;

/// Contains the data needed to highlight code written in a particular language.
///
/// This struct is immutable and can be shared between threads.
#[derive(Debug)]
pub struct HighlightConfiguration {
    pub grammar: Grammar,
    pub query: Query,
    pub(crate) injections_query: Query,
    pub(crate) combined_injections_patterns: Vec<Pattern>,
    first_highlights_pattern: Pattern,
    pub(crate) highlight_indices: ArcSwap<Vec<Highlight>>,
    pub(crate) non_local_variable_patterns: Vec<bool>,
    pub(crate) injection_content_capture: Option<Capture>,
    pub(crate) injection_language_capture: Option<Capture>,
    pub(crate) injection_filename_capture: Option<Capture>,
    pub(crate) injection_shebang_capture: Option<Capture>,
    pub(crate) local_scope_capture: Option<Capture>,
    pub(crate) local_def_capture: Option<Capture>,
    pub(crate) local_def_value_capture: Option<Capture>,
    pub(crate) local_ref_capture: Option<Capture>,
}

impl HighlightConfiguration {
    /// Creates a `HighlightConfiguration` for a given `Grammar` and set of highlighting
    /// queries.
    ///
    /// # Parameters
    ///
    /// * `language`  - The Tree-sitter `Grammar` that should be used for parsing.
    /// * `highlights_query` - A string containing tree patterns for syntax highlighting. This
    ///   should be non-empty, otherwise no syntax highlights will be added.
    /// * `injections_query` -  A string containing tree patterns for injecting other languages
    ///   into the document. This can be empty if no injections are desired.
    /// * `locals_query` - A string containing tree patterns for tracking local variable
    ///   definitions and references. This can be empty if local variable tracking is not needed.
    ///
    /// Returns a `HighlightConfiguration` that can then be used with the `highlight` method.
    pub fn new(
        grammar: Grammar,
        path: impl AsRef<Path>,
        highlights_query: &str,
        injection_query: &str,
        locals_query: &str,
    ) -> Result<Self, query::ParseError> {
        // Concatenate the query strings, keeping track of the start offset of each section.
        let mut query_source = String::new();
        query_source.push_str(locals_query);
        let highlights_query_offset = query_source.len();
        query_source.push_str(highlights_query);

        let mut non_local_variable_patterns = Vec::with_capacity(32);
        // Construct a single query by concatenating the three query strings, but record the
        // range of pattern indices that belong to each individual string.
        let query = Query::new(grammar, &query_source, path, |pattern, predicate| {
            match predicate {
                UserPredicate::IsPropertySet {
                    negate: true,
                    key: "local",
                    val: None,
                } => {
                    if non_local_variable_patterns.len() < pattern.idx() {
                        non_local_variable_patterns.resize(pattern.idx(), false)
                    }
                    non_local_variable_patterns[pattern.idx()] = true;
                }
                predicate => {
                    return Err(format!("unsupported predicate {predicate}").into());
                }
            }
            Ok(())
        })?;

        let mut combined_injections_patterns = Vec::new();
        let injections_query = Query::new(grammar, injection_query, path, |pattern, predicate| {
            match predicate {
                UserPredicate::SetProperty {
                    key: "injection.combined",
                    val: None,
                } => combined_injections_patterns.push(pattern),
                predicate => {
                    return Err(format!("unsupported predicate {predicate}").into());
                }
            }
            Ok(())
        })?;

        let first_highlights_pattern = query
            .patterns()
            .find(|pattern| query.start_byte_for_pattern(*pattern) >= highlights_query_offset)
            .unwrap_or(Pattern::SENTINEL);

        let injection_content_capture = query.get_capture("injection.content");
        let injection_language_capture = query.get_capture("injection.language");
        let injection_filename_capture = query.get_capture("injection.filename");
        let injection_shebang_capture = query.get_capture("injection.shebang");
        let local_def_capture = query.get_capture("local.definition");
        let local_def_value_capture = query.get_capture("local.definition-value");
        let local_ref_capture = query.get_capture("local.reference");
        let local_scope_capture = query.get_capture("local.scope");

        let highlight_indices =
            ArcSwap::from_pointee(vec![Highlight::NONE; query.num_captures() as usize]);
        Ok(Self {
            grammar,
            query,
            injections_query,
            combined_injections_patterns,
            first_highlights_pattern,
            highlight_indices,
            non_local_variable_patterns,
            injection_content_capture,
            injection_language_capture,
            injection_filename_capture,
            injection_shebang_capture,
            local_scope_capture,
            local_def_capture,
            local_def_value_capture,
            local_ref_capture,
        })
    }

    /// Set the list of recognized highlight names.
    ///
    /// Tree-sitter syntax-highlighting queries specify highlights in the form of dot-separated
    /// highlight names like `punctuation.bracket` and `function.method.builtin`. Consumers of
    /// these queries can choose to recognize highlights with different levels of specificity.
    /// For example, the string `function.builtin` will match against `function.builtin.constructor`
    /// but will not match `function.method.builtin` and `function.method`.
    ///
    /// When highlighting, results are returned as `Highlight` values, which contain the index
    /// of the matched highlight this list of highlight names.
    pub fn configure(&self, recognized_names: &[String]) {
        let mut capture_parts = Vec::new();
        let indices: Vec<_> = self
            .query
            .captures()
            .map(move |(_, capture_name)| {
                capture_parts.clear();
                capture_parts.extend(capture_name.split('.'));

                let mut best_index = u32::MAX;
                let mut best_match_len = 0;
                for (i, recognized_name) in recognized_names.iter().enumerate() {
                    let mut len = 0;
                    let mut matches = true;
                    for (i, part) in recognized_name.split('.').enumerate() {
                        match capture_parts.get(i) {
                            Some(capture_part) if *capture_part == part => len += 1,
                            _ => {
                                matches = false;
                                break;
                            }
                        }
                    }
                    if matches && len > best_match_len {
                        best_index = i as u32;
                        best_match_len = len;
                    }
                }
                Highlight(best_index)
            })
            .collect();

        self.highlight_indices.store(Arc::new(indices));
    }

    fn injection_pair<'a>(
        &self,
        query_match: &QueryMatch<'a, 'a>,
        source: RopeSlice<'a>,
    ) -> (
        Option<InjectionLanguageMarker<'a>>,
        Option<SyntaxTreeNode<'a>>,
    ) {
        let mut injection_capture = None;
        let mut content_node = None;

        for matched_node in query_match.matched_nodes() {
            let capture = Some(matched_node.capture);
            if capture == self.injection_language_capture {
                let name = byte_range_to_str(matched_node.syntax_node.byte_range(), source);
                injection_capture = Some(InjectionLanguageMarker::Name(name));
            } else if capture == self.injection_filename_capture {
                let name = byte_range_to_str(matched_node.syntax_node.byte_range(), source);
                let path = Path::new(name.as_ref()).to_path_buf();
                injection_capture = Some(InjectionLanguageMarker::Filename(path.into()));
            } else if capture == self.injection_shebang_capture {
                let node_slice = source.byte_slice(matched_node.syntax_node.byte_range());

                // some languages allow space and newlines before the actual string content
                // so a shebang could be on either the first or second line
                let lines = if let Ok(end) = node_slice.try_line_to_byte(2) {
                    node_slice.byte_slice(..end)
                } else {
                    node_slice
                };

                injection_capture = SHEBANG_REGEX
                    .captures_iter(lines.regex_input())
                    .map(|cap| {
                        let cap = lines.byte_slice(cap.get_group(1).unwrap().range());
                        InjectionLanguageMarker::Shebang(cap.into())
                    })
                    .next()
            } else if capture == self.injection_content_capture {
                content_node = Some(matched_node.syntax_node.clone());
            }
        }
        (injection_capture, content_node)
    }

    pub(super) fn injection_for_match<'a>(
        &self,
        query: &'a Query,
        query_match: &QueryMatch<'a, 'a>,
        source: RopeSlice<'a>,
    ) -> (
        Option<InjectionLanguageMarker<'a>>,
        Option<SyntaxTreeNode<'a>>,
        IncludedChildren,
    ) {
        let (mut injection_capture, content_node) = self.injection_pair(query_match, source);

        let mut included_children = IncludedChildren::default();
        for prop in query.property_settings(query_match.pattern_index) {
            match prop.key.as_ref() {
                // In addition to specifying the language name via the text of a
                // captured node, it can also be hard-coded via a `#set!` predicate
                // that sets the injection.language key.
                "injection.language" if injection_capture.is_none() => {
                    injection_capture = prop
                        .value
                        .as_ref()
                        .map(|s| InjectionLanguageMarker::Name(s.as_ref().into()));
                }

                // By default, injections do not include the *children* of an
                // `injection.content` node - only the ranges that belong to the
                // node itself. This can be changed using a `#set!` predicate that
                // sets the `injection.include-children` key.
                "injection.include-children" => included_children = IncludedChildren::All,

                // Some queries might only exclude named children but include unnamed
                // children in their `injection.content` node. This can be enabled using
                // a `#set!` predicate that sets the `injection.include-unnamed-children` key.
                "injection.include-unnamed-children" => {
                    included_children = IncludedChildren::Unnamed
                }
                _ => {}
            }
        }

        (injection_capture, content_node, included_children)
    }

    // pub fn load_query(
    //     &self,
    //     language: &str,
    //     filename: &str,
    //     read_query_text: impl FnMut(&str, &str) -> String,
    // ) -> Result<Option<Query>, QueryError> {
    //     let query_text = read_query(language, filename, read_query_text);
    //     if query_text.is_empty() {
    //         return Ok(None);
    //     }

    //     Query::new(&self.grammar, &query_text, ).map(Some)
    // }
}

/// reads a query by invoking `read_query_text`, handeles any `inherits` directives
pub fn read_query(
    language: &str,
    filename: &str,
    mut read_query_text: impl FnMut(&str, &str) -> String,
) -> String {
    fn read_query_impl(
        language: &str,
        filename: &str,
        read_query_text: &mut impl FnMut(&str, &str) -> String,
    ) -> String {
        static INHERITS_REGEX: Lazy<Regex> =
            Lazy::new(|| Regex::new(r";+\s*inherits\s*:?\s*([a-z_,()-]+)\s*").unwrap());

        let query = read_query_text(language, filename);

        // replaces all "; inherits <language>(,<language>)*" with the queries of the given language(s)
        INHERITS_REGEX
            .replace_all(&query, |captures: &regex::Captures| {
                captures[1]
                    .split(',')
                    .map(|language| {
                        format!(
                            "\n{}\n",
                            read_query_impl(language, filename, &mut *read_query_text)
                        )
                    })
                    .collect::<String>()
            })
            .to_string()
    }
    read_query_impl(language, filename, &mut read_query_text)
}

const SHEBANG: &str = r"#!\s*(?:\S*[/\\](?:env\s+(?:\-\S+\s+)*)?)?([^\s\.\d]+)";
static SHEBANG_REGEX: Lazy<rope::Regex> = Lazy::new(|| rope::Regex::new(SHEBANG).unwrap());

struct InjectionSettings {
    include_children: IncludedChildren,
    language: Option<QueryStr>,
}

#[derive(Debug, Clone)]
pub enum InjectionLanguageMarker<'a> {
    Name(Cow<'a, str>),
    Filename(Cow<'a, Path>),
    Shebang(String),
}

#[derive(Clone)]
enum IncludedChildren {
    None,
    All,
    Unnamed,
}

impl Default for IncludedChildren {
    fn default() -> Self {
        Self::None
    }
}