on 02cd68d help sign in
bat/src/syntax_mapping.rs
182 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 ]
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 mapping
107 }
109 pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
110 let glob = GlobBuilder::new(from)
111 .case_insensitive(false)
112 .literal_separator(true)
113 .build()?;
114 self.mappings.push((glob.compile_matcher(), to));
115 Ok(())
116 }
118 pub fn mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
119 &self.mappings
120 }
122 pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
123 let candidate = Candidate::new(path.as_ref());
124 let candidate_filename = path.as_ref().file_name().map(Candidate::new);
125 for (ref glob, ref syntax) in self.mappings.iter().rev() {
126 if glob.is_match_candidate(&candidate)
127 || candidate_filename
128 .as_ref()
129 .map_or(false, |filename| glob.is_match_candidate(filename))
130 {
131 return Some(*syntax);
132 }
133 }
134 None
135 }
138#[test]
139fn basic() {
140 let mut map = SyntaxMapping::empty();
141 map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
142 .ok();
143 map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
144 .ok();
146 assert_eq!(
147 map.get_syntax_for("/path/to/Cargo.lock"),
148 Some(MappingTarget::MapTo("TOML"))
149 );
150 assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
152 assert_eq!(
153 map.get_syntax_for("/path/to/.ignore"),
154 Some(MappingTarget::MapTo("Git Ignore"))
155 );
158#[test]
159fn user_can_override_builtin_mappings() {
160 let mut map = SyntaxMapping::builtin();
162 assert_eq!(
163 map.get_syntax_for("/etc/profile"),
164 Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
165 );
166 map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
167 .ok();
168 assert_eq!(
169 map.get_syntax_for("/etc/profile"),
170 Some(MappingTarget::MapTo("My Syntax"))
171 );
174#[test]
175fn builtin_mappings() {
176 let map = SyntaxMapping::builtin();
178 assert_eq!(
179 map.get_syntax_for("/path/to/build"),
180 Some(MappingTarget::MapToUnknown)
181 );