Only apply attrs to range in syntect editor if it does not match default

This commit is contained in:
Jeremy Soller 2023-12-14 09:10:09 -07:00
parent 19b4d8336e
commit c247e0527c

View file

@ -39,7 +39,7 @@ pub struct SyntaxEditor<'a> {
syntax: &'a SyntaxReference, syntax: &'a SyntaxReference,
theme: &'a SyntaxTheme, theme: &'a SyntaxTheme,
highlighter: Highlighter<'a>, highlighter: Highlighter<'a>,
syntax_cache: Vec<(ParseState, HighlightState)>, syntax_cache: Vec<(ParseState, ScopeStack)>,
} }
impl<'a> SyntaxEditor<'a> { impl<'a> SyntaxEditor<'a> {
@ -71,6 +71,20 @@ impl<'a> SyntaxEditor<'a> {
self.theme = theme; self.theme = theme;
self.highlighter = Highlighter::new(theme); self.highlighter = Highlighter::new(theme);
self.syntax_cache.clear(); self.syntax_cache.clear();
// Reset attrs to match default foreground and no highlighting
for line in self.editor.buffer_mut().lines.iter_mut() {
let mut attrs = line.attrs_list().defaults();
if let Some(foreground) = self.theme.settings.foreground {
attrs = attrs.color(Color::rgba(
foreground.r,
foreground.g,
foreground.b,
foreground.a,
));
}
line.set_attrs_list(AttrsList::new(attrs));
}
} }
true true
@ -89,10 +103,20 @@ impl<'a> SyntaxEditor<'a> {
&mut self, &mut self,
font_system: &mut FontSystem, font_system: &mut FontSystem,
path: P, path: P,
attrs: crate::Attrs, mut attrs: crate::Attrs,
) -> io::Result<()> { ) -> io::Result<()> {
let path = path.as_ref(); let path = path.as_ref();
// Set attrs to match default foreground
if let Some(foreground) = self.theme.settings.foreground {
attrs = attrs.color(Color::rgba(
foreground.r,
foreground.g,
foreground.b,
foreground.a,
));
}
let text = fs::read_to_string(path)?; let text = fs::read_to_string(path)?;
self.editor self.editor
.buffer_mut() .buffer_mut()
@ -214,16 +238,13 @@ impl<'a> Edit for SyntaxEditor<'a> {
} }
highlighted += 1; highlighted += 1;
let (mut parse_state, mut highlight_state) = let (mut parse_state, scope_stack) = if line_i > 0 && line_i <= self.syntax_cache.len()
if line_i > 0 && line_i <= self.syntax_cache.len() { {
self.syntax_cache[line_i - 1].clone() self.syntax_cache[line_i - 1].clone()
} else { } else {
( (ParseState::new(self.syntax), ScopeStack::new())
ParseState::new(self.syntax), };
HighlightState::new(&self.highlighter, ScopeStack::new()), let mut highlight_state = HighlightState::new(&self.highlighter, scope_stack);
)
};
let ops = parse_state let ops = parse_state
.parse_line(line.text(), &self.syntax_system.syntax_set) .parse_line(line.text(), &self.syntax_system.syntax_set)
.expect("failed to parse syntax"); .expect("failed to parse syntax");
@ -237,27 +258,27 @@ impl<'a> Edit for SyntaxEditor<'a> {
let attrs = line.attrs_list().defaults(); let attrs = line.attrs_list().defaults();
let mut attrs_list = AttrsList::new(attrs); let mut attrs_list = AttrsList::new(attrs);
for (style, _, range) in ranges { for (style, _, range) in ranges {
attrs_list.add_span( let span_attrs = attrs
range, .color(Color::rgba(
attrs style.foreground.r,
.color(Color::rgba( style.foreground.g,
style.foreground.r, style.foreground.b,
style.foreground.g, style.foreground.a,
style.foreground.b, ))
style.foreground.a, //TODO: background
)) .style(if style.font_style.contains(FontStyle::ITALIC) {
//TODO: background Style::Italic
.style(if style.font_style.contains(FontStyle::ITALIC) { } else {
Style::Italic Style::Normal
} else { })
Style::Normal .weight(if style.font_style.contains(FontStyle::BOLD) {
}) Weight::BOLD
.weight(if style.font_style.contains(FontStyle::BOLD) { } else {
Weight::BOLD Weight::NORMAL
} else { }); //TODO: underline
Weight::NORMAL if span_attrs != attrs {
}), //TODO: underline attrs_list.add_span(range, span_attrs);
); }
} }
// Update line attributes. This operation only resets if the line changes // Update line attributes. This operation only resets if the line changes
@ -274,7 +295,7 @@ impl<'a> Edit for SyntaxEditor<'a> {
} }
} }
let cache_item = (parse_state.clone(), highlight_state.clone()); let cache_item = (parse_state.clone(), highlight_state.path.clone());
if line_i < self.syntax_cache.len() { if line_i < self.syntax_cache.len() {
if self.syntax_cache[line_i] != cache_item { if self.syntax_cache[line_i] != cache_item {
self.syntax_cache[line_i] = cache_item; self.syntax_cache[line_i] = cache_item;