intentumdiff-python 0.0.1__py3-none-manylinux_2_39_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. intentumdiff/__init__.py +134 -0
  2. intentumdiff/_differ_gate.py +210 -0
  3. intentumdiff/_differ_presentation.py +786 -0
  4. intentumdiff/_differ_runtime.py +320 -0
  5. intentumdiff/analysis/__init__.py +1 -0
  6. intentumdiff/analysis/compile_commands.py +151 -0
  7. intentumdiff/analysis/cross_file.py +71 -0
  8. intentumdiff/analysis/diagnostics.py +309 -0
  9. intentumdiff/analysis/guardrail_reports.py +254 -0
  10. intentumdiff/analysis/guardrails.py +318 -0
  11. intentumdiff/analysis/invariances.py +1207 -0
  12. intentumdiff/analysis/keyed_profiles.py +25 -0
  13. intentumdiff/analysis/resource_profiles.py +14 -0
  14. intentumdiff/analysis/schema_resolver.py +816 -0
  15. intentumdiff/analysis/text_review.py +46 -0
  16. intentumdiff/analysis/user_schemas.py +430 -0
  17. intentumdiff/assets.py +45 -0
  18. intentumdiff/cache/__init__.py +37 -0
  19. intentumdiff/cache/duckdb_store.py +98 -0
  20. intentumdiff/cache/sqlite_store.py +149 -0
  21. intentumdiff/cache/store.py +190 -0
  22. intentumdiff/cli/__init__.py +199 -0
  23. intentumdiff/cli/_commands.py +2141 -0
  24. intentumdiff/cli/_parser.py +1061 -0
  25. intentumdiff/cli/_shared.py +644 -0
  26. intentumdiff/content_type.py +59 -0
  27. intentumdiff/core/__init__.py +1 -0
  28. intentumdiff/core/commit_differ.py +599 -0
  29. intentumdiff/core/config.py +51 -0
  30. intentumdiff/core/cst_serializer.py +101 -0
  31. intentumdiff/core/diffignore.py +87 -0
  32. intentumdiff/core/index.py +189 -0
  33. intentumdiff/core/indexer.py +792 -0
  34. intentumdiff/core/models.py +837 -0
  35. intentumdiff/differ.py +2195 -0
  36. intentumdiff/github_app.py +287 -0
  37. intentumdiff/intentumdiff_rust_core/__init__.py +7 -0
  38. intentumdiff/intentumdiff_rust_core/ffi.py +8 -0
  39. intentumdiff/intentumdiff_rust_core/libintentumdiff_rust_core.so +0 -0
  40. intentumdiff/invariances/__init__.py +2 -0
  41. intentumdiff/invariances/rules.schema.json +283 -0
  42. intentumdiff/invariances/rules.yaml +349 -0
  43. intentumdiff/live_server.py +1314 -0
  44. intentumdiff/lsp/__init__.py +67 -0
  45. intentumdiff/lsp/client.py +544 -0
  46. intentumdiff/lsp/config.py +96 -0
  47. intentumdiff/lsp/enricher.py +227 -0
  48. intentumdiff/lsp/exceptions.py +27 -0
  49. intentumdiff/lsp/launcher.py +273 -0
  50. intentumdiff/lsp/servers.py +356 -0
  51. intentumdiff/lsp_server/__init__.py +14 -0
  52. intentumdiff/lsp_server/_codelens.py +67 -0
  53. intentumdiff/lsp_server/_diagnostics.py +62 -0
  54. intentumdiff/lsp_server/_handlers.py +175 -0
  55. intentumdiff/lsp_server/server.py +205 -0
  56. intentumdiff/plugins/__init__.py +1 -0
  57. intentumdiff/plugins/adapter.py +397 -0
  58. intentumdiff/plugins/builtins.py +323 -0
  59. intentumdiff/plugins/exceptions.py +93 -0
  60. intentumdiff/plugins/hub.py +1155 -0
  61. intentumdiff/plugins/language_metadata.py +250 -0
  62. intentumdiff/plugins/loader.py +1252 -0
  63. intentumdiff/plugins/registry.py +1079 -0
  64. intentumdiff/plugins/registry.schema.json +99 -0
  65. intentumdiff/plugins/registry_schema.py +280 -0
  66. intentumdiff/plugins/wit/plugin.wit +351 -0
  67. intentumdiff/rust_core.py +2370 -0
  68. intentumdiff/serve/__init__.py +19 -0
  69. intentumdiff/serve/_app.py +485 -0
  70. intentumdiff/serve/_static/index.html +326 -0
  71. intentumdiff/serve/_static/playground.js +567 -0
  72. intentumdiff/sources/__init__.py +1 -0
  73. intentumdiff/sources/base.py +28 -0
  74. intentumdiff/sources/file_source.py +48 -0
  75. intentumdiff/sources/git_source.py +375 -0
  76. intentumdiff/sources/live_buffer_source.py +113 -0
  77. intentumdiff/sources/patch_source.py +126 -0
  78. intentumdiff/sources/string_source.py +44 -0
  79. intentumdiff/testing.py +165 -0
  80. intentumdiff/vcs/__init__.py +37 -0
  81. intentumdiff/vcs/_safe_path.py +50 -0
  82. intentumdiff/vcs/base.py +136 -0
  83. intentumdiff/vcs/git_backend.py +97 -0
  84. intentumdiff/vcs/git_cli.py +100 -0
  85. intentumdiff/vcs/hg_backend.py +94 -0
  86. intentumdiff/vcs/perforce_backend.py +109 -0
  87. intentumdiff/vcs/svn_backend.py +105 -0
  88. intentumdiff/wasm/.gitkeep +2 -0
  89. intentumdiff/wasm/abap_parser.wasm +0 -0
  90. intentumdiff/wasm/adf_parser.wasm +0 -0
  91. intentumdiff/wasm/asciidoc_parser.wasm +0 -0
  92. intentumdiff/wasm/asm_parser.wasm +0 -0
  93. intentumdiff/wasm/assemblyscript_parser.wasm +0 -0
  94. intentumdiff/wasm/astro_parser.wasm +0 -0
  95. intentumdiff/wasm/bash_parser.wasm +0 -0
  96. intentumdiff/wasm/clojure_parser.wasm +0 -0
  97. intentumdiff/wasm/cmake_parser.wasm +0 -0
  98. intentumdiff/wasm/cpp_parser.wasm +0 -0
  99. intentumdiff/wasm/csharp_parser.wasm +0 -0
  100. intentumdiff/wasm/css_parser.wasm +0 -0
  101. intentumdiff/wasm/dart_parser.wasm +0 -0
  102. intentumdiff/wasm/databricks_parser.wasm +0 -0
  103. intentumdiff/wasm/dax_parser.wasm +0 -0
  104. intentumdiff/wasm/delphi_parser.wasm +0 -0
  105. intentumdiff/wasm/dockerfile_parser.wasm +0 -0
  106. intentumdiff/wasm/elixir_parser.wasm +0 -0
  107. intentumdiff/wasm/freebasic_parser.wasm +0 -0
  108. intentumdiff/wasm/generic_parser.wasm +0 -0
  109. intentumdiff/wasm/gitignore_parser.wasm +0 -0
  110. intentumdiff/wasm/go_parser.wasm +0 -0
  111. intentumdiff/wasm/gomod_parser.wasm +0 -0
  112. intentumdiff/wasm/graphql_parser.wasm +0 -0
  113. intentumdiff/wasm/groovy_parser.wasm +0 -0
  114. intentumdiff/wasm/haskell_parser.wasm +0 -0
  115. intentumdiff/wasm/html_parser.wasm +0 -0
  116. intentumdiff/wasm/ini_parser.wasm +0 -0
  117. intentumdiff/wasm/java_parser.wasm +0 -0
  118. intentumdiff/wasm/js_ts_parser.wasm +0 -0
  119. intentumdiff/wasm/json_parser.wasm +0 -0
  120. intentumdiff/wasm/kotlin_parser.wasm +0 -0
  121. intentumdiff/wasm/latex_parser.wasm +0 -0
  122. intentumdiff/wasm/lua_parser.wasm +0 -0
  123. intentumdiff/wasm/make_parser.wasm +0 -0
  124. intentumdiff/wasm/markdown_parser.wasm +0 -0
  125. intentumdiff/wasm/mdx_parser.wasm +0 -0
  126. intentumdiff/wasm/ocaml_parser.wasm +0 -0
  127. intentumdiff/wasm/odin_parser.wasm +0 -0
  128. intentumdiff/wasm/parser_manifest.json +590 -0
  129. intentumdiff/wasm/perl_parser.wasm +0 -0
  130. intentumdiff/wasm/php_parser.wasm +0 -0
  131. intentumdiff/wasm/plsql_parser.wasm +0 -0
  132. intentumdiff/wasm/po_parser.wasm +0 -0
  133. intentumdiff/wasm/postscript_parser.wasm +0 -0
  134. intentumdiff/wasm/powershell_parser.wasm +0 -0
  135. intentumdiff/wasm/proto_parser.wasm +0 -0
  136. intentumdiff/wasm/puppet_parser.wasm +0 -0
  137. intentumdiff/wasm/python_parser.wasm +0 -0
  138. intentumdiff/wasm/qsharp_parser.wasm +0 -0
  139. intentumdiff/wasm/r_parser.wasm +0 -0
  140. intentumdiff/wasm/reasonml_parser.wasm +0 -0
  141. intentumdiff/wasm/ruby_parser.wasm +0 -0
  142. intentumdiff/wasm/rust_parser.wasm +0 -0
  143. intentumdiff/wasm/sas_parser.wasm +0 -0
  144. intentumdiff/wasm/scala_parser.wasm +0 -0
  145. intentumdiff/wasm/scss_parser.wasm +0 -0
  146. intentumdiff/wasm/sql_parser.wasm +0 -0
  147. intentumdiff/wasm/squirrel_parser.wasm +0 -0
  148. intentumdiff/wasm/svelte_parser.wasm +0 -0
  149. intentumdiff/wasm/swift_parser.wasm +0 -0
  150. intentumdiff/wasm/terraform_parser.wasm +0 -0
  151. intentumdiff/wasm/toml_parser.wasm +0 -0
  152. intentumdiff/wasm/tsql_parser.wasm +0 -0
  153. intentumdiff/wasm/vbnet_parser.wasm +0 -0
  154. intentumdiff/wasm/vue_parser.wasm +0 -0
  155. intentumdiff/wasm/wat_parser.wasm +0 -0
  156. intentumdiff/wasm/xml_parser.wasm +0 -0
  157. intentumdiff/wasm/yaml_parser.wasm +0 -0
  158. intentumdiff/wasm/zig_parser.wasm +0 -0
  159. intentumdiff/watcher.py +259 -0
  160. intentumdiff_python-0.0.1.dist-info/METADATA +112 -0
  161. intentumdiff_python-0.0.1.dist-info/RECORD +165 -0
  162. intentumdiff_python-0.0.1.dist-info/WHEEL +4 -0
  163. intentumdiff_python-0.0.1.dist-info/entry_points.txt +80 -0
  164. intentumdiff_python-0.0.1.dist-info/licenses/LICENSE +21 -0
  165. intentumdiff_python-0.0.1.dist-info/sboms/intentumdiff-rust-core.cyclonedx.json +10968 -0
@@ -0,0 +1,205 @@
1
+ """
2
+ intentumdiff.lsp_server.server
3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
+
5
+ Factory function that creates and wires a pygls ``LanguageServer`` for
6
+ ``intentumdiff lsp-server``.
7
+
8
+ Usage::
9
+
10
+ from intentumdiff.lsp_server import create_server
11
+
12
+ server = create_server()
13
+ server.start_io() # stdio transport (--stdio)
14
+ # or
15
+ server.start_tcp("127.0.0.1", 2087) # TCP transport (--tcp PORT)
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import json
21
+ import logging
22
+ from pathlib import Path
23
+
24
+ from lsprotocol import types as lsp_types
25
+ try:
26
+ from pygls.lsp.server import LanguageServer # pygls >= 2.x
27
+ except ImportError:
28
+ from pygls.server import LanguageServer # pygls 1.x
29
+
30
+ from intentumdiff.core.models import DiffConfig
31
+ from intentumdiff.differ import SemanticDiffer
32
+ from intentumdiff.lsp_server._codelens import semantic_diff_to_codelens
33
+ from intentumdiff.lsp_server._diagnostics import semantic_diff_to_diagnostics
34
+ from intentumdiff.lsp_server._handlers import (
35
+ _ServerState,
36
+ _do_diff,
37
+ schedule_diff,
38
+ uri_to_path,
39
+ )
40
+
41
+ log = logging.getLogger(__name__)
42
+
43
+
44
+ def create_server(
45
+ config: DiffConfig | None = None,
46
+ ref: str = "HEAD",
47
+ debounce: float = 0.15,
48
+ ) -> LanguageServer:
49
+ """Create a fully-wired ``LanguageServer`` instance.
50
+
51
+ Parameters
52
+ ----------
53
+ config:
54
+ ``DiffConfig`` to pass to ``SemanticDiffer``. Defaults to
55
+ ``DiffConfig()`` (all defaults).
56
+ ref:
57
+ Git ref to compare the live buffer against (default: ``"HEAD"``).
58
+ debounce:
59
+ Keystroke debounce delay in seconds (default: ``0.15``).
60
+
61
+ Returns
62
+ -------
63
+ LanguageServer
64
+ Ready to start via ``.start_io()`` or ``.start_tcp(host, port)``.
65
+ """
66
+ differ = SemanticDiffer(config or DiffConfig())
67
+ state = _ServerState(differ=differ, ref=ref, debounce=debounce)
68
+
69
+ server = LanguageServer(
70
+ name="intentumdiff-lsp",
71
+ version="1.0.0",
72
+ text_document_sync_kind=lsp_types.TextDocumentSyncKind.Full,
73
+ )
74
+
75
+ # ------------------------------------------------------------------
76
+ # Capability check after initialization handshake
77
+ # ------------------------------------------------------------------
78
+
79
+ @server.feature(lsp_types.INITIALIZED)
80
+ def _on_initialized(params: lsp_types.InitializedParams) -> None:
81
+ """Read client capabilities to decide whether to send refresh requests."""
82
+ try:
83
+ # pygls stores client capabilities on server.workspace after init.
84
+ client_caps = getattr(server.workspace, "client_capabilities", None)
85
+ if client_caps is None:
86
+ return
87
+ ws_caps = getattr(client_caps, "workspace", None)
88
+ if ws_caps is None:
89
+ return
90
+ cl_caps = getattr(ws_caps, "code_lens", None)
91
+ state.supports_codelens_refresh = bool(
92
+ getattr(cl_caps, "refresh_support", False)
93
+ )
94
+ except Exception: # noqa: BLE001
95
+ log.debug("Could not read LSP client capabilities", exc_info=True)
96
+
97
+ # ------------------------------------------------------------------
98
+ # Document lifecycle handlers (push model)
99
+ # ------------------------------------------------------------------
100
+
101
+ @server.feature(lsp_types.TEXT_DOCUMENT_DID_OPEN)
102
+ async def _did_open(params: lsp_types.DidOpenTextDocumentParams) -> None:
103
+ uri = params.text_document.uri
104
+ content = params.text_document.text
105
+ await schedule_diff(server, state, uri, content)
106
+
107
+ @server.feature(lsp_types.TEXT_DOCUMENT_DID_CHANGE)
108
+ async def _did_change(params: lsp_types.DidChangeTextDocumentParams) -> None:
109
+ uri = params.text_document.uri
110
+ if not params.content_changes:
111
+ return
112
+ # Full sync — the last (and only) item is always the complete document.
113
+ content = params.content_changes[-1].text
114
+ await schedule_diff(server, state, uri, content)
115
+
116
+ @server.feature(lsp_types.TEXT_DOCUMENT_DID_CLOSE)
117
+ def _did_close(params: lsp_types.DidCloseTextDocumentParams) -> None:
118
+ uri = params.text_document.uri
119
+ state.diff_cache.pop(uri, None)
120
+ task = state.pending_tasks.pop(uri, None)
121
+ if task is not None and not task.done():
122
+ task.cancel()
123
+
124
+ # ------------------------------------------------------------------
125
+ # Code-lens handler (pull model — returns from cache synchronously)
126
+ # ------------------------------------------------------------------
127
+
128
+ @server.feature(
129
+ lsp_types.TEXT_DOCUMENT_CODE_LENS,
130
+ lsp_types.CodeLensOptions(resolve_provider=False),
131
+ )
132
+ def _code_lens(params: lsp_types.CodeLensParams) -> list[lsp_types.CodeLens]:
133
+ uri = params.text_document.uri
134
+ diff = state.diff_cache.get(uri)
135
+ if diff is None:
136
+ return []
137
+ return semantic_diff_to_codelens(diff)
138
+
139
+ # ------------------------------------------------------------------
140
+ # Custom request: intentumdiff/semanticDiff
141
+ # ------------------------------------------------------------------
142
+
143
+ @server.feature("intentumdiff/semanticDiff")
144
+ def _semantic_diff(params: dict) -> dict:
145
+ """Return a JSON-serialisable ``SemanticDiff`` for the requested URIs.
146
+
147
+ Parameters (request params dict)
148
+ ---------------------------------
149
+ oldUri : str
150
+ ``file://`` URI of the *old* version.
151
+ newUri : str
152
+ ``file://`` URI of the *new* version.
153
+
154
+ If ``oldUri == newUri`` the workspace-tracked buffer content is diffed
155
+ against the committed ``ref`` version. Otherwise both files are read
156
+ from disk and compared directly.
157
+ """
158
+ old_uri: str = params.get("oldUri", "")
159
+ new_uri: str = params.get("newUri", "")
160
+ if not old_uri or not new_uri:
161
+ return {"error": "oldUri and newUri are required"}
162
+
163
+ try:
164
+ if old_uri == new_uri:
165
+ # Same URI — use live workspace content vs. HEAD.
166
+ doc = server.workspace.get_text_document(new_uri)
167
+ content = doc.source
168
+ diff = _do_diff(state, server, new_uri, content)
169
+ else:
170
+ # Different URIs — read both from disk and compare.
171
+ from intentumdiff.sources.string_source import StringSource
172
+
173
+ # Derive workspace root for containment check.
174
+ workspace_root: Path | None = None
175
+ root_uri_str = getattr(getattr(server, "workspace", None), "root_uri", None)
176
+ if root_uri_str:
177
+ try:
178
+ workspace_root = uri_to_path(root_uri_str)
179
+ except Exception:
180
+ workspace_root = None
181
+
182
+ if workspace_root is None:
183
+ return {"error": "intentumdiff/semanticDiff requires a valid workspace root"}
184
+
185
+ old_path = uri_to_path(old_uri, workspace_root=workspace_root)
186
+ new_path = uri_to_path(new_uri, workspace_root=workspace_root)
187
+ old_content = old_path.read_text(encoding="utf-8", errors="replace")
188
+ new_content = new_path.read_text(encoding="utf-8", errors="replace")
189
+ source = StringSource(
190
+ old_content=old_content,
191
+ new_content=new_content,
192
+ filename=new_path.name,
193
+ )
194
+ diff = state.differ.diff(source)
195
+
196
+ if diff is None:
197
+ return {"error": "Diff computation failed"}
198
+
199
+ return json.loads(diff.model_dump_json())
200
+
201
+ except Exception as exc: # noqa: BLE001
202
+ log.warning("intentumdiff/semanticDiff error: %s", exc)
203
+ return {"error": str(exc)}
204
+
205
+ return server
@@ -0,0 +1 @@
1
+ # intentumdiff/plugins
@@ -0,0 +1,397 @@
1
+ """
2
+ intentumdiff.plugins.adapter
3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
+
5
+ Adapts a ``LoadedPlugin`` to a typed Python interface, handling:
6
+
7
+ - JSON (de)serialisation of inputs and outputs
8
+ - Pydantic validation of plugin output (``SemanticNode`` trees)
9
+ - Fuel-exhaustion and sandbox-violation error mapping
10
+
11
+ Two concrete adapters are provided:
12
+ ``ParserAdapter`` — wraps a parser plugin
13
+ ``RendererAdapter`` — wraps a renderer plugin
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ import json
19
+ import logging
20
+ import re
21
+
22
+ from pydantic import ValidationError
23
+
24
+ from intentumdiff.core.models import LanguagePluginInfo, SemanticNode
25
+ from intentumdiff.plugins.exceptions import PluginOutputError
26
+ from intentumdiff.plugins.language_metadata import fallback_language_info
27
+ from intentumdiff.plugins.loader import LoadedPlugin
28
+
29
+ logger = logging.getLogger(__name__)
30
+
31
+ _MAX_DISPLAY_CHARS = 128
32
+ _MAX_SHORT_NAME_CHARS = 64
33
+ _MAX_MONACO_CHARS = 64
34
+ _MAX_FILENAME_CHARS = 128
35
+ _MAX_EXTENSION_CHARS = 32
36
+ _MAX_EXTENSIONS = 32
37
+ _MAX_AUTHOR_CHARS = 128
38
+ _MAX_VERSION_CHARS = 64
39
+ _MAX_LAST_UPDATED_CHARS = 32
40
+
41
+ _MONACO_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9_+.-]{0,63}$")
42
+ _EXTENSION_RE = re.compile(
43
+ r"^(?:\.[A-Za-z0-9][A-Za-z0-9_+.-]{0,31}|[A-Za-z0-9][A-Za-z0-9_+.-]{0,31})$"
44
+ )
45
+ _LAST_UPDATED_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$")
46
+
47
+
48
+ def _as_clean_text(value: object, fallback: str, *, max_chars: int) -> str:
49
+ if not isinstance(value, str):
50
+ return fallback
51
+ text = value.strip()
52
+ if not text:
53
+ return fallback
54
+ if len(text) > max_chars:
55
+ return fallback
56
+ if any(ord(ch) < 32 or ord(ch) == 127 for ch in text):
57
+ return fallback
58
+ return text
59
+
60
+
61
+ def _as_clean_monaco(value: object, fallback: str) -> str:
62
+ text = _as_clean_text(value, fallback, max_chars=_MAX_MONACO_CHARS)
63
+ if not _MONACO_RE.fullmatch(text):
64
+ return fallback
65
+ return text
66
+
67
+
68
+ def _as_clean_filename(value: object, fallback: str) -> str:
69
+ text = _as_clean_text(value, fallback, max_chars=_MAX_FILENAME_CHARS)
70
+ if (
71
+ text in {".", ".."}
72
+ or ".." in text
73
+ or "/" in text
74
+ or "\\" in text
75
+ or ":" in text
76
+ ):
77
+ return fallback
78
+ return text
79
+
80
+
81
+ def _as_clean_extensions(value: object, fallback: list[str]) -> list[str]:
82
+ if not isinstance(value, list):
83
+ return fallback
84
+ result: list[str] = []
85
+ seen: set[str] = set()
86
+ for item in value[:_MAX_EXTENSIONS]:
87
+ text = _as_clean_text(item, "", max_chars=_MAX_EXTENSION_CHARS)
88
+ if not text:
89
+ continue
90
+ if ".." in text or "/" in text or "\\" in text or ":" in text:
91
+ continue
92
+ if not _EXTENSION_RE.fullmatch(text):
93
+ continue
94
+ if text not in seen:
95
+ result.append(text)
96
+ seen.add(text)
97
+ return result or fallback
98
+
99
+
100
+ def _as_clean_last_updated(value: object, fallback: str) -> str:
101
+ text = _as_clean_text(value, fallback, max_chars=_MAX_LAST_UPDATED_CHARS)
102
+ if text != fallback and not _LAST_UPDATED_RE.fullmatch(text):
103
+ return fallback
104
+ return text
105
+
106
+
107
+ class ParserAdapter:
108
+ """Typed wrapper around a loaded parser plugin."""
109
+
110
+ def __init__(self, plugin: LoadedPlugin) -> None:
111
+ self._plugin = plugin
112
+ self._grammar_id: str | None = None
113
+ self._language_ids: list[str] | None = None
114
+ self._language_info: list[LanguagePluginInfo] | None = None
115
+ self._trivia_types: list[str] | None = None
116
+ self.plugin_id: str = ""
117
+ self.package_name: str = ""
118
+ self.package_version: str = ""
119
+ self.author: str = ""
120
+ self.provenance: str = "" # set by registry: "<package> <version>"
121
+
122
+ @property
123
+ def grammar_id(self) -> str:
124
+ if self._grammar_id is None:
125
+ self._grammar_id = self._plugin.call_grammar_id()
126
+ return self._grammar_id
127
+
128
+ @property
129
+ def language_ids(self) -> list[str]:
130
+ if self._language_ids is None:
131
+ self._language_ids = self._plugin.call_language_ids()
132
+ return self._language_ids
133
+
134
+ @property
135
+ def trivia_node_types(self) -> list[str]:
136
+ if self._trivia_types is None:
137
+ self._trivia_types = self._plugin.call_trivia_node_types()
138
+ return self._trivia_types
139
+
140
+ @property
141
+ def wasm_path(self) -> str:
142
+ """Filesystem path of the backing ``.wasm`` file."""
143
+ return self._plugin.wasm_path
144
+
145
+ @property
146
+ def is_trusted(self) -> bool:
147
+ return self._plugin.trusted
148
+
149
+ @property
150
+ def priority(self) -> int:
151
+ return self._plugin.call_priority("parser")
152
+
153
+ @property
154
+ def parser_mode(self) -> str:
155
+ return self._plugin.call_parser_mode()
156
+
157
+ @property
158
+ def language_info(self) -> list[LanguagePluginInfo]:
159
+ """Return plugin-owned language metadata with host-owned fields attached."""
160
+ if self._language_info is None:
161
+ self._language_info = self._load_language_info()
162
+ return self._language_info
163
+
164
+ def _fallback_language_info(self, language_id: str) -> LanguagePluginInfo:
165
+ return fallback_language_info(
166
+ language_id,
167
+ plugin_id=self.plugin_id or self.grammar_id,
168
+ grammar_id=self.grammar_id,
169
+ priority=self.priority,
170
+ is_trusted=self.is_trusted,
171
+ provenance=self.provenance,
172
+ author=self.author,
173
+ plugin_version=self.package_version,
174
+ )
175
+
176
+ def _load_language_info(self) -> list[LanguagePluginInfo]:
177
+ by_language = {
178
+ lang: self._fallback_language_info(lang)
179
+ for lang in self.language_ids
180
+ if isinstance(lang, str) and lang
181
+ }
182
+ try:
183
+ records = self._plugin.call_language_info()
184
+ except Exception as exc:
185
+ logger.debug(
186
+ "Parser plugin %r language-info unavailable: %s",
187
+ self.plugin_id or self.grammar_id,
188
+ exc,
189
+ )
190
+ return list(by_language.values())
191
+
192
+ declared_languages = set(by_language)
193
+ for raw in records:
194
+ if not isinstance(raw, dict):
195
+ continue
196
+ lang = str(raw.get("language_id") or "")
197
+ if not lang or lang not in declared_languages:
198
+ logger.debug(
199
+ "Parser plugin %r ignored unclaimed language-info record %r",
200
+ self.plugin_id or self.grammar_id,
201
+ lang,
202
+ )
203
+ continue
204
+ base = by_language.get(lang) or self._fallback_language_info(lang)
205
+ by_language[lang] = LanguagePluginInfo(
206
+ language_id=lang,
207
+ language_name=_as_clean_text(
208
+ raw.get("language_name"),
209
+ base.language_name,
210
+ max_chars=_MAX_DISPLAY_CHARS,
211
+ ),
212
+ language_short_name=_as_clean_text(
213
+ raw.get("language_short_name"),
214
+ base.language_short_name,
215
+ max_chars=_MAX_SHORT_NAME_CHARS,
216
+ ),
217
+ monaco_language=_as_clean_monaco(
218
+ raw.get("monaco_language"),
219
+ base.monaco_language,
220
+ ),
221
+ default_filename=_as_clean_filename(
222
+ raw.get("default_filename"),
223
+ base.default_filename,
224
+ ),
225
+ language_file_extensions=_as_clean_extensions(
226
+ raw.get("language_file_extensions", []),
227
+ base.language_file_extensions,
228
+ ),
229
+ author=_as_clean_text(
230
+ raw.get("author"),
231
+ base.author,
232
+ max_chars=_MAX_AUTHOR_CHARS,
233
+ ),
234
+ plugin_version=_as_clean_text(
235
+ raw.get("plugin_version"),
236
+ base.plugin_version,
237
+ max_chars=_MAX_VERSION_CHARS,
238
+ ),
239
+ last_updated=_as_clean_last_updated(
240
+ raw.get("last_updated"),
241
+ base.last_updated,
242
+ ),
243
+ plugin_id=self.plugin_id or self.grammar_id,
244
+ grammar_id=self.grammar_id,
245
+ priority=self.priority,
246
+ is_trusted=self.is_trusted,
247
+ provenance=self.provenance,
248
+ )
249
+ return sorted(by_language.values(), key=lambda info: info.language_id)
250
+
251
+ def can_parse(self, content: str) -> bool:
252
+ """Return ``True`` if this parser claims to handle ``content``.
253
+
254
+ Passes an empty filename so the plugin must rely on content-sniffing
255
+ rather than file-extension heuristics. Only the first 4096 bytes are
256
+ inspected.
257
+ """
258
+ return bool(self._plugin.call_detect_language("", content[:4096]))
259
+
260
+ def detect_language(self, filename: str, content: str) -> str:
261
+ return self._plugin.call_detect_language(filename, content)
262
+
263
+ def preprocess_source(self, source: str) -> str:
264
+ return self._plugin.call_preprocess_source(source)
265
+
266
+ def playground_example(self, language: str) -> dict[str, str] | None:
267
+ """Return the ``{"old": ..., "new": ...}`` example pair for *language*.
268
+
269
+ Returns ``None`` if the plugin raises or returns empty strings.
270
+ """
271
+ try:
272
+ ex = self._plugin.call_example(language)
273
+ except Exception:
274
+ return None
275
+ if ex.get("old") and ex.get("new"):
276
+ return ex
277
+ return None
278
+
279
+ def process(
280
+ self, input_: str, language: str, filename: str, fuel: int | None = None
281
+ ) -> SemanticNode:
282
+ """
283
+ Call the plugin's ``process`` function and validate the returned JSON.
284
+
285
+ Raises
286
+ ------
287
+ PluginOutputError
288
+ If the plugin returns malformed JSON or a JSON object with an
289
+ ``"error"`` key, or if Pydantic validation fails.
290
+ """
291
+ raw = self._plugin.call_process(input_, language, filename, fuel=fuel)
292
+
293
+ try:
294
+ data = json.loads(raw)
295
+ except json.JSONDecodeError as exc:
296
+ raise PluginOutputError(self.grammar_id, f"invalid JSON: {exc}") from exc
297
+
298
+ if isinstance(data, dict) and "error" in data:
299
+ raise PluginOutputError(self.grammar_id, data["error"])
300
+
301
+ try:
302
+ return SemanticNode.model_validate(data)
303
+ except ValidationError as exc:
304
+ raise PluginOutputError(
305
+ self.grammar_id,
306
+ f"SemanticNode validation failed: {exc}",
307
+ ) from exc
308
+
309
+ def drain_telemetry(self) -> list[dict[str, Any]]:
310
+ return self._plugin.drain_telemetry()
311
+
312
+
313
+ class RendererAdapter:
314
+ """Typed wrapper around a loaded renderer plugin."""
315
+
316
+ def __init__(self, plugin: LoadedPlugin) -> None:
317
+ self._plugin = plugin
318
+ self._format_name: str | None = None
319
+ self.provenance: str = "" # set by registry: "<package> <version>"
320
+
321
+ @property
322
+ def format_name(self) -> str:
323
+ if self._format_name is None:
324
+ self._format_name = self._plugin.call_format_name()
325
+ return self._format_name
326
+
327
+ @property
328
+ def priority(self) -> int:
329
+ return self._plugin.call_priority("renderer")
330
+
331
+ @property
332
+ def supported_options(self) -> list[str]:
333
+ return self._plugin.call_supported_options()
334
+
335
+ def render(self, diff_json: str, fuel: int | None = None) -> str:
336
+ return self._plugin.call_render(diff_json, fuel=fuel)
337
+
338
+ def drain_telemetry(self) -> list[dict[str, Any]]:
339
+ return self._plugin.drain_telemetry()
340
+
341
+
342
+ class EnricherAdapter:
343
+ """Typed wrapper around a loaded enricher plugin."""
344
+
345
+ def __init__(self, plugin: LoadedPlugin) -> None:
346
+ self._plugin = plugin
347
+ self._language_ids: list[str] | None = None
348
+ self.provenance: str = "" # set by registry: "<package> <version>"
349
+
350
+ @property
351
+ def language_ids(self) -> list[str]:
352
+ if self._language_ids is None:
353
+ self._language_ids = self._plugin.call_language_ids()
354
+ return self._language_ids
355
+
356
+ @property
357
+ def priority(self) -> int:
358
+ return self._plugin.call_priority()
359
+
360
+ def enrich(
361
+ self, tree_json: str, raw_source: str, language: str, filename: str, fuel: int | None = None
362
+ ) -> str:
363
+ return self._plugin.call_enrich(tree_json, raw_source, language, filename, fuel=fuel)
364
+
365
+ def drain_telemetry(self) -> list[dict[str, Any]]:
366
+ return self._plugin.drain_telemetry()
367
+
368
+
369
+ class DiffAnalyzerAdapter:
370
+ """Typed wrapper around a loaded diff-analyzer plugin."""
371
+
372
+ def __init__(self, plugin: "LoadedPlugin") -> None:
373
+ self._plugin = plugin
374
+ self._language_ids: list[str] | None = None
375
+ self.provenance: str = ""
376
+
377
+ @property
378
+ def language_ids(self) -> list[str]:
379
+ if self._language_ids is None:
380
+ self._language_ids = self._plugin.call_language_ids()
381
+ return self._language_ids
382
+
383
+ @property
384
+ def priority(self) -> int:
385
+ return self._plugin.call_priority()
386
+
387
+ def analyze_diff(
388
+ self,
389
+ diff_json: str,
390
+ language: str,
391
+ filename: str,
392
+ fuel: int | None = None,
393
+ ) -> str:
394
+ return self._plugin.call_analyze_diff(diff_json, language, filename, fuel=fuel)
395
+
396
+ def drain_telemetry(self) -> list[dict[str, Any]]:
397
+ return self._plugin.drain_telemetry()