3use crate::error::Result;
5use globset::{Candidate, GlobBuilder, GlobMatcher};
7#[derive(Debug, Clone, Copy, PartialEq)]
8pub enum MappingTarget<'a> {
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> {
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();
28 .insert("build", MappingTarget::MapToUnknown)
31 .insert("**/.ssh/config", MappingTarget::MapTo("SSH Config"))
36 MappingTarget::MapTo("Bourne Again Shell (bash)"),
42 MappingTarget::MapTo("Bourne Again Shell (bash)"),
48 .insert("rails", MappingTarget::MapToUnknown)
51 // Nginx and Apache syntax files both want to style all ".conf" files
52 // see #1131 and #1137
54 .insert("*.conf", MappingTarget::MapToUnknown)
58 "/etc/nginx/**/*.conf",
59 "/etc/nginx/sites-*/**/*",
63 mapping.insert(glob, MappingTarget::MapTo("nginx")).unwrap();
67 "/etc/apache2/**/*.conf",
68 "/etc/apache2/sites-*/**/*",
72 .insert(glob, MappingTarget::MapTo("Apache Conf"))
77 "**/systemd/**/*.conf",
78 "**/systemd/**/*.example",
97 .insert(glob, MappingTarget::MapTo("INI"))
102 mapping.insert("*.hook", MappingTarget::MapTo("INI")).unwrap();
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)
112 self.mappings.push((glob.compile_matcher(), to));
116 pub fn mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
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
127 .map_or(false, |filename| glob.is_match_candidate(filename))
129 return Some(*syntax);
138 let mut map = SyntaxMapping::empty();
139 map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
141 map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
145 map.get_syntax_for("/path/to/Cargo.lock"),
146 Some(MappingTarget::MapTo("TOML"))
148 assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
151 map.get_syntax_for("/path/to/.ignore"),
152 Some(MappingTarget::MapTo("Git Ignore"))
157fn user_can_override_builtin_mappings() {
158 let mut map = SyntaxMapping::builtin();
161 map.get_syntax_for("/etc/profile"),
162 Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
164 map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
167 map.get_syntax_for("/etc/profile"),
168 Some(MappingTarget::MapTo("My Syntax"))
173fn builtin_mappings() {
174 let map = SyntaxMapping::builtin();
177 map.get_syntax_for("/path/to/build"),
178 Some(MappingTarget::MapToUnknown)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list