summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilfred Hughes <me@wilfred.me.uk>2023-03-02 22:25:14 -0800
committerWilfred Hughes <me@wilfred.me.uk>2023-03-02 22:25:14 -0800
commit54eb22ea989a982decd749c0cceec0b0ab7fda12 (patch)
tree73df5e57fc4423ecfc4dde330eb2056417c33632
parentdae8c8a3b57aab57d93a60d53bd29859db80d8ba (diff)
Use FileFormat consistently everywhere0.44.0
-rw-r--r--src/display/inline.rs8
-rw-r--r--src/display/side_by_side.rs52
-rw-r--r--src/display/style.rs16
-rw-r--r--src/main.rs24
-rw-r--r--src/summary.rs3
5 files changed, 47 insertions, 56 deletions
diff --git a/src/display/inline.rs b/src/display/inline.rs
index cbe89c152..8e88127cd 100644
--- a/src/display/inline.rs
+++ b/src/display/inline.rs
@@ -7,7 +7,8 @@ use crate::{
display::style::{self, apply_colors, apply_line_number_color},
lines::{format_line_num, split_on_newlines, MaxLine},
options::DisplayOptions,
- parse::{guess_language::Language, syntax::MatchedPos}, summary::FileFormat,
+ parse::syntax::MatchedPos,
+ summary::FileFormat,
};
pub fn print(
@@ -20,7 +21,6 @@ pub fn print(
lhs_display_path: &str,
rhs_display_path: &str,
file_format: &FileFormat,
- language: Option<Language>,
) {
let (lhs_colored_lines, rhs_colored_lines) = if display_options.use_color {
(
@@ -28,7 +28,7 @@ pub fn print(
lhs_src,
Side::Left,
display_options.syntax_highlight,
- language,
+ file_format,
display_options.background_color,
lhs_positions,
),
@@ -36,7 +36,7 @@ pub fn print(
rhs_src,
Side::Right,
display_options.syntax_highlight,
- language,
+ file_format,
display_options.background_color,
rhs_positions,
),
diff --git a/src/display/side_by_side.rs b/src/display/side_by_side.rs
index 1f53642c7..478ff337e 100644
--- a/src/display/side_by_side.rs
+++ b/src/display/side_by_side.rs
@@ -17,10 +17,7 @@ use crate::{
},
lines::{codepoint_len, format_line_num, split_on_newlines, LineNumber},
options::{DisplayMode, DisplayOptions},
- parse::{
- guess_language::Language,
- syntax::{zip_pad_shorter, MatchedPos},
- },
+ parse::syntax::{zip_pad_shorter, MatchedPos},
positions::SingleLineSpan,
summary::FileFormat,
};
@@ -70,7 +67,7 @@ fn format_missing_line_num(
fn display_single_column(
lhs_display_path: &str,
rhs_display_path: &str,
- lang_name: &FileFormat,
+ file_format: &FileFormat,
src_lines: &[String],
side: Side,
display_options: &DisplayOptions,
@@ -85,7 +82,7 @@ fn display_single_column(
rhs_display_path,
1,
1,
- lang_name,
+ file_format,
display_options,
));
header_line.push('\n');
@@ -255,15 +252,20 @@ pub fn lines_with_novel(
fn highlight_positions(
background: BackgroundColor,
syntax_highlight: bool,
- language: Option<Language>,
+ file_format: &FileFormat,
lhs_mps: &[MatchedPos],
rhs_mps: &[MatchedPos],
) -> (
FxHashMap<LineNumber, Vec<(SingleLineSpan, Style)>>,
FxHashMap<LineNumber, Vec<(SingleLineSpan, Style)>>,
) {
- let lhs_positions =
- color_positions(Side::Left, background, syntax_highlight, language, lhs_mps);
+ let lhs_positions = color_positions(
+ Side::Left,
+ background,
+ syntax_highlight,
+ file_format,
+ 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 {
@@ -271,8 +273,13 @@ fn highlight_positions(
styles.push((span, style));
}
- let rhs_positions =
- color_positions(Side::Right, background, syntax_highlight, language, rhs_mps);
+ let rhs_positions = color_positions(
+ Side::Right,
+ background,
+ syntax_highlight,
+ file_format,
+ 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);
@@ -311,8 +318,7 @@ pub fn print(
display_options: &DisplayOptions,
lhs_display_path: &str,
rhs_display_path: &str,
- lang_name: &FileFormat,
- language: Option<Language>,
+ file_format: &FileFormat,
lhs_src: &str,
rhs_src: &str,
lhs_mps: &[MatchedPos],
@@ -324,7 +330,7 @@ pub fn print(
lhs_src,
Side::Left,
display_options.syntax_highlight,
- language,
+ file_format,
display_options.background_color,
lhs_mps,
),
@@ -332,7 +338,7 @@ pub fn print(
rhs_src,
Side::Right,
display_options.syntax_highlight,
- language,
+ file_format,
display_options.background_color,
rhs_mps,
),
@@ -354,7 +360,7 @@ pub fn print(
for line in display_single_column(
lhs_display_path,
rhs_display_path,
- lang_name,
+ file_format,
&rhs_colored_lines,
Side::Right,
display_options,
@@ -368,7 +374,7 @@ pub fn print(
for line in display_single_column(
lhs_display_path,
rhs_display_path,
- lang_name,
+ file_format,
&lhs_colored_lines,
Side::Left,
display_options,
@@ -384,7 +390,7 @@ pub fn print(
highlight_positions(
display_options.background_color,
display_options.syntax_highlight,
- language,
+ file_format,
lhs_mps,
rhs_mps,
)
@@ -410,7 +416,7 @@ pub fn print(
rhs_display_path,
i + 1,
hunks.len(),
- lang_name,
+ file_format,
display_options
)
);
@@ -593,7 +599,10 @@ pub fn print(
#[cfg(test)]
mod tests {
- use crate::syntax::{AtomKind, MatchKind, TokenKind};
+ use crate::{
+ parse::guess_language::Language,
+ syntax::{AtomKind, MatchKind, TokenKind},
+ };
use super::*;
use pretty_assertions::assert_eq;
@@ -719,8 +728,7 @@ mod tests {
&DisplayOptions::default(),
"foo-old.el",
"foo-new.el",
- &FileFormat::SupportedLanguage(Language::Python),
- Some(Language::EmacsLisp),
+ &FileFormat::SupportedLanguage(Language::EmacsLisp),
"foo",
"bar",
&lhs_mps,
diff --git a/src/display/style.rs b/src/display/style.rs
index 401eb089f..bc4221609 100644
--- a/src/display/style.rs
+++ b/src/display/style.rs
@@ -4,11 +4,9 @@ use crate::{
constants::Side,
lines::{byte_len, split_on_newlines, LineNumber},
options::DisplayOptions,
- parse::{
- guess_language::Language,
- syntax::{AtomKind, MatchKind, MatchedPos, TokenKind},
- },
- positions::SingleLineSpan, summary::FileFormat,
+ parse::syntax::{AtomKind, MatchKind, MatchedPos, TokenKind},
+ positions::SingleLineSpan,
+ summary::FileFormat,
};
use owo_colors::{OwoColorize, Style};
use rustc_hash::FxHashMap;
@@ -328,7 +326,7 @@ pub fn color_positions(
side: Side,
background: BackgroundColor,
syntax_highlight: bool,
- language: Option<Language>,
+ file_format: &FileFormat,
positions: &[MatchedPos],
) -> Vec<(SingleLineSpan, Style)> {
let mut styles = vec![];
@@ -384,7 +382,7 @@ pub fn color_positions(
// Underline novel words inside comments in code, but
// don't apply it to every single line in plaintext.
- if language.is_some() {
+ if matches!(file_format, FileFormat::SupportedLanguage(_)) {
style = style.underline();
}
@@ -408,11 +406,11 @@ pub fn apply_colors(
s: &str,
side: Side,
syntax_highlight: bool,
- language: Option<Language>,
+ file_format: &FileFormat,
background: BackgroundColor,
positions: &[MatchedPos],
) -> Vec<String> {
- let styles = color_positions(side, background, syntax_highlight, language, positions);
+ let styles = color_positions(side, background, syntax_highlight, file_format, positions);
let lines = split_on_newlines(s);
style_lines(&lines, &styles)
}
diff --git a/src/main.rs b/src/main.rs
index 55970850e..1c8190532 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -312,7 +312,6 @@ fn check_only_text(
lhs_display_path: lhs_display_path.into(),
rhs_display_path: rhs_display_path.into(),
file_format: file_format.clone(),
- language_used: None,
lhs_src: FileContent::Text(lhs_src.into()),
rhs_src: FileContent::Text(rhs_src.into()),
lhs_positions: vec![],
@@ -340,7 +339,6 @@ fn diff_file_content(
lhs_display_path: lhs_display_path.into(),
rhs_display_path: rhs_display_path.into(),
file_format: FileFormat::Binary,
- language_used: None,
lhs_src: FileContent::Binary,
rhs_src: FileContent::Binary,
lhs_positions: vec![],
@@ -384,7 +382,6 @@ fn diff_file_content(
lhs_display_path: lhs_display_path.into(),
rhs_display_path: rhs_display_path.into(),
file_format,
- language_used: language,
lhs_src: FileContent::Text("".into()),
rhs_src: FileContent::Text("".into()),
lhs_positions: vec![],
@@ -395,7 +392,6 @@ fn diff_file_content(
};
}
- let mut language_used = None;
let (file_format, lhs_positions, rhs_positions) = match lang_config {
None => {
let file_format = FileFormat::PlainText;
@@ -434,13 +430,10 @@ fn diff_file_content(
};
let has_syntactic_changes = lhs != rhs;
-
- language_used = language;
return DiffResult {
lhs_display_path: lhs_display_path.into(),
rhs_display_path: rhs_display_path.into(),
file_format,
- language_used,
lhs_src: FileContent::Text(lhs_src),
rhs_src: FileContent::Text(rhs_src),
lhs_positions: vec![],
@@ -491,7 +484,6 @@ fn diff_file_content(
rhs_positions,
)
} else {
- language_used = language;
// TODO: Make this .expect() unnecessary.
let language = language.expect(
"If we had a ts_lang, we must have guessed the language",
@@ -585,7 +577,6 @@ fn diff_file_content(
lhs_display_path: lhs_display_path.into(),
rhs_display_path: rhs_display_path.into(),
file_format,
- language_used,
lhs_src: FileContent::Text(lhs_src),
rhs_src: FileContent::Text(rhs_src),
lhs_positions,
@@ -641,7 +632,6 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
(FileContent::Text(lhs_src), FileContent::Text(rhs_src)) => {
let hunks = &summary.hunks;
- let display_language = &summary.file_format;
if !summary.has_syntactic_changes {
if display_options.print_unchanged {
println!(
@@ -651,11 +641,11 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
&summary.rhs_display_path,
1,
1,
- &display_language,
+ &summary.file_format,
display_options
)
);
- match display_language {
+ match summary.file_format {
_ if summary.lhs_src == summary.rhs_src => {
println!("No changes.\n");
}
@@ -678,11 +668,11 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
&summary.rhs_display_path,
1,
1,
- &display_language,
+ &summary.file_format,
display_options
)
);
- match display_language {
+ match summary.file_format {
FileFormat::SupportedLanguage(_) => {
println!("Has syntactic changes.\n");
}
@@ -705,8 +695,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
hunks,
&summary.lhs_display_path,
&summary.rhs_display_path,
- &display_language,
- summary.language_used,
+ &summary.file_format,
);
}
DisplayMode::SideBySide | DisplayMode::SideBySideShowBoth => {
@@ -715,8 +704,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
display_options,
&summary.lhs_display_path,
&summary.rhs_display_path,
- &display_language,
- summary.language_used,
+ &summary.file_format,
lhs_src,
rhs_src,
&summary.lhs_positions,
diff --git a/src/summary.rs b/src/summary.rs
index 9cccba95c..6a6692ef0 100644
--- a/src/summary.rs
+++ b/src/summary.rs
@@ -41,9 +41,6 @@ pub struct DiffResult {
pub rhs_display_path: String,
pub file_format: FileFormat,
- /// The language used to parse the file.
- pub language_used: Option<crate::parse::guess_language::Language>,
-
pub lhs_src: FileContent,
pub rhs_src: FileContent,
pub hunks: Vec<Hunk>,