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();
27 .insert("build", MappingTarget::MapToUnknown)
30 .insert("**/.ssh/config", MappingTarget::MapTo("SSH Config"))
35 MappingTarget::MapTo("Bourne Again Shell (bash)"),
41 MappingTarget::MapTo("Bourne Again Shell (bash)"),
47 .insert("rails", MappingTarget::MapToUnknown)
51 "**/systemd/**/*.conf",
52 "**/systemd/**/*.example",
71 .insert(glob, MappingTarget::MapTo("INI"))
76 mapping.insert("*.hook", MappingTarget::MapTo("INI")).unwrap();
81 pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
82 let glob = GlobBuilder::new(from)
83 .case_insensitive(false)
84 .literal_separator(true)
86 self.mappings.push((glob.compile_matcher(), to));
90 pub fn mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
94 pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
95 let candidate = Candidate::new(path.as_ref());
96 let canddidate_filename = path.as_ref().file_name().map(Candidate::new);
97 for (ref glob, ref syntax) in self.mappings.iter().rev() {
98 if glob.is_match_candidate(&candidate)
99 || canddidate_filename
101 .map_or(false, |filename| glob.is_match_candidate(filename))
103 return Some(*syntax);
112 let mut map = SyntaxMapping::empty();
113 map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
115 map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
119 map.get_syntax_for("/path/to/Cargo.lock"),
120 Some(MappingTarget::MapTo("TOML"))
122 assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
125 map.get_syntax_for("/path/to/.ignore"),
126 Some(MappingTarget::MapTo("Git Ignore"))
131fn user_can_override_builtin_mappings() {
132 let mut map = SyntaxMapping::builtin();
135 map.get_syntax_for("/etc/profile"),
136 Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
138 map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
141 map.get_syntax_for("/etc/profile"),
142 Some(MappingTarget::MapTo("My Syntax"))
147fn builtin_mappings() {
148 let map = SyntaxMapping::builtin();
151 map.get_syntax_for("/path/to/build"),
152 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