Store theme scopes on the loader, this way theme isn't passed around

This commit is contained in:
Blaž Hrastnik 2022-01-03 12:52:01 +09:00
parent 6728e44490
commit 53d881f172
4 changed files with 32 additions and 51 deletions

View file

@ -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<Arc<LanguageConfiguration>>,
language_config_ids_by_file_type: HashMap<String, usize>, // Vec<usize>
language_config_ids_by_shebang: HashMap<String, usize>,
scopes: ArcSwap<Vec<String>>,
}
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<Item = &Arc<LanguageConfiguration>> {
self.language_configs.iter()
pub fn set_scopes(&self, scopes: Vec<String>) {
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<Arc<Vec<String>>> {
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::<Vec<_>>(),
)
language_config.highlight_config(&self.loader.scopes.load())
})
};

View file

@ -139,7 +139,7 @@ impl<T> FilePicker<T> {
(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)
}

View file

@ -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<Arc<syntax::Loader>>,
) -> Result<Self, Error> {
// 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<syntax::Loader>) {
pub fn detect_language(&mut self, config_loader: Arc<syntax::Loader>) {
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<Arc<helix_core::syntax::LanguageConfiguration>>,
loader: Option<Arc<helix_core::syntax::Loader>>,
) {
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<syntax::Loader>,
) {
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.

View file

@ -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()),
)?;