Pass the detected language to the styling logic

pull/381/head
Wilfred Hughes 2022-09-15 09:27:34 +07:00
parent 3c51f58d8e
commit 7d849582ce
5 changed files with 30 additions and 6 deletions

@ -6,7 +6,7 @@ use crate::{
display::style::{self, apply_colors},
lines::{format_line_num, split_on_newlines, MaxLine},
options::DisplayOptions,
parse::syntax::MatchedPos,
parse::{guess_language::Language, syntax::MatchedPos},
};
use owo_colors::colored::*;
@ -20,6 +20,7 @@ pub fn print(
lhs_display_path: &str,
rhs_display_path: &str,
lang_name: &str,
language: Option<Language>,
) {
let (lhs_colored_lines, rhs_colored_lines) = if display_options.use_color {
(
@ -27,6 +28,7 @@ pub fn print(
lhs_src,
true,
display_options.syntax_highlight,
language,
display_options.background_color,
lhs_positions,
),
@ -34,6 +36,7 @@ pub fn print(
rhs_src,
false,
display_options.syntax_highlight,
language,
display_options.background_color,
rhs_positions,
),

@ -13,7 +13,10 @@ use crate::{
},
lines::{codepoint_len, format_line_num, split_on_newlines, LineNumber},
options::{DisplayMode, DisplayOptions},
parse::syntax::{zip_pad_shorter, MatchedPos},
parse::{
guess_language::Language,
syntax::{zip_pad_shorter, MatchedPos},
},
positions::SingleLineSpan,
};
@ -255,13 +258,14 @@ pub fn lines_with_novel(
fn highlight_positions(
background: BackgroundColor,
syntax_highlight: bool,
language: Option<Language>,
lhs_mps: &[MatchedPos],
rhs_mps: &[MatchedPos],
) -> (
FxHashMap<LineNumber, Vec<(SingleLineSpan, Style)>>,
FxHashMap<LineNumber, Vec<(SingleLineSpan, Style)>>,
) {
let lhs_positions = color_positions(true, background, syntax_highlight, lhs_mps);
let lhs_positions = color_positions(true, background, syntax_highlight, language, lhs_mps);
// Preallocate the hashmap assuming the average line will have 2 items on it.
let mut lhs_styles: FxHashMap<LineNumber, Vec<(SingleLineSpan, Style)>> = FxHashMap::default();
for (span, style) in lhs_positions {
@ -269,7 +273,7 @@ fn highlight_positions(
styles.push((span, style));
}
let rhs_positions = color_positions(false, background, syntax_highlight, rhs_mps);
let rhs_positions = color_positions(false, background, syntax_highlight, language, rhs_mps);
let mut rhs_styles: FxHashMap<LineNumber, Vec<(SingleLineSpan, Style)>> = FxHashMap::default();
for (span, style) in rhs_positions {
let styles = rhs_styles.entry(span.line).or_insert_with(Vec::new);
@ -309,6 +313,7 @@ pub fn print(
lhs_display_path: &str,
rhs_display_path: &str,
lang_name: &str,
language: Option<Language>,
lhs_src: &str,
rhs_src: &str,
lhs_mps: &[MatchedPos],
@ -320,6 +325,7 @@ pub fn print(
lhs_src,
true,
display_options.syntax_highlight,
language,
display_options.background_color,
lhs_mps,
),
@ -327,6 +333,7 @@ pub fn print(
rhs_src,
false,
display_options.syntax_highlight,
language,
display_options.background_color,
rhs_mps,
),
@ -378,6 +385,7 @@ pub fn print(
highlight_positions(
display_options.background_color,
display_options.syntax_highlight,
language,
lhs_mps,
rhs_mps,
)
@ -734,6 +742,7 @@ mod tests {
"foo-old.el",
"foo-new.el",
"Emacs Lisp",
Some(Language::EmacsLisp),
"foo",
"bar",
&lhs_mps,

@ -4,7 +4,10 @@ use crate::{
constants::Side,
lines::{byte_len, split_on_newlines, LineNumber},
options::DisplayOptions,
parse::syntax::{AtomKind, MatchKind, MatchedPos, TokenKind},
parse::{
guess_language::Language,
syntax::{AtomKind, MatchKind, MatchedPos, TokenKind},
},
positions::SingleLineSpan,
};
use owo_colors::{OwoColorize, Style};
@ -267,6 +270,7 @@ pub fn color_positions(
is_lhs: bool,
background: BackgroundColor,
syntax_highlight: bool,
_language: Option<Language>,
positions: &[MatchedPos],
) -> Vec<(SingleLineSpan, Style)> {
let mut styles = vec![];
@ -338,10 +342,11 @@ pub fn apply_colors(
s: &str,
is_lhs: bool,
syntax_highlight: bool,
language: Option<Language>,
background: BackgroundColor,
positions: &[MatchedPos],
) -> Vec<String> {
let styles = color_positions(is_lhs, background, syntax_highlight, positions);
let styles = color_positions(is_lhs, background, syntax_highlight, language, positions);
let lines = split_on_newlines(s);
style_lines(&lines, &styles)
}

@ -251,6 +251,7 @@ fn diff_file_content(
lhs_display_path: lhs_display_path.into(),
rhs_display_path: rhs_display_path.into(),
language: None,
detected_language: None,
lhs_src: FileContent::Binary(lhs_bytes.to_vec()),
rhs_src: FileContent::Binary(rhs_bytes.to_vec()),
lhs_positions: vec![],
@ -291,6 +292,7 @@ fn diff_file_content(
lhs_display_path: lhs_display_path.into(),
rhs_display_path: rhs_display_path.into(),
language: language.map(|l| language_name(l).into()),
detected_language: language,
lhs_src: FileContent::Text("".into()),
rhs_src: FileContent::Text("".into()),
lhs_positions: vec![],
@ -377,6 +379,7 @@ fn diff_file_content(
lhs_display_path: lhs_display_path.into(),
rhs_display_path: rhs_display_path.into(),
language: lang_name,
detected_language: language,
lhs_src: FileContent::Text(lhs_src),
rhs_src: FileContent::Text(rhs_src),
lhs_positions,
@ -477,6 +480,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
&summary.lhs_display_path,
&summary.rhs_display_path,
&lang_name,
summary.detected_language,
);
}
DisplayMode::SideBySide | DisplayMode::SideBySideShowBoth => {
@ -486,6 +490,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
&summary.lhs_display_path,
&summary.rhs_display_path,
&lang_name,
summary.detected_language,
lhs_src,
rhs_src,
&summary.lhs_positions,

@ -12,7 +12,9 @@ pub enum FileContent {
pub struct DiffResult {
pub lhs_display_path: String,
pub rhs_display_path: String,
// TODO: rename to display_language.
pub language: Option<String>,
pub detected_language: Option<crate::parse::guess_language::Language>,
pub lhs_src: FileContent,
pub rhs_src: FileContent,
pub lhs_positions: Vec<MatchedPos>,