2020-03-22 09:55:13 +01:00
|
|
|
use std::path::Path;
|
|
|
|
|
2020-04-22 21:45:47 +02:00
|
|
|
use crate::error::Result;
|
2021-11-19 17:05:23 +01:00
|
|
|
use ignored_suffixes::IgnoredSuffixes;
|
2020-03-22 09:55:13 +01:00
|
|
|
|
|
|
|
use globset::{Candidate, GlobBuilder, GlobMatcher};
|
|
|
|
|
2021-11-19 17:05:23 +01:00
|
|
|
pub mod ignored_suffixes;
|
|
|
|
|
2022-09-04 00:02:08 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2022-01-02 21:46:15 +01:00
|
|
|
#[non_exhaustive]
|
2020-03-22 09:55:13 +01:00
|
|
|
pub enum MappingTarget<'a> {
|
2021-10-25 17:59:12 +02:00
|
|
|
/// For mapping a path to a specific syntax.
|
2020-03-22 09:55:13 +01:00
|
|
|
MapTo(&'a str),
|
2021-10-25 17:59:12 +02:00
|
|
|
|
|
|
|
/// For mapping a path (typically an extension-less file name) to an unknown
|
|
|
|
/// syntax. This typically means later using the contents of the first line
|
|
|
|
/// of the file to determine what syntax to use.
|
2020-03-22 09:55:13 +01:00
|
|
|
MapToUnknown,
|
2021-10-25 17:59:12 +02:00
|
|
|
|
|
|
|
/// For mapping a file extension (e.g. `*.conf`) to an unknown syntax. This
|
|
|
|
/// typically means later using the contents of the first line of the file
|
|
|
|
/// to determine what syntax to use. However, if a syntax handles a file
|
|
|
|
/// name that happens to have the given file extension (e.g. `resolv.conf`),
|
|
|
|
/// then that association will have higher precedence, and the mapping will
|
|
|
|
/// be ignored.
|
|
|
|
MapExtensionToUnknown,
|
2020-03-22 09:55:13 +01:00
|
|
|
}
|
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>)>,
|
2021-11-19 17:05:23 +01:00
|
|
|
pub(crate) ignored_suffixes: IgnoredSuffixes<'a>,
|
2020-03-22 09:55:13 +01:00
|
|
|
}
|
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();
|
2022-03-06 20:01:49 +01:00
|
|
|
mapping
|
|
|
|
.insert(".clang-format", MappingTarget::MapTo("YAML"))
|
|
|
|
.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();
|
2023-06-01 07:17:08 +02:00
|
|
|
mapping
|
|
|
|
.insert(
|
|
|
|
"os-release",
|
|
|
|
MappingTarget::MapTo("Bourne Again Shell (bash)"),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-02-16 21:51:57 +01:00
|
|
|
mapping
|
|
|
|
.insert("*.pac", MappingTarget::MapTo("JavaScript (Babel)"))
|
|
|
|
.unwrap();
|
2022-07-04 23:19:09 +02:00
|
|
|
mapping
|
|
|
|
.insert("fish_history", MappingTarget::MapTo("YAML"))
|
|
|
|
.unwrap();
|
2020-03-22 09:55:13 +01:00
|
|
|
|
2023-04-08 00:41:02 +02:00
|
|
|
mapping
|
|
|
|
.insert("*.jsonl", MappingTarget::MapTo("JSON"))
|
|
|
|
.unwrap();
|
|
|
|
|
2022-06-04 14:12:42 +02:00
|
|
|
// See #2151, https://nmap.org/book/nse-language.html
|
|
|
|
mapping
|
|
|
|
.insert("*.nse", MappingTarget::MapTo("Lua"))
|
|
|
|
.unwrap();
|
|
|
|
|
2020-05-24 10:22:32 +02:00
|
|
|
// See #1008
|
|
|
|
mapping
|
|
|
|
.insert("rails", MappingTarget::MapToUnknown)
|
|
|
|
.unwrap();
|
|
|
|
|
2023-07-02 13:50:37 +02:00
|
|
|
mapping
|
|
|
|
.insert("Containerfile", MappingTarget::MapTo("Dockerfile"))
|
|
|
|
.unwrap();
|
|
|
|
|
2023-08-02 09:04:55 +02:00
|
|
|
mapping
|
|
|
|
.insert("*.ksh", MappingTarget::MapTo("Bourne Again Shell (bash)"))
|
|
|
|
.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
|
2021-10-25 17:59:12 +02:00
|
|
|
.insert("*.conf", MappingTarget::MapExtensionToUnknown)
|
2021-02-16 21:39:38 +01:00
|
|
|
.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();
|
|
|
|
}
|
|
|
|
|
2021-09-10 21:56:40 +02:00
|
|
|
for glob in &[
|
2020-06-20 05:49:06 +02:00
|
|
|
"**/systemd/**/*.conf",
|
|
|
|
"**/systemd/**/*.example",
|
|
|
|
"*.automount",
|
|
|
|
"*.device",
|
|
|
|
"*.dnssd",
|
|
|
|
"*.link",
|
|
|
|
"*.mount",
|
|
|
|
"*.netdev",
|
|
|
|
"*.network",
|
|
|
|
"*.nspawn",
|
|
|
|
"*.path",
|
|
|
|
"*.service",
|
|
|
|
"*.scope",
|
|
|
|
"*.slice",
|
|
|
|
"*.socket",
|
|
|
|
"*.swap",
|
|
|
|
"*.target",
|
|
|
|
"*.timer",
|
2021-09-10 21:56:40 +02:00
|
|
|
] {
|
2020-09-20 20:47:21 +02:00
|
|
|
mapping.insert(glob, MappingTarget::MapTo("INI")).unwrap();
|
2020-06-20 05:49:06 +02:00
|
|
|
}
|
|
|
|
|
2022-04-27 22:51:10 +02:00
|
|
|
// unix mail spool
|
|
|
|
for glob in &["/var/spool/mail/*", "/var/mail/*"] {
|
|
|
|
mapping.insert(glob, MappingTarget::MapTo("Email")).unwrap()
|
|
|
|
}
|
|
|
|
|
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();
|
2023-08-07 22:37:13 +02:00
|
|
|
|
|
|
|
mapping
|
|
|
|
.insert("*.ron", MappingTarget::MapTo("Rust"))
|
|
|
|
.unwrap();
|
2020-06-20 05:49:20 +02:00
|
|
|
|
2022-02-26 17:01:00 +01:00
|
|
|
// Global git config files rooted in `$XDG_CONFIG_HOME/git/` or `$HOME/.config/git/`
|
|
|
|
// See e.g. https://git-scm.com/docs/git-config#FILES
|
2023-09-01 21:11:41 +02:00
|
|
|
match (
|
|
|
|
std::env::var_os("XDG_CONFIG_HOME").filter(|val| !val.is_empty()),
|
|
|
|
std::env::var_os("HOME")
|
|
|
|
.filter(|val| !val.is_empty())
|
|
|
|
.map(|home| Path::new(&home).join(".config")),
|
|
|
|
) {
|
|
|
|
(Some(xdg_config_home), Some(default_config_home))
|
|
|
|
if xdg_config_home == default_config_home => {
|
|
|
|
insert_git_config_global(&mut mapping, &xdg_config_home)
|
|
|
|
}
|
|
|
|
(Some(xdg_config_home), Some(default_config_home)) /* else guard */ => {
|
|
|
|
insert_git_config_global(&mut mapping, &xdg_config_home);
|
|
|
|
insert_git_config_global(&mut mapping, &default_config_home)
|
|
|
|
}
|
|
|
|
(Some(config_home), None) => insert_git_config_global(&mut mapping, &config_home),
|
|
|
|
(None, Some(config_home)) => insert_git_config_global(&mut mapping, &config_home),
|
|
|
|
(None, None) => (),
|
|
|
|
};
|
2022-02-26 17:01:00 +01:00
|
|
|
|
|
|
|
fn insert_git_config_global(mapping: &mut SyntaxMapping, config_home: impl AsRef<Path>) {
|
|
|
|
let git_config_path = config_home.as_ref().join("git");
|
2020-09-23 21:08:47 +02:00
|
|
|
|
|
|
|
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)
|
2023-09-14 16:04:43 +02:00
|
|
|
.case_insensitive(true)
|
2020-03-22 09:55:13 +01:00
|
|
|
.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>> {
|
2022-08-16 22:42:15 +02:00
|
|
|
// Try matching on the file name as-is.
|
2021-09-10 21:58:46 +02:00
|
|
|
let candidate = Candidate::new(&path);
|
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
|
|
|
}
|
2022-08-16 22:42:15 +02:00
|
|
|
// Try matching on the file name after removing an ignored suffix.
|
|
|
|
let file_name = path.as_ref().file_name()?;
|
|
|
|
self.ignored_suffixes
|
|
|
|
.try_with_stripped_suffix(file_name, |stripped_file_name| {
|
|
|
|
Ok(self.get_syntax_for(stripped_file_name))
|
|
|
|
})
|
|
|
|
.ok()?
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|
2021-11-19 17:05:23 +01:00
|
|
|
|
|
|
|
pub fn insert_ignored_suffix(&mut self, suffix: &'a str) {
|
|
|
|
self.ignored_suffixes.add_suffix(suffix);
|
|
|
|
}
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|
|
|
|
|
2023-09-01 21:11:41 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
|
|
fn basic() {
|
|
|
|
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);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
map.get_syntax_for("/path/to/.ignore"),
|
|
|
|
Some(MappingTarget::MapTo("Git Ignore"))
|
|
|
|
);
|
|
|
|
}
|
2020-03-22 09:55:13 +01:00
|
|
|
|
2023-09-01 21:11:41 +02: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"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn builtin_mappings() {
|
|
|
|
let map = SyntaxMapping::builtin();
|
2020-03-22 10:37:35 +01:00
|
|
|
|
2023-09-01 21:11:41 +02:00
|
|
|
assert_eq!(
|
|
|
|
map.get_syntax_for("/path/to/build"),
|
|
|
|
Some(MappingTarget::MapToUnknown)
|
|
|
|
);
|
|
|
|
}
|
2018-10-17 22:30:09 +02:00
|
|
|
|
2023-09-01 21:11:41 +02:00
|
|
|
#[test]
|
|
|
|
/// verifies that SyntaxMapping::builtin() doesn't repeat `Glob`-based keys
|
|
|
|
fn no_duplicate_builtin_keys() {
|
|
|
|
let mappings = SyntaxMapping::builtin().mappings;
|
|
|
|
for i in 0..mappings.len() {
|
|
|
|
let tail = mappings[i + 1..].into_iter();
|
|
|
|
let (dupl, _): (Vec<_>, Vec<_>) =
|
|
|
|
tail.partition(|item| item.0.glob() == mappings[i].0.glob());
|
|
|
|
|
|
|
|
// emit repeats on failure
|
|
|
|
assert_eq!(
|
|
|
|
dupl.len(),
|
|
|
|
0,
|
|
|
|
"Glob pattern `{}` mapped to multiple: {:?}",
|
|
|
|
mappings[i].0.glob().glob(),
|
|
|
|
{
|
|
|
|
let (_, mut dupl_targets): (Vec<GlobMatcher>, Vec<MappingTarget>) =
|
|
|
|
dupl.into_iter().cloned().unzip();
|
|
|
|
dupl_targets.push(mappings[i].1)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|