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"))
106 if let Some(xdg_config_home) = std::env::var_os("XDG_CONFIG_HOME") {
107 let git_config_path = Path::new(&xdg_config_home).join("git");
111 &git_config_path.join("config").to_string_lossy(),
112 MappingTarget::MapTo("Git Config"),
118 &git_config_path.join("ignore").to_string_lossy(),
119 MappingTarget::MapTo("Git Ignore"),
125 &git_config_path.join("attributes").to_string_lossy(),
126 MappingTarget::MapTo("Git Attributes"),
134 pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
135 let glob = GlobBuilder::new(from)
136 .case_insensitive(false)
137 .literal_separator(true)
139 self.mappings.push((glob.compile_matcher(), to));
143 pub fn mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
147 pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
148 let candidate = Candidate::new(path.as_ref());
149 let candidate_filename = path.as_ref().file_name().map(Candidate::new);
150 for (ref glob, ref syntax) in self.mappings.iter().rev() {
151 if glob.is_match_candidate(&candidate)
152 || candidate_filename
154 .map_or(false, |filename| glob.is_match_candidate(filename))
156 return Some(*syntax);
165 let mut map = SyntaxMapping::empty();
166 map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
168 map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
172 map.get_syntax_for("/path/to/Cargo.lock"),
173 Some(MappingTarget::MapTo("TOML"))
175 assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
178 map.get_syntax_for("/path/to/.ignore"),
179 Some(MappingTarget::MapTo("Git Ignore"))
184fn user_can_override_builtin_mappings() {
185 let mut map = SyntaxMapping::builtin();
188 map.get_syntax_for("/etc/profile"),
189 Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
191 map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
194 map.get_syntax_for("/etc/profile"),
195 Some(MappingTarget::MapTo("My Syntax"))
200fn builtin_mappings() {
201 let map = SyntaxMapping::builtin();
204 map.get_syntax_for("/path/to/build"),
205 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