Skip to content

Commit

Permalink
simplify loc computation for markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh committed Dec 28, 2024
1 parent ebba333 commit 3a8ad24
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 47 deletions.
7 changes: 0 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion languages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,6 @@ Markdown:
- red
chip: "#083FA1"
icon: '\u{E73E}'
line_types: [code, comments]
Nim:
type: programming
ascii: |
Expand Down
24 changes: 11 additions & 13 deletions src/info/langs/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
23 changes: 0 additions & 23 deletions src/info/langs/language.tera
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
Expand Down
6 changes: 3 additions & 3 deletions src/info/langs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ pub fn get_loc_by_language_sorted(
language_types: &[LanguageType],
include_hidden: bool,
) -> Result<Vec<(Language, usize)>> {
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);

Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit 3a8ad24

Please sign in to comment.