3use crate::error::Result;
4use ignored_suffixes::IgnoredSuffixes;
6use globset::{Candidate, GlobBuilder, GlobMatcher};
8pub mod ignored_suffixes;
10#[derive(Debug, Clone, Copy, PartialEq)]
12pub enum MappingTarget<'a> {
13 /// For mapping a path to a specific syntax.
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.
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
27 MapExtensionToUnknown,
30#[derive(Debug, Clone, Default)]
31pub struct SyntaxMapping<'a> {
32 mappings: Vec<(GlobMatcher, MappingTarget<'a>)>,
33 pub(crate) ignored_suffixes: IgnoredSuffixes<'a>,
36impl<'a> SyntaxMapping<'a> {
37 pub fn empty() -> SyntaxMapping<'a> {
41 pub fn builtin() -> SyntaxMapping<'a> {
42 let mut mapping = Self::empty();
43 mapping.insert("*.h", MappingTarget::MapTo("C++")).unwrap();
44 mapping.insert("*.fs", MappingTarget::MapTo("F#")).unwrap();
46 .insert("build", MappingTarget::MapToUnknown)
49 .insert("**/.ssh/config", MappingTarget::MapTo("SSH Config"))
54 MappingTarget::MapTo("Bourne Again Shell (bash)"),
60 MappingTarget::MapTo("Bourne Again Shell (bash)"),
64 .insert("*.pac", MappingTarget::MapTo("JavaScript (Babel)"))
69 .insert("rails", MappingTarget::MapToUnknown)
72 // Nginx and Apache syntax files both want to style all ".conf" files
73 // see #1131 and #1137
75 .insert("*.conf", MappingTarget::MapExtensionToUnknown)
79 "/etc/nginx/**/*.conf",
80 "/etc/nginx/sites-*/**/*",
84 mapping.insert(glob, MappingTarget::MapTo("nginx")).unwrap();
88 "/etc/apache2/**/*.conf",
89 "/etc/apache2/sites-*/**/*",
93 .insert(glob, MappingTarget::MapTo("Apache Conf"))
98 "**/systemd/**/*.conf",
99 "**/systemd/**/*.example",
117 mapping.insert(glob, MappingTarget::MapTo("INI")).unwrap();
122 .insert("*.hook", MappingTarget::MapTo("INI"))
125 if let Some(xdg_config_home) = std::env::var_os("XDG_CONFIG_HOME") {
126 let git_config_path = Path::new(&xdg_config_home).join("git");
130 &git_config_path.join("config").to_string_lossy(),
131 MappingTarget::MapTo("Git Config"),
137 &git_config_path.join("ignore").to_string_lossy(),
138 MappingTarget::MapTo("Git Ignore"),
144 &git_config_path.join("attributes").to_string_lossy(),
145 MappingTarget::MapTo("Git Attributes"),
153 pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
154 let glob = GlobBuilder::new(from)
155 .case_insensitive(false)
156 .literal_separator(true)
158 self.mappings.push((glob.compile_matcher(), to));
162 pub fn mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
166 pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
167 let candidate = Candidate::new(&path);
168 let candidate_filename = path.as_ref().file_name().map(Candidate::new);
169 for (ref glob, ref syntax) in self.mappings.iter().rev() {
170 if glob.is_match_candidate(&candidate)
171 || candidate_filename
173 .map_or(false, |filename| glob.is_match_candidate(filename))
175 return Some(*syntax);
181 pub fn insert_ignored_suffix(&mut self, suffix: &'a str) {
182 self.ignored_suffixes.add_suffix(suffix);
188 let mut map = SyntaxMapping::empty();
189 map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
191 map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
195 map.get_syntax_for("/path/to/Cargo.lock"),
196 Some(MappingTarget::MapTo("TOML"))
198 assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
201 map.get_syntax_for("/path/to/.ignore"),
202 Some(MappingTarget::MapTo("Git Ignore"))
207fn user_can_override_builtin_mappings() {
208 let mut map = SyntaxMapping::builtin();
211 map.get_syntax_for("/etc/profile"),
212 Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
214 map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
217 map.get_syntax_for("/etc/profile"),
218 Some(MappingTarget::MapTo("My Syntax"))
223fn builtin_mappings() {
224 let map = SyntaxMapping::builtin();
227 map.get_syntax_for("/path/to/build"),
228 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