@@ -1,6 +1,8 @@
11use crate::{regex::Regex, Change, Rope, RopeSlice, Transaction};
22pub use helix_syntax::{get_language, get_language_name, Lang};
33
4+use arc_swap::ArcSwap;
5+
46use std::{
57 borrow::Cow,
68 cell::RefCell,
@@ -143,35 +145,48 @@ fn read_query(language: &str, filename: &str) -> String {
143145}
144146
145147impl LanguageConfiguration {
146− pub fn highlight_config(&self, scopes: &[String]) -> Option<Arc<HighlightConfiguration>> {
147− self.highlight_config
148− .get_or_init(|| {
149− let language = get_language_name(self.language_id).to_ascii_lowercase();
148+ fn initialize_highlight(&self, scopes: &[String]) -> Option<Arc<HighlightConfiguration>> {
149+ let language = get_language_name(self.language_id).to_ascii_lowercase();
150150
151− let highlights_query = read_query(&language, "highlights.scm");
152− // always highlight syntax errors
153− // highlights_query += "\n(ERROR) @error";
151+ let highlights_query = read_query(&language, "highlights.scm");
152+ // always highlight syntax errors
153+ // highlights_query += "\n(ERROR) @error";
154154
155− let injections_query = read_query(&language, "injections.scm");
155+ let injections_query = read_query(&language, "injections.scm");
156156
157− let locals_query = "";
157+ let locals_query = "";
158158
159− if highlights_query.is_empty() {
160− None
161− } else {
162− let language = get_language(self.language_id);
163− let mut config = HighlightConfiguration::new(
164− language,
165− &highlights_query,
166− &injections_query,
167− locals_query,
168− )
169− .unwrap(); // TODO: no unwrap
170− config.configure(scopes);
171− Some(Arc::new(config))
172− }
173− })
174− .clone()
159+ if highlights_query.is_empty() {
160+ None
161+ } else {
162+ let language = get_language(self.language_id);
163+ let mut config = HighlightConfiguration::new(
164+ language,
165+ &highlights_query,
166+ &injections_query,
167+ locals_query,
168+ )
169+ .unwrap(); // TODO: no unwrap
170+ config.configure(scopes);
171+ Some(Arc::new(config))
172+ }
173+ }
174+
175+ pub fn highlight_config(&self, scopes: &[String]) -> Option<Arc<HighlightConfiguration>> {
176+ if let Some(config) = self.highlight_config.get() {
177+ if let Some(config) = config {
178+ config.configure(scopes);
179+ }
180+ config.clone()
181+ } else {
182+ self.highlight_config
183+ .get_or_init(|| self.initialize_highlight(scopes))
184+ .clone()
185+ }
186+ }
187+
188+ pub fn is_highlight_initialized(&self) -> bool {
189+ self.highlight_config.get().is_some()
175190 }
176191
177192 pub fn indent_query(&self) -> Option<&IndentQuery> {
@@ -190,22 +205,18 @@ impl LanguageConfiguration {
190205 }
191206}
192207
193−pub static LOADER: OnceCell<Loader> = OnceCell::new();
194−
195208#[derive(Debug)]
196209pub struct Loader {
197210 // highlight_names ?
198211 language_configs: Vec<Arc<LanguageConfiguration>>,
199212 language_config_ids_by_file_type: HashMap<String, usize>, // Vec<usize>
200− scopes: Vec<String>,
201213}
202214
203215impl Loader {
204− pub fn new(config: Configuration, scopes: Vec<String>) -> Self {
216+ pub fn new(config: Configuration) -> Self {
205217 let mut loader = Self {
206218 language_configs: Vec::new(),
207219 language_config_ids_by_file_type: HashMap::new(),
208− scopes,
209220 };
210221
211222 for config in config.language {
@@ -225,10 +236,6 @@ impl Loader {
225236 loader
226237 }
227238
228− pub fn scopes(&self) -> &[String] {
229− &self.scopes
230− }
231−
232239 pub fn language_config_for_file_name(&self, path: &Path) -> Option<Arc<LanguageConfiguration>> {
233240 // Find all the language configurations that match this file name
234241 // or a suffix of the file name.
@@ -253,6 +260,10 @@ impl Loader {
253260 .find(|config| config.scope == scope)
254261 .cloned()
255262 }
263+
264+ pub fn language_configs_iter(&self) -> impl Iterator<Item = &Arc<LanguageConfiguration>> {
265+ self.language_configs.iter()
266+ }
256267}
257268
258269pub struct TsParser {
@@ -771,7 +782,7 @@ pub struct HighlightConfiguration {
771782 combined_injections_query: Option<Query>,
772783 locals_pattern_index: usize,
773784 highlights_pattern_index: usize,
774− highlight_indices: Vec<Option<Highlight>>,
785+ highlight_indices: ArcSwap<Vec<Option<Highlight>>>,
775786 non_local_variable_patterns: Vec<bool>,
776787 injection_content_capture_index: Option<u32>,
777788 injection_language_capture_index: Option<u32>,
@@ -923,7 +934,7 @@ impl HighlightConfiguration {
923934 }
924935 }
925936
926− let highlight_indices = vec![None; query.capture_names().len()];
937+ let highlight_indices = ArcSwap::from_pointee(vec![None; query.capture_names().len()]);
927938 Ok(Self {
928939 language,
929940 query,
@@ -956,17 +967,20 @@ impl HighlightConfiguration {
956967 ///
957968 /// When highlighting, results are returned as `Highlight` values, which contain the index
958969 /// of the matched highlight this list of highlight names.
959− pub fn configure(&mut self, recognized_names: &[String]) {
970+ pub fn configure(&self, recognized_names: &[String]) {
960971 let mut capture_parts = Vec::new();
961− self.highlight_indices.clear();
962− self.highlight_indices
963− .extend(self.query.capture_names().iter().map(move |capture_name| {
972+ let indices: Vec<_> = self
973+ .query
974+ .capture_names()
975+ .iter()
976+ .map(move |capture_name| {
964977 capture_parts.clear();
965978 capture_parts.extend(capture_name.split('.'));
966979
967980 let mut best_index = None;
968981 let mut best_match_len = 0;
969982 for (i, recognized_name) in recognized_names.iter().enumerate() {
983+ let recognized_name = recognized_name;
970984 let mut len = 0;
971985 let mut matches = true;
972986 for part in recognized_name.split('.') {
@@ -982,7 +996,10 @@ impl HighlightConfiguration {
982996 }
983997 }
984998 best_index.map(Highlight)
985− }));
999+ })
1000+ .collect();
1001+
1002+ self.highlight_indices.store(Arc::new(indices));
9861003 }
9871004}
9881005
@@ -1561,7 +1578,7 @@ where
15611578 }
15621579 }
15631580
1564− let current_highlight = layer.config.highlight_indices[capture.index as usize];
1581+ let current_highlight = layer.config.highlight_indices.load()[capture.index as usize];
15651582
15661583 // If this node represents a local definition, then store the current
15671584 // highlight value on the local scope entry representing this node.