bat_impl/
syntax_mapping.rs

1use std::path::Path;
2
3use crate::error::Result;
4use ignored_suffixes::IgnoredSuffixes;
5
6use globset::{Candidate, GlobBuilder, GlobMatcher};
7
8pub mod ignored_suffixes;
9
10#[derive(Debug, Clone, Copy, PartialEq)]
11#[non_exhaustive]
12pub enum MappingTarget<'a> {
13    /// For mapping a path to a specific syntax.
14    MapTo(&'a str),
15
16    /// For mapping a path (typically an extension-less file name) to an unknown
17    /// syntax. This typically means later using the contents of the first line
18    /// of the file to determine what syntax to use.
19    MapToUnknown,
20
21    /// For mapping a file extension (e.g. `*.conf`) to an unknown syntax. This
22    /// typically means later using the contents of the first line of the file
23    /// to determine what syntax to use. However, if a syntax handles a file
24    /// name that happens to have the given file extension (e.g. `resolv.conf`),
25    /// then that association will have higher precedence, and the mapping will
26    /// be ignored.
27    MapExtensionToUnknown,
28}
29
30#[derive(Debug, Clone, Default)]
31pub struct SyntaxMapping<'a> {
32    mappings: Vec<(GlobMatcher, MappingTarget<'a>)>,
33    pub(crate) ignored_suffixes: IgnoredSuffixes<'a>,
34}
35
36impl<'a> SyntaxMapping<'a> {
37    pub fn empty() -> SyntaxMapping<'a> {
38        Default::default()
39    }
40
41    pub fn builtin() -> SyntaxMapping<'a> {
42        let mut mapping = Self::empty();
43        mapping.insert("*.h", MappingTarget::MapTo("C++")).unwrap();
44        mapping
45            .insert(".clang-format", MappingTarget::MapTo("YAML"))
46            .unwrap();
47        mapping.insert("*.fs", MappingTarget::MapTo("F#")).unwrap();
48        mapping
49            .insert("build", MappingTarget::MapToUnknown)
50            .unwrap();
51        mapping
52            .insert("**/.ssh/config", MappingTarget::MapTo("SSH Config"))
53            .unwrap();
54        mapping
55            .insert(
56                "**/bat/config",
57                MappingTarget::MapTo("Bourne Again Shell (bash)"),
58            )
59            .unwrap();
60        mapping
61            .insert(
62                "/etc/profile",
63                MappingTarget::MapTo("Bourne Again Shell (bash)"),
64            )
65            .unwrap();
66        mapping
67            .insert("*.pac", MappingTarget::MapTo("JavaScript (Babel)"))
68            .unwrap();
69
70        // See #2151, https://nmap.org/book/nse-language.html
71        mapping
72            .insert("*.nse", MappingTarget::MapTo("Lua"))
73            .unwrap();
74
75        // See #1008
76        mapping
77            .insert("rails", MappingTarget::MapToUnknown)
78            .unwrap();
79
80        // Nginx and Apache syntax files both want to style all ".conf" files
81        // see #1131 and #1137
82        mapping
83            .insert("*.conf", MappingTarget::MapExtensionToUnknown)
84            .unwrap();
85
86        for glob in &[
87            "/etc/nginx/**/*.conf",
88            "/etc/nginx/sites-*/**/*",
89            "nginx.conf",
90            "mime.types",
91        ] {
92            mapping.insert(glob, MappingTarget::MapTo("nginx")).unwrap();
93        }
94
95        for glob in &[
96            "/etc/apache2/**/*.conf",
97            "/etc/apache2/sites-*/**/*",
98            "httpd.conf",
99        ] {
100            mapping
101                .insert(glob, MappingTarget::MapTo("Apache Conf"))
102                .unwrap();
103        }
104
105        for glob in &[
106            "**/systemd/**/*.conf",
107            "**/systemd/**/*.example",
108            "*.automount",
109            "*.device",
110            "*.dnssd",
111            "*.link",
112            "*.mount",
113            "*.netdev",
114            "*.network",
115            "*.nspawn",
116            "*.path",
117            "*.service",
118            "*.scope",
119            "*.slice",
120            "*.socket",
121            "*.swap",
122            "*.target",
123            "*.timer",
124        ] {
125            mapping.insert(glob, MappingTarget::MapTo("INI")).unwrap();
126        }
127
128        // unix mail spool
129        for glob in &["/var/spool/mail/*", "/var/mail/*"] {
130            mapping.insert(glob, MappingTarget::MapTo("Email")).unwrap()
131        }
132
133        // pacman hooks
134        mapping
135            .insert("*.hook", MappingTarget::MapTo("INI"))
136            .unwrap();
137
138        // Global git config files rooted in `$XDG_CONFIG_HOME/git/` or `$HOME/.config/git/`
139        // See e.g. https://git-scm.com/docs/git-config#FILES
140        if let Some(xdg_config_home) =
141            std::env::var_os("XDG_CONFIG_HOME").filter(|val| !val.is_empty())
142        {
143            insert_git_config_global(&mut mapping, &xdg_config_home);
144        }
145        if let Some(default_config_home) = std::env::var_os("HOME")
146            .filter(|val| !val.is_empty())
147            .map(|home| Path::new(&home).join(".config"))
148        {
149            insert_git_config_global(&mut mapping, &default_config_home);
150        }
151
152        fn insert_git_config_global(mapping: &mut SyntaxMapping, config_home: impl AsRef<Path>) {
153            let git_config_path = config_home.as_ref().join("git");
154
155            mapping
156                .insert(
157                    &git_config_path.join("config").to_string_lossy(),
158                    MappingTarget::MapTo("Git Config"),
159                )
160                .ok();
161
162            mapping
163                .insert(
164                    &git_config_path.join("ignore").to_string_lossy(),
165                    MappingTarget::MapTo("Git Ignore"),
166                )
167                .ok();
168
169            mapping
170                .insert(
171                    &git_config_path.join("attributes").to_string_lossy(),
172                    MappingTarget::MapTo("Git Attributes"),
173                )
174                .ok();
175        }
176
177        mapping
178    }
179
180    pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
181        let glob = GlobBuilder::new(from)
182            .case_insensitive(false)
183            .literal_separator(true)
184            .build()?;
185        self.mappings.push((glob.compile_matcher(), to));
186        Ok(())
187    }
188
189    pub fn mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
190        &self.mappings
191    }
192
193    pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
194        let candidate = Candidate::new(&path);
195        let candidate_filename = path.as_ref().file_name().map(Candidate::new);
196        for (ref glob, ref syntax) in self.mappings.iter().rev() {
197            if glob.is_match_candidate(&candidate)
198                || candidate_filename
199                    .as_ref()
200                    .map_or(false, |filename| glob.is_match_candidate(filename))
201            {
202                return Some(*syntax);
203            }
204        }
205        None
206    }
207
208    pub fn insert_ignored_suffix(&mut self, suffix: &'a str) {
209        self.ignored_suffixes.add_suffix(suffix);
210    }
211}
212
213#[test]
214fn basic() {
215    let mut map = SyntaxMapping::empty();
216    map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
217        .ok();
218    map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
219        .ok();
220
221    assert_eq!(
222        map.get_syntax_for("/path/to/Cargo.lock"),
223        Some(MappingTarget::MapTo("TOML"))
224    );
225    assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
226
227    assert_eq!(
228        map.get_syntax_for("/path/to/.ignore"),
229        Some(MappingTarget::MapTo("Git Ignore"))
230    );
231}
232
233#[test]
234fn user_can_override_builtin_mappings() {
235    let mut map = SyntaxMapping::builtin();
236
237    assert_eq!(
238        map.get_syntax_for("/etc/profile"),
239        Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
240    );
241    map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
242        .ok();
243    assert_eq!(
244        map.get_syntax_for("/etc/profile"),
245        Some(MappingTarget::MapTo("My Syntax"))
246    );
247}
248
249#[test]
250fn builtin_mappings() {
251    let map = SyntaxMapping::builtin();
252
253    assert_eq!(
254        map.get_syntax_for("/path/to/build"),
255        Some(MappingTarget::MapToUnknown)
256    );
257}