Browse Source

simplify loc computation for markdown

pull/1502/head
o2sh 12 months ago
parent
commit
3a8ad24e08
No known key found for this signature in database
GPG Key ID: E68B161DA72A5D7C
  1. 7
      CONTRIBUTING.md
  2. 1
      languages.yaml
  3. 24
      src/info/langs/language.rs
  4. 23
      src/info/langs/language.tera
  5. 6
      src/info/langs/mod.rs

7
CONTRIBUTING.md

@ -52,13 +52,6 @@ CSharp: # required, this will be the name of the enum variant for the language a @@ -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

1
languages.yaml

@ -1654,7 +1654,6 @@ Markdown: @@ -1654,7 +1654,6 @@ Markdown:
- red
chip: "#083FA1"
icon: '\u{E73E}'
line_types: [code, comments]
Nim:
type: programming
ascii: |

24
src/info/langs/language.rs

@ -202,32 +202,30 @@ impl InfoField for LanguagesInfo { @@ -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() {

23
src/info/langs/language.tera

@ -123,29 +123,6 @@ impl Language { @@ -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 -%}

6
src/info/langs/mod.rs

@ -19,10 +19,10 @@ pub fn get_loc_by_language_sorted( @@ -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);
@ -61,7 +61,7 @@ pub fn get_total_loc(loc_by_language: &[(Language, usize)]) -> usize { @@ -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],

Loading…
Cancel
Save