on ccdea6a help sign in
bat/src/syntax_mapping.rs
180 lines 4.9 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 // Nginx and Apache syntax files both want to style all ".conf" files
52 // see #1131 and #1137
53 mapping
54 .insert("*.conf", MappingTarget::MapToUnknown)
55 .unwrap();
57 for glob in &[
58 "/etc/nginx/**/*.conf",
59 "/etc/nginx/sites-*/**/*",
60 "nginx.conf",
61 "mime.types",
62 ] {
63 mapping.insert(glob, MappingTarget::MapTo("nginx")).unwrap();
64 }
66 for glob in &[
67 "/etc/apache2/**/*.conf",
68 "/etc/apache2/sites-*/**/*",
69 "httpd.conf",
70 ] {
71 mapping
72 .insert(glob, MappingTarget::MapTo("Apache Conf"))
73 .unwrap();
74 }
76 for glob in [
77 "**/systemd/**/*.conf",
78 "**/systemd/**/*.example",
79 "*.automount",
80 "*.device",
81 "*.dnssd",
82 "*.link",
83 "*.mount",
84 "*.netdev",
85 "*.network",
86 "*.nspawn",
87 "*.path",
88 "*.service",
89 "*.scope",
90 "*.slice",
91 "*.socket",
92 "*.swap",
93 "*.target",
94 "*.timer",
95 ].iter() {
96 mapping
97 .insert(glob, MappingTarget::MapTo("INI"))
98 .unwrap();
99 }
101 // pacman hooks
102 mapping.insert("*.hook", MappingTarget::MapTo("INI")).unwrap();
104 mapping
105 }
107 pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
108 let glob = GlobBuilder::new(from)
109 .case_insensitive(false)
110 .literal_separator(true)
111 .build()?;
112 self.mappings.push((glob.compile_matcher(), to));
113 Ok(())
114 }
116 pub fn mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
117 &self.mappings
118 }
120 pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
121 let candidate = Candidate::new(path.as_ref());
122 let candidate_filename = path.as_ref().file_name().map(Candidate::new);
123 for (ref glob, ref syntax) in self.mappings.iter().rev() {
124 if glob.is_match_candidate(&candidate)
125 || candidate_filename
126 .as_ref()
127 .map_or(false, |filename| glob.is_match_candidate(filename))
128 {
129 return Some(*syntax);
130 }
131 }
132 None
133 }
136#[test]
137fn basic() {
138 let mut map = SyntaxMapping::empty();
139 map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
140 .ok();
141 map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
142 .ok();
144 assert_eq!(
145 map.get_syntax_for("/path/to/Cargo.lock"),
146 Some(MappingTarget::MapTo("TOML"))
147 );
148 assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
150 assert_eq!(
151 map.get_syntax_for("/path/to/.ignore"),
152 Some(MappingTarget::MapTo("Git Ignore"))
153 );
156#[test]
157fn user_can_override_builtin_mappings() {
158 let mut map = SyntaxMapping::builtin();
160 assert_eq!(
161 map.get_syntax_for("/etc/profile"),
162 Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
163 );
164 map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
165 .ok();
166 assert_eq!(
167 map.get_syntax_for("/etc/profile"),
168 Some(MappingTarget::MapTo("My Syntax"))
169 );
172#[test]
173fn builtin_mappings() {
174 let map = SyntaxMapping::builtin();
176 assert_eq!(
177 map.get_syntax_for("/path/to/build"),
178 Some(MappingTarget::MapToUnknown)
179 );