2020-03-22 09:55:13 +01:00
|
|
|
use std::path::Path;
|
|
|
|
|
2020-04-22 21:45:47 +02:00
|
|
|
use crate::error::Result;
|
2020-03-22 09:55:13 +01:00
|
|
|
|
|
|
|
use globset::{Candidate, GlobBuilder, GlobMatcher};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub enum MappingTarget<'a> {
|
|
|
|
MapTo(&'a str),
|
|
|
|
MapToUnknown,
|
|
|
|
}
|
2018-10-17 22:30:09 +02:00
|
|
|
|
2019-03-08 11:46:49 +01:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2020-03-22 09:55:13 +01:00
|
|
|
pub struct SyntaxMapping<'a> {
|
|
|
|
mappings: Vec<(GlobMatcher, MappingTarget<'a>)>,
|
|
|
|
}
|
2018-10-17 22:30:09 +02:00
|
|
|
|
2020-03-22 09:55:13 +01:00
|
|
|
impl<'a> SyntaxMapping<'a> {
|
|
|
|
pub fn empty() -> SyntaxMapping<'a> {
|
2019-03-08 11:46:49 +01:00
|
|
|
Default::default()
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|
|
|
|
|
2020-03-22 09:55:13 +01:00
|
|
|
pub fn builtin() -> SyntaxMapping<'a> {
|
|
|
|
let mut mapping = Self::empty();
|
2020-03-22 10:37:35 +01:00
|
|
|
mapping.insert("*.h", MappingTarget::MapTo("C++")).unwrap();
|
2020-06-27 02:42:05 +02:00
|
|
|
mapping.insert("*.fs", MappingTarget::MapTo("F#")).unwrap();
|
2020-03-22 09:55:13 +01:00
|
|
|
mapping
|
|
|
|
.insert("build", MappingTarget::MapToUnknown)
|
|
|
|
.unwrap();
|
2020-03-22 10:37:35 +01:00
|
|
|
mapping
|
|
|
|
.insert("**/.ssh/config", MappingTarget::MapTo("SSH Config"))
|
|
|
|
.unwrap();
|
2020-04-22 23:58:41 +02:00
|
|
|
mapping
|
2020-04-23 00:56:35 +02:00
|
|
|
.insert(
|
|
|
|
"**/bat/config",
|
|
|
|
MappingTarget::MapTo("Bourne Again Shell (bash)"),
|
|
|
|
)
|
2020-04-22 23:58:41 +02:00
|
|
|
.unwrap();
|
2020-03-22 10:37:35 +01:00
|
|
|
mapping
|
|
|
|
.insert(
|
|
|
|
"/etc/profile",
|
|
|
|
MappingTarget::MapTo("Bourne Again Shell (bash)"),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-02-16 21:51:57 +01:00
|
|
|
mapping
|
|
|
|
.insert("*.pac", MappingTarget::MapTo("JavaScript (Babel)"))
|
|
|
|
.unwrap();
|
2020-03-22 09:55:13 +01:00
|
|
|
|
2020-05-24 10:22:32 +02:00
|
|
|
// See #1008
|
|
|
|
mapping
|
|
|
|
.insert("rails", MappingTarget::MapToUnknown)
|
|
|
|
.unwrap();
|
|
|
|
|
2020-08-16 22:15:59 +02:00
|
|
|
// Nginx and Apache syntax files both want to style all ".conf" files
|
|
|
|
// see #1131 and #1137
|
|
|
|
mapping
|
|
|
|
.insert("*.conf", MappingTarget::MapToUnknown)
|
|
|
|
.unwrap();
|
|
|
|
|
2021-02-16 21:39:38 +01:00
|
|
|
// Re-insert a mapping for resolv.conf, see #1510
|
|
|
|
mapping
|
|
|
|
.insert("resolv.conf", MappingTarget::MapTo("resolv"))
|
|
|
|
.unwrap();
|
|
|
|
|
2020-08-16 22:15:59 +02:00
|
|
|
for glob in &[
|
|
|
|
"/etc/nginx/**/*.conf",
|
|
|
|
"/etc/nginx/sites-*/**/*",
|
|
|
|
"nginx.conf",
|
|
|
|
"mime.types",
|
|
|
|
] {
|
|
|
|
mapping.insert(glob, MappingTarget::MapTo("nginx")).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
for glob in &[
|
|
|
|
"/etc/apache2/**/*.conf",
|
|
|
|
"/etc/apache2/sites-*/**/*",
|
|
|
|
"httpd.conf",
|
|
|
|
] {
|
|
|
|
mapping
|
|
|
|
.insert(glob, MappingTarget::MapTo("Apache Conf"))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2020-06-20 05:49:06 +02:00
|
|
|
for glob in [
|
|
|
|
"**/systemd/**/*.conf",
|
|
|
|
"**/systemd/**/*.example",
|
|
|
|
"*.automount",
|
|
|
|
"*.device",
|
|
|
|
"*.dnssd",
|
|
|
|
"*.link",
|
|
|
|
"*.mount",
|
|
|
|
"*.netdev",
|
|
|
|
"*.network",
|
|
|
|
"*.nspawn",
|
|
|
|
"*.path",
|
|
|
|
"*.service",
|
|
|
|
"*.scope",
|
|
|
|
"*.slice",
|
|
|
|
"*.socket",
|
|
|
|
"*.swap",
|
|
|
|
"*.target",
|
|
|
|
"*.timer",
|
2020-09-20 20:47:21 +02:00
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
{
|
|
|
|
mapping.insert(glob, MappingTarget::MapTo("INI")).unwrap();
|
2020-06-20 05:49:06 +02:00
|
|
|
}
|
|
|
|
|
2020-06-20 05:49:20 +02:00
|
|
|
// pacman hooks
|
2020-09-20 20:47:21 +02:00
|
|
|
mapping
|
|
|
|
.insert("*.hook", MappingTarget::MapTo("INI"))
|
|
|
|
.unwrap();
|
2020-06-20 05:49:20 +02:00
|
|
|
|
2020-09-23 21:08:47 +02:00
|
|
|
if let Some(xdg_config_home) = std::env::var_os("XDG_CONFIG_HOME") {
|
|
|
|
let git_config_path = Path::new(&xdg_config_home).join("git");
|
|
|
|
|
|
|
|
mapping
|
|
|
|
.insert(
|
2020-10-01 20:34:33 +02:00
|
|
|
&git_config_path.join("config").to_string_lossy(),
|
2020-09-23 21:08:47 +02:00
|
|
|
MappingTarget::MapTo("Git Config"),
|
|
|
|
)
|
2020-10-01 20:25:55 +02:00
|
|
|
.ok();
|
2020-09-23 21:08:47 +02:00
|
|
|
|
|
|
|
mapping
|
|
|
|
.insert(
|
2020-10-01 20:34:33 +02:00
|
|
|
&git_config_path.join("ignore").to_string_lossy(),
|
2020-09-23 21:08:47 +02:00
|
|
|
MappingTarget::MapTo("Git Ignore"),
|
|
|
|
)
|
2020-10-01 20:25:55 +02:00
|
|
|
.ok();
|
2020-09-23 21:08:47 +02:00
|
|
|
|
|
|
|
mapping
|
|
|
|
.insert(
|
2020-10-01 20:34:33 +02:00
|
|
|
&git_config_path.join("attributes").to_string_lossy(),
|
2020-09-23 21:08:47 +02:00
|
|
|
MappingTarget::MapTo("Git Attributes"),
|
|
|
|
)
|
2020-10-01 20:25:55 +02:00
|
|
|
.ok();
|
2020-09-23 21:08:47 +02:00
|
|
|
}
|
|
|
|
|
2020-03-22 09:55:13 +01:00
|
|
|
mapping
|
|
|
|
}
|
|
|
|
|
|
|
|
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(())
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|
|
|
|
|
2020-06-03 10:05:31 +02:00
|
|
|
pub fn mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
|
2020-05-30 03:53:31 +02:00
|
|
|
&self.mappings
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:29:34 +02:00
|
|
|
pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
|
2020-03-22 09:55:13 +01:00
|
|
|
let candidate = Candidate::new(path.as_ref());
|
2020-08-06 09:20:33 +02:00
|
|
|
let candidate_filename = path.as_ref().file_name().map(Candidate::new);
|
2020-03-22 10:37:35 +01:00
|
|
|
for (ref glob, ref syntax) in self.mappings.iter().rev() {
|
2020-03-22 09:55:13 +01:00
|
|
|
if glob.is_match_candidate(&candidate)
|
2020-08-06 09:20:33 +02:00
|
|
|
|| candidate_filename
|
2020-03-22 09:55:13 +01:00
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |filename| glob.is_match_candidate(filename))
|
|
|
|
{
|
|
|
|
return Some(*syntax);
|
|
|
|
}
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|
2020-03-22 09:55:13 +01:00
|
|
|
None
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic() {
|
2020-03-22 09:55:13 +01:00
|
|
|
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!(
|
|
|
|
map.get_syntax_for("/path/to/Cargo.lock"),
|
|
|
|
Some(MappingTarget::MapTo("TOML"))
|
|
|
|
);
|
|
|
|
assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
|
2018-10-17 22:30:09 +02:00
|
|
|
|
2020-03-22 09:55:13 +01:00
|
|
|
assert_eq!(
|
|
|
|
map.get_syntax_for("/path/to/.ignore"),
|
|
|
|
Some(MappingTarget::MapTo("Git Ignore"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-22 10:37:35 +01:00
|
|
|
#[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"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-22 09:55:13 +01:00
|
|
|
#[test]
|
|
|
|
fn builtin_mappings() {
|
|
|
|
let map = SyntaxMapping::builtin();
|
2018-10-17 22:30:09 +02:00
|
|
|
|
2020-03-22 09:55:13 +01:00
|
|
|
assert_eq!(
|
|
|
|
map.get_syntax_for("/path/to/build"),
|
|
|
|
Some(MappingTarget::MapToUnknown)
|
|
|
|
);
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|