Permalink
Browse files

reimplemented comment syntax again, added OCaml, and Standard ML

  • Loading branch information...
Aaronepower committed Jan 26, 2016
1 parent 3293a71 commit c4889327f8ab4006e6d701b827d7a63a3ad57b7c
Showing with 130 additions and 84 deletions.
  1. +0 −1 .gitignore
  2. +25 −0 CONTRIBUTING.md
  3. +50 −0 Cargo.lock
  4. +3 −3 Cargo.toml
  5. +13 −0 LICENCE-APACHE
  6. +1 −1 LICENSE → LICENCE-MIT
  7. +3 −4 README.md
  8. +7 −25 src/fsutil.rs
  9. +22 −49 src/language.rs
  10. +6 −1 src/main.rs
@@ -2,5 +2,4 @@ target
src/test
.settings
.commit_message
*.lock
*.bk
@@ -0,0 +1,25 @@
# Contributing to Tokei

* [Language Addition](#language-addition)
* [Bug Reports](#bug-reports)

# Language Addition
In order to properly add a language to count, there is a couple of key pieces needed.

* Name of the language
* Any file extensions associated with the language
* The comment syntax
- Does it have multiple single line comment symbols?
- Does it only contain single line comments? Or only multi-line comments?
- Is just C style comments? `/* */, //`

# Bug Reports
Please include the error message, and a minimum working example including the file, or file structure.

```
This file crashes the program.
<filename>
\`\`\`
\`\`\`
```

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -3,13 +3,13 @@
# found in the LICENSE file.
[package]
name = "tokei"
version = "1.3.0"
version = "1.3.1"
authors = ["Aaronepower <theaaronepower@gmail.com>"]
repository = "https://github.com/Aaronepower/tokei.git"
homepage = "https://aaronepower.github.io/tokei/"
license = "MIT"
license = "APACHE(2.0)/MIT"
readme = "README.md"
description = "Count lines of code within a directory"
description = "Count lines of code within a directory, quickly."

[profile.dev]
debug = true
@@ -0,0 +1,13 @@
Copyright 2016 Aaron Power

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
@@ -1,6 +1,6 @@
MIT License (MIT)

Copyright (c) 2015 Aaron Power
Copyright (c) 2016 Aaron Power

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -135,7 +135,7 @@ YAML

## Common issues

If you get errors like the following, it is mostly like due to having folders with paths that too long. For example NPM<3.0.0 `node_modules` generates long path files, either use the `exclude` argument, or in the case of NPM, update to >3.0.0
If you get errors like the following, it is mostly like due to having folders with paths that too long. For example NPM<3.0.0 `node_modules` generates long path files, which causes problems on windows. You can exclude these paths with the`exclude` argument, or in the case of NPM, update to >3.0.0, and update your codebase dependencies.
```
"The system cannot find the path specified.\r\n"
```
@@ -149,7 +149,6 @@ Illegal instruction: 4

See CONTRIBUTORS.md for a full list of contributors.

This application is free software: you can redistribute it and/or modify
it under the terms of the MIT License.
Tokei is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE for more information.
See [LICENCE-APACHE](./LICENCE-APACHE), [LICENCE-MIT](./LICENCE-MIT) for more information.
@@ -9,41 +9,24 @@ use std::fs::metadata;
use self::glob::glob;

pub fn contains_comments(file: &str, comment: &str, comment_end: &str) -> bool {
let mut in_quotes = false;
let mut in_comments: usize = 0;
'window: for chars in file.chars().collect::<Vec<char>>().windows(comment.len()) {

let section = {
let mut section = String::new();
for ch in chars {
section.push(*ch);
}
section
};
let mut chars = chars.iter();
while let Some(ch) = chars.next() {
let ch = *ch;
if ch == '"' {
in_quotes = !in_quotes;
// This is to solve the problem of having "".
if let Some(ch) = chars.next() {
if *ch == '"' {
in_quotes = !in_quotes;
}
}

continue 'window;
}
if in_quotes {
continue;
}
if section == comment {
in_comments += 1;
continue 'window;
} else if section == comment_end {
if section == comment {
in_comments += 1;
continue 'window;
} else if section == comment_end {
if in_comments != 0 {
in_comments -= 1;
continue 'window;
}
continue 'window;
}
}
in_comments != 0
@@ -106,10 +89,9 @@ pub fn get_all_files(path: String, ignored_directories: &[String]) -> Vec<String
#[allow(dead_code, unused_imports)]
mod tests {
use super::*;

#[test]
fn comment_start_in_quotes() {
assert!(!contains_comments("Hello \"/*\" World", "/*", "*/"));
assert!(contains_comments("Hello \"/*\" World", "/*", "*/"));
}

#[test]
@@ -4,7 +4,7 @@

use std::fmt;

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct Language<'a> {
pub name: &'a str,
pub line_comment: &'a str,
@@ -21,88 +21,61 @@ pub struct Language<'a> {


impl<'a> Language<'a> {
pub fn new<'b>(name: &'a str,
line_comment: &'a str,
multi_line: &'a str,
multi_line_end: &'a str)
-> Language<'a> {
pub fn new(name: &'a str,
line_comment: &'a str,
multi_line: &'a str,
multi_line_end: &'a str)
-> Self {

Language {
name: name,
line_comment: line_comment,
multi_line: multi_line,
multi_line_end: multi_line_end,
files: Vec::new(),
code: 0,
comments: 0,
blanks: 0,
lines: 0,
total: 0,
printed: false,
..Self::default()
}
}

pub fn new_c(name: &'a str) -> Language<'a> {
pub fn new_c(name: &'a str) -> Self {
Language {
name: name,
line_comment: "//",
multi_line: "/*",
multi_line_end: "*/",
files: Vec::new(),
code: 0,
comments: 0,
blanks: 0,
lines: 0,
total: 0,
printed: false,
..Self::default()
}
}

pub fn new_html(name: &'a str) -> Language<'a> {
pub fn new_html(name: &'a str) -> Self {
Language {
name: name,
line_comment: "<!--",
multi_line: "<!--",
multi_line_end: "-->",
files: Vec::new(),
code: 0,
comments: 0,
blanks: 0,
lines: 0,
total: 0,
printed: false,
..Self::default()
}
}

pub fn new_blank(name: &'a str) -> Language<'a> {
pub fn new_blank(name: &'a str) -> Self {
Language { name: name, ..Self::default() }
}

pub fn new_single(name: &'a str, line_comment: &'a str) -> Self {
Language {
name: name,
line_comment: "",
line_comment: line_comment,
multi_line: "",
multi_line_end: "",
files: Vec::new(),
code: 0,
comments: 0,
blanks: 0,
lines: 0,
total: 0,
printed: false,
..Self::default()
}
}

pub fn new_single(name: &'a str, line_comment: &'a str) -> Language<'a> {
pub fn new_multi(name: &'a str, multi_line: &'a str, multi_line_end: &'a str) -> Self {
Language {
name: name,
line_comment: line_comment,
multi_line: "",
multi_line_end: "",
files: Vec::new(),
code: 0,
comments: 0,
blanks: 0,
lines: 0,
total: 0,
printed: false,
multi_line: multi_line,
multi_line_end: multi_line_end,
..Self::default()
}
}

@@ -59,15 +59,17 @@ fn main() {
let markdown = RefCell::new(Language::new_blank("Markdown"));
let objective_c = RefCell::new(Language::new_c("Objective-C"));
let objective_cpp = RefCell::new(Language::new_c("Objective-C++"));
let ocaml = RefCell::new(Language::new_multi("OCaml", "(*", "*)"));
let php = RefCell::new(Language::new("PHP", "#,//", "/*", "*/"));
let pascal = RefCell::new(Language::new("Pascal", "//,(*", "{", "}"));
let perl = RefCell::new(Language::new("Perl", "#", "=", "=cut"));
let python = RefCell::new(Language::new("Python", "#", "'''", "'''"));
let r = RefCell::new(Language::new("R", "#", "", ""));
let r = RefCell::new(Language::new_single("R", "#"));
let ruby = RefCell::new(Language::new("Ruby", "#", "=begin", "=end"));
let ruby_html = RefCell::new(Language::new_html("Ruby HTML"));
let rust = RefCell::new(Language::new("Rust", "//,///,//!", "/*", "*/"));
let sass = RefCell::new(Language::new_c("Sass"));
let sml = RefCell::new(Language::new_multi("Standard ML", "(*", "*)"));
let sql = RefCell::new(Language::new("SQL", "--", "/*", "*/"));
let swift = RefCell::new(Language::new_c("Swift"));
let tex = RefCell::new(Language::new_single("TeX", "%"));
@@ -128,6 +130,8 @@ fn main() {
languages.insert("less", &less);
languages.insert("m", &objective_c);
languages.insert("md", &markdown);
languages.insert("ml", &ocaml);
languages.insert("mli", &ocaml);
languages.insert("mm", &objective_cpp);
languages.insert("php", &php);
languages.insert("pas", &pascal);
@@ -140,6 +144,7 @@ fn main() {
languages.insert("rs", &rust);
languages.insert("sass", &sass);
languages.insert("scss", &sass);
languages.insert("sml", &sml);
languages.insert("sql", &sql);
languages.insert("swift", &swift);
languages.insert("tex", &tex);

0 comments on commit c488932

Please sign in to comment.