Skip to content

Commit d1cc9ec

Browse files
committed
fix count comment lines for all langs
1 parent e448a7c commit d1cc9ec

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

spice/analyzers/count_comment_lines.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,49 @@
22
# 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
33
# COMMENT LINE IS A LINE THAT EXCLUSIVELY HAS A COMMENT
44
# 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)"""
7-
# split the code into lines
5+
from utils.get_lexer import get_lexer_for_file
6+
import os
7+
8+
def count_comment_lines(file_path):
9+
"""Count lines that are exclusively comments in a file.
10+
11+
Args:
12+
file_path (str): Path to the file to analyze
13+
14+
Returns:
15+
int: Number of lines that are exclusively comments
16+
"""
17+
# Get the appropriate lexer for the file
18+
Lexer = get_lexer_for_file(file_path)
19+
lexer = Lexer()
20+
21+
# Read the file content
22+
with open(file_path, 'r', encoding='utf-8') as f:
23+
code = f.read()
24+
25+
# Split into lines
826
lines = code.splitlines()
927
comment_count = 0
1028

1129
for line in lines:
12-
# Remove leading whitespace
30+
# Remove leading/trailing whitespace
1331
stripped = line.strip()
14-
# Check if this line consists only of a comment
15-
if stripped and stripped.startswith('#'):
32+
33+
# Skip empty lines
34+
if not stripped:
35+
continue
36+
37+
# Tokenize the line
38+
tokens = lexer.tokenize(stripped)
39+
40+
# Check if the line consists only of comments
41+
is_comment_only = True
42+
for token in tokens:
43+
if token.type != 'Comment':
44+
is_comment_only = False
45+
break
46+
47+
if is_comment_only:
1648
comment_count += 1
1749

1850
return comment_count

0 commit comments

Comments
 (0)