Skip to content

Commit 2af6b5b

Browse files
committed
fix comment counting
1 parent 3a41567 commit 2af6b5b

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed

spice/analyze.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# gustavo testando alguma coisa
44
from spice.analyzers.identation import detect_indentation
5-
5+
from spice.utils.get_langague import detect_language
66

77

88
# this is the analyze function
@@ -26,6 +26,8 @@ def analyze_file(file_path: str, selected_stats=None):
2626
"file_name": os.path.basename(file_path)
2727
}
2828

29+
LANG = detect_language(file_path)
30+
2931
# read the code file only once and load it into memory
3032
with open(file_path, "r", encoding="utf-8") as file:
3133
code = file.read()
@@ -38,7 +40,7 @@ def analyze_file(file_path: str, selected_stats=None):
3840
# comment line count if requested
3941
if "comment_line_count" in selected_stats:
4042
from spice.analyzers.count_comment_lines import count_comment_lines
41-
results["comment_line_count"] = count_comment_lines(code)
43+
results["comment_line_count"] = count_comment_lines(code, LANG)
4244

4345
# @gtins botei sua funcao aqui pq ela usa o codigo raw e nao o tokenizado, ai so tirei ela ali de baixo pra nao ficar chamando o parser sem precisar
4446
# edit: ok i see whats going on, instead of appending the results to the resuls, this will itself print the results to the terminal
Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,63 @@
1-
# this will count comment lines, since our AST/Parser doesn't include comment lines, this needs to be done in the tokenized output of the lexer
2-
# not sure about that first line, im pretty sure like about 200% sure this is analyzing the raw code and not the tokenized code but ok
1+
# this will count comment lines for Python, JavaScript, Ruby, and Go
32
# COMMENT LINE IS A LINE THAT EXCLUSIVELY HAS A COMMENT
4-
# so like: y = 5 #sets y to 5 IS NOT A COMMENT LINE!!!!!!!!
5-
def count_comment_lines(code):
6-
"""Count lines that are exclusively comments (no code on the same line)"""
3+
def count_comment_lines(code, lang):
74
# split the code into lines
85
lines = code.splitlines()
96
comment_count = 0
107

8+
# Set language-specific comment markers
9+
if lang.lower() == "python":
10+
single_comment = "#"
11+
multi_start = '"""'
12+
alt_multi_start = "'''"
13+
elif lang.lower() == "javascript" or lang.lower() == "go":
14+
single_comment = "//"
15+
multi_start = "/*"
16+
elif lang.lower() == "ruby":
17+
single_comment = "#"
18+
multi_start = "=begin"
19+
else:
20+
raise ValueError(f"Unsupported language: {lang}")
21+
22+
# Track if we're inside a multi-line comment
23+
in_multi_comment = False
24+
1125
for line in lines:
1226
# Remove leading whitespace
1327
stripped = line.strip()
14-
# Check if this line consists only of a comment
15-
if stripped and stripped.startswith('#'):
28+
29+
# Skip empty lines
30+
if not stripped:
31+
continue
32+
33+
# Handle multi-line comment blocks
34+
if in_multi_comment:
35+
comment_count += 1
36+
# Check for end of multi-line comment
37+
if lang == "python" and (stripped.endswith('"""') or stripped.endswith("'''")):
38+
in_multi_comment = False
39+
elif (lang == "javascript" or lang == "go") and "*/" in stripped:
40+
in_multi_comment = False
41+
elif lang == "ruby" and stripped == "=end":
42+
in_multi_comment = False
43+
continue
44+
45+
# Check for start of multi-line comment
46+
if lang == "python" and (stripped.startswith('"""') or stripped.startswith("'''")):
47+
in_multi_comment = True
48+
comment_count += 1
49+
continue
50+
elif (lang == "javascript" or lang == "go") and stripped.startswith("/*"):
51+
in_multi_comment = True
52+
comment_count += 1
53+
continue
54+
elif lang == "ruby" and stripped == "=begin":
55+
in_multi_comment = True
56+
comment_count += 1
57+
continue
58+
59+
# Check for single-line comments
60+
if stripped.startswith(single_comment):
1661
comment_count += 1
1762

1863
return comment_count

spice/utils/get_langague.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
3+
def detect_language(file_path):
4+
_, ext = os.path.splitext(file_path)
5+
6+
if ext == ".rb":
7+
return "ruby"
8+
elif ext == ".py":
9+
return "python"
10+
elif ext == ".js":
11+
return "javascript"
12+
elif ext == ".go":
13+
return "go"
14+
else:
15+
raise ValueError(f"Unsupported file extension: {ext}")
16+
17+
# Example usage:
18+
if __name__ == "__main__":
19+
for path in ["example.py", "example.js", "example.rb", "example.go"]:
20+
print(f"{path}: {detect_language(path)}")

0 commit comments

Comments
 (0)