|
15 | 15 | from collections import defaultdict |
16 | 16 | from contextlib import contextmanager |
17 | 17 | from pathlib import Path |
| 18 | +from typing import TYPE_CHECKING |
18 | 19 |
|
19 | 20 | from codewiki.src.be.dependency_analyzer.models.core import CallRelationship, Node |
20 | 21 | from codewiki.src.be.dependency_analyzer.utils.external_symbols import ( |
|
26 | 27 | from codewiki.src.be.dependency_analyzer.utils.patterns import CODE_EXTENSIONS |
27 | 28 | from codewiki.src.be.dependency_analyzer.utils.security import safe_open_text |
28 | 29 |
|
| 30 | +if TYPE_CHECKING: |
| 31 | + from codewiki.src.be.dependency_analyzer.analyzers.lean import LeanFile, LeanScope |
| 32 | + from codewiki.src.be.dependency_analyzer.analyzers.lean_resolution import LeanNameResolver |
| 33 | + |
29 | 34 | logger = logging.getLogger(__name__) |
30 | 35 |
|
31 | 36 |
|
@@ -76,6 +81,7 @@ def __init__(self): |
76 | 81 | self._dart_unscoped = False |
77 | 82 | self._go_visible: dict[str, set[str]] = {} |
78 | 83 | self._go_index = None |
| 84 | + self._reset_lean_state() |
79 | 85 | self.call_relationships: list[CallRelationship] = [] |
80 | 86 | self._python_project_modules: set = set() |
81 | 87 | self._python_external_import_roots: set = set() |
@@ -108,6 +114,7 @@ def analyze_code_files(self, code_files: list[dict], base_dir: str) -> dict: |
108 | 114 | self._dart_unscoped = False |
109 | 115 | self._go_visible = {} |
110 | 116 | self._go_index = None |
| 117 | + self._reset_lean_state() |
111 | 118 |
|
112 | 119 | files_analyzed = 0 |
113 | 120 | files_failed = 0 |
@@ -293,6 +300,8 @@ def _analyze_code_file(self, repo_dir: str, file_info: dict): |
293 | 300 | self._analyze_go_file(file_path, content, repo_dir) |
294 | 301 | elif language == "zig": |
295 | 302 | self._analyze_zig_file(file_path, content, repo_dir) |
| 303 | + elif language == "lean": |
| 304 | + self._analyze_lean_file(file_path, content, repo_dir) |
296 | 305 | elif language == "vue": |
297 | 306 | self._analyze_vue_file(file_path, content, repo_dir) |
298 | 307 | # else: |
@@ -668,6 +677,59 @@ def _analyze_vue_file(self, file_path: str, content: str, repo_dir: str): |
668 | 677 | except Exception: |
669 | 678 | logger.exception(f"Failed to analyze Vue file {file_path}") |
670 | 679 |
|
| 680 | + def _reset_lean_state(self) -> None: |
| 681 | + self._lean_files: dict[str, LeanFile] = {} |
| 682 | + self._lean_scopes: dict[str, LeanScope] = {} |
| 683 | + self._lean_aliases: dict[str, list[str]] = defaultdict(list) |
| 684 | + self._lean_private: set[str] = set() |
| 685 | + self._lean_protected: set[str] = set() |
| 686 | + self._lean_resolver: LeanNameResolver | None = None |
| 687 | + |
| 688 | + def _analyze_lean_file(self, file_path: str, content: str, repo_dir: str): |
| 689 | + """ |
| 690 | + Analyze a Lean 4 file. |
| 691 | +
|
| 692 | + Besides components and references, records the file's imports, each |
| 693 | + declaration's namespaces and open namespaces, and the constructor and |
| 694 | + field names of its structures, for Lean resolution by exact name. |
| 695 | +
|
| 696 | + Args: |
| 697 | + file_path: Path to the Lean file |
| 698 | + content: File content string |
| 699 | + repo_dir: Repository base directory |
| 700 | + """ |
| 701 | + from codewiki.src.be.dependency_analyzer.analyzers.lean import TreeSitterLeanAnalyzer |
| 702 | + |
| 703 | + try: |
| 704 | + analyzer = TreeSitterLeanAnalyzer(str(file_path), content, repo_path=repo_dir) |
| 705 | + |
| 706 | + for func in analyzer.nodes: |
| 707 | + self.functions[func.id] = func |
| 708 | + |
| 709 | + self.call_relationships.extend(analyzer.call_relationships) |
| 710 | + self._lean_files[analyzer.relative_path] = analyzer.scan |
| 711 | + self._lean_scopes.update(analyzer.scopes) |
| 712 | + for alias, node_id in analyzer.aliases.items(): |
| 713 | + self._lean_aliases[alias].append(node_id) |
| 714 | + self._lean_private.update(analyzer.private_ids) |
| 715 | + self._lean_protected.update(analyzer.protected_ids) |
| 716 | + except Exception: |
| 717 | + logger.exception(f"Failed to analyze Lean file {file_path}") |
| 718 | + |
| 719 | + def _resolve_lean_callee(self, callee: str, caller: Node) -> str | None: |
| 720 | + from codewiki.src.be.dependency_analyzer.analyzers.lean_resolution import LeanNameResolver |
| 721 | + |
| 722 | + if self._lean_resolver is None: |
| 723 | + self._lean_resolver = LeanNameResolver( |
| 724 | + self.functions, |
| 725 | + self._lean_files, |
| 726 | + self._lean_scopes, |
| 727 | + self._lean_aliases, |
| 728 | + self._lean_private, |
| 729 | + self._lean_protected, |
| 730 | + ) |
| 731 | + return self._lean_resolver.resolve(callee, caller) |
| 732 | + |
671 | 733 | def _analyze_go_file(self, file_path: str, content: str, repo_dir: str): |
672 | 734 | """ |
673 | 735 | Analyze Go file using tree-sitter based analyzer. |
@@ -860,11 +922,11 @@ def _resolve_call_relationships(self): |
860 | 922 | self.call_relationships.extend(link_go_interfaces(self.functions)) |
861 | 923 |
|
862 | 924 | def _has_authoritative_resolution(self, relationship: CallRelationship) -> bool: |
863 | | - """A Dart or Go caller's callee that the language's scoped resolution |
| 925 | + """A Dart, Go or Lean caller's callee that the language's scoped resolution |
864 | 926 | rejected is external or invisible; drop it so ast_parser's name-only |
865 | 927 | fallback cannot bind it to an unrelated component.""" |
866 | 928 | caller = self.functions.get(relationship.caller) |
867 | | - return caller is not None and caller.language in ("dart", "go") |
| 929 | + return caller is not None and caller.language in ("dart", "go", "lean") |
868 | 930 |
|
869 | 931 | def _dotted_project_packages(self) -> dict[str, set]: |
870 | 932 | """Project packages/namespaces, partitioned by language. Java and C# |
@@ -1007,6 +1069,9 @@ def _resolve_callee( |
1007 | 1069 | if caller_language == "go" and caller is not None: |
1008 | 1070 | # So is Go's: a name resolves only in the package it names. |
1009 | 1071 | return self._resolve_go_callee(relationship.callee, caller) |
| 1072 | + if caller_language == "lean" and caller is not None: |
| 1073 | + # And Lean's: only an exact FQN in a visible file. |
| 1074 | + return self._resolve_lean_callee(relationship.callee, caller) |
1010 | 1075 | if lang_indexes: |
1011 | 1076 | match = self._resolve_callee_in( |
1012 | 1077 | relationship, lang_indexes["exact"], lang_indexes["simple"] |
@@ -1152,6 +1217,8 @@ def _generate_visualization_data(self) -> dict: |
1152 | 1217 | node_classes.append("lang-go") |
1153 | 1218 | elif file_ext == ".zig": |
1154 | 1219 | node_classes.append("lang-zig") |
| 1220 | + elif file_ext == ".lean": |
| 1221 | + node_classes.append("lang-lean") |
1155 | 1222 | elif file_ext == ".vue": |
1156 | 1223 | node_classes.append("lang-vue") |
1157 | 1224 |
|
|
0 commit comments