@@ -2,21 +2,161 @@ use crate::{Change, Rope, RopeSlice, Transaction};
22pub use helix_syntax::LANG;
33pub use helix_syntax::{get_language, get_language_name};
44
5+use std::collections::HashMap;
6+use std::path::{Path, PathBuf};
7+use std::sync::Arc;
8+
9+use once_cell::sync::OnceCell;
10+
11+// largely based on tree-sitter/cli/src/loader.rs
12+pub struct LanguageConfiguration {
13+ pub(crate) scope: String, // source.rust
14+ pub(crate) file_types: Vec<String>, // filename ends_with? <Gemfile, rb, etc>
15+
16+ pub(crate) path: PathBuf,
17+
18+ // content_regex
19+ // injection_regex
20+ // first_line_regex
21+ //
22+ // root_path
23+ //
24+ pub(crate) language_id: LANG,
25+ pub(crate) highlight_config: OnceCell<Option<Arc<HighlightConfiguration>>>,
26+ // tags_config OnceCell<> https://github.com/tree-sitter/tree-sitter/pull/583
27+}
28+
29+impl LanguageConfiguration {
30+ pub fn highlight_config(
31+ &self,
32+ scopes: &[String],
33+ ) -> Result<Option<&Arc<HighlightConfiguration>>, anyhow::Error> {
34+ self.highlight_config
35+ .get_or_try_init(|| {
36+ // let name = get_language_name(&self.language_id);
37+
38+ let highlights_query =
39+ std::fs::read_to_string(self.path.join("queries/highlights.scm"))
40+ .unwrap_or(String::new());
41+
42+ let injections_query =
43+ std::fs::read_to_string(self.path.join("queries/injections.scm"))
44+ .unwrap_or(String::new());
45+
46+ let locals_query = "";
47+
48+ if highlights_query.is_empty() {
49+ Ok(None)
50+ } else {
51+ let language = get_language(&self.language_id);
52+ let mut config = HighlightConfiguration::new(
53+ language,
54+ &highlights_query,
55+ &injections_query,
56+ &locals_query,
57+ )
58+ .unwrap(); // TODO: no unwrap
59+ config.configure(&scopes);
60+ Ok(Some(Arc::new(config)))
61+ }
62+ })
63+ .map(Option::as_ref)
64+ }
65+}
66+
67+use once_cell::sync::Lazy;
68+
69+pub(crate) static LOADER: Lazy<Loader> = Lazy::new(|| Loader::init());
70+
71+pub struct Loader {
72+ // highlight_names ?
73+ language_configs: Vec<Arc<LanguageConfiguration>>,
74+ language_config_ids_by_file_type: HashMap<String, usize>, // Vec<usize>
75+}
76+
77+impl Loader {
78+ fn init() -> Loader {
79+ let mut loader = Loader {
80+ language_configs: Vec::new(),
81+ language_config_ids_by_file_type: HashMap::new(),
82+ };
83+
84+ // hardcoded from now, might load from toml
85+ let configs = vec![
86+ LanguageConfiguration {
87+ scope: "source.rust".to_string(),
88+ file_types: vec!["rs".to_string()],
89+ language_id: LANG::Rust,
90+ highlight_config: OnceCell::new(),
91+ //
92+ path: "../helix-syntax/languages/tree-sitter-rust".into(),
93+ },
94+ LanguageConfiguration {
95+ scope: "source.toml".to_string(),
96+ file_types: vec!["toml".to_string()],
97+ language_id: LANG::Toml,
98+ highlight_config: OnceCell::new(),
99+ //
100+ path: "../helix-syntax/languages/tree-sitter-toml".into(),
101+ },
102+ ];
103+
104+ for config in configs {
105+ // get the next id
106+ let language_id = loader.language_configs.len();
107+
108+ for file_type in &config.file_types {
109+ // entry().or_insert(Vec::new).push(language_id);
110+ loader
111+ .language_config_ids_by_file_type
112+ .insert(file_type.clone(), language_id);
113+ }
114+
115+ loader.language_configs.push(Arc::new(config));
116+ }
117+
118+ loader
119+ }
120+
121+ pub fn language_config_for_file_name(&self, path: &Path) -> Option<Arc<LanguageConfiguration>> {
122+ // Find all the language configurations that match this file name
123+ // or a suffix of the file name.
124+ let configuration_id = path
125+ .file_name()
126+ .and_then(|n| n.to_str())
127+ .and_then(|file_name| self.language_config_ids_by_file_type.get(file_name))
128+ .or_else(|| {
129+ path.extension()
130+ .and_then(|extension| extension.to_str())
131+ .and_then(|extension| self.language_config_ids_by_file_type.get(extension))
132+ });
133+
134+ configuration_id.and_then(|&id| self.language_configs.get(id).cloned())
135+
136+ // TODO: content_regex handling conflict resolution
137+ }
138+}
139+
140+//
141+
5142pub struct Syntax {
6− grammar: Language,
143+ // grammar: Grammar,
7144 parser: Parser,
8145 cursors: Vec<QueryCursor>,
9146
10− config: HighlightConfiguration,
147+ config: Arc<HighlightConfiguration>,
11148
12149 root_layer: LanguageLayer,
13150}
14151
15152impl Syntax {
16153 // buffer, grammar, config, grammars, sync_timeout?
17− pub fn new(language: LANG, source: &Rope, config: HighlightConfiguration) -> Self {
154+ pub fn new(
155+ /*language: LANG,*/ source: &Rope,
156+ config: Arc<HighlightConfiguration>,
157+ ) -> Self {
18158 // fetch grammar for parser based on language string
19− let grammar = get_language(&language);
159+ // let grammar = get_language(&language);
20160 let parser = Parser::new();
21161
22162 let root_layer = LanguageLayer::new();
@@ -25,7 +165,7 @@ impl Syntax {
25165 // track scope_descriptor: a Vec of scopes for item in tree
26166
27167 let mut syntax = Self {
28− grammar,
168+ // grammar,
29169 parser,
30170 cursors: Vec::new(),
31171 config,
@@ -48,10 +188,6 @@ impl Syntax {
48188 syntax
49189 }
50190
51− pub fn configure(&mut self, scopes: &[String]) {
52− self.config.configure(scopes)
53− }
54−
55191 pub fn update(&mut self, source: &Rope, changeset: &ChangeSet) -> Result<(), Error> {
56192 self.root_layer
57193 .update(&mut self.parser, &self.config, source, changeset)
@@ -88,9 +224,9 @@ impl Syntax {
88224 let mut cursor = QueryCursor::new(); // reuse a pool
89225 let tree_ref = unsafe { mem::transmute::<_, &'static Tree>(self.tree()) };
90226 let cursor_ref = unsafe { mem::transmute::<_, &'static mut QueryCursor>(&mut cursor) };
91− let query_ref = unsafe { mem::transmute::<_, &'static mut Query>(&mut self.config.query) };
227+ let query_ref = unsafe { mem::transmute::<_, &'static Query>(&self.config.query) };
92228 let config_ref =
93− unsafe { mem::transmute::<_, &'static HighlightConfiguration>(&self.config) };
229+ unsafe { mem::transmute::<_, &'static HighlightConfiguration>(self.config.as_ref()) };
94230
95231 // TODO: if reusing cursors this might need resetting
96232 if let Some(range) = &range {
@@ -432,8 +568,8 @@ impl LanguageLayer {
432568use std::sync::atomic::{AtomicUsize, Ordering};
433569use std::{iter, mem, ops, str, usize};
434570use tree_sitter::{
435− Language, Node, Parser, Point, Query, QueryCaptures, QueryCursor, QueryError, QueryMatch,
436− Range, Tree,
571+ Language as Grammar, Node, Parser, Point, Query, QueryCaptures, QueryCursor, QueryError,
572+ QueryMatch, Range, Tree,
437573};
438574
439575const CANCELLATION_CHECK_INTERVAL: usize = 100;
@@ -462,7 +598,7 @@ pub enum HighlightEvent {
462598///
463599/// This struct is immutable and can be shared between threads.
464600pub struct HighlightConfiguration {
465− pub language: Language,
601+ pub language: Grammar,
466602 pub query: Query,
467603 combined_injections_query: Option<Query>,
468604 locals_pattern_index: usize,
@@ -477,16 +613,6 @@ pub struct HighlightConfiguration {
477613 local_ref_capture_index: Option<u32>,
478614}
479615
480−/// Performs syntax highlighting, recognizing a given list of highlight names.
481−///
482−/// For the best performance `Highlighter` values should be reused between
483−/// syntax highlighting calls. A separate highlighter is needed for each thread that
484−/// is performing highlighting.
485−pub struct Highlighter {
486− parser: Parser,
487− cursors: Vec<QueryCursor>,
488−}
489−
490616#[derive(Debug)]
491617struct LocalDef<'a> {
492618 name: &'a str,
@@ -527,70 +653,13 @@ struct HighlightIterLayer<'a> {
527653 depth: usize,
528654}
529655
530−impl Default for Highlighter {
531− fn default() -> Self {
532− Highlighter {
533− parser: Parser::new(),
534− cursors: Vec::new(),
535− }
536− }
537−}
538−
539−impl Highlighter {
540− pub fn new() -> Self {
541− Self::default()
542− }
543−
544− pub fn parser(&mut self) -> &mut Parser {
545− &mut self.parser
546− }
547−
548− // /// Iterate over the highlighted regions for a given slice of source code.
549− // pub fn highlight<'a>(
550− // &'a mut self,
551− // config: &'a HighlightConfiguration,
552− // source: &'a [u8],
553− // cancellation_flag: Option<&'a AtomicUsize>,
554− // mut injection_callback: impl FnMut(&str) -> Option<&'a HighlightConfiguration> + 'a,
555− // ) -> Result<impl Iterator<Item = Result<HighlightEvent, Error>> + 'a, Error> {
556− // let layers = HighlightIterLayer::new(
557− // source,
558− // self,
559− // cancellation_flag,
560− // &mut injection_callback,
561− // config,
562− // 0,
563− // vec![Range {
564− // start_byte: 0,
565− // end_byte: usize::MAX,
566− // start_point: Point::new(0, 0),
567− // end_point: Point::new(usize::MAX, usize::MAX),
568− // }],
569− // )?;
570− // assert_ne!(layers.len(), 0);
571− // let mut result = HighlightIter {
572− // source,
573− // byte_offset: 0,
574− // injection_callback,
575− // cancellation_flag,
576− // highlighter: self,
577− // iter_count: 0,
578− // layers,
579− // next_event: None,
580− // last_highlight_range: None,
581− // };
582− // result.sort_layers();
583− // Ok(result)
584− // }
585−}
586−
587656impl HighlightConfiguration {
588− /// Creates a `HighlightConfiguration` for a given `Language` and set of highlighting
657+ /// Creates a `HighlightConfiguration` for a given `Grammar` and set of highlighting
589658 /// queries.
590659 ///
591660 /// # Parameters
592661 ///
593− /// * `language` - The Tree-sitter `Language` that should be used for parsing.
662+ /// * `language` - The Tree-sitter `Grammar` that should be used for parsing.
594663 /// * `highlights_query` - A string containing tree patterns for syntax highlighting. This
595664 /// should be non-empty, otherwise no syntax highlights will be added.
596665 /// * `injections_query` - A string containing tree patterns for injecting other languages
@@ -600,7 +669,7 @@ impl HighlightConfiguration {
600669 ///
601670 /// Returns a `HighlightConfiguration` that can then be used with the `highlight` method.
602671 pub fn new(
603− language: Language,
672+ language: Grammar,
604673 highlights_query: &str,
605674 injection_query: &str,
606675 locals_query: &str,