on master help sign in
Store theme scopes on the loader, this way theme isn't passed around
commit53d881f17229e1fcda4f1e139258059b85e6beeb authorBlaž Hrastnik <blaz@mxxn.io> dateMon 3 Jan 2022, 12:52 (5 years ago) parent6728e44
4 files changed+32−51
helix-core/src/syntax.rs+23−30 helix-term/src/ui/picker.rs+1−1 helix-view/src/document.rs+6−11 helix-view/src/editor.rs+2−9
helix-core/src/syntax.rs+23−30
@@ -8,7 +8,7 @@ use crate::{
88
99pub use helix_syntax::get_language;
1010
11−use arc_swap::ArcSwap;
11+use arc_swap::{ArcSwap, Guard};
1212use slotmap::{DefaultKey as LayerId, HopSlotMap};
1313
1414use std::{
@@ -264,12 +264,16 @@ impl LanguageConfiguration {
264264 }
265265}
266266
267+// Expose loader as Lazy<> global since it's always static?
268+
267269#[derive(Debug)]
268270pub struct Loader {
269271 // highlight_names ?
270272 language_configs: Vec<Arc<LanguageConfiguration>>,
271273 language_config_ids_by_file_type: HashMap<String, usize>, // Vec<usize>
272274 language_config_ids_by_shebang: HashMap<String, usize>,
275+
276+ scopes: ArcSwap<Vec<String>>,
273277}
274278
275279impl Loader {
@@ -278,6 +282,7 @@ impl Loader {
278282 language_configs: Vec::new(),
279283 language_config_ids_by_file_type: HashMap::new(),
280284 language_config_ids_by_shebang: HashMap::new(),
285+ scopes: ArcSwap::from_pointee(Vec::new()),
281286 };
282287
283288 for config in config.language {
@@ -363,8 +368,22 @@ impl Loader {
363368 }
364369 None
365370 }
366− pub fn language_configs_iter(&self) -> impl Iterator<Item = &Arc<LanguageConfiguration>> {
367− self.language_configs.iter()
371+
372+ pub fn set_scopes(&self, scopes: Vec<String>) {
373+ self.scopes.store(Arc::new(scopes));
374+
375+ // Reconfigure existing grammars
376+ for config in self
377+ .language_configs
378+ .iter()
379+ .filter(|cfg| cfg.is_highlight_initialized())
380+ {
381+ config.reconfigure(&self.scopes());
382+ }
383+ }
384+
385+ pub fn scopes(&self) -> Guard<Arc<Vec<String>>> {
386+ self.scopes.load()
368387 }
369388}
370389
@@ -447,33 +466,7 @@ impl Syntax {
447466 self.loader
448467 .language_configuration_for_injection_string(language)
449468 .and_then(|language_config| {
450− // TODO: get these theme.scopes from somewhere, probably make them settable on Loader
451− let scopes = &[
452− "attribute",
453− "constant",
454− "function.builtin",
455− "function",
456− "keyword",
457− "operator",
458− "property",
459− "punctuation",
460− "punctuation.bracket",
461− "punctuation.delimiter",
462− "string",
463− "string.special",
464− "tag",
465− "type",
466− "type.builtin",
467− "variable",
468− "variable.builtin",
469− "variable.parameter",
470− ];
471− language_config.highlight_config(
472− &scopes
473− .iter()
474− .map(|scope| scope.to_string())
475− .collect::<Vec<_>>(),
476− )
469+ language_config.highlight_config(&self.loader.scopes.load())
477470 })
478471 };
479472
helix-term/src/ui/picker.rs+1−1
@@ -139,7 +139,7 @@ impl<T> FilePicker<T> {
139139 (size, _) if size > MAX_FILE_SIZE_FOR_PREVIEW => CachedPreview::LargeFile,
140140 _ => {
141141 // TODO: enable syntax highlighting; blocked by async rendering
142− Document::open(path, None, Some(&editor.theme), None)
142+ Document::open(path, None, None)
143143 .map(|doc| CachedPreview::Document(Box::new(doc)))
144144 .unwrap_or(CachedPreview::NotFound)
145145 }
helix-view/src/document.rs+6−11
@@ -19,7 +19,7 @@ use helix_core::{
1919};
2020use helix_lsp::util::LspFormatting;
2121
22−use crate::{DocumentId, Theme, ViewId};
22+use crate::{DocumentId, ViewId};
2323
2424/// 8kB of buffer space for encoding and decoding `Rope`s.
2525const BUF_SIZE: usize = 8192;
@@ -358,7 +358,6 @@ impl Document {
358358 pub fn open(
359359 path: &Path,
360360 encoding: Option<&'static encoding::Encoding>,
361− theme: Option<&Theme>,
362361 config_loader: Option<Arc<syntax::Loader>>,
363362 ) -> Result<Self, Error> {
364363 // Open the file if it exists, otherwise assume it is a new file (and thus empty).
@@ -376,7 +375,7 @@ impl Document {
376375 // set the path and try detecting the language
377376 doc.set_path(Some(path))?;
378377 if let Some(loader) = config_loader {
379− doc.detect_language(theme, loader);
378+ doc.detect_language(loader);
380379 }
381380
382381 doc.detect_indent_and_line_ending();
@@ -498,12 +497,12 @@ impl Document {
498497 }
499498
500499 /// Detect the programming language based on the file type.
501− pub fn detect_language(&mut self, theme: Option<&Theme>, config_loader: Arc<syntax::Loader>) {
500+ pub fn detect_language(&mut self, config_loader: Arc<syntax::Loader>) {
502501 if let Some(path) = &self.path {
503502 let language_config = config_loader
504503 .language_config_for_file_name(path)
505504 .or_else(|| config_loader.language_config_for_shebang(self.text()));
506− self.set_language(theme, language_config, Some(config_loader));
505+ self.set_language(language_config, Some(config_loader));
507506 }
508507 }
509508
@@ -577,16 +576,13 @@ impl Document {
577576 /// if it exists.
578577 pub fn set_language(
579578 &mut self,
580− theme: Option<&Theme>,
581579 language_config: Option<Arc<helix_core::syntax::LanguageConfiguration>>,
582580 loader: Option<Arc<helix_core::syntax::Loader>>,
583581 ) {
584582 if let (Some(language_config), Some(loader)) = (language_config, loader) {
585− let scopes = theme.map(|theme| theme.scopes()).unwrap_or(&[]);
586− if let Some(highlight_config) = language_config.highlight_config(scopes) {
583+ if let Some(highlight_config) = language_config.highlight_config(&loader.scopes()) {
587584 let syntax = Syntax::new(&self.text, highlight_config, loader);
588585 self.syntax = Some(syntax);
589− // TODO: config.configure(scopes) is now delayed, is that ok?
590586 }
591587
592588 self.language = Some(language_config);
@@ -601,12 +597,11 @@ impl Document {
601597 pub fn set_language2(
602598 &mut self,
603599 scope: &str,
604− theme: Option<&Theme>,
605600 config_loader: Arc<syntax::Loader>,
606601 ) {
607602 let language_config = config_loader.language_config_for_scope(scope);
608603
609− self.set_language(theme, language_config, Some(config_loader));
604+ self.set_language(language_config, Some(config_loader));
610605 }
611606
612607 /// Set the LSP.
helix-view/src/editor.rs+2−9
@@ -268,13 +268,7 @@ impl Editor {
268268 }
269269
270270 let scopes = theme.scopes();
271− for config in self
272− .syn_loader
273− .language_configs_iter()
274− .filter(|cfg| cfg.is_highlight_initialized())
275− {
276− config.reconfigure(scopes);
277− }
271+ self.syn_loader.set_scopes(scopes.to_vec());
278272
279273 self.theme = theme;
280274 self._refresh();
@@ -283,7 +277,7 @@ impl Editor {
283277 /// Refreshes the language server for a given document
284278 pub fn refresh_language_server(&mut self, doc_id: DocumentId) -> Option<()> {
285279 let doc = self.documents.get_mut(&doc_id)?;
286− doc.detect_language(Some(&self.theme), self.syn_loader.clone());
280+ doc.detect_language(self.syn_loader.clone());
287281 Self::launch_language_server(&mut self.language_servers, doc)
288282 }
289283
@@ -465,7 +459,6 @@ impl Editor {
465459 let mut doc = Document::open(
466460 &path,
467461 None,
468− Some(&self.theme),
469462 Some(self.syn_loader.clone()),
470463 )?;
471464