Flip the dependency between editor and theme

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Antonio Scandurra 2021-10-05 19:21:13 +02:00
parent f09798c4a7
commit f70e3878b6
12 changed files with 94 additions and 94 deletions

View file

@ -1,5 +1,6 @@
use crate::syntax_theme::SyntaxTheme;
use gpui::fonts::HighlightStyle;
use std::sync::Arc;
use theme::SyntaxTheme;
#[derive(Clone, Debug)]
pub struct HighlightMap(Arc<[HighlightId]>);
@ -49,6 +50,20 @@ impl HighlightMap {
}
}
impl HighlightId {
pub fn style(&self, theme: &SyntaxTheme) -> Option<HighlightStyle> {
theme
.highlights
.get(self.0 as usize)
.map(|entry| entry.1.clone())
}
#[cfg(any(test, feature = "test-support"))]
pub fn name<'a>(&self, theme: &'a SyntaxTheme) -> Option<&'a str> {
theme.highlights.get(self.0 as usize).map(|e| e.0.as_str())
}
}
impl Default for HighlightMap {
fn default() -> Self {
Self(Arc::new([]))
@ -89,8 +104,8 @@ mod tests {
];
let map = HighlightMap::new(capture_names, &theme);
assert_eq!(theme.highlight_name(map.get(0)), Some("function"));
assert_eq!(theme.highlight_name(map.get(1)), Some("function.async"));
assert_eq!(theme.highlight_name(map.get(2)), Some("variable.builtin"));
assert_eq!(map.get(0).name(&theme), Some("function"));
assert_eq!(map.get(1).name(&theme), Some("function.async"));
assert_eq!(map.get(2).name(&theme), Some("variable.builtin"));
}
}

View file

@ -1,9 +1,10 @@
use crate::{HighlightMap, SyntaxTheme};
use crate::{HighlightMap};
use parking_lot::Mutex;
use serde::Deserialize;
use std::{path::Path, str, sync::Arc};
use tree_sitter::{Language as Grammar, Query};
pub use tree_sitter::{Parser, Tree};
use theme::SyntaxTheme;
#[derive(Default, Deserialize)]
pub struct LanguageConfig {

View file

@ -7,7 +7,6 @@ mod point;
pub mod random_char_iter;
pub mod rope;
mod selection;
mod syntax_theme;
pub use anchor::*;
use anyhow::{anyhow, Result};
@ -42,7 +41,6 @@ use std::{
time::{Duration, Instant, SystemTime, UNIX_EPOCH},
};
use sum_tree::{Bias, FilterCursor, SumTree};
pub use syntax_theme::SyntaxTheme;
use tree_sitter::{InputEdit, Parser, QueryCursor};
pub trait File {

View file

@ -1,50 +0,0 @@
use std::collections::HashMap;
use crate::HighlightId;
use gpui::fonts::HighlightStyle;
use serde::Deserialize;
#[derive(Default)]
pub struct SyntaxTheme {
pub(crate) highlights: Vec<(String, HighlightStyle)>,
}
impl SyntaxTheme {
pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
Self { highlights }
}
pub fn highlight_style(&self, id: HighlightId) -> Option<HighlightStyle> {
self.highlights
.get(id.0 as usize)
.map(|entry| entry.1.clone())
}
#[cfg(any(test, feature = "test-support"))]
pub fn highlight_name(&self, id: HighlightId) -> Option<&str> {
self.highlights.get(id.0 as usize).map(|e| e.0.as_str())
}
}
impl<'de> Deserialize<'de> for SyntaxTheme {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
let mut result = Self::new(Vec::new());
for (key, style) in syntax_data {
match result
.highlights
.binary_search_by(|(needle, _)| needle.cmp(&key))
{
Ok(i) | Err(i) => {
result.highlights.insert(i, (key, style));
}
}
}
Ok(result)
}
}