From 8d0fdc4a2e18748606c1f98a531f4b29e4a5bc9a Mon Sep 17 00:00:00 2001 From: Gustavo Date: Fri, 14 Mar 2025 20:14:14 -0300 Subject: [PATCH] feat: added identaton in the main file --- cli/commands/analyze.py | 6 ++++-- spice/analyze.py | 13 +++++-------- spice/identation.py | 27 +++++++++++++++++++-------- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/cli/commands/analyze.py b/cli/commands/analyze.py index cb47993..8f9c5fc 100644 --- a/cli/commands/analyze.py +++ b/cli/commands/analyze.py @@ -16,14 +16,16 @@ def analyze_command(file, all, json_output, LANG_FILE): available_stats = [ "line_count", "function_count", - "comment_line_count" + "comment_line_count", + "identation_level" ] # dictionary for the stats UPDATE THIS WHEN NEEDED PLEASE !!!!!!!! stats_labels = { "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") + "comment_line_count": messages.get("comment_line_count_option", "Comment Line Count"), + "identation_level": messages.get("identation_level", "Identation Level") } # If --all flag is used, skip the selection menu and use all stats diff --git a/spice/analyze.py b/spice/analyze.py index 5d5f748..c5a26b0 100644 --- a/spice/analyze.py +++ b/spice/analyze.py @@ -160,11 +160,8 @@ def count_comment_lines(code): return comment_count def analyze_code_structure(code): - indentation_info = detect_indentation(code) - - print(f"Detected Indentation Type: {indentation_info['indent_type']}") - print(f"Detected Indentation Size: {indentation_info['indent_size']}") - for line, level in indentation_info["levels"]: - # print(f"Indentation Level {level}: {line}") - print(f"Detected Indentation Type: {indentation_info['indent_type']}") - print(f"Detected Indentation Size: {indentation_info['indent_size']}") \ No newline at end of file + indentation_info = detect_indentation(code) + + print(f"Detected Indentation Type: {indentation_info['indent_type']}") + print(f"Detected Indentation Size: {indentation_info['indent_size']}") + \ No newline at end of file diff --git a/spice/identation.py b/spice/identation.py index d79102b..abaa8ff 100644 --- a/spice/identation.py +++ b/spice/identation.py @@ -1,28 +1,39 @@ -import os import re def detect_indentation(code): lines = code.split('\n') indentation_counts = {'tab': 0, 'space': 0} indentation_levels = [] + indent_sizes = {} for line in lines: - if line.strip() == '': - continue # skip empty lines + if line.strip() == '': # Skip empty lines + continue + leading_whitespace = re.match(r'^\s*', line).group() - #detect space, tab or new line within the function + if '\t' in leading_whitespace: indentation_counts['tab'] += 1 if ' ' in leading_whitespace: indentation_counts['space'] += 1 if '\t' in leading_whitespace and ' ' in leading_whitespace: - print(f"Identação mista detectada: {line}") + print(f"⚠ Identação mista detectada: {line}") + indent_level = len(leading_whitespace) - indentation_levels.append((line.strip(), indent_level)) + # Track indentation levels (ignoring zero indents) + if indent_level > 0: + indentation_levels.append((line.strip(), indent_level)) + indent_sizes[indent_level] = indent_sizes.get(indent_level, 0) + 1 + + # Decide indentation type indent_type = 'tab' if indentation_counts['tab'] > indentation_counts['space'] else 'space' - # qual estilo de identaçao for mais frequente será enviado para a variavel - indent_size = 4 # tipo um padrao de identacao + + # Determine the most common indent size + if indent_sizes: + indent_size = max(indent_sizes, key=indent_sizes.get) + else: + indent_size = 4 # Default if no indentation found return { "indent_type": indent_type,