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",
98 mapping.insert(glob, MappingTarget::MapTo("INI")).unwrap();
103 .insert("*.hook", MappingTarget::MapTo("INI"))
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)
114 self.mappings.push((glob.compile_matcher(), to));
118 pub fn mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
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
129 .map_or(false, |filename| glob.is_match_candidate(filename))
131 return Some(*syntax);
140 let mut map = SyntaxMapping::empty();
141 map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
143 map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
147 map.get_syntax_for("/path/to/Cargo.lock"),
148 Some(MappingTarget::MapTo("TOML"))
150 assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
153 map.get_syntax_for("/path/to/.ignore"),
154 Some(MappingTarget::MapTo("Git Ignore"))
159fn user_can_override_builtin_mappings() {
160 let mut map = SyntaxMapping::builtin();
163 map.get_syntax_for("/etc/profile"),
164 Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
166 map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
169 map.get_syntax_for("/etc/profile"),
170 Some(MappingTarget::MapTo("My Syntax"))
175fn builtin_mappings() {
176 let map = SyntaxMapping::builtin();
179 map.get_syntax_for("/path/to/build"),
180 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