@@ -191,63 +191,50 @@ impl HighlightingAssets {
191191 input: &mut OpenedInput,
192192 mapping: &SyntaxMapping,
193193 ) -> &SyntaxReference {
194− let syntax = if let Some(language) = language {
194+ let syntax = if match input.kind {
195+ OpenedInputKind::ThemePreviewFile => true,
196+ _ => false,
197+ } {
198+ // FIXME: replace the above match statement with matches macro when min Rust >= 1.42.0
199+ self.syntax_set.find_syntax_by_name("Rust")
200+ } else if let Some(language) = language {
195201 self.syntax_set.find_syntax_by_token(language)
196202 } else {
197− match input.kind {
198− OpenedInputKind::OrdinaryFile(ref actual_path) => {
199− let path_str = input
200− .metadata
201− .user_provided_name
202− .as_ref()
203− .unwrap_or(actual_path);
204− let path = Path::new(path_str);
205− let line_syntax = self.get_first_line_syntax(&mut input.reader);
206−
207− let absolute_path = path.canonicalize().ok().unwrap_or_else(|| path.to_owned());
208− match mapping.get_syntax_for(absolute_path) {
209− Some(MappingTarget::MapTo(syntax_name)) => {
210− // TODO: we should probably return an error here if this syntax can not be
211− // found. Currently, we just fall back to 'plain'.
212− self.syntax_set.find_syntax_by_name(syntax_name)
213− }
214− Some(MappingTarget::MapToUnknown) => line_syntax,
215− None => {
216− let file_name = path.file_name().unwrap_or_default();
217− self.get_extension_syntax(file_name).or(line_syntax)
218− }
203+ let line_syntax = self.get_first_line_syntax(&mut input.reader);
204+
205+ // Get the path of the file:
206+ // If this was set by the metadata, that will take priority.
207+ // If it wasn't, it will use the real file path (if available).
208+ let path_str =
209+ input
210+ .metadata
211+ .user_provided_name
212+ .as_ref()
213+ .or_else(|| match input.kind {
214+ OpenedInputKind::OrdinaryFile(ref path) => Some(path),
215+ _ => None,
216+ });
217+
218+ if let Some(path_str) = path_str {
219+ // If a path was provided, we try and detect the syntax based on extension mappings.
220+ let path = Path::new(path_str);
221+ let absolute_path = path.canonicalize().ok().unwrap_or_else(|| path.to_owned());
222+
223+ match mapping.get_syntax_for(absolute_path) {
224+ Some(MappingTarget::MapToUnknown) => line_syntax,
225+ Some(MappingTarget::MapTo(syntax_name)) => {
226+ // TODO: we should probably return an error here if this syntax can not be
227+ // found. Currently, we just fall back to 'plain'.
228+ self.syntax_set.find_syntax_by_name(syntax_name)
219229 }
220− }
221− OpenedInputKind::StdIn => {
222− if let Some(ref name) = input.metadata.user_provided_name {
223− self.get_extension_syntax(&name)
224− .or_else(|| self.get_first_line_syntax(&mut input.reader))
225− } else {
226− self.get_first_line_syntax(&mut input.reader)
227− }
228− }
229− OpenedInputKind::CustomReader => {
230− if let Some(ref path_str) = input.metadata.user_provided_name {
231− let path = Path::new(path_str);
232− let absolute_path =
233− path.canonicalize().ok().unwrap_or_else(|| path.to_owned());
234− let line_syntax = self.get_first_line_syntax(&mut input.reader);
235−
236− match mapping.get_syntax_for(absolute_path) {
237− Some(MappingTarget::MapTo(syntax_name)) => {
238− self.syntax_set.find_syntax_by_name(syntax_name)
239− }
240− Some(MappingTarget::MapToUnknown) => line_syntax,
241− None => {
242− let file_name = path.file_name().unwrap_or_default();
243− self.get_extension_syntax(file_name).or(line_syntax)
244− }
245− }
246− } else {
247− self.get_first_line_syntax(&mut input.reader)
230+ None => {
231+ let file_name = path.file_name().unwrap_or_default();
232+ self.get_extension_syntax(file_name).or(line_syntax)
248233 }
249234 }
250− OpenedInputKind::ThemePreviewFile => self.syntax_set.find_syntax_by_name("Rust"),
235+ } else {
236+ // If a path wasn't provided, we fall back to the detect first-line syntax.
237+ line_syntax
251238 }
252239 };
253240