diff --git a/cli/main.py b/cli/main.py deleted file mode 100644 index a452d9c..0000000 --- a/cli/main.py +++ /dev/null @@ -1,94 +0,0 @@ -import os -import sys -import typer - -# this is the analyzer -from spice.analyze import analyze_file - -# here we import utilities -from cli.utils.get_translation import get_translation - -# here we import the commands -from cli.commands.translate import translate_command -from cli.commands.hello import hello_command -from cli.commands.version import version_command -from cli.commands.analyze import analyze_command - - -# initialize typer -app = typer.Typer() - -# add the current directory (cli) to the sys.path -sys.path.append('cli') - -# get current directory, this is needed for it to work on other peoples computers via pip -CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) - -# select a file to save the current selected langague (if saved to memory it wont persist between commands) -LANG_FILE = os.path.join(CURRENT_DIR, "lang.txt") - - - -# SPICE TRANSLATE COMMAND -@app.command() -def translate(): - """ - Set the language for CLI messages. - """ - - translate_command(LANG_FILE) - -# -- end -- # - - -# SPICE HELLO COMMAND -@app.command() -def hello(): - """ - Welcome message. - """ - - hello_command(LANG_FILE) - -# -- end -- # - - -# SPICE VERSION COMMAND -@app.command() -def version(): - """ - Display the current version of the application. - """ - - version_command(LANG_FILE, CURRENT_DIR) - -#--- end ---# - - -# SPICE ANALYZE COMMAND -@app.command() -def analyze( - file: str, - all: bool = typer.Option(False, "--all", help="Analyze all stats without selection menu"), - json_output: bool = typer.Option(False, "--json", help="Output results in JSON format") -): - """ - Analyze the given file. - """ - - analyze_command(file, all, json_output, LANG_FILE) - -# -- end -- # - - -def main(): - app() # run typer - -# -- end -- # - - -# whatever the fuck this is python makes no sense -if __name__ == "__main__": - main() - -# -- end -- # \ No newline at end of file diff --git a/spice/analyze.py b/spice/analyze.py index 7a1e3e7..5d5f748 100644 --- a/spice/analyze.py +++ b/spice/analyze.py @@ -9,6 +9,9 @@ from lexers.javascript.javascriptlexer import JavaScriptLexer from lexers.golang.golexer import GoLexer +# gustavo testando alguma coisa + +from spice.identation import detect_indentation # this will read the file extension and return the correct lexer def get_lexer_for_file(file_path): @@ -40,7 +43,7 @@ def analyze_file(file_path: str, selected_stats=None): """ # default to all stats if none specified if selected_stats is None: - selected_stats = ["line_count", "function_count", "comment_line_count"] + selected_stats = ["line_count", "function_count", "comment_line_count", "identation_level"] # initialize results with the file name (dont change this please) results = { @@ -80,6 +83,8 @@ def analyze_file(file_path: str, selected_stats=None): # count functions results["function_count"] = count_functions(ast) + if "identation_level" in selected_stats: + analyze_code_structure(code) return results @@ -152,4 +157,14 @@ def count_comment_lines(code): if stripped and stripped.startswith('#'): comment_count += 1 - return comment_count \ No newline at end of file + 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 diff --git a/spice/identation.py b/spice/identation.py new file mode 100644 index 0000000..d79102b --- /dev/null +++ b/spice/identation.py @@ -0,0 +1,31 @@ +import os +import re + +def detect_indentation(code): + lines = code.split('\n') + indentation_counts = {'tab': 0, 'space': 0} + indentation_levels = [] + + for line in lines: + if line.strip() == '': + continue # skip empty lines + 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}") + indent_level = len(leading_whitespace) + indentation_levels.append((line.strip(), indent_level)) + + 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 + + return { + "indent_type": indent_type, + "indent_size": indent_size, + "levels": indentation_levels + }