on v0.17.1 help sign in
bat/src/syntax_mapping.rs
207 lines 5.8 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 ]
96 .iter()
97 {
98 mapping.insert(glob, MappingTarget::MapTo("INI")).unwrap();
99 }
101 // pacman hooks
102 mapping
103 .insert("*.hook", MappingTarget::MapTo("INI"))
104 .unwrap();
106 if let Some(xdg_config_home) = std::env::var_os("XDG_CONFIG_HOME") {
107 let git_config_path = Path::new(&xdg_config_home).join("git");
109 mapping
110 .insert(
111 &git_config_path.join("config").to_string_lossy(),
112 MappingTarget::MapTo("Git Config"),
113 )
114 .ok();
116 mapping
117 .insert(
118 &git_config_path.join("ignore").to_string_lossy(),
119 MappingTarget::MapTo("Git Ignore"),
120 )
121 .ok();
123 mapping
124 .insert(
125 &git_config_path.join("attributes").to_string_lossy(),
126 MappingTarget::MapTo("Git Attributes"),
127 )
128 .ok();
129 }
131 mapping
132 }
134 pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
135 let glob = GlobBuilder::new(from)
136 .case_insensitive(false)
137 .literal_separator(true)
138 .build()?;
139 self.mappings.push((glob.compile_matcher(), to));
140 Ok(())
141 }
143 pub fn mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
144 &self.mappings
145 }
147 pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
148 let candidate = Candidate::new(path.as_ref());
149 let candidate_filename = path.as_ref().file_name().map(Candidate::new);
150 for (ref glob, ref syntax) in self.mappings.iter().rev() {
151 if glob.is_match_candidate(&candidate)
152 || candidate_filename
153 .as_ref()
154 .map_or(false, |filename| glob.is_match_candidate(filename))
155 {
156 return Some(*syntax);
157 }
158 }
159 None
160 }
163#[test]
164fn basic() {
165 let mut map = SyntaxMapping::empty();
166 map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
167 .ok();
168 map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
169 .ok();
171 assert_eq!(
172 map.get_syntax_for("/path/to/Cargo.lock"),
173 Some(MappingTarget::MapTo("TOML"))
174 );
175 assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
177 assert_eq!(
178 map.get_syntax_for("/path/to/.ignore"),
179 Some(MappingTarget::MapTo("Git Ignore"))
180 );
183#[test]
184fn user_can_override_builtin_mappings() {
185 let mut map = SyntaxMapping::builtin();
187 assert_eq!(
188 map.get_syntax_for("/etc/profile"),
189 Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
190 );
191 map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
192 .ok();
193 assert_eq!(
194 map.get_syntax_for("/etc/profile"),
195 Some(MappingTarget::MapTo("My Syntax"))
196 );
199#[test]
200fn builtin_mappings() {
201 let map = SyntaxMapping::builtin();
203 assert_eq!(
204 map.get_syntax_for("/path/to/build"),
205 Some(MappingTarget::MapToUnknown)
206 );