mirror of
https://github.com/sharkdp/bat.git
synced 2025-02-21 12:28:30 +00:00
Add more default mappings, reverse traversal
This commit is contained in:
parent
978def2d40
commit
dfd3ef022e
@ -285,6 +285,7 @@ mod tests {
|
|||||||
fn syntax_detection_well_defined_mapping_for_duplicate_extensions() {
|
fn syntax_detection_well_defined_mapping_for_duplicate_extensions() {
|
||||||
let test = SyntaxDetectionTest::new();
|
let test = SyntaxDetectionTest::new();
|
||||||
|
|
||||||
|
assert_eq!(test.syntax_name("test.h"), "C++");
|
||||||
assert_eq!(test.syntax_name("test.sass"), "Sass");
|
assert_eq!(test.syntax_name("test.sass"), "Sass");
|
||||||
assert_eq!(test.syntax_name("test.hs"), "Haskell (improved)");
|
assert_eq!(test.syntax_name("test.hs"), "Haskell (improved)");
|
||||||
assert_eq!(test.syntax_name("test.js"), "JavaScript (Babel)");
|
assert_eq!(test.syntax_name("test.js"), "JavaScript (Babel)");
|
||||||
@ -309,11 +310,11 @@ mod tests {
|
|||||||
fn syntax_detection_with_custom_mapping() {
|
fn syntax_detection_with_custom_mapping() {
|
||||||
let mut test = SyntaxDetectionTest::new();
|
let mut test = SyntaxDetectionTest::new();
|
||||||
|
|
||||||
assert_ne!(test.syntax_name("test.h"), "C++");
|
|
||||||
test.syntax_mapping
|
|
||||||
.insert("*.h", MappingTarget::MapTo("C++"))
|
|
||||||
.ok();
|
|
||||||
assert_eq!(test.syntax_name("test.h"), "C++");
|
assert_eq!(test.syntax_name("test.h"), "C++");
|
||||||
|
test.syntax_mapping
|
||||||
|
.insert("*.h", MappingTarget::MapTo("C"))
|
||||||
|
.ok();
|
||||||
|
assert_eq!(test.syntax_name("test.h"), "C");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -22,9 +22,19 @@ impl<'a> SyntaxMapping<'a> {
|
|||||||
|
|
||||||
pub fn builtin() -> SyntaxMapping<'a> {
|
pub fn builtin() -> SyntaxMapping<'a> {
|
||||||
let mut mapping = Self::empty();
|
let mut mapping = Self::empty();
|
||||||
|
mapping.insert("*.h", MappingTarget::MapTo("C++")).unwrap();
|
||||||
mapping
|
mapping
|
||||||
.insert("build", MappingTarget::MapToUnknown)
|
.insert("build", MappingTarget::MapToUnknown)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
mapping
|
||||||
|
.insert("**/.ssh/config", MappingTarget::MapTo("SSH Config"))
|
||||||
|
.unwrap();
|
||||||
|
mapping
|
||||||
|
.insert(
|
||||||
|
"/etc/profile",
|
||||||
|
MappingTarget::MapTo("Bourne Again Shell (bash)"),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
mapping
|
mapping
|
||||||
}
|
}
|
||||||
@ -41,7 +51,7 @@ impl<'a> SyntaxMapping<'a> {
|
|||||||
pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
|
pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
|
||||||
let candidate = Candidate::new(path.as_ref());
|
let candidate = Candidate::new(path.as_ref());
|
||||||
let canddidate_filename = path.as_ref().file_name().map(Candidate::new);
|
let canddidate_filename = path.as_ref().file_name().map(Candidate::new);
|
||||||
for (ref glob, ref syntax) in &self.mappings {
|
for (ref glob, ref syntax) in self.mappings.iter().rev() {
|
||||||
if glob.is_match_candidate(&candidate)
|
if glob.is_match_candidate(&candidate)
|
||||||
|| canddidate_filename
|
|| canddidate_filename
|
||||||
.as_ref()
|
.as_ref()
|
||||||
@ -74,6 +84,22 @@ fn basic() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn user_can_override_builtin_mappings() {
|
||||||
|
let mut map = SyntaxMapping::builtin();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
map.get_syntax_for("/etc/profile"),
|
||||||
|
Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
|
||||||
|
);
|
||||||
|
map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
|
||||||
|
.ok();
|
||||||
|
assert_eq!(
|
||||||
|
map.get_syntax_for("/etc/profile"),
|
||||||
|
Some(MappingTarget::MapTo("My Syntax"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn builtin_mappings() {
|
fn builtin_mappings() {
|
||||||
let map = SyntaxMapping::builtin();
|
let map = SyntaxMapping::builtin();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user