Only set language_used after a full syntactic diff

This fixes cases where the language is detected but the file hits the
byte limit.

Fixes #462.
pull/464/head
Wilfred Hughes 2023-01-14 12:52:08 +07:00
parent 6eed874362
commit efec759504
4 changed files with 17 additions and 10 deletions

@ -4,6 +4,11 @@
Improved CSS parsing and HTML sublanguage parsing. Improved CSS parsing and HTML sublanguage parsing.
### Display
Fixed an issue with unwanted underlines with textual diffing when
DFT_BYTE_LIMIT is reached.
### Build ### Build
Renamed the vendored parser directory to vendored_parsers/, as `cargo Renamed the vendored parser directory to vendored_parsers/, as `cargo

@ -71,7 +71,7 @@ sample_files/html_simple_before.html sample_files/html_simple_after.html
ce3bfa12bc21d0eb5528766e18387e86 - ce3bfa12bc21d0eb5528766e18387e86 -
sample_files/huge_cpp_before.cpp sample_files/huge_cpp_after.cpp sample_files/huge_cpp_before.cpp sample_files/huge_cpp_after.cpp
5ce6b92e727e0cd4cb2bf40272c4b952 - 8910dbf7dae13b1a7229b0497602b414 -
sample_files/identical_before.scala sample_files/identical_after.scala sample_files/identical_before.scala sample_files/identical_after.scala
9c7319f61833e46a0a8cb6c01cc997c9 - 9c7319f61833e46a0a8cb6c01cc997c9 -

@ -297,7 +297,7 @@ fn diff_file_content(
lhs_display_path: lhs_display_path.into(), lhs_display_path: lhs_display_path.into(),
rhs_display_path: rhs_display_path.into(), rhs_display_path: rhs_display_path.into(),
display_language: None, display_language: None,
detected_language: None, language_used: None,
lhs_src: FileContent::Binary, lhs_src: FileContent::Binary,
rhs_src: FileContent::Binary, rhs_src: FileContent::Binary,
lhs_positions: vec![], lhs_positions: vec![],
@ -326,7 +326,7 @@ fn diff_file_content(
FileArgument::DevNull => (&lhs_src, Path::new(&lhs_display_path)), FileArgument::DevNull => (&lhs_src, Path::new(&lhs_display_path)),
}; };
let mut language = language_override.or_else(|| guess(guess_path, guess_src)); let language = language_override.or_else(|| guess(guess_path, guess_src));
let lang_config = language.map(tsp::from_language); let lang_config = language.map(tsp::from_language);
if lhs_bytes == rhs_bytes { if lhs_bytes == rhs_bytes {
@ -336,7 +336,7 @@ fn diff_file_content(
lhs_display_path: lhs_display_path.into(), lhs_display_path: lhs_display_path.into(),
rhs_display_path: rhs_display_path.into(), rhs_display_path: rhs_display_path.into(),
display_language: language.map(|l| language_name(l).into()), display_language: language.map(|l| language_name(l).into()),
detected_language: language, language_used: language,
lhs_src: FileContent::Text("".into()), lhs_src: FileContent::Text("".into()),
rhs_src: FileContent::Text("".into()), rhs_src: FileContent::Text("".into()),
lhs_positions: vec![], lhs_positions: vec![],
@ -347,6 +347,7 @@ fn diff_file_content(
}; };
} }
let mut language_used = None;
let (lang_name, lhs_positions, rhs_positions) = match lang_config { let (lang_name, lhs_positions, rhs_positions) = match lang_config {
_ if lhs_bytes.len() > diff_options.byte_limit _ if lhs_bytes.len() > diff_options.byte_limit
|| rhs_bytes.len() > diff_options.byte_limit => || rhs_bytes.len() > diff_options.byte_limit =>
@ -370,11 +371,12 @@ fn diff_file_content(
let lang_name = language.map(|l| language_name(l).into()); let lang_name = language.map(|l| language_name(l).into());
let has_syntactic_changes = lhs != rhs; let has_syntactic_changes = lhs != rhs;
language_used = language;
return DiffResult { return DiffResult {
lhs_display_path: lhs_display_path.into(), lhs_display_path: lhs_display_path.into(),
rhs_display_path: rhs_display_path.into(), rhs_display_path: rhs_display_path.into(),
display_language: lang_name, display_language: lang_name,
detected_language: language, language_used,
lhs_src: FileContent::Text(lhs_src), lhs_src: FileContent::Text(lhs_src),
rhs_src: FileContent::Text(rhs_src), rhs_src: FileContent::Text(rhs_src),
lhs_positions: vec![], lhs_positions: vec![],
@ -415,13 +417,13 @@ fn diff_file_content(
if exceeded_graph_limit { if exceeded_graph_limit {
let lhs_positions = line_parser::change_positions(&lhs_src, &rhs_src); let lhs_positions = line_parser::change_positions(&lhs_src, &rhs_src);
let rhs_positions = line_parser::change_positions(&rhs_src, &lhs_src); let rhs_positions = line_parser::change_positions(&rhs_src, &lhs_src);
language = None;
( (
Some("Text (exceeded DFT_GRAPH_LIMIT)".into()), Some("Text (exceeded DFT_GRAPH_LIMIT)".into()),
lhs_positions, lhs_positions,
rhs_positions, rhs_positions,
) )
} else { } else {
language_used = language;
// TODO: Make this .expect() unnecessary. // TODO: Make this .expect() unnecessary.
let language = let language =
language.expect("If we had a ts_lang, we must have guessed the language"); language.expect("If we had a ts_lang, we must have guessed the language");
@ -462,7 +464,7 @@ fn diff_file_content(
lhs_display_path: lhs_display_path.into(), lhs_display_path: lhs_display_path.into(),
rhs_display_path: rhs_display_path.into(), rhs_display_path: rhs_display_path.into(),
display_language: lang_name, display_language: lang_name,
detected_language: language, language_used,
lhs_src: FileContent::Text(lhs_src), lhs_src: FileContent::Text(lhs_src),
rhs_src: FileContent::Text(rhs_src), rhs_src: FileContent::Text(rhs_src),
lhs_positions, lhs_positions,
@ -578,7 +580,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
&summary.lhs_display_path, &summary.lhs_display_path,
&summary.rhs_display_path, &summary.rhs_display_path,
&display_language, &display_language,
summary.detected_language, summary.language_used,
); );
} }
DisplayMode::SideBySide | DisplayMode::SideBySideShowBoth => { DisplayMode::SideBySide | DisplayMode::SideBySideShowBoth => {
@ -588,7 +590,7 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
&summary.lhs_display_path, &summary.lhs_display_path,
&summary.rhs_display_path, &summary.rhs_display_path,
&display_language, &display_language,
summary.detected_language, summary.language_used,
lhs_src, lhs_src,
rhs_src, rhs_src,
&summary.lhs_positions, &summary.lhs_positions,

@ -13,7 +13,7 @@ pub struct DiffResult {
pub lhs_display_path: String, pub lhs_display_path: String,
pub rhs_display_path: String, pub rhs_display_path: String,
pub display_language: Option<String>, pub display_language: Option<String>,
pub detected_language: Option<crate::parse::guess_language::Language>, pub language_used: Option<crate::parse::guess_language::Language>,
pub lhs_src: FileContent, pub lhs_src: FileContent,
pub rhs_src: FileContent, pub rhs_src: FileContent,
pub hunks: Vec<Hunk>, pub hunks: Vec<Hunk>,