Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
feat: Add Lean 4 language support (lightweight regex tier) (#600)
Matt Van Hornaf8215444543
diff --git a/docs/LANGUAGE_SUPPORT.md b/docs/LANGUAGE_SUPPORT.mdindex 616fad176..e4f29e690 100644--- a/docs/LANGUAGE_SUPPORT.md+++ b/docs/LANGUAGE_SUPPORT.md@@ -173,11 +173,13 @@ honest file-to-file dependencies, no symbol-level claims.| Language | Extensions | Import Forms Resolved ||----------|-----------|----------------------|-| **Elixir** | `.ex` `.exs` | `alias` / `import` / `use` / `require` (incl. `Foo.{Bar, Baz}` brace groups) -> `defmodule` index + Mix `lib/` path convention (umbrella `apps/*/lib` included); Elixir/OTP stdlib filtered after local-miss |-| **Clojure** | `.clj` `.cljc` `.cljs` | `(:require …)` / `(:use …)` vectors and bare specs (string/comment-safe) -> `(ns …)` index + classpath convention (dashes ↔ underscores); `clojure.*`/`cljs.*` filtered |-| **Haskell** | `.hs` `.lhs` | `import [safe] [qualified] ["pkg"] Foo.Bar` -> `module` declaration index + trailing-PascalCase path inverse (handles any hs-source-dirs without parsing `.cabal`); base-ish namespaces filtered after local-miss |-| **Erlang** | `.erl` `.hrl` | `-include` / `-include_lib` / `-behaviour` + module-qualified calls (`mod:fun(`) -> `-module()` index; qualified calls are strict local-hit-or-drop (never mint externals) |-| **F#** | `.fs` `.fsi` `.fsx` | `open` -> file-level `namespace`/`module` index (unambiguous single-file declarations only), plus the fsproj `<Compile Include>` compile-order dependency spine (a real F# constraint: files may only reference earlier files) |+| **Elixir** | `.ex` `.exs` | `alias` / `import` / `use` / `require` (incl. `Foo.{Bar, Baz}` brace groups) → `defmodule` index + Mix `lib/` path convention (umbrella `apps/*/lib` included); Elixir/OTP stdlib filtered after local-miss |+| **Dart** | `.dart` | `import` / `export` (re-export) / `part` / `part of` URIs; `package:` URIs via every `pubspec.yaml` `name:` (monorepos included); `dart:` SDK URIs filtered; foreign packages → `external:pub:<name>` |+| **Clojure** | `.clj` `.cljc` `.cljs` | `(:require …)` / `(:use …)` vectors and bare specs (string/comment-safe) → `(ns …)` index + classpath convention (dashes ↔ underscores); `clojure.*`/`cljs.*` filtered |+| **Haskell** | `.hs` `.lhs` | `import [safe] [qualified] ["pkg"] Foo.Bar` → `module` declaration index + trailing-PascalCase path inverse (handles any hs-source-dirs without parsing `.cabal`); base-ish namespaces filtered after local-miss |+| **Lean 4** | `.lean` | `import Foo.Bar`, `import all Foo.Bar`, visibility imports (`public import Foo.Bar` / `private import Foo.Bar` / `meta import Foo.Bar`), and `open [scoped] Foo` → path-derived module index (`Mathlib/Data/Nat/Basic.lean` → `Mathlib.Data.Nat.Basic`); core/library roots filtered after local-miss |+| **Erlang** | `.erl` `.hrl` | `-include` / `-include_lib` / `-behaviour` + module-qualified calls (`mod:fun(`) → `-module()` index; qualified calls are strict local-hit-or-drop (never mint externals) |+| **F#** | `.fs` `.fsi` `.fsx` | `open` → file-level `namespace`/`module` index (unambiguous single-file declarations only), plus the fsproj `<Compile Include>` compile-order dependency spine (a real F# constraint: files may only reference earlier files) |### SQL + dbt@@ -231,6 +233,7 @@ routine), `sql_update_delete_without_where`, and `sql_cartesian_join`and never move the defect headline; dbt/Jinja templates and garbledparses emit nothing. See `docs/CODE_HEALTH.md`.+### Structural (git + file tree only)These languages are tracked in git history (blame, hotspot analysis,@@ -279,9 +282,9 @@ Extension/filename -> LanguageTag (via LanguageRegistry)Go: go.mod module path strippingRust: crate::/self::/super::, mod.rs probingC/C++: compile_commands.json include directories+ Lightweight tier (Elixir/Dart/Clojure/Haskell/Lean 4/Erlang/F#):Dart: package:/dart:/relative URIs via the pubspec name map +library-name index (dotted part-of)- Lightweight tier (Elixir/Clojure/Haskell/Erlang/F#/dbt SQL):regex-extracted imports vs a declared-module-name index(dbt: ref()/source() vs a per-project model-name index)Other: stem-map fallback (filename matching)diff --git a/packages/core/src/repowise/core/ingestion/languages/specs/__init__.py b/packages/core/src/repowise/core/ingestion/languages/specs/__init__.pyindex 33a1e83db..6ee8335ff 100644--- a/packages/core/src/repowise/core/ingestion/languages/specs/__init__.py+++ b/packages/core/src/repowise/core/ingestion/languages/specs/__init__.py@@ -32,6 +32,7 @@ from .javascript import SPEC as _JAVASCRIPTfrom .json import SPEC as _JSONfrom .julia import SPEC as _JULIAfrom .kotlin import SPEC as _KOTLIN+from .lean import SPEC as _LEANfrom .luau import SPEC as _LUAUfrom .makefile import SPEC as _MAKEFILEfrom .markdown import SPEC as _MARKDOWN@@ -119,6 +120,7 @@ ALL_SPECS: tuple[LanguageSpec, ...] = (_CLOJURE,_ELM,_HASKELL,+ _LEAN,_OCAML,_FSHARP,_CRYSTAL,diff --git a/packages/core/src/repowise/core/ingestion/languages/specs/lean.py b/packages/core/src/repowise/core/ingestion/languages/specs/lean.pynew file mode 100644index 000000000..3c65e2a49--- /dev/null+++ b/packages/core/src/repowise/core/ingestion/languages/specs/lean.py@@ -0,0 +1,15 @@+"""LanguageSpec for lean (extracted from the registry data table)."""++from ..spec import LanguageSpec++SPEC = LanguageSpec(+ tag="lean",+ display_name="Lean 4",+ entry_point_patterns=("Main.lean",),+ # Lake build manifests / toolchain pin — package plumbing, not domain code.+ manifest_files=("lakefile.lean", "lakefile.toml", "lean-toolchain"),+ extensions=frozenset({".lean"}),+ is_passthrough=True,+ # Lightweight regex resolver: import/open statements → module-name index.+ import_support="partial",+)diff --git a/packages/core/src/repowise/core/ingestion/lightweight_imports/__init__.py b/packages/core/src/repowise/core/ingestion/lightweight_imports/__init__.pyindex 77d5f2e3c..0c619daa3 100644--- a/packages/core/src/repowise/core/ingestion/lightweight_imports/__init__.py+++ b/packages/core/src/repowise/core/ingestion/lightweight_imports/__init__.py@@ -20,8 +20,10 @@ from .elixir import extract_elixir_importsfrom .erlang import extract_erlang_importsfrom .fsharp import extract_fsharp_importsfrom .haskell import extract_haskell_imports+from .lean import extract_lean_importsfrom .sql import extract_dbt_imports+ExtractorFn = Callable[[str], list[Import]]_EXTRACTORS: dict[str, ExtractorFn] = {@@ -29,6 +31,7 @@ _EXTRACTORS: dict[str, ExtractorFn] = {"dart": extract_dart_imports,"clojure": extract_clojure_imports,"haskell": extract_haskell_imports,+ "lean": extract_lean_imports,"erlang": extract_erlang_imports,"fsharp": extract_fsharp_imports,# dbt {{ ref() }} / {{ source() }}, the only import system .sql filesdiff --git a/packages/core/src/repowise/core/ingestion/lightweight_imports/lean.py b/packages/core/src/repowise/core/ingestion/lightweight_imports/lean.pynew file mode 100644index 000000000..bc714c9f1--- /dev/null+++ b/packages/core/src/repowise/core/ingestion/lightweight_imports/lean.py@@ -0,0 +1,98 @@+"""Regex import extraction for Lean 4.++Captured forms:++ import Foo.Bar+ import all Foo.Bar+ public import Foo.Bar+ private import Foo.Bar+ meta import Foo.Bar+ open Foo.Bar+ open scoped Foo+ open Foo.Bar Baz.Qux++For ``open`` statements, only the first dotted namespace is captured. The file+graph only needs a module target, matching the minimal one-edge-per-statement+shape used by the Haskell extractor.+"""++from __future__ import annotations++import re++from ..models import Import++# A dotted module path. Each component starts with a Unicode letter or+# underscore ("[^\W\d]" is a word char that is not a digit) and continues with+# word characters or a prime ("'"), matching Lean/Mathlib identifiers that use+# Unicode. Components are joined by dots, and a trailing dot is never captured+# because each dot must be followed by another component.+_MODULE = r"[^\W\d][\w']*(?:\.[^\W\d][\w']*)*"++_IMPORT_RE = re.compile(+ r"^(?:(?:public|private|meta)[ \t]+)?import[ \t]+(?:all[ \t]+)?"+ r"(" + _MODULE + r")"+ r"|^open[ \t]+(?:scoped[ \t]+)?(" + _MODULE + r")",+ re.M,+)+++def _strip_comments(text: str) -> str:+ """Blank out Lean comments so they can't produce false import edges.++ Removes ``--`` line comments and nested ``/- ... -/`` block comments,+ replacing their characters with spaces (newlines kept). Preserving the+ line and column layout means the ``^``-anchored import regex still sees+ real statements at their original positions.+ """+ out: list[str] = []+ i = 0+ n = len(text)+ depth = 0 # nesting depth of /- -/ block comments+ while i < n:+ pair = text[i : i + 2]+ if depth > 0:+ if pair == "/-":+ depth += 1+ out.append(" ")+ i += 2+ elif pair == "-/":+ depth -= 1+ out.append(" ")+ i += 2+ else:+ out.append("\n" if text[i] == "\n" else " ")+ i += 1+ elif pair == "/-":+ depth += 1+ out.append(" ")+ i += 2+ elif pair == "--":+ while i < n and text[i] != "\n":+ out.append(" ")+ i += 1+ else:+ out.append(text[i])+ i += 1+ return "".join(out)+++def extract_lean_imports(text: str) -> list[Import]:+ text = _strip_comments(text)+ imports: list[Import] = []+ seen: set[str] = set()+ for match in _IMPORT_RE.finditer(text):+ module = match.group(1) or match.group(2)+ if module in seen:+ continue+ seen.add(module)+ imports.append(+ Import(+ raw_statement=match.group(0).strip(),+ module_path=module,+ imported_names=[],+ is_relative=False,+ resolved_file=None,+ )+ )+ return importsdiff --git a/packages/core/src/repowise/core/ingestion/models.py b/packages/core/src/repowise/core/ingestion/models.pyindex 72493c372..3a6b6aa94 100644--- a/packages/core/src/repowise/core/ingestion/models.py+++ b/packages/core/src/repowise/core/ingestion/models.py@@ -43,6 +43,7 @@ LanguageTag = Literal["erlang","clojure","haskell",+ "lean","ocaml","fsharp","crystal",diff --git a/packages/core/src/repowise/core/ingestion/resolvers/__init__.py b/packages/core/src/repowise/core/ingestion/resolvers/__init__.pyindex 07ad52ebf..6c47deaa5 100644--- a/packages/core/src/repowise/core/ingestion/resolvers/__init__.py+++ b/packages/core/src/repowise/core/ingestion/resolvers/__init__.py@@ -17,6 +17,7 @@ from .go import resolve_go_importfrom .haskell import resolve_haskell_importfrom .java import resolve_java_importfrom .kotlin import resolve_kotlin_import+from .lean import resolve_lean_importfrom .luau import resolve_luau_importfrom .php import resolve_php_importfrom .python import resolve_python_import@@ -50,6 +51,7 @@ _RESOLVERS: dict[str, ResolverFn] = {"dart": resolve_dart_import,"clojure": resolve_clojure_import,"haskell": resolve_haskell_import,+ "lean": resolve_lean_import,"erlang": resolve_erlang_import,"fsharp": resolve_fsharp_import,# dbt ref()/source(), gated on dbt_project.yml via the model index.diff --git a/packages/core/src/repowise/core/ingestion/resolvers/lean.py b/packages/core/src/repowise/core/ingestion/resolvers/lean.pynew file mode 100644index 000000000..19b6cccb2--- /dev/null+++ b/packages/core/src/repowise/core/ingestion/resolvers/lean.py@@ -0,0 +1,55 @@+"""Lean 4 import resolution (lightweight regex tier).++Lean modules are path-derived rather than declared with a ``module`` head:+``Mathlib/Data/Nat/Basic.lean`` defines ``Mathlib.Data.Nat.Basic``. The index+therefore uses only the path-convention inverse for ``.lean`` files.++Core and common-library roots (``Init``/``Std``/``Mathlib``/...) are dropped+after a local miss. This matches the Haskell resolver's tradeoff: local files+shadow well-known roots, while unresolved standard-library-like imports do not+mint noisy external nodes.+"""++from __future__ import annotations++import re+from typing import TYPE_CHECKING++from .module_name_index import get_or_build_module_index, lookup_module++if TYPE_CHECKING:+ from .context import ResolverContext++# Lean modules are path-derived, so this never-match sentinel disables declaration hits.+_MODULE_DECL_RE = re.compile(r"(?!x)x")++_STDLIB_PREFIXES = frozenset({"Init", "Std", "Lean", "Mathlib", "Batteries", "Aesop", "Qq"})+++def _path_to_module(path: str) -> str | None:+ stem = path.rsplit(".", 1)[0]+ segments = [s for s in stem.split("/") if s]+ if not segments:+ return None+ return ".".join(segments)+++def _get_index(ctx: ResolverContext) -> dict[str, list[str]]:+ return get_or_build_module_index(+ ctx,+ cache_attr="_lean_module_index",+ extensions=(".lean",),+ declaration_re=_MODULE_DECL_RE,+ path_to_module=_path_to_module,+ )+++def resolve_lean_import(module_path: str, importer_path: str, ctx: ResolverContext) -> str | None:+ hit = lookup_module(_get_index(ctx), module_path)+ if hit and hit != importer_path:+ return hit+ if hit == importer_path:+ return None+ if module_path.split(".", 1)[0] in _STDLIB_PREFIXES:+ return None+ return f"external:{module_path}"diff --git a/tests/fixtures/lang_samples/lean/BigOperators.lean b/tests/fixtures/lang_samples/lean/BigOperators.leannew file mode 100644index 000000000..3aa153bd9--- /dev/null+++ b/tests/fixtures/lang_samples/lean/BigOperators.lean@@ -0,0 +1,5 @@+namespace BigOperators++def marker : Nat := 0++end BigOperatorsdiff --git a/tests/fixtures/lang_samples/lean/Main.lean b/tests/fixtures/lang_samples/lean/Main.leannew file mode 100644index 000000000..708259597--- /dev/null+++ b/tests/fixtures/lang_samples/lean/Main.lean@@ -0,0 +1,5 @@+public import Public.Visible+import Plain.Local+open scoped BigOperators++def main : Nat := Plain.Local.answerdiff --git a/tests/fixtures/lang_samples/lean/Plain/Local.lean b/tests/fixtures/lang_samples/lean/Plain/Local.leannew file mode 100644index 000000000..b393c5156--- /dev/null+++ b/tests/fixtures/lang_samples/lean/Plain/Local.lean@@ -0,0 +1,5 @@+namespace Plain.Local++def answer : Nat := 42++end Plain.Localdiff --git a/tests/fixtures/lang_samples/lean/Public/Visible.lean b/tests/fixtures/lang_samples/lean/Public/Visible.leannew file mode 100644index 000000000..a547c4b50--- /dev/null+++ b/tests/fixtures/lang_samples/lean/Public/Visible.lean@@ -0,0 +1,5 @@+namespace Public.Visible++def visible : Nat := 1++end Public.Visiblediff --git a/tests/unit/ingestion/test_language_capabilities.py b/tests/unit/ingestion/test_language_capabilities.pyindex 22f18ba64..4441c257e 100644--- a/tests/unit/ingestion/test_language_capabilities.py+++ b/tests/unit/ingestion/test_language_capabilities.py@@ -154,6 +154,7 @@ _PARTIAL = {"elixir","clojure","haskell",+ "lean","erlang","fsharp",# dbt ref()/source() lineage (model-name index gated on dbt_project.yml).@@ -203,6 +204,7 @@ _ENTRY_PATTERNS_BY_LANGUAGE = {"clojure": ("core.clj", "main.clj"),"dart": ("main.dart",),"haskell": ("Main.hs",),+ "lean": ("Main.lean",),"ocaml": ("main.ml",),"erlang": ("*_app.erl",),"fsharp": ("Program.fs",),diff --git a/tests/unit/ingestion/test_lightweight_resolvers.py b/tests/unit/ingestion/test_lightweight_resolvers.pyindex 93e2840c7..a8bdee97b 100644--- a/tests/unit/ingestion/test_lightweight_resolvers.py+++ b/tests/unit/ingestion/test_lightweight_resolvers.py@@ -12,6 +12,7 @@ from pathlib import Pathimport networkx as nx+from repowise.core.ingestion.graph import GraphBuilderfrom repowise.core.ingestion.lightweight_imports import (LIGHTWEIGHT_IMPORT_LANGUAGES,extract_lightweight_imports,@@ -21,7 +22,9 @@ from repowise.core.ingestion.lightweight_imports.dart import extract_dart_importfrom repowise.core.ingestion.lightweight_imports.elixir import extract_elixir_importsfrom repowise.core.ingestion.lightweight_imports.erlang import extract_erlang_importsfrom repowise.core.ingestion.lightweight_imports.haskell import extract_haskell_imports+from repowise.core.ingestion.lightweight_imports.lean import extract_lean_importsfrom repowise.core.ingestion.models import FileInfo+from repowise.core.ingestion.parser import ASTParserfrom repowise.core.ingestion.resolvers import resolve_importfrom repowise.core.ingestion.resolvers.clojure import resolve_clojure_importfrom repowise.core.ingestion.resolvers.context import ResolverContext@@ -29,6 +32,9 @@ from repowise.core.ingestion.resolvers.dart import resolve_dart_importfrom repowise.core.ingestion.resolvers.elixir import resolve_elixir_importfrom repowise.core.ingestion.resolvers.erlang import resolve_erlang_importfrom repowise.core.ingestion.resolvers.haskell import resolve_haskell_import+from repowise.core.ingestion.resolvers.lean import resolve_lean_import++LANG_SAMPLE_FIXTURES = Path(__file__).resolve().parents[2] / "fixtures" / "lang_samples"def _ctx(repo: Path | None, files: dict[str, str]) -> ResolverContext:@@ -81,6 +87,7 @@ class TestDispatch:"dart","clojure","haskell",+ "lean","erlang","fsharp","sql",@@ -449,6 +456,116 @@ class TestHaskellResolution:)+# ---------------------------------------------------------------------------+# Lean+# ---------------------------------------------------------------------------+++class TestLeanExtraction:+ def test_import_and_open_forms(self) -> None:+ src = (+ "import Init\n"+ "import all All.Theorems\n"+ "public import Public.Visible\n"+ "private import Private.Hidden\n"+ "meta import Meta.Tools\n"+ "public import all Public.All\n"+ "import Mathlib.Data.Nat.Basic\n"+ "open Foo.Bar\n"+ "open scoped Nat\n"+ )+ assert _modules(extract_lean_imports(src)) == [+ "Init",+ "All.Theorems",+ "Public.Visible",+ "Private.Hidden",+ "Meta.Tools",+ "Public.All",+ "Mathlib.Data.Nat.Basic",+ "Foo.Bar",+ "Nat",+ ]++ def test_comment_and_indented_not_captured(self) -> None:+ src = "-- import Fake.Mod\nlet x := 1\n import Nested.NotReal\n"+ assert extract_lean_imports(src) == []++ def test_block_comment_not_captured(self) -> None:+ src = "/- import Foo.Bar -/\nimport Real.Mod\n"+ assert _modules(extract_lean_imports(src)) == ["Real.Mod"]++ def test_nested_block_comment_not_captured(self) -> None:+ src = "/- outer /- import Nested.Fake -/ import Still.Fake -/\nimport Real.Mod\n"+ assert _modules(extract_lean_imports(src)) == ["Real.Mod"]++ def test_trailing_line_comment_still_captures(self) -> None:+ src = "import Foo.Bar -- import Fake.Mod\n"+ assert _modules(extract_lean_imports(src)) == ["Foo.Bar"]++ def test_unicode_identifier_round_trips(self) -> None:+ src = "import Mathlib.Algebra.Ω.Café'\n"+ assert _modules(extract_lean_imports(src)) == ["Mathlib.Algebra.Ω.Café'"]++ def test_trailing_dot_not_captured(self) -> None:+ src = "import Foo.\n"+ assert _modules(extract_lean_imports(src)) == ["Foo"]++ def test_dedup(self) -> None:+ src = "import Foo.Bar\nimport Foo.Bar\n"+ assert _modules(extract_lean_imports(src)) == ["Foo.Bar"]+++class TestLeanResolution:+ def test_resolves_to_file(self, tmp_path: Path) -> None:+ ctx = _ctx(tmp_path, {"Mathlib/Data/Nat/Basic.lean": "", "Main.lean": ""})+ assert resolve_lean_import("Mathlib.Data.Nat.Basic", "Main.lean", ctx) == (+ "Mathlib/Data/Nat/Basic.lean"+ )++ def test_local_shadows_stdlib(self, tmp_path: Path) -> None:+ ctx = _ctx(tmp_path, {"Init/Data.lean": "", "Main.lean": ""})+ assert resolve_lean_import("Init.Data", "Main.lean", ctx) == "Init/Data.lean"++ def test_stdlib_dropped_after_local_miss(self, tmp_path: Path) -> None:+ ctx = _ctx(tmp_path, {"Main.lean": ""})+ assert resolve_lean_import("Init", "Main.lean", ctx) is None+ assert resolve_lean_import("Std.Data.HashMap", "Main.lean", ctx) is None++ def test_unknown_module_external(self, tmp_path: Path) -> None:+ ctx = _ctx(tmp_path, {"Main.lean": ""})+ assert resolve_lean_import("ThirdParty.Lib", "Main.lean", ctx) == (+ "external:ThirdParty.Lib"+ )++ def test_self_reference_returns_none(self, tmp_path: Path) -> None:+ ctx = _ctx(tmp_path, {"Foo/Bar.lean": ""})+ assert resolve_lean_import("Foo.Bar", "Foo/Bar.lean", ctx) is None++ def test_fixture_parses_and_resolves_graph_edges(self) -> None:+ root = LANG_SAMPLE_FIXTURES / "lean"+ parser = ASTParser()+ builder = GraphBuilder(repo_path=root)+ main_modules: list[str] = []++ for path in sorted(root.rglob("*.lean")):+ rel = path.relative_to(root).as_posix()+ source = path.read_bytes()+ info = _file_info(rel, "lean")+ info.abs_path = str(path)+ info.size_bytes = len(source)+ parsed = parser.parse_file(info, source)+ if rel == "Main.lean":+ main_modules = _modules(parsed.imports)+ builder.add_file(parsed)++ graph = builder.build()+ assert main_modules == ["Public.Visible", "Plain.Local", "BigOperators"]+ assert graph.has_edge("Main.lean", "Public/Visible.lean")+ assert graph.has_edge("Main.lean", "Plain/Local.lean")+ assert graph.has_edge("Main.lean", "BigOperators.lean")+ assert not graph.has_node("external:scoped")++# ---------------------------------------------------------------------------# Erlang# ---------------------------------------------------------------------------