on 0cc1084 help sign in
bat/src/syntax_mapping.rs
155 lines 4.2 kB history raw
1use std::path::Path;
3use crate::error::Result;
5use globset::{Candidate, GlobBuilder, GlobMatcher};
7#[derive(Debug, Clone, Copy, PartialEq)]
8pub enum MappingTarget<'a> {
9 MapTo(&'a str),
10 MapToUnknown,
13#[derive(Debug, Clone, Default)]
14pub struct SyntaxMapping<'a> {
15 mappings: Vec<(GlobMatcher, MappingTarget<'a>)>,
18impl<'a> SyntaxMapping<'a> {
19 pub fn empty() -> SyntaxMapping<'a> {
20 Default::default()
21 }
23 pub fn builtin() -> SyntaxMapping<'a> {
24 let mut mapping = Self::empty();
25 mapping.insert("*.h", MappingTarget::MapTo("C++")).unwrap();
26 mapping.insert("*.fs", MappingTarget::MapTo("F#")).unwrap();
27 mapping
28 .insert("build", MappingTarget::MapToUnknown)
29 .unwrap();
30 mapping
31 .insert("**/.ssh/config", MappingTarget::MapTo("SSH Config"))
32 .unwrap();
33 mapping
34 .insert(
35 "**/bat/config",
36 MappingTarget::MapTo("Bourne Again Shell (bash)"),
37 )
38 .unwrap();
39 mapping
40 .insert(
41 "/etc/profile",
42 MappingTarget::MapTo("Bourne Again Shell (bash)"),
43 )
44 .unwrap();
46 // See #1008
47 mapping
48 .insert("rails", MappingTarget::MapToUnknown)
49 .unwrap();
51 for glob in [
52 "**/systemd/**/*.conf",
53 "**/systemd/**/*.example",
54 "*.automount",
55 "*.device",
56 "*.dnssd",
57 "*.link",
58 "*.mount",
59 "*.netdev",
60 "*.network",
61 "*.nspawn",
62 "*.path",
63 "*.service",
64 "*.scope",
65 "*.slice",
66 "*.socket",
67 "*.swap",
68 "*.target",
69 "*.timer",
70 ].iter() {
71 mapping
72 .insert(glob, MappingTarget::MapTo("INI"))
73 .unwrap();
74 }
76 // pacman hooks
77 mapping.insert("*.hook", MappingTarget::MapTo("INI")).unwrap();
79 mapping
80 }
82 pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
83 let glob = GlobBuilder::new(from)
84 .case_insensitive(false)
85 .literal_separator(true)
86 .build()?;
87 self.mappings.push((glob.compile_matcher(), to));
88 Ok(())
89 }
91 pub fn mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
92 &self.mappings
93 }
95 pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
96 let candidate = Candidate::new(path.as_ref());
97 let canddidate_filename = path.as_ref().file_name().map(Candidate::new);
98 for (ref glob, ref syntax) in self.mappings.iter().rev() {
99 if glob.is_match_candidate(&candidate)
100 || canddidate_filename
101 .as_ref()
102 .map_or(false, |filename| glob.is_match_candidate(filename))
103 {
104 return Some(*syntax);
105 }
106 }
107 None
108 }
111#[test]
112fn basic() {
113 let mut map = SyntaxMapping::empty();
114 map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
115 .ok();
116 map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
117 .ok();
119 assert_eq!(
120 map.get_syntax_for("/path/to/Cargo.lock"),
121 Some(MappingTarget::MapTo("TOML"))
122 );
123 assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
125 assert_eq!(
126 map.get_syntax_for("/path/to/.ignore"),
127 Some(MappingTarget::MapTo("Git Ignore"))
128 );
131#[test]
132fn user_can_override_builtin_mappings() {
133 let mut map = SyntaxMapping::builtin();
135 assert_eq!(
136 map.get_syntax_for("/etc/profile"),
137 Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
138 );
139 map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
140 .ok();
141 assert_eq!(
142 map.get_syntax_for("/etc/profile"),
143 Some(MappingTarget::MapTo("My Syntax"))
144 );
147#[test]
148fn builtin_mappings() {
149 let map = SyntaxMapping::builtin();
151 assert_eq!(
152 map.get_syntax_for("/path/to/build"),
153 Some(MappingTarget::MapToUnknown)
154 );