From f39487fca9301c45eaf50af437a269d3c0600f61 Mon Sep 17 00:00:00 2001 From: Ethan P Date: Mon, 11 May 2020 19:22:13 -0700 Subject: [PATCH] Make syntax detection more consistent for Reader and File inputs --- src/assets.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/assets.rs b/src/assets.rs index 4f8556f0..58ac2c08 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -218,7 +218,7 @@ impl HighlightingAssets { } } } - OpenedInputKind::StdIn | OpenedInputKind::CustomReader => { + OpenedInputKind::StdIn => { if let Some(ref name) = input.metadata.user_provided_name { self.get_extension_syntax(&name) .or_else(|| self.get_first_line_syntax(&mut input.reader)) @@ -226,6 +226,29 @@ impl HighlightingAssets { self.get_first_line_syntax(&mut input.reader) } } + OpenedInputKind::CustomReader => { + if let Some(ref path_str) = input.metadata.user_provided_name { + let path = Path::new(path_str); + let absolute_path = + path.canonicalize().ok().unwrap_or_else(|| path.to_owned()); + let line_syntax = self.get_first_line_syntax(&mut input.reader); + + match mapping.get_syntax_for(path_str) { + Some(MappingTarget::MapTo(syntax_name)) => { + println!("Mapped {:?} as {:?}", path_str, syntax_name); + self.syntax_set.find_syntax_by_name(syntax_name) + } + Some(MappingTarget::MapToUnknown) => line_syntax, + None => { + println!("Test {:?}", path_str); + let file_name = path.file_name().unwrap_or_default(); + self.get_extension_syntax(file_name).or(line_syntax) + } + } + } else { + self.get_first_line_syntax(&mut input.reader) + } + } OpenedInputKind::ThemePreviewFile => self.syntax_set.find_syntax_by_name("Rust"), } };