askrobots / askrobots_commitgen
Public
AI-Powered Git Commit Message Generator
danq9
Create commit message adjusting OpenAI initialization and error handling.
c0d2e25
2 years, 6 months ago
History
code_overview.py · 1.6 KB · 45 lines
| 1 | from tree_sitter import Language, Parser, Node |
| 2 | |
| 3 | def print_function_info(node: Node, indent: str): |
| 4 | # Print function or class name |
| 5 | name_node = node.child_by_field_name('name') |
| 6 | name = name_node.text.decode('utf8') if name_node else "<unnamed>" |
| 7 | print(f"{indent}Type: {node.type}, Name: {name}") |
| 8 | |
| 9 | # Optionally, print details about parameters, return type, etc. |
| 10 | |
| 11 | # Print details about the function or class body |
| 12 | body_node = node.child_by_field_name('body') |
| 13 | if body_node: |
| 14 | print(f"{indent}Body:") |
| 15 | print_structure(body_node, indent + " ") |
| 16 | |
| 17 | def print_structure(node: Node, indent: str): |
| 18 | for child in node.children: |
| 19 | if child.type in ['function_definition', 'class_definition']: |
| 20 | print_function_info(child, indent) |
| 21 | elif child.type in ['if_statement', 'for_statement', 'while_statement']: |
| 22 | # You can add more conditions or types as needed |
| 23 | print(f"{indent}{child.type} [Start: {child.start_point}, End: {child.end_point}]") |
| 24 | |
| 25 | def get_code_overview(file_path: str): |
| 26 | PY_LANGUAGE = Language('build/my-languages.so', 'python') |
| 27 | parser = Parser() |
| 28 | parser.set_language(PY_LANGUAGE) |
| 29 | |
| 30 | with open(file_path, 'rb') as file: |
| 31 | source_code = file.read() |
| 32 | |
| 33 | tree = parser.parse(source_code) |
| 34 | root_node = tree.root_node |
| 35 | print(f"Overview of {file_path}:") |
| 36 | print_structure(root_node, "") |
| 37 | |
| 38 | if __name__ == "__main__": |
| 39 | import sys |
| 40 | if len(sys.argv) != 2: |
| 41 | print("Usage: python code_overview.py <path_to_file>") |
| 42 | sys.exit(1) |
| 43 | |
| 44 | file_path = sys.argv[1] |
| 45 | get_code_overview(file_path) |