summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/delta.rs11
-rw-r--r--src/paint.rs27
2 files changed, 21 insertions, 17 deletions
diff --git a/src/delta.rs b/src/delta.rs
index 266417e2..2667c7ee 100644
--- a/src/delta.rs
+++ b/src/delta.rs
@@ -72,10 +72,7 @@ where
} else if line.starts_with("diff --git ") {
painter.paint_buffered_lines();
state = State::FileMeta;
- painter.syntax = match parse::get_file_extension_from_diff_line(&line) {
- Some(extension) => assets.syntax_set.find_syntax_by_extension(extension),
- None => None,
- };
+ painter.set_syntax(parse::get_file_extension_from_diff_line(&line));
} else if (line.starts_with("--- ") || line.starts_with("rename from "))
&& config.opt.file_style != cli::SectionStyle::Plain
{
@@ -88,15 +85,13 @@ where
handle_file_meta_header_line(&mut painter, &minus_file, &plus_file, config)?;
} else if line.starts_with("@@ ") {
state = State::HunkMeta;
- if painter.syntax.is_some() {
- painter.reset_highlighter();
- }
+ painter.set_highlighter();
if config.opt.hunk_style != cli::SectionStyle::Plain {
painter.emit()?;
handle_hunk_meta_line(&mut painter, &line, config)?;
continue;
}
- } else if state.is_in_hunk() && painter.syntax.is_some() {
+ } else if state.is_in_hunk() {
state = handle_hunk_line(&mut painter, &line, state, config);
painter.emit()?;
continue;
diff --git a/src/paint.rs b/src/paint.rs
index bd7dd354..d291417a 100644
--- a/src/paint.rs
+++ b/src/paint.rs
@@ -2,7 +2,7 @@ use std::io::Write;
use syntect::easy::HighlightLines;
use syntect::highlighting::{Style, StyleModifier};
-use syntect::parsing::SyntaxReference;
+use syntect::parsing::{SyntaxReference, SyntaxSet};
use unicode_segmentation::UnicodeSegmentation;
use crate::bat::assets::HighlightingAssets;
@@ -15,7 +15,7 @@ pub struct Painter<'a> {
pub minus_lines: Vec<String>,
pub plus_lines: Vec<String>,
pub writer: &'a mut dyn Write,
- pub syntax: Option<&'a SyntaxReference>,
+ pub syntax: &'a SyntaxReference,
pub highlighter: HighlightLines<'a>,
pub config: &'a config::Config<'a>,
pub output_buffer: String,
@@ -27,24 +27,33 @@ impl<'a> Painter<'a> {
config: &'a config::Config,
assets: &'a HighlightingAssets,
) -> Self {
- let dummy_highlighter = HighlightLines::new(
- assets.syntax_set.find_syntax_by_extension("txt").unwrap(),
- &assets.theme_set.themes[style::DEFAULT_LIGHT_THEME],
- );
+ let default_syntax = Self::get_syntax(config.syntax_set, None);
+ let dummy_theme = &assets.theme_set.themes[style::DEFAULT_LIGHT_THEME];
+ let dummy_highlighter = HighlightLines::new(default_syntax, dummy_theme);
Self {
minus_lines: Vec::new(),
plus_lines: Vec::new(),
output_buffer: String::new(),
- syntax: None,
+ syntax: default_syntax,
highlighter: dummy_highlighter,
writer,
config,
}
}
- pub fn reset_highlighter(&mut self) {
+ pub fn set_syntax(&mut self, extension: Option<&str>) {
+ self.syntax = Painter::get_syntax(self.config.syntax_set, extension);
+ }
+
+ fn get_syntax(syntax_set: &'a SyntaxSet, extension: Option<&str>) -> &'a SyntaxReference {
+ syntax_set
+ .find_syntax_by_extension(extension.unwrap_or("txt"))
+ .unwrap_or_else(|| Painter::get_syntax(syntax_set, Some("txt")))
+ }
+
+ pub fn set_highlighter(&mut self) {
if let Some(theme) = self.config.theme {
- self.highlighter = HighlightLines::new(self.syntax.unwrap(), theme)
+ self.highlighter = HighlightLines::new(self.syntax, theme)
};
}