From 53d881f17229e1fcda4f1e139258059b85e6beeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Mon, 3 Jan 2022 12:52:01 +0900 Subject: [PATCH] Store theme scopes on the loader, this way theme isn't passed around --- helix-core/src/syntax.rs | 53 ++++++++++++++++--------------------- helix-term/src/ui/picker.rs | 2 +- helix-view/src/document.rs | 17 +++++------- helix-view/src/editor.rs | 11 ++------ 4 files changed, 32 insertions(+), 51 deletions(-) diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 858b9bdfe..8a9735690 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -8,7 +8,7 @@ use crate::{ pub use helix_syntax::get_language; -use arc_swap::ArcSwap; +use arc_swap::{ArcSwap, Guard}; use slotmap::{DefaultKey as LayerId, HopSlotMap}; use std::{ @@ -264,12 +264,16 @@ impl LanguageConfiguration { } } +// Expose loader as Lazy<> global since it's always static? + #[derive(Debug)] pub struct Loader { // highlight_names ? language_configs: Vec>, language_config_ids_by_file_type: HashMap, // Vec language_config_ids_by_shebang: HashMap, + + scopes: ArcSwap>, } impl Loader { @@ -278,6 +282,7 @@ impl Loader { language_configs: Vec::new(), language_config_ids_by_file_type: HashMap::new(), language_config_ids_by_shebang: HashMap::new(), + scopes: ArcSwap::from_pointee(Vec::new()), }; for config in config.language { @@ -363,8 +368,22 @@ impl Loader { } None } - pub fn language_configs_iter(&self) -> impl Iterator> { - self.language_configs.iter() + + pub fn set_scopes(&self, scopes: Vec) { + self.scopes.store(Arc::new(scopes)); + + // Reconfigure existing grammars + for config in self + .language_configs + .iter() + .filter(|cfg| cfg.is_highlight_initialized()) + { + config.reconfigure(&self.scopes()); + } + } + + pub fn scopes(&self) -> Guard>> { + self.scopes.load() } } @@ -447,33 +466,7 @@ impl Syntax { self.loader .language_configuration_for_injection_string(language) .and_then(|language_config| { - // TODO: get these theme.scopes from somewhere, probably make them settable on Loader - let scopes = &[ - "attribute", - "constant", - "function.builtin", - "function", - "keyword", - "operator", - "property", - "punctuation", - "punctuation.bracket", - "punctuation.delimiter", - "string", - "string.special", - "tag", - "type", - "type.builtin", - "variable", - "variable.builtin", - "variable.parameter", - ]; - language_config.highlight_config( - &scopes - .iter() - .map(|scope| scope.to_string()) - .collect::>(), - ) + language_config.highlight_config(&self.loader.scopes.load()) }) }; diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 00236050f..e96928093 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -139,7 +139,7 @@ impl FilePicker { (size, _) if size > MAX_FILE_SIZE_FOR_PREVIEW => CachedPreview::LargeFile, _ => { // TODO: enable syntax highlighting; blocked by async rendering - Document::open(path, None, Some(&editor.theme), None) + Document::open(path, None, None) .map(|doc| CachedPreview::Document(Box::new(doc))) .unwrap_or(CachedPreview::NotFound) } diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 0ac8e80b8..0b5cfd22c 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -19,7 +19,7 @@ use helix_core::{ }; use helix_lsp::util::LspFormatting; -use crate::{DocumentId, Theme, ViewId}; +use crate::{DocumentId, ViewId}; /// 8kB of buffer space for encoding and decoding `Rope`s. const BUF_SIZE: usize = 8192; @@ -358,7 +358,6 @@ impl Document { pub fn open( path: &Path, encoding: Option<&'static encoding::Encoding>, - theme: Option<&Theme>, config_loader: Option>, ) -> Result { // Open the file if it exists, otherwise assume it is a new file (and thus empty). @@ -376,7 +375,7 @@ impl Document { // set the path and try detecting the language doc.set_path(Some(path))?; if let Some(loader) = config_loader { - doc.detect_language(theme, loader); + doc.detect_language(loader); } doc.detect_indent_and_line_ending(); @@ -498,12 +497,12 @@ impl Document { } /// Detect the programming language based on the file type. - pub fn detect_language(&mut self, theme: Option<&Theme>, config_loader: Arc) { + pub fn detect_language(&mut self, config_loader: Arc) { if let Some(path) = &self.path { let language_config = config_loader .language_config_for_file_name(path) .or_else(|| config_loader.language_config_for_shebang(self.text())); - self.set_language(theme, language_config, Some(config_loader)); + self.set_language(language_config, Some(config_loader)); } } @@ -577,16 +576,13 @@ impl Document { /// if it exists. pub fn set_language( &mut self, - theme: Option<&Theme>, language_config: Option>, loader: Option>, ) { if let (Some(language_config), Some(loader)) = (language_config, loader) { - let scopes = theme.map(|theme| theme.scopes()).unwrap_or(&[]); - if let Some(highlight_config) = language_config.highlight_config(scopes) { + if let Some(highlight_config) = language_config.highlight_config(&loader.scopes()) { let syntax = Syntax::new(&self.text, highlight_config, loader); self.syntax = Some(syntax); - // TODO: config.configure(scopes) is now delayed, is that ok? } self.language = Some(language_config); @@ -601,12 +597,11 @@ impl Document { pub fn set_language2( &mut self, scope: &str, - theme: Option<&Theme>, config_loader: Arc, ) { let language_config = config_loader.language_config_for_scope(scope); - self.set_language(theme, language_config, Some(config_loader)); + self.set_language(language_config, Some(config_loader)); } /// Set the LSP. diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 9ad1558b1..caf2bce74 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -268,13 +268,7 @@ impl Editor { } let scopes = theme.scopes(); - for config in self - .syn_loader - .language_configs_iter() - .filter(|cfg| cfg.is_highlight_initialized()) - { - config.reconfigure(scopes); - } + self.syn_loader.set_scopes(scopes.to_vec()); self.theme = theme; self._refresh(); @@ -283,7 +277,7 @@ impl Editor { /// Refreshes the language server for a given document pub fn refresh_language_server(&mut self, doc_id: DocumentId) -> Option<()> { let doc = self.documents.get_mut(&doc_id)?; - doc.detect_language(Some(&self.theme), self.syn_loader.clone()); + doc.detect_language(self.syn_loader.clone()); Self::launch_language_server(&mut self.language_servers, doc) } @@ -465,7 +459,6 @@ impl Editor { let mut doc = Document::open( &path, None, - Some(&self.theme), Some(self.syn_loader.clone()), )?;