Skip to content

Commit 3a8ad24

Browse files
committed
simplify loc computation for markdown
1 parent ebba333 commit 3a8ad24

File tree

5 files changed

+14
-47
lines changed

5 files changed

+14
-47
lines changed

CONTRIBUTING.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@ CSharp: # required, this will be the name of the enum variant for the language a
5252
serialization: c# # required only if the Enum name `CSharp` doesn't match the display name `C#`
5353
```
5454
55-
> [!NOTE]
56-
> An additional field, `line_types` can also be set on a language's attributes. It has been excluded because
57-
> it is not necessary for the majority of languages. By default, only a language's lines of code are counted, but this
58-
> field can be used to count other lines, too. For example, `line_types: [code, comments]`. This is useful in languages
59-
> like Markdown, where the significant lines are mostly comments. A list of available fields to be used can be found in
60-
> [tokei's documentation](https://docs.rs/tokei/latest/tokei/struct.Language.html#fields).
61-
6255
- link 1: https://github.com/XAMPPRocky/tokei#supported-languages
6356
- link 2: https://github.com/github/linguist/blob/master/lib/linguist/languages.yml
6457
- link 3: https://www.nerdfonts.com/cheat-sheet

languages.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,6 @@ Markdown:
16541654
- red
16551655
chip: "#083FA1"
16561656
icon: '\u{E73E}'
1657-
line_types: [code, comments]
16581657
Nim:
16591658
type: programming
16601659
ascii: |

src/info/langs/language.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,32 +202,30 @@ impl InfoField for LanguagesInfo {
202202
}
203203
}
204204

205-
/// Counts the lines-of-code of a tokei `Language`. Takes into
206-
/// account that a prose language's comments *are* its code.
207205
pub fn loc(language_type: &tokei::LanguageType, language: &tokei::Language) -> usize {
208-
__loc(language_type, language)
206+
__loc(language_type, language.code, language.comments)
209207
+ language
210208
.children
211209
.iter()
212210
.fold(0, |sum, (lang_type, reports)| {
213-
sum + reports
214-
.iter()
215-
.fold(0, |sum, report| sum + stats_loc(lang_type, &report.stats))
211+
sum + reports.iter().fold(0, |sum, report| {
212+
let stats = report.stats.summarise();
213+
sum + __loc(lang_type, stats.code, stats.comments)
214+
})
216215
})
217216
}
218217

219-
/// Counts the lines-of-code of a tokei `Report`. This is the child of a
220-
/// `tokei::CodeStats`.
221-
pub fn stats_loc(language_type: &tokei::LanguageType, stats: &tokei::CodeStats) -> usize {
222-
let stats = stats.summarise();
223-
__stats_loc(language_type, &stats)
218+
fn __loc(language_type: &tokei::LanguageType, code: usize, comments: usize) -> usize {
219+
match language_type {
220+
tokei::LanguageType::Markdown => code + comments,
221+
_ => code,
222+
}
224223
}
225224

226225
#[cfg(test)]
227226
mod test {
228-
use rstest::rstest;
229-
230227
use super::*;
228+
use rstest::rstest;
231229

232230
#[test]
233231
fn test_display_languages_info() {

src/info/langs/language.tera

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -123,29 +123,6 @@ impl Language {
123123
}
124124
}
125125

126-
fn __loc(language_type: &tokei::LanguageType, language: &tokei::Language) -> usize {
127-
match language_type {
128-
{% for language, attrs in languages -%}
129-
{%- set line_types = attrs.line_types | default(value=['code']) -%}
130-
tokei::LanguageType::{{ language }} => language.{{ line_types.0 }}{% for line_type in line_types | slice(start=1) %} + language.{{ line_type }}{% endfor %},
131-
{% endfor %}
132-
_ => unimplemented!("Language Type {:?}", language_type),
133-
}
134-
}
135-
136-
137-
fn __stats_loc(language_type: &tokei::LanguageType, stats: &tokei::CodeStats) -> usize {
138-
match language_type {
139-
{% for language, attrs in languages -%}
140-
{%- set line_types = attrs.line_types | default(value=['code']) -%}
141-
{%- if attrs.line_types -%}
142-
tokei::LanguageType::{{ language }} => stats.{{ line_types.0 }}{% for line_type in line_types | slice(start=1) %} + stats.{{ line_type }}{% endfor %},
143-
{% endif -%}
144-
{% endfor %}
145-
_ => stats.code
146-
}
147-
}
148-
149126
{% for language, attrs in languages -%}
150127
{% if attrs.colors.rgb %}
151128
{% set ansi_length = attrs.colors.ansi | length -%}

src/info/langs/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ pub fn get_loc_by_language_sorted(
1919
language_types: &[LanguageType],
2020
include_hidden: bool,
2121
) -> Result<Vec<(Language, usize)>> {
22-
let stats = get_statistics(dir, globs_to_exclude, language_types, include_hidden);
22+
let locs = get_locs(dir, globs_to_exclude, language_types, include_hidden);
2323

2424
let loc_by_language =
25-
get_loc_by_language(&stats).context("Could not find any source code in this repository")?;
25+
get_loc_by_language(&locs).context("Could not find any source code in this repository")?;
2626

2727
let loc_by_language_sorted = sort_by_loc(loc_by_language);
2828

@@ -61,7 +61,7 @@ pub fn get_total_loc(loc_by_language: &[(Language, usize)]) -> usize {
6161
total_loc
6262
}
6363

64-
fn get_statistics(
64+
fn get_locs(
6565
dir: &Path,
6666
globs_to_exclude: &[String],
6767
language_types: &[LanguageType],

0 commit comments

Comments
 (0)