Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only check tools name from shebang/env #1200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions languages.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"name": "APL",
"line_comment": ["⍝"],
"extensions": ["apl", "aplf", "apls"],
"quotes": [["'", "'"]],
"quotes": [["'", "'"]]
},
"Arduino": {
"name": "Arduino C++",
Expand Down Expand Up @@ -130,7 +130,7 @@
},
"AWK": {
"line_comment": ["#"],
"shebangs": ["#!/bin/awk -f"],
"shebangs": ["awk"],
"extensions": ["awk"]
},
"Ballerina": {
Expand All @@ -143,10 +143,9 @@
},
"Bash": {
"name": "BASH",
"shebangs": ["#!/bin/bash"],
"shebangs": ["bash"],
"line_comment": ["#"],
"quotes": [["\\\"", "\\\""], ["'", "'"]],
"env": ["bash"],
"extensions": ["bash"]
},
"Batch": {
Expand Down Expand Up @@ -229,6 +228,10 @@
"quotes": [["\\\"", "\\\""], ["\\\"\\\"\\\"", "\\\"\\\"\\\""]],
"extensions": ["ceylon"]
},
"Cgi": {
"name": "CGI",
"extensions": ["cgi"]
},
"Chapel": {
"line_comment": ["//"],
"multi_line_comments": [["/*", "*/"]],
Expand Down Expand Up @@ -329,9 +332,8 @@
},
"Crystal": {
"line_comment": ["#"],
"shebangs": ["#!/usr/bin/crystal"],
"shebangs": ["crystal"],
"quotes": [["\\\"", "\\\""], ["'", "'"]],
"env": ["crystal"],
"extensions": ["cr"]
},
"CSharp": {
Expand All @@ -344,9 +346,9 @@
},
"CShell": {
"name": "C Shell",
"shebangs": ["#!/bin/csh"],
"shebangs": ["csh"],
"line_comment": ["#"],
"env": ["csh"],
"filenames": [".cshrc"],
"extensions": ["csh"]
},
"Css": {
Expand Down Expand Up @@ -376,10 +378,10 @@
"extensions": ["cue"]
},
"Cython": {
"shebangs": ["cython"],
"line_comment": ["#"],
"doc_quotes": [["\\\"\\\"\\\"", "\\\"\\\"\\\""], ["'''", "'''"]],
"quotes": [["\\\"", "\\\""], ["'", "'"]],
"env": ["cython"],
"extensions": ["pyx", "pxd", "pxi"]
},
"D": {
Expand Down Expand Up @@ -502,9 +504,9 @@
"extensions": ["elm"]
},
"Elvish": {
"shebangs": ["elvish"],
"line_comment": ["#"],
"quotes": [["\\\"", "\\\""], ["'", "'"]],
"env": ["elvish"],
"extensions": ["elv"]
},
"EmacsDevEnv": {
Expand Down Expand Up @@ -538,10 +540,9 @@
"extensions": ["fnl"]
},
"Fish": {
"shebangs": ["#!/bin/fish"],
"shebangs": ["fish"],
"line_comment": ["#"],
"quotes": [["\\\"", "\\\""], ["'", "'"]],
"env": ["fish"],
"extensions": ["fish"]
},
"FlatBuffers": {
Expand Down Expand Up @@ -683,10 +684,10 @@
"extensions": ["gql", "graphql"]
},
"Groovy": {
"shebangs": ["groovy"],
"line_comment": ["//"],
"multi_line_comments": [["/*", "*/"]],
"quotes": [["\\\"", "\\\""]],
"env": ["groovy"],
"extensions": ["groovy", "grt", "gtpl", "gvy"]
},
"Gwion": {
Expand Down Expand Up @@ -899,8 +900,7 @@
"extensions": ["ipynb"]
},
"Just": {
"shebangs": ["#!/usr/bin/env just --justfile"],
"env": ["just"],
"shebangs": ["just"],
"line_comment": ["#"],
"extensions": ["just"],
"filenames": ["justfile"]
Expand All @@ -927,10 +927,9 @@
},
"Ksh": {
"name": "Korn shell",
"shebangs": ["#!/bin/ksh"],
"shebangs": ["ksh"],
"line_comment": ["#"],
"quotes": [["\\\"", "\\\""], ["'", "'"]],
"env": ["ksh"],
"extensions": ["ksh"]
},
"Lalrpop": {
Expand Down Expand Up @@ -1256,11 +1255,11 @@
"extensions": ["pas"]
},
"Perl": {
"shebangs": ["#!/usr/bin/perl"],
"shebangs": ["perl"],
"line_comment": ["#"],
"multi_line_comments": [["=pod", "=cut"]],
"quotes": [["\\\"", "\\\""], ["'", "'"]],
"extensions": ["pl", "pm"]
"extensions": ["pl", "pm", "t"]
},
"Pest": {
"line_comment": ["//"],
Expand Down Expand Up @@ -1386,10 +1385,10 @@
"nested": true
},
"Python": {
"shebangs": ["python"],
"line_comment": ["#"],
"doc_quotes": [["\\\"\\\"\\\"", "\\\"\\\"\\\""], ["'''", "'''"]],
"quotes": [["\\\"", "\\\""], ["'", "'"]],
"env": ["python", "python2", "python3"],
"mime": ["text/x-python"],
"extensions": ["py", "pyw", "pyi"]
},
Expand Down Expand Up @@ -1425,10 +1424,10 @@
"extensions": ["r"]
},
"Racket": {
"shebangs": ["racket"],
"line_comment": [";"],
"multi_line_comments": [["#|", "|#"]],
"nested": true,
"env": ["racket"],
"extensions": ["rkt", "scrbl"]
},
"Rakefile": {
Expand All @@ -1439,7 +1438,7 @@
"extensions": ["rake"]
},
"Raku": {
"shebangs": ["#!/usr/bin/raku", "#!/usr/bin/perl6"],
"shebangs": ["raku", "perl6"],
"line_comment": ["#"],
"multi_line_comments": [
["#`(", ")"],
Expand Down Expand Up @@ -1472,7 +1471,6 @@
["=begin SYNOPSIS", "=end SYNOPSIS"],
["=begin ", "=end "]
],
"env": ["raku", "perl6"],
"extensions": ["raku", "rakumod", "rakutest", "pm6", "pl6", "p6"]
},
"Razor": {
Expand Down Expand Up @@ -1529,10 +1527,10 @@
"extensions": ["spec"]
},
"Ruby": {
"shebangs": ["ruby"],
"line_comment": ["#"],
"multi_line_comments": [["=begin", "=end"]],
"quotes": [["\\\"", "\\\""], ["'", "'"]],
"env": ["ruby"],
"extensions": ["rb"]
},
"RubyHtml": {
Expand Down Expand Up @@ -1581,10 +1579,9 @@
},
"Sh": {
"name": "Shell",
"shebangs": ["#!/bin/sh"],
"shebangs": ["sh"],
"line_comment": ["#"],
"quotes": [["\\\"", "\\\""], ["'", "'"]],
"env": ["sh"],
"extensions": ["sh"]
},
"ShaderLab": {
Expand Down Expand Up @@ -1942,6 +1939,7 @@
},
"VimScript": {
"name": "Vim Script",
"filenames": [".vimrc"],
"line_comment": ["\\\""],
"quotes": [["\\\"", "\\\""], ["'", "'"]],
"extensions": ["vim"]
Expand Down Expand Up @@ -2029,7 +2027,7 @@
"extensions": ["zok"]
},
"Zsh": {
"shebangs": ["#!/bin/zsh"],
"shebangs": ["zsh"],
"line_comment": ["#"],
"quotes": [["\\\"", "\\\""], ["'", "'"]],
"extensions": ["zsh"]
Expand Down
65 changes: 36 additions & 29 deletions src/language/language_type.tera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl LanguageType {
/// ```
/// use tokei::LanguageType;
/// let lang = LanguageType::Bash;
/// assert_eq!(lang.shebangs(), &["#!/bin/bash"]);
/// assert_eq!(lang.shebangs(), &["bash"]);
/// ```
pub fn shebangs(self) -> &'static [&'static str] {
match self {
Expand Down Expand Up @@ -397,40 +397,47 @@ impl LanguageType {
let first_line = buf.split(|b| *b == b'\n').next()?;
let first_line = std::str::from_utf8(first_line).ok()?;

let mut words = first_line.split_whitespace();
match words.next() {
{# First match against any shebang paths, and then check if the
language matches any found in the environment shebang path. #}
// Normalize: Remove `#!` and all spaces after it, eg
// #! /bin/bash => /bin/bash
// #! /usr/bin/env perl => /usr/bin/env perl
let shebang_line = first_line.strip_prefix("#!")?.trim_start();
let mut words = shebang_line.split_whitespace();
let tool_path = words.next()?;

// Handle `env` by replacing it with the word after `env`, if any
let tool_name = if tool_path.ends_with("env") {
words.next().unwrap_or("env")
} else {
tool_path
};

// Extract the last part of the tool path (e.g., "bash" from "/usr/local/bin/bash")
let tool_name = Path::new(tool_name).file_name()?.to_str()?;
match tool_name {
// do exact match first, so perl6 => Raku instead of Perl
{% for key, value in languages -%}
{%- if value.shebangs %}
{%- for item in value.shebangs %}| Some("{{item}}") {% endfor %}=> Some({{key}}),
{%- for item in value.shebangs %}| "{{item}}" {% endfor %}=> Some({{key}}),
{% endif -%}
{%- endfor %}

Some("#!/usr/bin/env") => {
if let Some(word) = words.next() {
match word {
{% for key, value in languages -%}
{%- if value.env -%}
{%- for item in value.env %}
{% if loop.index == 1 %}
_ if word.starts_with("{{item}}")
{% else %}
|| word.starts_with("{{item}}")
{% endif %}
{% endfor %}=> Some({{key}}),
{% endif -%}
{%- endfor %}
env => {
warn!("Unknown environment: {:?}", env);
None
}
}
} else {
None
_ => {
// then try starts_with, especially for python, so python3.13 => Python
// however, pythonabc will also be recognized as Python
match tool_name {
{% for key, value in languages -%}
{%- if value.shebangs -%}
{%- for item in value.shebangs %}
{% if loop.index == 1 %}
_ if tool_name.starts_with("{{item}}")
{% else %}
|| tool_name.starts_with("{{item}}")
{% endif %}
{% endfor %}=> Some({{key}}),
{% endif -%}
{%- endfor %}
_ => None,
}
}
_ => None,
}
}
}
Expand Down