From 3a1e398edc56133d8e749e9e4c84c878cc07441b Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Fri, 15 Nov 2024 23:11:25 -0800 Subject: [PATCH] Improve handling of named pipe arguments Use display_name in more places, and prefer file names with extensions when we have two arguments. Fixes #783 --- CHANGELOG.md | 4 ++++ src/main.rs | 9 ++++----- src/options.rs | 8 +++++++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7a118d0d9..2ffcebe692 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ Improved handling of multiline strings, which could cause a crash if they occurred at the end of the file. This was particularly noticeable with YAML. +### Parsing + +Improved language detection when one argument is a named pipe. + ### Syntax Highlighting Improved syntax highlighting, particularly for keywords. diff --git a/src/main.rs b/src/main.rs index dccdc7dcd8..4298fbea50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -546,13 +546,12 @@ fn diff_file_content( diff_options: &DiffOptions, overrides: &[(LanguageOverride, Vec)], ) -> DiffResult { - let (guess_src, guess_path) = match rhs_path { - FileArgument::NamedPath(path) => (&rhs_src, Path::new(path)), - FileArgument::Stdin => (&rhs_src, Path::new(&display_path)), - FileArgument::DevNull => (&lhs_src, Path::new(&display_path)), + let guess_src = match rhs_path { + FileArgument::DevNull => &lhs_src, + _ => &rhs_src, }; - let language = guess(guess_path, guess_src, overrides); + let language = guess(Path::new(display_path), guess_src, overrides); let lang_config = language.map(|lang| (lang, tsp::from_language(lang))); if lhs_src == rhs_src { diff --git a/src/options.rs b/src/options.rs index bd26e72b57..fea871582e 100644 --- a/src/options.rs +++ b/src/options.rs @@ -556,7 +556,13 @@ fn build_display_path(lhs_path: &FileArgument, rhs_path: &FileArgument) -> Strin match common_path_suffix(lhs, rhs) { Some(common_suffix) => common_suffix, - None => rhs.display().to_string(), + None => { + if rhs.extension().is_some() { + rhs.display().to_string() + } else { + lhs.display().to_string() + } + } } } (FileArgument::NamedPath(p), _) | (_, FileArgument::NamedPath(p)) => {