From 59140b458c98fd9585b6c7207a106d73bbbb39f1 Mon Sep 17 00:00:00 2001 From: Ethan P Date: Wed, 13 May 2020 01:56:19 -0700 Subject: [PATCH] Consolidate syntax detection behavior for all InputKind types --- src/assets.rs | 91 ++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 52 deletions(-) diff --git a/src/assets.rs b/src/assets.rs index 00a1c2f6..0448df09 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -191,63 +191,50 @@ impl HighlightingAssets { input: &mut OpenedInput, mapping: &SyntaxMapping, ) -> &SyntaxReference { - let syntax = if let Some(language) = language { + let syntax = if match input.kind { + OpenedInputKind::ThemePreviewFile => true, + _ => false, + } { + // FIXME: replace the above match statement with matches macro when min Rust >= 1.42.0 + self.syntax_set.find_syntax_by_name("Rust") + } else if let Some(language) = language { self.syntax_set.find_syntax_by_token(language) } else { - match input.kind { - OpenedInputKind::OrdinaryFile(ref actual_path) => { - let path_str = input - .metadata - .user_provided_name - .as_ref() - .unwrap_or(actual_path); - let path = Path::new(path_str); - let line_syntax = self.get_first_line_syntax(&mut input.reader); - - let absolute_path = path.canonicalize().ok().unwrap_or_else(|| path.to_owned()); - match mapping.get_syntax_for(absolute_path) { - Some(MappingTarget::MapTo(syntax_name)) => { - // TODO: we should probably return an error here if this syntax can not be - // found. Currently, we just fall back to 'plain'. - self.syntax_set.find_syntax_by_name(syntax_name) - } - Some(MappingTarget::MapToUnknown) => line_syntax, - None => { - let file_name = path.file_name().unwrap_or_default(); - self.get_extension_syntax(file_name).or(line_syntax) - } + let line_syntax = self.get_first_line_syntax(&mut input.reader); + + // Get the path of the file: + // If this was set by the metadata, that will take priority. + // If it wasn't, it will use the real file path (if available). + let path_str = + input + .metadata + .user_provided_name + .as_ref() + .or_else(|| match input.kind { + OpenedInputKind::OrdinaryFile(ref path) => Some(path), + _ => None, + }); + + if let Some(path_str) = path_str { + // If a path was provided, we try and detect the syntax based on extension mappings. + let path = Path::new(path_str); + let absolute_path = path.canonicalize().ok().unwrap_or_else(|| path.to_owned()); + + match mapping.get_syntax_for(absolute_path) { + Some(MappingTarget::MapToUnknown) => line_syntax, + Some(MappingTarget::MapTo(syntax_name)) => { + // TODO: we should probably return an error here if this syntax can not be + // found. Currently, we just fall back to 'plain'. + self.syntax_set.find_syntax_by_name(syntax_name) } - } - 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)) - } else { - 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(absolute_path) { - Some(MappingTarget::MapTo(syntax_name)) => { - self.syntax_set.find_syntax_by_name(syntax_name) - } - Some(MappingTarget::MapToUnknown) => line_syntax, - None => { - 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) + None => { + let file_name = path.file_name().unwrap_or_default(); + self.get_extension_syntax(file_name).or(line_syntax) } } - OpenedInputKind::ThemePreviewFile => self.syntax_set.find_syntax_by_name("Rust"), + } else { + // If a path wasn't provided, we fall back to the detect first-line syntax. + line_syntax } };