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)
52 "**/systemd/**/*.conf",
53 "**/systemd/**/*.example",
72 .insert(glob, MappingTarget::MapTo("INI"))
77 mapping.insert("*.hook", MappingTarget::MapTo("INI")).unwrap();
82 pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
83 let glob = GlobBuilder::new(from)
84 .case_insensitive(false)
85 .literal_separator(true)
87 self.mappings.push((glob.compile_matcher(), to));
91 pub fn mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
95 pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
96 let candidate = Candidate::new(path.as_ref());
97 let canddidate_filename = path.as_ref().file_name().map(Candidate::new);
98 for (ref glob, ref syntax) in self.mappings.iter().rev() {
99 if glob.is_match_candidate(&candidate)
100 || canddidate_filename
102 .map_or(false, |filename| glob.is_match_candidate(filename))
104 return Some(*syntax);
113 let mut map = SyntaxMapping::empty();
114 map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
116 map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
120 map.get_syntax_for("/path/to/Cargo.lock"),
121 Some(MappingTarget::MapTo("TOML"))
123 assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
126 map.get_syntax_for("/path/to/.ignore"),
127 Some(MappingTarget::MapTo("Git Ignore"))
132fn user_can_override_builtin_mappings() {
133 let mut map = SyntaxMapping::builtin();
136 map.get_syntax_for("/etc/profile"),
137 Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
139 map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
142 map.get_syntax_for("/etc/profile"),
143 Some(MappingTarget::MapTo("My Syntax"))
148fn builtin_mappings() {
149 let map = SyntaxMapping::builtin();
152 map.get_syntax_for("/path/to/build"),
153 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