Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli/commands/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def analyze_command(file, all, json_output, LANG_FILE):
"line_count",
"function_count",
"comment_line_count",
"inline_comment_count",
"indentation_level"
]

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

Expand Down
2 changes: 2 additions & 0 deletions cli/translations/en.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
"line_count": "The file has {count} lines",
"function_count": "The file has {count} functions",
"comment_line_count": "The file has {count} comment lines",
"inline_comment_count": "The file has {count} inline comments",
# keys for analyze command checkbox menu
"select_stats": "Select stats to display:",
"line_count_option": "Line Count",
"function_count_option": "Function Count",
"comment_line_count_option": "Comment Line Count",
"inline_comment_count_option": "Inline Comment Count",
"no_stats_selected": "No stats selected. Analysis cancelled.",
"confirm_and_analyze": "Confirm and analyze",
"checkbox_hint": "(Use space to select, enter to confirm)",
Expand Down
4 changes: 3 additions & 1 deletion cli/translations/fremen.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
messages = {
# keys for the hello command
"welcome": "🌶️ Salam, wanderer, and welcome to the sietch of [bold red]SpiceCode[/]! 🌶️",
"description": "🔥 The [yellow]Fedaykin CLI[/] that ignites your code with spice, as fierce as Arrakis dunes 🥵",
"description": "🔥 The [yellow]Fedaykin CLI[/] that ignites your code with spice, as fierce as Arrakis' dunes 🥵",
# error messages
"error": "خطأ:",
# keys for the analyze command output
"analyzing_file": "Deciphering the file's sand-script",
"line_count": "The file spans {count} dunes",
"function_count": "The file holds {count} sacred routines",
"comment_line_count": "The file whispers {count} lines of hidden lore",
"inline_comment_count": "The file contains {count} passages of dual meaning",
# keys for analyze command checkbox menu
"select_stats": "Choose the omens to unveil:",
"line_count_option": "Dune Count",
"function_count_option": "Sacred Routines",
"comment_line_count_option": "Whispered Lore",
"inline_comment_count_option": "Passages of Dual Meaning",
"no_stats_selected": "No omens were heeded. The analysis fades into the sands.",
"confirm_and_analyze": "Seal your fate and analyze",
"checkbox_hint": "(Use space to mark, enter to proceed)"
Expand Down
2 changes: 2 additions & 0 deletions cli/translations/pt-br.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
"line_count": "O arquivo tem {count} linhas",
"function_count": "O arquivo tem {count} funções",
"comment_line_count": "O arquivo tem {count} linhas de comentário",
"inline_comment_count": "O arquivo tem {count} comentários inline",
# chaves para o menu de seleção do comando analyze
"select_stats": "Selecione as estatísticas para exibir:",
"line_count_option": "Contagem de Linhas",
"function_count_option": "Contagem de Funções",
"comment_line_count_option": "Contagem de Linhas de Comentário",
"inline_comment_count_option": "Contagem de Comentários Inline",
"no_stats_selected": "Nenhuma estatística selecionada. Análise cancelada.",
"confirm_and_analyze": "Confirmar e analisar",
"checkbox_hint": "(Use espaço para selecionar, enter para confirmar)"
Expand Down
13 changes: 11 additions & 2 deletions spice/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def analyze_file(file_path: str, selected_stats: Optional[List[str]] = None) ->
Args:
file_path (str): Path to the file to analyze
selected_stats (list, optional): List of stats to compute. If None, compute all stats.
Valid stats are: "line_count", "function_count", "comment_line_count", "indentation_level"
Valid stats are: "line_count", "function_count", "comment_line_count",
"inline_comment_count", "indentation_level"

Returns:
dict: Dictionary containing the requested stats and file information
Expand All @@ -34,7 +35,7 @@ def analyze_file(file_path: str, selected_stats: Optional[List[str]] = None) ->
raise ValueError("File has no extension")

# Define valid stats
valid_stats = ["line_count", "function_count", "comment_line_count", "indentation_level"]
valid_stats = ["line_count", "function_count", "comment_line_count", "inline_comment_count", "indentation_level"]

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

# inline comment count if requested
if "inline_comment_count" in selected_stats:
from spice.analyzers.count_inline_comments import count_inline_comments
from utils.get_lexer import get_lexer_for_file
LexerClass = get_lexer_for_file(file_path)
lexer = LexerClass(source_code=code) # Pass source_code explicitly
results["inline_comment_count"] = count_inline_comments(file_path)

# indentation analysis if requested
if "indentation_level" in selected_stats:
indentation_info = detect_indentation(code)
Expand Down
52 changes: 52 additions & 0 deletions spice/analyzers/count_inline_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# this will count inline comments, which are lines that have both code and comments
# INLINE COMMENT LINE IS A LINE THAT HAS BOTH CODE AND A COMMENT
# so like: y = 5 #sets y to 5 IS AN INLINE COMMENT LINE!!!!!!!!
from utils.get_lexer import get_lexer_for_file
from lexers.token import TokenType
import os

def count_inline_comments(file_path):
"""Count lines that have both code and comments in a file.

Args:
file_path (str): Path to the file to analyze

Returns:
int: Number of lines that have both code and comments
"""
# Get the appropriate lexer for the file
Lexer = get_lexer_for_file(file_path)

# Read the file content
with open(file_path, 'r', encoding='utf-8') as f:
code = f.read()

# Initialize lexer with source code
lexer = Lexer(source_code=code)

# Get all tokens
tokens = lexer.tokenize()

# Group tokens by line number
tokens_by_line = {}
for token in tokens:
if token.line not in tokens_by_line:
tokens_by_line[token.line] = []
tokens_by_line[token.line].append(token)

# Count lines that have both code and comment tokens
inline_comment_count = 0
for line_num, line_tokens in tokens_by_line.items():
has_comment = False
has_code = False

for token in line_tokens:
if token.type == TokenType.COMMENT:
has_comment = True
elif token.type not in [TokenType.NEWLINE, TokenType.COMMENT]:
has_code = True

if has_comment and has_code:
inline_comment_count += 1

return inline_comment_count