mirror of
https://github.com/sharkdp/bat.git
synced 2025-09-02 03:12:25 +01:00
Initial implementation of glob-based syntax mapping
This commit is contained in:
@@ -9,7 +9,7 @@ use syntect::parsing::{SyntaxReference, SyntaxSet, SyntaxSetBuilder};
|
||||
|
||||
use crate::errors::*;
|
||||
use crate::inputfile::{InputFile, InputFileReader};
|
||||
use crate::syntax_mapping::SyntaxMapping;
|
||||
use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct HighlightingAssets {
|
||||
@@ -184,25 +184,28 @@ impl HighlightingAssets {
|
||||
(Some(language), _) => self.syntax_set.find_syntax_by_token(language),
|
||||
(None, InputFile::Ordinary(filename)) => {
|
||||
let path = Path::new(filename);
|
||||
|
||||
let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
||||
let extension = path.extension().and_then(|x| x.to_str()).unwrap_or("");
|
||||
|
||||
let file_name = mapping.replace(file_name);
|
||||
let extension = mapping.replace(extension);
|
||||
|
||||
let ext_syntax = self
|
||||
.syntax_set
|
||||
.find_syntax_by_extension(&file_name)
|
||||
.or_else(|| self.syntax_set.find_syntax_by_extension(&extension));
|
||||
let line_syntax = if ext_syntax.is_none() {
|
||||
String::from_utf8(reader.first_line.clone())
|
||||
.ok()
|
||||
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let line_syntax = String::from_utf8(reader.first_line.clone())
|
||||
.ok()
|
||||
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l));
|
||||
|
||||
ext_syntax.or(line_syntax)
|
||||
dbg!(path);
|
||||
match dbg!(mapping.get_syntax_for(path)) {
|
||||
Some(MappingTarget::MapTo(syntax_name)) => {
|
||||
// TODO: we should probably return an error here if this syntax can not be
|
||||
// found. Currently, we just fall back to 'plain'.
|
||||
self.syntax_set.find_syntax_by_name(syntax_name)
|
||||
}
|
||||
Some(MappingTarget::MapToUnknown) => line_syntax,
|
||||
None => ext_syntax.or(line_syntax),
|
||||
}
|
||||
}
|
||||
(None, InputFile::StdIn) => String::from_utf8(reader.first_line.clone())
|
||||
.ok()
|
||||
@@ -225,19 +228,19 @@ mod tests {
|
||||
|
||||
use crate::assets::HighlightingAssets;
|
||||
use crate::inputfile::InputFile;
|
||||
use crate::syntax_mapping::SyntaxMapping;
|
||||
use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
|
||||
|
||||
struct SyntaxDetectionTest {
|
||||
struct SyntaxDetectionTest<'a> {
|
||||
assets: HighlightingAssets,
|
||||
pub syntax_mapping: SyntaxMapping,
|
||||
pub syntax_mapping: SyntaxMapping<'a>,
|
||||
temp_dir: TempDir,
|
||||
}
|
||||
|
||||
impl SyntaxDetectionTest {
|
||||
impl<'a> SyntaxDetectionTest<'a> {
|
||||
fn new() -> Self {
|
||||
SyntaxDetectionTest {
|
||||
assets: HighlightingAssets::from_binary(),
|
||||
syntax_mapping: SyntaxMapping::new(),
|
||||
syntax_mapping: SyntaxMapping::builtin(),
|
||||
temp_dir: TempDir::new("bat_syntax_detection_tests")
|
||||
.expect("creation of temporary directory"),
|
||||
}
|
||||
@@ -294,6 +297,10 @@ mod tests {
|
||||
test.syntax_name_with_content("my_script", "#!/bin/bash"),
|
||||
"Bourne Again Shell (bash)"
|
||||
);
|
||||
assert_eq!(
|
||||
test.syntax_name_with_content("build", "#!/bin/bash"),
|
||||
"Bourne Again Shell (bash)"
|
||||
);
|
||||
assert_eq!(test.syntax_name_with_content("my_script", "<?php"), "PHP");
|
||||
}
|
||||
|
||||
@@ -302,7 +309,20 @@ mod tests {
|
||||
let mut test = SyntaxDetectionTest::new();
|
||||
|
||||
assert_ne!(test.syntax_name("test.h"), "C++");
|
||||
test.syntax_mapping.insert("h", "cpp");
|
||||
test.syntax_mapping
|
||||
.insert("*.h", MappingTarget::MapTo("C++"))
|
||||
.ok();
|
||||
assert_eq!(test.syntax_name("test.h"), "C++");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn syntax_detection_is_case_sensitive() {
|
||||
let mut test = SyntaxDetectionTest::new();
|
||||
|
||||
assert_ne!(test.syntax_name("README.MD"), "Markdown");
|
||||
test.syntax_mapping
|
||||
.insert("*.MD", MappingTarget::MapTo("Markdown"))
|
||||
.ok();
|
||||
assert_eq!(test.syntax_name("README.MD"), "Markdown");
|
||||
}
|
||||
}
|
||||
|
@@ -18,8 +18,8 @@ use ansi_term;
|
||||
|
||||
use bat::{
|
||||
config::{
|
||||
Config, HighlightedLineRanges, InputFile, LineRange, LineRanges, OutputWrap, PagingMode,
|
||||
StyleComponent, StyleComponents, SyntaxMapping,
|
||||
Config, HighlightedLineRanges, InputFile, LineRange, LineRanges, MappingTarget, OutputWrap,
|
||||
PagingMode, StyleComponent, StyleComponents, SyntaxMapping,
|
||||
},
|
||||
errors::*,
|
||||
HighlightingAssets,
|
||||
@@ -104,7 +104,7 @@ impl App {
|
||||
}
|
||||
};
|
||||
|
||||
let mut syntax_mapping = SyntaxMapping::new();
|
||||
let mut syntax_mapping = SyntaxMapping::builtin();
|
||||
|
||||
if let Some(values) = self.matches.values_of("map-syntax") {
|
||||
for from_to in values {
|
||||
@@ -114,7 +114,7 @@ impl App {
|
||||
return Err("Invalid syntax mapping. The format of the -m/--map-syntax option is 'from:to'.".into());
|
||||
}
|
||||
|
||||
syntax_mapping.insert(parts[0], parts[1]);
|
||||
syntax_mapping.insert(parts[0], MappingTarget::MapTo(parts[1]))?;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
pub use crate::inputfile::InputFile;
|
||||
pub use crate::line_range::{HighlightedLineRanges, LineRange, LineRanges};
|
||||
pub use crate::style::{StyleComponent, StyleComponents};
|
||||
pub use crate::syntax_mapping::SyntaxMapping;
|
||||
pub use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
|
||||
pub use crate::wrap::OutputWrap;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
@@ -60,7 +60,7 @@ pub struct Config<'a> {
|
||||
pub theme: String,
|
||||
|
||||
/// File extension/name mappings
|
||||
pub syntax_mapping: SyntaxMapping,
|
||||
pub syntax_mapping: SyntaxMapping<'a>,
|
||||
|
||||
/// Command to start the pager
|
||||
pub pager: Option<&'a str>,
|
||||
|
@@ -6,6 +6,7 @@ error_chain! {
|
||||
Io(::std::io::Error);
|
||||
SyntectError(::syntect::LoadingError);
|
||||
ParseIntError(::std::num::ParseIntError);
|
||||
GlobParsingError(::globset::Error);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,35 +1,85 @@
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::errors::Result;
|
||||
|
||||
use globset::{Candidate, GlobBuilder, GlobMatcher};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum MappingTarget<'a> {
|
||||
MapTo(&'a str),
|
||||
MapToUnknown,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct SyntaxMapping(HashMap<String, String>);
|
||||
pub struct SyntaxMapping<'a> {
|
||||
mappings: Vec<(GlobMatcher, MappingTarget<'a>)>,
|
||||
}
|
||||
|
||||
impl SyntaxMapping {
|
||||
pub fn new() -> SyntaxMapping {
|
||||
impl<'a> SyntaxMapping<'a> {
|
||||
pub fn empty() -> SyntaxMapping<'a> {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, from: impl Into<String>, to: impl Into<String>) -> Option<String> {
|
||||
self.0.insert(from.into(), to.into())
|
||||
pub fn builtin() -> SyntaxMapping<'a> {
|
||||
let mut mapping = Self::empty();
|
||||
mapping
|
||||
.insert("build", MappingTarget::MapToUnknown)
|
||||
.unwrap();
|
||||
|
||||
mapping
|
||||
}
|
||||
|
||||
pub(crate) fn replace<'a>(&self, input: impl Into<Cow<'a, str>>) -> Cow<'a, str> {
|
||||
let input = input.into();
|
||||
match self.0.get(input.as_ref()) {
|
||||
Some(s) => Cow::from(s.clone()),
|
||||
None => input,
|
||||
pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
|
||||
let glob = GlobBuilder::new(from)
|
||||
.case_insensitive(false)
|
||||
.literal_separator(true)
|
||||
.build()?;
|
||||
self.mappings.push((glob.compile_matcher(), to));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
|
||||
let candidate = Candidate::new(path.as_ref());
|
||||
let canddidate_filename = path.as_ref().file_name().map(Candidate::new);
|
||||
for (ref glob, ref syntax) in &self.mappings {
|
||||
if glob.is_match_candidate(&candidate)
|
||||
|| canddidate_filename
|
||||
.as_ref()
|
||||
.map_or(false, |filename| glob.is_match_candidate(filename))
|
||||
{
|
||||
return Some(*syntax);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic() {
|
||||
let mut map = SyntaxMapping::new();
|
||||
map.insert("Cargo.lock", "toml");
|
||||
map.insert(".ignore", ".gitignore");
|
||||
let mut map = SyntaxMapping::empty();
|
||||
map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
|
||||
.ok();
|
||||
map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
|
||||
.ok();
|
||||
|
||||
assert_eq!("toml", map.replace("Cargo.lock"));
|
||||
assert_eq!("other.lock", map.replace("other.lock"));
|
||||
assert_eq!(
|
||||
map.get_syntax_for("/path/to/Cargo.lock"),
|
||||
Some(MappingTarget::MapTo("TOML"))
|
||||
);
|
||||
assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
|
||||
|
||||
assert_eq!(".gitignore", map.replace(".ignore"));
|
||||
assert_eq!(
|
||||
map.get_syntax_for("/path/to/.ignore"),
|
||||
Some(MappingTarget::MapTo("Git Ignore"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builtin_mappings() {
|
||||
let map = SyntaxMapping::builtin();
|
||||
|
||||
assert_eq!(
|
||||
map.get_syntax_for("/path/to/build"),
|
||||
Some(MappingTarget::MapToUnknown)
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user