on 89521a5 help sign in
bat/src/syntax_mapping.rs
154 lines 4.1 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
27 .insert("build", MappingTarget::MapToUnknown)
28 .unwrap();
29 mapping
30 .insert("**/.ssh/config", MappingTarget::MapTo("SSH Config"))
31 .unwrap();
32 mapping
33 .insert(
34 "**/bat/config",
35 MappingTarget::MapTo("Bourne Again Shell (bash)"),
36 )
37 .unwrap();
38 mapping
39 .insert(
40 "/etc/profile",
41 MappingTarget::MapTo("Bourne Again Shell (bash)"),
42 )
43 .unwrap();
45 // See #1008
46 mapping
47 .insert("rails", MappingTarget::MapToUnknown)
48 .unwrap();
50 for glob in [
51 "**/systemd/**/*.conf",
52 "**/systemd/**/*.example",
53 "*.automount",
54 "*.device",
55 "*.dnssd",
56 "*.link",
57 "*.mount",
58 "*.netdev",
59 "*.network",
60 "*.nspawn",
61 "*.path",
62 "*.service",
63 "*.scope",
64 "*.slice",
65 "*.socket",
66 "*.swap",
67 "*.target",
68 "*.timer",
69 ].iter() {
70 mapping
71 .insert(glob, MappingTarget::MapTo("INI"))
72 .unwrap();
73 }
75 // pacman hooks
76 mapping.insert("*.hook", MappingTarget::MapTo("INI")).unwrap();
78 mapping
79 }
81 pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
82 let glob = GlobBuilder::new(from)
83 .case_insensitive(false)
84 .literal_separator(true)
85 .build()?;
86 self.mappings.push((glob.compile_matcher(), to));
87 Ok(())
88 }
90 pub fn mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
91 &self.mappings
92 }
94 pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
95 let candidate = Candidate::new(path.as_ref());
96 let canddidate_filename = path.as_ref().file_name().map(Candidate::new);
97 for (ref glob, ref syntax) in self.mappings.iter().rev() {
98 if glob.is_match_candidate(&candidate)
99 || canddidate_filename
100 .as_ref()
101 .map_or(false, |filename| glob.is_match_candidate(filename))
102 {
103 return Some(*syntax);
104 }
105 }
106 None
107 }
110#[test]
111fn basic() {
112 let mut map = SyntaxMapping::empty();
113 map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
114 .ok();
115 map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
116 .ok();
118 assert_eq!(
119 map.get_syntax_for("/path/to/Cargo.lock"),
120 Some(MappingTarget::MapTo("TOML"))
121 );
122 assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
124 assert_eq!(
125 map.get_syntax_for("/path/to/.ignore"),
126 Some(MappingTarget::MapTo("Git Ignore"))
127 );
130#[test]
131fn user_can_override_builtin_mappings() {
132 let mut map = SyntaxMapping::builtin();
134 assert_eq!(
135 map.get_syntax_for("/etc/profile"),
136 Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
137 );
138 map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
139 .ok();
140 assert_eq!(
141 map.get_syntax_for("/etc/profile"),
142 Some(MappingTarget::MapTo("My Syntax"))
143 );
146#[test]
147fn builtin_mappings() {
148 let map = SyntaxMapping::builtin();
150 assert_eq!(
151 map.get_syntax_for("/path/to/build"),
152 Some(MappingTarget::MapToUnknown)
153 );