Add language detection based on whole file names

pull/78/head
Wilfred Hughes 2021-12-27 12:38:22 +07:00
parent 35c481289a
commit 91db24fc9a
2 changed files with 30 additions and 3 deletions

@ -1,5 +1,9 @@
## 0.14 (unreleased)
### Parsing
Improved language detection for whole file names without an extension.
### Display
Display width can now be overridden by setting the environment

@ -33,12 +33,29 @@ pub enum Language {
use Language::*;
pub fn guess(path: &Path) -> Option<Language> {
if let Some(lang) = from_name(path) {
return Some(lang);
}
match path.extension() {
Some(extension) => from_extension(extension),
None => None,
}
}
fn from_name(path: &Path) -> Option<Language> {
match path.file_name() {
Some(name) => match name.to_string_lossy().borrow() {
".bashrc" | ".bash_profile" | ".profile" => Some(Bash),
".emacs" | "_emacs" | "Cask" => Some(EmacsLisp),
"TARGETS" | "BUCK" | "DEPS" => Some(Python),
"Gemfile" | "Rakefile" => Some(Ruby),
_ => None,
},
None => None,
}
}
fn from_extension(extension: &OsStr) -> Option<Language> {
// TODO: find a nice way to extract name and extension information
// from the package.json in these parsers.
@ -61,7 +78,7 @@ fn from_extension(extension: &OsStr) -> Option<Language> {
}
"cs" => Some(CSharp),
"css" => Some(Css),
"el" | ".emacs" => Some(EmacsLisp),
"el" => Some(EmacsLisp),
"ex" | "exs" => Some(Elixir),
"go" => Some(Go),
"hs" => Some(Haskell),
@ -72,8 +89,8 @@ fn from_extension(extension: &OsStr) -> Option<Language> {
"lisp" | "lsp" | "asd" => Some(CommonLisp),
"ml" => Some(OCaml),
"mli" => Some(OCamlInterface),
"py" | "py3" | "pyi" | "TARGETS" | "BUCK" | "bzl" => Some(Python),
"rb" | "spec" | "rake" => Some(Ruby),
"py" | "py3" | "pyi" | "bzl" => Some(Python),
"rb" | "builder" | "spec" | "rake" => Some(Ruby),
"rs" => Some(Rust),
"ts" => Some(TypeScript),
"tsx" => Some(Tsx),
@ -91,4 +108,10 @@ mod tests {
let path = Path::new("foo.el");
assert_eq!(guess(path), Some(EmacsLisp));
}
#[test]
fn test_guess_by_whole_name() {
let path = Path::new("foo/.bashrc");
assert_eq!(guess(path), Some(Bash));
}
}