3use crate::error::Result;
4use ignored_suffixes::IgnoredSuffixes;
6use globset::{Candidate, GlobBuilder, GlobMatcher};
8pub mod ignored_suffixes;
10#[derive(Debug, Clone, Copy, PartialEq)]
11pub enum MappingTarget<'a> {
12 /// For mapping a path to a specific syntax.
15 /// For mapping a path (typically an extension-less file name) to an unknown
16 /// syntax. This typically means later using the contents of the first line
17 /// of the file to determine what syntax to use.
20 /// For mapping a file extension (e.g. `*.conf`) to an unknown syntax. This
21 /// typically means later using the contents of the first line of the file
22 /// to determine what syntax to use. However, if a syntax handles a file
23 /// name that happens to have the given file extension (e.g. `resolv.conf`),
24 /// then that association will have higher precedence, and the mapping will
26 MapExtensionToUnknown,
29#[derive(Debug, Clone, Default)]
30pub struct SyntaxMapping<'a> {
31 mappings: Vec<(GlobMatcher, MappingTarget<'a>)>,
32 pub(crate) ignored_suffixes: IgnoredSuffixes<'a>,
35impl<'a> SyntaxMapping<'a> {
36 pub fn empty() -> SyntaxMapping<'a> {
40 pub fn builtin() -> SyntaxMapping<'a> {
41 let mut mapping = Self::empty();
42 mapping.insert("*.h", MappingTarget::MapTo("C++")).unwrap();
43 mapping.insert("*.fs", MappingTarget::MapTo("F#")).unwrap();
45 .insert("build", MappingTarget::MapToUnknown)
48 .insert("**/.ssh/config", MappingTarget::MapTo("SSH Config"))
53 MappingTarget::MapTo("Bourne Again Shell (bash)"),
59 MappingTarget::MapTo("Bourne Again Shell (bash)"),
63 .insert("*.pac", MappingTarget::MapTo("JavaScript (Babel)"))
68 .insert("rails", MappingTarget::MapToUnknown)
71 // Nginx and Apache syntax files both want to style all ".conf" files
72 // see #1131 and #1137
74 .insert("*.conf", MappingTarget::MapExtensionToUnknown)
78 "/etc/nginx/**/*.conf",
79 "/etc/nginx/sites-*/**/*",
83 mapping.insert(glob, MappingTarget::MapTo("nginx")).unwrap();
87 "/etc/apache2/**/*.conf",
88 "/etc/apache2/sites-*/**/*",
92 .insert(glob, MappingTarget::MapTo("Apache Conf"))
97 "**/systemd/**/*.conf",
98 "**/systemd/**/*.example",
116 mapping.insert(glob, MappingTarget::MapTo("INI")).unwrap();
121 .insert("*.hook", MappingTarget::MapTo("INI"))
124 if let Some(xdg_config_home) = std::env::var_os("XDG_CONFIG_HOME") {
125 let git_config_path = Path::new(&xdg_config_home).join("git");
129 &git_config_path.join("config").to_string_lossy(),
130 MappingTarget::MapTo("Git Config"),
136 &git_config_path.join("ignore").to_string_lossy(),
137 MappingTarget::MapTo("Git Ignore"),
143 &git_config_path.join("attributes").to_string_lossy(),
144 MappingTarget::MapTo("Git Attributes"),
152 pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
153 let glob = GlobBuilder::new(from)
154 .case_insensitive(false)
155 .literal_separator(true)
157 self.mappings.push((glob.compile_matcher(), to));
161 pub fn mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
165 pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
166 let candidate = Candidate::new(&path);
167 let candidate_filename = path.as_ref().file_name().map(Candidate::new);
168 for (ref glob, ref syntax) in self.mappings.iter().rev() {
169 if glob.is_match_candidate(&candidate)
170 || candidate_filename
172 .map_or(false, |filename| glob.is_match_candidate(filename))
174 return Some(*syntax);
180 pub fn insert_ignored_suffix(&mut self, suffix: &'a str) {
181 self.ignored_suffixes.add_suffix(suffix);
187 let mut map = SyntaxMapping::empty();
188 map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
190 map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
194 map.get_syntax_for("/path/to/Cargo.lock"),
195 Some(MappingTarget::MapTo("TOML"))
197 assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
200 map.get_syntax_for("/path/to/.ignore"),
201 Some(MappingTarget::MapTo("Git Ignore"))
206fn user_can_override_builtin_mappings() {
207 let mut map = SyntaxMapping::builtin();
210 map.get_syntax_for("/etc/profile"),
211 Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
213 map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
216 map.get_syntax_for("/etc/profile"),
217 Some(MappingTarget::MapTo("My Syntax"))
222fn builtin_mappings() {
223 let map = SyntaxMapping::builtin();
226 map.get_syntax_for("/path/to/build"),
227 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