Skip to content

Commit bdef661

Browse files
authored
Merge pull request #137 from spicecode-cli/count-inline-comments
Count inline comments
2 parents fc252d8 + dd4a0c7 commit bdef661

6 files changed

Lines changed: 72 additions & 3 deletions

File tree

cli/commands/analyze.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def analyze_command(file, all, json_output, LANG_FILE):
1717
"line_count",
1818
"function_count",
1919
"comment_line_count",
20+
"inline_comment_count",
2021
"indentation_level"
2122
]
2223

@@ -25,6 +26,7 @@ def analyze_command(file, all, json_output, LANG_FILE):
2526
"line_count": messages.get("line_count_option", "Line Count"),
2627
"function_count": messages.get("function_count_option", "Function Count"),
2728
"comment_line_count": messages.get("comment_line_count_option", "Comment Line Count"),
29+
"inline_comment_count": messages.get("inline_comment_count_option", "Inline Comment Count"),
2830
"indentation_level": messages.get("indentation_level_option", "Indentation Analysis")
2931
}
3032

cli/translations/en.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
"line_count": "The file has {count} lines",
1010
"function_count": "The file has {count} functions",
1111
"comment_line_count": "The file has {count} comment lines",
12+
"inline_comment_count": "The file has {count} inline comments",
1213
# keys for analyze command checkbox menu
1314
"select_stats": "Select stats to display:",
1415
"line_count_option": "Line Count",
1516
"function_count_option": "Function Count",
1617
"comment_line_count_option": "Comment Line Count",
18+
"inline_comment_count_option": "Inline Comment Count",
1719
"no_stats_selected": "No stats selected. Analysis cancelled.",
1820
"confirm_and_analyze": "Confirm and analyze",
1921
"checkbox_hint": "(Use space to select, enter to confirm)",

cli/translations/fremen.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
messages = {
22
# keys for the hello command
33
"welcome": "🌶️ Salam, wanderer, and welcome to the sietch of [bold red]SpiceCode[/]! 🌶️",
4-
"description": "🔥 The [yellow]Fedaykin CLI[/] that ignites your code with spice, as fierce as Arrakis dunes 🥵",
4+
"description": "🔥 The [yellow]Fedaykin CLI[/] that ignites your code with spice, as fierce as Arrakis' dunes 🥵",
55
# error messages
66
"error": "خطأ:",
77
# keys for the analyze command output
88
"analyzing_file": "Deciphering the file's sand-script",
99
"line_count": "The file spans {count} dunes",
1010
"function_count": "The file holds {count} sacred routines",
1111
"comment_line_count": "The file whispers {count} lines of hidden lore",
12+
"inline_comment_count": "The file contains {count} passages of dual meaning",
1213
# keys for analyze command checkbox menu
1314
"select_stats": "Choose the omens to unveil:",
1415
"line_count_option": "Dune Count",
1516
"function_count_option": "Sacred Routines",
1617
"comment_line_count_option": "Whispered Lore",
18+
"inline_comment_count_option": "Passages of Dual Meaning",
1719
"no_stats_selected": "No omens were heeded. The analysis fades into the sands.",
1820
"confirm_and_analyze": "Seal your fate and analyze",
1921
"checkbox_hint": "(Use space to mark, enter to proceed)"

cli/translations/pt-br.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
"line_count": "O arquivo tem {count} linhas",
1010
"function_count": "O arquivo tem {count} funções",
1111
"comment_line_count": "O arquivo tem {count} linhas de comentário",
12+
"inline_comment_count": "O arquivo tem {count} comentários inline",
1213
# chaves para o menu de seleção do comando analyze
1314
"select_stats": "Selecione as estatísticas para exibir:",
1415
"line_count_option": "Contagem de Linhas",
1516
"function_count_option": "Contagem de Funções",
1617
"comment_line_count_option": "Contagem de Linhas de Comentário",
18+
"inline_comment_count_option": "Contagem de Comentários Inline",
1719
"no_stats_selected": "Nenhuma estatística selecionada. Análise cancelada.",
1820
"confirm_and_analyze": "Confirmar e analisar",
1921
"checkbox_hint": "(Use espaço para selecionar, enter para confirmar)"

spice/analyze.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def analyze_file(file_path: str, selected_stats: Optional[List[str]] = None) ->
1010
Args:
1111
file_path (str): Path to the file to analyze
1212
selected_stats (list, optional): List of stats to compute. If None, compute all stats.
13-
Valid stats are: "line_count", "function_count", "comment_line_count", "indentation_level"
13+
Valid stats are: "line_count", "function_count", "comment_line_count",
14+
"inline_comment_count", "indentation_level"
1415
1516
Returns:
1617
dict: Dictionary containing the requested stats and file information
@@ -34,7 +35,7 @@ def analyze_file(file_path: str, selected_stats: Optional[List[str]] = None) ->
3435
raise ValueError("File has no extension")
3536

3637
# Define valid stats
37-
valid_stats = ["line_count", "function_count", "comment_line_count", "indentation_level"]
38+
valid_stats = ["line_count", "function_count", "comment_line_count", "inline_comment_count", "indentation_level"]
3839

3940
# default to all stats if none specified
4041
if selected_stats is None:
@@ -71,6 +72,14 @@ def analyze_file(file_path: str, selected_stats: Optional[List[str]] = None) ->
7172
lexer = LexerClass(source_code=code) # Pass source_code explicitly
7273
results["comment_line_count"] = count_comment_lines(file_path)
7374

75+
# inline comment count if requested
76+
if "inline_comment_count" in selected_stats:
77+
from spice.analyzers.count_inline_comments import count_inline_comments
78+
from utils.get_lexer import get_lexer_for_file
79+
LexerClass = get_lexer_for_file(file_path)
80+
lexer = LexerClass(source_code=code) # Pass source_code explicitly
81+
results["inline_comment_count"] = count_inline_comments(file_path)
82+
7483
# indentation analysis if requested
7584
if "indentation_level" in selected_stats:
7685
indentation_info = detect_indentation(code)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# this will count inline comments, which are lines that have both code and comments
2+
# INLINE COMMENT LINE IS A LINE THAT HAS BOTH CODE AND A COMMENT
3+
# so like: y = 5 #sets y to 5 IS AN INLINE COMMENT LINE!!!!!!!!
4+
from utils.get_lexer import get_lexer_for_file
5+
from lexers.token import TokenType
6+
import os
7+
8+
def count_inline_comments(file_path):
9+
"""Count lines that have both code and 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 have both code and comments
16+
"""
17+
# Get the appropriate lexer for the file
18+
Lexer = get_lexer_for_file(file_path)
19+
20+
# Read the file content
21+
with open(file_path, 'r', encoding='utf-8') as f:
22+
code = f.read()
23+
24+
# Initialize lexer with source code
25+
lexer = Lexer(source_code=code)
26+
27+
# Get all tokens
28+
tokens = lexer.tokenize()
29+
30+
# Group tokens by line number
31+
tokens_by_line = {}
32+
for token in tokens:
33+
if token.line not in tokens_by_line:
34+
tokens_by_line[token.line] = []
35+
tokens_by_line[token.line].append(token)
36+
37+
# Count lines that have both code and comment tokens
38+
inline_comment_count = 0
39+
for line_num, line_tokens in tokens_by_line.items():
40+
has_comment = False
41+
has_code = False
42+
43+
for token in line_tokens:
44+
if token.type == TokenType.COMMENT:
45+
has_comment = True
46+
elif token.type not in [TokenType.NEWLINE, TokenType.COMMENT]:
47+
has_code = True
48+
49+
if has_comment and has_code:
50+
inline_comment_count += 1
51+
52+
return inline_comment_count

0 commit comments

Comments
 (0)