ai-editor-tree-engine-dev 1.0.176__py3-none-any.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 (121) hide show
  1. ai_editor_tree_engine_dev-1.0.176.dist-info/METADATA +35 -0
  2. ai_editor_tree_engine_dev-1.0.176.dist-info/RECORD +121 -0
  3. ai_editor_tree_engine_dev-1.0.176.dist-info/WHEEL +5 -0
  4. ai_editor_tree_engine_dev-1.0.176.dist-info/licenses/LICENSE +21 -0
  5. ai_editor_tree_engine_dev-1.0.176.dist-info/top_level.txt +1 -0
  6. tree_engine/PUBLIC_API.md +161 -0
  7. tree_engine/__init__.py +327 -0
  8. tree_engine/checks/__init__.py +60 -0
  9. tree_engine/checks/builtin/__init__.py +5 -0
  10. tree_engine/checks/builtin/compile_check.py +53 -0
  11. tree_engine/checks/builtin/csv_syntax.py +51 -0
  12. tree_engine/checks/builtin/docstrings.py +211 -0
  13. tree_engine/checks/builtin/encoding.py +55 -0
  14. tree_engine/checks/builtin/fleet_rules.py +227 -0
  15. tree_engine/checks/builtin/format_syntax.py +82 -0
  16. tree_engine/checks/builtin/jsonl_syntax.py +49 -0
  17. tree_engine/checks/builtin/shipped.py +52 -0
  18. tree_engine/checks/builtin/syntax.py +118 -0
  19. tree_engine/checks/builtin/xml_wellformed.py +51 -0
  20. tree_engine/checks/chain.py +332 -0
  21. tree_engine/checks/external/__init__.py +5 -0
  22. tree_engine/checks/external/_base.py +61 -0
  23. tree_engine/checks/external/bandit.py +53 -0
  24. tree_engine/checks/external/black.py +48 -0
  25. tree_engine/checks/external/flake8.py +38 -0
  26. tree_engine/checks/external/isort.py +38 -0
  27. tree_engine/checks/external/mypy.py +45 -0
  28. tree_engine/checks/external/ruff.py +45 -0
  29. tree_engine/checks/external/runner.py +197 -0
  30. tree_engine/checks/model.py +110 -0
  31. tree_engine/checks/options.py +73 -0
  32. tree_engine/checks/plugin.py +171 -0
  33. tree_engine/checks/registry.py +349 -0
  34. tree_engine/core/address.py +376 -0
  35. tree_engine/core/identifier_map.py +266 -0
  36. tree_engine/core/identity.py +226 -0
  37. tree_engine/core/instrumentation.py +399 -0
  38. tree_engine/core/integrity.py +341 -0
  39. tree_engine/core/integrity_full.py +382 -0
  40. tree_engine/core/journal.py +260 -0
  41. tree_engine/core/journal_compose.py +571 -0
  42. tree_engine/core/live_tree.py +785 -0
  43. tree_engine/core/locking.py +188 -0
  44. tree_engine/core/move.py +585 -0
  45. tree_engine/core/move_references.py +323 -0
  46. tree_engine/core/node_types.py +408 -0
  47. tree_engine/core/nodes.py +348 -0
  48. tree_engine/core/offsets.py +24 -0
  49. tree_engine/core/operations.py +825 -0
  50. tree_engine/core/plugin_boundary.py +160 -0
  51. tree_engine/core/position_map.py +263 -0
  52. tree_engine/core/position_transfer.py +512 -0
  53. tree_engine/core/range_refresh.py +366 -0
  54. tree_engine/core/reference_cache.py +360 -0
  55. tree_engine/core/references.py +223 -0
  56. tree_engine/core/reparse.py +378 -0
  57. tree_engine/core/short_id.py +296 -0
  58. tree_engine/core/subtree_apply.py +397 -0
  59. tree_engine/core/subtree_copy.py +399 -0
  60. tree_engine/core/transactions.py +397 -0
  61. tree_engine/core/trivia.py +396 -0
  62. tree_engine/core/updates.py +398 -0
  63. tree_engine/core/validation.py +377 -0
  64. tree_engine/errors.py +104 -0
  65. tree_engine/exceptions.py +376 -0
  66. tree_engine/facade.py +4524 -0
  67. tree_engine/plugins/_statements.py +369 -0
  68. tree_engine/plugins/bsl/block_edit.py +449 -0
  69. tree_engine/plugins/bsl/blocks.py +127 -0
  70. tree_engine/plugins/bsl/checker.py +297 -0
  71. tree_engine/plugins/bsl/dialects.py +303 -0
  72. tree_engine/plugins/bsl/generator.py +481 -0
  73. tree_engine/plugins/bsl/import_map.py +995 -0
  74. tree_engine/plugins/bsl/plugin.py +452 -0
  75. tree_engine/plugins/contract.py +355 -0
  76. tree_engine/plugins/cpp/import_map.py +505 -0
  77. tree_engine/plugins/cpp/languages.py +99 -0
  78. tree_engine/plugins/cpp/plugin.py +501 -0
  79. tree_engine/plugins/csv_format.py +676 -0
  80. tree_engine/plugins/detachable.py +156 -0
  81. tree_engine/plugins/fallback.py +421 -0
  82. tree_engine/plugins/ini_format.py +330 -0
  83. tree_engine/plugins/json_format.py +813 -0
  84. tree_engine/plugins/json_pointer.py +203 -0
  85. tree_engine/plugins/jsonl_format.py +392 -0
  86. tree_engine/plugins/markdown_format.py +748 -0
  87. tree_engine/plugins/onec_query_format.py +61 -0
  88. tree_engine/plugins/parse_position.py +54 -0
  89. tree_engine/plugins/plain_text.py +1543 -0
  90. tree_engine/plugins/python/export_map.py +306 -0
  91. tree_engine/plugins/python/import_map.py +429 -0
  92. tree_engine/plugins/python/plugin.py +1710 -0
  93. tree_engine/plugins/registry.py +497 -0
  94. tree_engine/plugins/scan.py +87 -0
  95. tree_engine/plugins/selection.py +228 -0
  96. tree_engine/plugins/sql_format.py +61 -0
  97. tree_engine/plugins/toml_format.py +1072 -0
  98. tree_engine/plugins/xml_format.py +911 -0
  99. tree_engine/plugins/yaml/builder.py +49 -0
  100. tree_engine/plugins/yaml/emitter.py +225 -0
  101. tree_engine/plugins/yaml/flow.py +227 -0
  102. tree_engine/plugins/yaml/plugin.py +458 -0
  103. tree_engine/plugins/yaml/ranges.py +52 -0
  104. tree_engine/plugins/yaml/reader.py +359 -0
  105. tree_engine/plugins/yaml/scanner.py +298 -0
  106. tree_engine/query/adapter.py +283 -0
  107. tree_engine/query/engine.py +853 -0
  108. tree_engine/query/inspection.py +498 -0
  109. tree_engine/query/outline.py +288 -0
  110. tree_engine/query/predicates.py +263 -0
  111. tree_engine/query/rendered_lines.py +46 -0
  112. tree_engine/query/results.py +206 -0
  113. tree_engine/query/selector.py +400 -0
  114. tree_engine/query/xml_query.py +747 -0
  115. tree_engine/storage/codec.py +400 -0
  116. tree_engine/storage/document_bridge.py +579 -0
  117. tree_engine/storage/file_txn.py +459 -0
  118. tree_engine/storage/history.py +106 -0
  119. tree_engine/storage/lifecycle.py +622 -0
  120. tree_engine/storage/schema.py +403 -0
  121. tree_engine/storage/session_guard.py +398 -0
@@ -0,0 +1,366 @@
1
+ """Pointwise ``buffer_range`` maintenance after a mutation (bug b548f445, D-AL rule 3).
2
+
3
+ Author: Vasiliy Zdanovskiy
4
+ email: vasilyvz@gmail.com
5
+
6
+ The json, yaml, toml and plain_text plugins set ``buffer_range`` at parse time but, unlike python,
7
+ their splices are not measured by ``core.operations``'s bookkeeping, so after a mutation the ranges
8
+ are re-derived here, for the container that was touched and nothing else:
9
+
10
+ * json, toml -- the touched container's own rendering is parsed once with its plugin (a bounded
11
+ parse of that subtree's text, never the document) and the resulting relative ranges are copied
12
+ onto the live subtree at its unchanged start offset;
13
+ * yaml, plain_text -- a node's text is exactly ``head`` + children + ``tail`` (yaml) or the tiling
14
+ of its lines and separators (plain_text), so ranges follow from lengths alone.
15
+
16
+ The size change of the touched container (``delta``) is then propagated the way the splice
17
+ bookkeeping does it: every ancestor's end grows by ``delta`` and everything positioned after the
18
+ container moves by ``delta``. No document render, no document walk, no re-index. When a shape
19
+ cannot be trusted its ranges are cleared: absent is safe, wrong is not.
20
+ """
21
+
22
+ from __future__ import annotations
23
+
24
+ import re
25
+ from typing import Any, Dict, List, Optional, Sequence, Tuple
26
+
27
+ from tree_engine.core.live_tree import to_frozen
28
+ from tree_engine.core.operations import _extend_range, _shift_positioned_after
29
+
30
+ __all__ = ["RANGED_FORMATS", "refresh_ranges"]
31
+
32
+ RANGED_FORMATS = frozenset({"json", "yaml", "toml", "plain_text"})
33
+
34
+ _LINE_BREAK_END = re.compile(r"(?:\r\n|[\n\r\x0b\x0c\x1c-\x1e\x85

])\Z")
35
+
36
+
37
+ def _blen(text: str) -> int:
38
+ return len(text) if text.isascii() else len(text.encode("utf-8", "surrogateescape"))
39
+
40
+
41
+ def _field(node: Any, name: str) -> str:
42
+ value = node.fields.get(name, "")
43
+ return value if isinstance(value, str) else ""
44
+
45
+
46
+ def _preorder(node: Any) -> List[Any]:
47
+ out: List[Any] = []
48
+ stack = [node]
49
+ while stack:
50
+ current = stack.pop()
51
+ out.append(current)
52
+ stack.extend(reversed(list(current.children)))
53
+ return out
54
+
55
+
56
+ def _clear(node: Any) -> None:
57
+ for item in _preorder(node):
58
+ item.buffer_range = None
59
+
60
+
61
+ def _propagate(node: Any, old_end: int, delta: int) -> None:
62
+ """Ancestors of ``node`` grow by ``delta``; whatever follows ``node`` moves by ``delta``."""
63
+ if not delta:
64
+ return
65
+ current = node
66
+ while True:
67
+ parent = current.parent
68
+ if parent is None:
69
+ return
70
+ _extend_range(parent, delta)
71
+ for sibling in list(parent.children):
72
+ if sibling is not current:
73
+ _shift_positioned_after(sibling, delta, old_end)
74
+ current = parent
75
+
76
+
77
+ # -- json / toml: bounded parse of the touched container's own text ---------
78
+
79
+
80
+ def _json_root(node: Any) -> Any:
81
+ while node.kind == "json:Member" and node.parent is not None:
82
+ node = node.parent
83
+ return node
84
+
85
+
86
+ def _toml_root(node: Any) -> Any:
87
+ while node.parent is not None and node.kind not in (
88
+ "toml:Document", "toml:Table", "toml:ArrayOfTables"
89
+ ):
90
+ node = node.parent
91
+ return node
92
+
93
+
94
+ def _reparse_container(plugin: Any, node: Any, wrapped: bool) -> bool:
95
+ """Re-derive ``node``'s subtree ranges from a parse of its own text; propagate its size change."""
96
+ old = node.buffer_range
97
+ if old is None:
98
+ return False
99
+ text = plugin.render_fragment(to_frozen(node))
100
+ new_len = _blen(text)
101
+ fresh_root: Optional[Any] = None
102
+ try:
103
+ parsed = plugin.parse_document(text).root
104
+ if wrapped and parsed.kind != node.kind:
105
+ kids = list(parsed.children)
106
+ parsed = kids[0] if len(kids) == 1 else None
107
+ fresh_root = parsed
108
+ except Exception:
109
+ fresh_root = None
110
+ live = _preorder(node)
111
+ fresh = _preorder(fresh_root) if fresh_root is not None else []
112
+ if len(live) == len(fresh) and all(
113
+ len(a.children) == len(b.children) for a, b in zip(live, fresh)
114
+ ):
115
+ base = old[0]
116
+ for a, b in zip(live, fresh):
117
+ span = b.buffer_range
118
+ a.buffer_range = None if span is None else (span[0] + base, span[1] + base)
119
+ else:
120
+ _clear(node)
121
+ node.buffer_range = (old[0], old[0] + new_len)
122
+ node.buffer_range = (old[0], old[0] + new_len)
123
+ _propagate(node, old[1], old[0] + new_len - old[1])
124
+ return True
125
+
126
+
127
+ # -- yaml: head + children + tail ------------------------------------------
128
+
129
+
130
+ def _yaml_special(node: Any) -> Optional[str]:
131
+ """The ``"key: {}"`` / ``"- []"`` render of an entry whose block value kept only trivia."""
132
+ if (
133
+ node.kind in ("yaml:Pair", "yaml:Item")
134
+ and _field(node, "head").endswith("\n")
135
+ and len(node.children) == 1
136
+ and node.children[0].kind in ("yaml:Mapping", "yaml:Sequence")
137
+ and all(c.kind in ("yaml:Comment", "yaml:Blank") for c in node.children[0].children)
138
+ ):
139
+ return "{}" if node.children[0].kind == "yaml:Mapping" else "[]"
140
+ return None
141
+
142
+
143
+ def _yaml_length(node: Any, memo: Dict[int, Optional[int]]) -> Optional[int]:
144
+ if id(node) in memo:
145
+ return memo[id(node)]
146
+ result: Optional[int]
147
+ fields = node.fields
148
+ if "raw" in fields and isinstance(fields["raw"], str) and not node.children:
149
+ result = _blen(fields["raw"])
150
+ elif "head" in fields and "tail" in fields:
151
+ parts = [_yaml_length(child, memo) for child in node.children]
152
+ if any(p is None for p in parts):
153
+ result = None
154
+ else:
155
+ extra = 3 if _yaml_special(node) else 0
156
+ result = _blen(_field(node, "head")) + extra + sum(parts) + _blen(_field(node, "tail")) # type: ignore[arg-type]
157
+ elif not node.children and not fields.get("head") and not fields.get("tail") and node.kind in (
158
+ "yaml:Document", "yaml:Mapping", "yaml:Sequence", "yaml:Trivia"
159
+ ):
160
+ result = 0
161
+ elif node.children and "head" not in fields and "raw" not in fields:
162
+ parts = [_yaml_length(child, memo) for child in node.children]
163
+ result = None if any(p is None for p in parts) else sum(parts) # type: ignore[arg-type]
164
+ else:
165
+ result = None
166
+ memo[id(node)] = result
167
+ return result
168
+
169
+
170
+ def _yaml_place(node: Any, start: int, memo: Dict[int, Optional[int]]) -> None:
171
+ size = _yaml_length(node, memo)
172
+ if size is None:
173
+ _clear(node)
174
+ return
175
+ node.buffer_range = (start, start + size)
176
+ special = _yaml_special(node)
177
+ cursor = start + _blen(_field(node, "head"))
178
+ if special:
179
+ cursor += 3
180
+ container = node.children[0]
181
+ container.buffer_range = (cursor, cursor + sum(_yaml_length(c, memo) or 0 for c in container.children))
182
+ for trivia in container.children:
183
+ _yaml_place(trivia, cursor, memo)
184
+ cursor += _yaml_length(trivia, memo) or 0
185
+ return
186
+ for child in node.children:
187
+ _yaml_place(child, cursor, memo)
188
+ cursor += _yaml_length(child, memo) or 0
189
+
190
+
191
+ def _refresh_yaml(roots: Sequence[Any], document_root: Any) -> None:
192
+ memo: Dict[int, Optional[int]] = {}
193
+ for node in roots:
194
+ old = node.buffer_range
195
+ if old is None or node is document_root:
196
+ memo.clear()
197
+ _yaml_place(document_root, 0, memo)
198
+ continue
199
+ memo.clear()
200
+ size = _yaml_length(node, memo)
201
+ _yaml_place(node, old[0], memo)
202
+ if size is not None:
203
+ _propagate(node, old[1], old[0] + size - old[1])
204
+
205
+
206
+ # -- plain_text: lines and separators tile the document ---------------------
207
+
208
+
209
+ def _text_length(node: Any, memo: Dict[int, int]) -> int:
210
+ if id(node) in memo:
211
+ return memo[id(node)]
212
+ if node.children:
213
+ own = sum(_text_length(child, memo) for child in node.children)
214
+ else:
215
+ value = node.fields.get("source_text", node.fields.get("text", ""))
216
+ own = _blen(value) if isinstance(value, str) else 0
217
+ for name in ("separator", "leading_separator", "trailing_separator"):
218
+ own += _blen(_field(node, name))
219
+ memo[id(node)] = own
220
+ return own
221
+
222
+
223
+ def _text_place(node: Any, start: int, memo: Dict[int, int]) -> None:
224
+ node.buffer_range = (start, start + _text_length(node, memo))
225
+ cursor = start + _blen(_field(node, "leading_separator"))
226
+ for child in node.children:
227
+ _text_place(child, cursor, memo)
228
+ cursor += _text_length(child, memo)
229
+
230
+
231
+ def _last_line(paragraph: Any) -> Optional[Any]:
232
+ node = paragraph
233
+ while node.children:
234
+ node = node.children[-1]
235
+ return node if node is not paragraph and node.kind == "plain_text:line" else None
236
+
237
+
238
+ _BREAK_CHARS = "\r\n\x0b\x0c\x1c\x1d\x1e\x85\u2028\u2029"
239
+
240
+
241
+ def _fresh_parse_shape(paragraphs: Sequence[Any]) -> bool:
242
+ """Whether the paragraph level has the shape a fresh parse of its text would have: every
243
+ paragraph holds only lines, and each boundary is a real blank line (two or more line breaks
244
+ between one paragraph's text and the next's). A boundary of a single break would re-parse as
245
+ ONE paragraph; the plugin's own hooks deliberately leave such splices (and their separator
246
+ attribution) alone, so nothing is rewritten for them."""
247
+ for index, paragraph in enumerate(paragraphs):
248
+ if not paragraph.children or not all(c.kind == "plain_text:line" for c in paragraph.children):
249
+ return False
250
+ if index == len(paragraphs) - 1:
251
+ continue
252
+ text = _field(paragraph.children[-1], "text")
253
+ run = text[len(text.rstrip(_BREAK_CHARS)):] + _field(paragraph, "separator")
254
+ if len(run.splitlines(keepends=True)) < 2:
255
+ return False
256
+ return True
257
+
258
+
259
+ def _canonicalize_text(root: Any, plugin: Any) -> None:
260
+ """Attribute the blank-line run at each paragraph end the way a fresh parse does.
261
+
262
+ The plain_text plugin stores separator bytes verbatim, so a splice can leave the same bytes
263
+ attributed differently (the line breaks after a paragraph's last line sitting on the line
264
+ instead of in the paragraph's ``separator`` / the root's ``trailing_separator``). The rendered
265
+ bytes are identical either way; the canonical attribution makes every line's range equal its
266
+ own text. Only a paragraph whose end is not already canonical is touched, and only its last
267
+ line, its ``separator``, its ``line_count`` and (for the last paragraph) the root's
268
+ ``trailing_separator`` can change."""
269
+ paragraphs = [p for p in root.children if p.kind == "plain_text:paragraph"]
270
+ if len(paragraphs) != len(root.children):
271
+ return
272
+ if not paragraphs or not _fresh_parse_shape(paragraphs):
273
+ return
274
+ for index, paragraph in enumerate(paragraphs):
275
+ last = paragraph.children[-1]
276
+ text = _field(last, "text")
277
+ separator = _field(paragraph, "separator")
278
+ is_last = index == len(paragraphs) - 1
279
+ ends_with_break = bool(_LINE_BREAK_END.search(text))
280
+ single_break = ends_with_break and not _LINE_BREAK_END.search(
281
+ text[: len(text) - (2 if text.endswith("\r\n") else 1)]
282
+ )
283
+ if is_last:
284
+ canonical = not ends_with_break and separator == ""
285
+ else:
286
+ canonical = single_break
287
+ if canonical:
288
+ if int(paragraph.fields.get("line_count", -1)) != len(paragraph.children):
289
+ paragraph.fields = {**paragraph.fields, "line_count": len(paragraph.children)}
290
+ continue
291
+ joined = "".join(_field(c, "text") for c in paragraph.children[:-1])
292
+ edge = _field(root, "trailing_separator") if is_last else ""
293
+ layout = plugin_split(plugin, joined + text + separator + edge)
294
+ if layout is None:
295
+ continue
296
+ lines, total = layout
297
+ if len(lines) != len(paragraph.children):
298
+ continue
299
+ if is_last:
300
+ keep, new_sep, new_trailing = lines[-1], "", total
301
+ else:
302
+ pieces = total.splitlines(keepends=True)
303
+ first = pieces[0] if pieces else ""
304
+ keep, new_sep, new_trailing = lines[-1] + first, total[len(first):], None
305
+ for line, new_text in zip(paragraph.children, list(lines[:-1]) + [keep]):
306
+ if _field(line, "text") != new_text:
307
+ line.fields = {**line.fields, "text": new_text}
308
+ fields = dict(paragraph.fields)
309
+ fields["separator"] = new_sep
310
+ fields["line_count"] = len(paragraph.children)
311
+ paragraph.fields = fields
312
+ if new_trailing is not None and _field(root, "trailing_separator") != new_trailing:
313
+ root.fields = {**root.fields, "trailing_separator": new_trailing}
314
+
315
+
316
+ def plugin_split(plugin: Any, text: str) -> Optional[Tuple[List[str], str]]:
317
+ """``(lines, trailing run)`` of a single-paragraph text via the plugin's own paragraph rule."""
318
+ from tree_engine.plugins.plain_text import split_text_paragraphs
319
+
320
+ layout = split_text_paragraphs(text)
321
+ if len(layout.paragraphs) != 1 or layout.leading_separator or layout.paragraphs[0].separator:
322
+ return None
323
+ return list(layout.paragraphs[0].lines), layout.trailing_separator
324
+
325
+
326
+ # -- entry point ------------------------------------------------------------
327
+
328
+
329
+ def refresh_ranges(document: Any, touched: Sequence[Any], plugin: Any) -> None:
330
+ """Re-derive ``buffer_range`` around the nodes a mutation touched (see the module docstring)."""
331
+ fmt = document.format_id
332
+ if fmt not in RANGED_FORMATS or document.root is None:
333
+ return
334
+ root = document.root
335
+ if fmt == "plain_text":
336
+ _canonicalize_text(root, plugin)
337
+ _text_place(root, 0, {})
338
+ return
339
+ pick = {"json": _json_root, "toml": _toml_root}.get(fmt, lambda n: n)
340
+ candidates: List[Any] = []
341
+ for node in touched:
342
+ top = node
343
+ while top.parent is not None:
344
+ top = top.parent
345
+ if top is not root:
346
+ continue
347
+ candidates.append(pick(node))
348
+ chosen: List[Any] = []
349
+ ids = {id(n) for n in candidates}
350
+ for node in candidates:
351
+ ancestor = node.parent
352
+ covered = False
353
+ while ancestor is not None:
354
+ if id(ancestor) in ids:
355
+ covered = True
356
+ break
357
+ ancestor = ancestor.parent
358
+ if not covered and all(node is not c for c in chosen):
359
+ chosen.append(node)
360
+ if fmt == "yaml":
361
+ _refresh_yaml(chosen, root)
362
+ return
363
+ for node in chosen:
364
+ if not _reparse_container(plugin, node, wrapped=fmt == "toml" and node.kind != "toml:Document"):
365
+ _clear(root)
366
+ return