diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0d0f9bf8..dc71ec14 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -52,13 +52,6 @@ CSharp: # required, this will be the name of the enum variant for the language a serialization: c# # required only if the Enum name `CSharp` doesn't match the display name `C#` ``` -> [!NOTE] -> An additional field, `line_types` can also be set on a language's attributes. It has been excluded because -> it is not necessary for the majority of languages. By default, only a language's lines of code are counted, but this -> field can be used to count other lines, too. For example, `line_types: [code, comments]`. This is useful in languages -> like Markdown, where the significant lines are mostly comments. A list of available fields to be used can be found in -> [tokei's documentation](https://docs.rs/tokei/latest/tokei/struct.Language.html#fields). - - link 1: https://github.com/XAMPPRocky/tokei#supported-languages - link 2: https://github.com/github/linguist/blob/master/lib/linguist/languages.yml - link 3: https://www.nerdfonts.com/cheat-sheet diff --git a/languages.yaml b/languages.yaml index a9c088c2..d1514993 100644 --- a/languages.yaml +++ b/languages.yaml @@ -1654,7 +1654,6 @@ Markdown: - red chip: "#083FA1" icon: '\u{E73E}' - line_types: [code, comments] Nim: type: programming ascii: | diff --git a/src/info/langs/language.rs b/src/info/langs/language.rs index 11d1952b..f9f9686a 100644 --- a/src/info/langs/language.rs +++ b/src/info/langs/language.rs @@ -202,32 +202,30 @@ impl InfoField for LanguagesInfo { } } -/// Counts the lines-of-code of a tokei `Language`. Takes into -/// account that a prose language's comments *are* its code. pub fn loc(language_type: &tokei::LanguageType, language: &tokei::Language) -> usize { - __loc(language_type, language) + __loc(language_type, language.code, language.comments) + language .children .iter() .fold(0, |sum, (lang_type, reports)| { - sum + reports - .iter() - .fold(0, |sum, report| sum + stats_loc(lang_type, &report.stats)) + sum + reports.iter().fold(0, |sum, report| { + let stats = report.stats.summarise(); + sum + __loc(lang_type, stats.code, stats.comments) + }) }) } -/// Counts the lines-of-code of a tokei `Report`. This is the child of a -/// `tokei::CodeStats`. -pub fn stats_loc(language_type: &tokei::LanguageType, stats: &tokei::CodeStats) -> usize { - let stats = stats.summarise(); - __stats_loc(language_type, &stats) +fn __loc(language_type: &tokei::LanguageType, code: usize, comments: usize) -> usize { + match language_type { + tokei::LanguageType::Markdown => code + comments, + _ => code, + } } #[cfg(test)] mod test { - use rstest::rstest; - use super::*; + use rstest::rstest; #[test] fn test_display_languages_info() { diff --git a/src/info/langs/language.tera b/src/info/langs/language.tera index c707dba5..05943cc5 100644 --- a/src/info/langs/language.tera +++ b/src/info/langs/language.tera @@ -123,29 +123,6 @@ impl Language { } } -fn __loc(language_type: &tokei::LanguageType, language: &tokei::Language) -> usize { - match language_type { - {% for language, attrs in languages -%} - {%- set line_types = attrs.line_types | default(value=['code']) -%} - tokei::LanguageType::{{ language }} => language.{{ line_types.0 }}{% for line_type in line_types | slice(start=1) %} + language.{{ line_type }}{% endfor %}, - {% endfor %} - _ => unimplemented!("Language Type {:?}", language_type), - } -} - - -fn __stats_loc(language_type: &tokei::LanguageType, stats: &tokei::CodeStats) -> usize { - match language_type { - {% for language, attrs in languages -%} - {%- set line_types = attrs.line_types | default(value=['code']) -%} - {%- if attrs.line_types -%} - tokei::LanguageType::{{ language }} => stats.{{ line_types.0 }}{% for line_type in line_types | slice(start=1) %} + stats.{{ line_type }}{% endfor %}, - {% endif -%} - {% endfor %} - _ => stats.code - } -} - {% for language, attrs in languages -%} {% if attrs.colors.rgb %} {% set ansi_length = attrs.colors.ansi | length -%} diff --git a/src/info/langs/mod.rs b/src/info/langs/mod.rs index 932ab877..f723d992 100644 --- a/src/info/langs/mod.rs +++ b/src/info/langs/mod.rs @@ -19,10 +19,10 @@ pub fn get_loc_by_language_sorted( language_types: &[LanguageType], include_hidden: bool, ) -> Result> { - let stats = get_statistics(dir, globs_to_exclude, language_types, include_hidden); + let locs = get_locs(dir, globs_to_exclude, language_types, include_hidden); let loc_by_language = - get_loc_by_language(&stats).context("Could not find any source code in this repository")?; + get_loc_by_language(&locs).context("Could not find any source code in this repository")?; let loc_by_language_sorted = sort_by_loc(loc_by_language); @@ -61,7 +61,7 @@ pub fn get_total_loc(loc_by_language: &[(Language, usize)]) -> usize { total_loc } -fn get_statistics( +fn get_locs( dir: &Path, globs_to_exclude: &[String], language_types: &[LanguageType],