@better-fs-tools/write 0.0.0-tegami-trusted-publish-setup → 0.2.0

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 (196) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +449 -6
  3. package/dist/authorize/ask-before-write.d.ts +11 -0
  4. package/dist/authorize/ask-before-write.js +49 -0
  5. package/dist/authorize/index.d.ts +3 -0
  6. package/dist/authorize/index.js +3 -0
  7. package/dist/authorize/protect-paths.d.ts +12 -0
  8. package/dist/authorize/protect-paths.js +74 -0
  9. package/dist/authorize/write-authorizers.d.ts +9 -0
  10. package/dist/authorize/write-authorizers.js +48 -0
  11. package/dist/bundle/create-fs-tools.d.ts +93 -0
  12. package/dist/bundle/create-fs-tools.js +130 -0
  13. package/dist/codecs/index.d.ts +1 -0
  14. package/dist/codecs/index.js +1 -0
  15. package/dist/codecs/utf8.d.ts +7 -0
  16. package/dist/codecs/utf8.js +61 -0
  17. package/dist/contract/codec.d.ts +26 -0
  18. package/dist/contract/codec.js +0 -0
  19. package/dist/contract/context.d.ts +14 -0
  20. package/dist/contract/context.js +0 -0
  21. package/dist/contract/deps.d.ts +63 -0
  22. package/dist/contract/deps.js +0 -0
  23. package/dist/contract/extensions.d.ts +103 -0
  24. package/dist/contract/extensions.js +0 -0
  25. package/dist/contract/format.d.ts +15 -0
  26. package/dist/contract/format.js +0 -0
  27. package/dist/contract/input.d.ts +35 -0
  28. package/dist/contract/input.js +0 -0
  29. package/dist/contract/limits.d.ts +26 -0
  30. package/dist/contract/limits.js +0 -0
  31. package/dist/contract/locks.d.ts +14 -0
  32. package/dist/contract/locks.js +0 -0
  33. package/dist/contract/matcher.d.ts +30 -0
  34. package/dist/contract/matcher.js +0 -0
  35. package/dist/contract/messages.d.ts +188 -0
  36. package/dist/contract/messages.js +0 -0
  37. package/dist/contract/patch.d.ts +50 -0
  38. package/dist/contract/patch.js +0 -0
  39. package/dist/contract/preconditions.d.ts +13 -0
  40. package/dist/contract/preconditions.js +5 -0
  41. package/dist/contract/result.d.ts +102 -0
  42. package/dist/contract/result.js +0 -0
  43. package/dist/core/abort.d.ts +4 -0
  44. package/dist/core/abort.js +23 -0
  45. package/dist/core/authorize.d.ts +9 -0
  46. package/dist/core/authorize.js +87 -0
  47. package/dist/core/bytes.d.ts +3 -0
  48. package/dist/core/bytes.js +14 -0
  49. package/dist/core/commit-patch.d.ts +20 -0
  50. package/dist/core/commit-patch.js +224 -0
  51. package/dist/core/commit.d.ts +8 -0
  52. package/dist/core/commit.js +77 -0
  53. package/dist/core/create-tools.d.ts +23 -0
  54. package/dist/core/create-tools.js +54 -0
  55. package/dist/core/deps.d.ts +11 -0
  56. package/dist/core/deps.js +210 -0
  57. package/dist/core/diff.d.ts +10 -0
  58. package/dist/core/diff.js +221 -0
  59. package/dist/core/encode.d.ts +3 -0
  60. package/dist/core/encode.js +26 -0
  61. package/dist/core/extension-error.d.ts +8 -0
  62. package/dist/core/extension-error.js +17 -0
  63. package/dist/core/format.d.ts +4 -0
  64. package/dist/core/format.js +22 -0
  65. package/dist/core/guards.d.ts +3 -0
  66. package/dist/core/guards.js +45 -0
  67. package/dist/core/hints.d.ts +14 -0
  68. package/dist/core/hints.js +75 -0
  69. package/dist/core/hooks.d.ts +15 -0
  70. package/dist/core/hooks.js +108 -0
  71. package/dist/core/input.d.ts +20 -0
  72. package/dist/core/input.js +91 -0
  73. package/dist/core/limits.d.ts +10 -0
  74. package/dist/core/limits.js +48 -0
  75. package/dist/core/line-index.d.ts +10 -0
  76. package/dist/core/line-index.js +38 -0
  77. package/dist/core/load.d.ts +14 -0
  78. package/dist/core/load.js +221 -0
  79. package/dist/core/lock.d.ts +2 -0
  80. package/dist/core/lock.js +56 -0
  81. package/dist/core/match.d.ts +15 -0
  82. package/dist/core/match.js +66 -0
  83. package/dist/core/messages.d.ts +8 -0
  84. package/dist/core/messages.js +140 -0
  85. package/dist/core/outcomes.d.ts +21 -0
  86. package/dist/core/outcomes.js +127 -0
  87. package/dist/core/parse-patch.d.ts +3 -0
  88. package/dist/core/parse-patch.js +64 -0
  89. package/dist/core/patch-hunks.d.ts +25 -0
  90. package/dist/core/patch-hunks.js +177 -0
  91. package/dist/core/patch-pipeline.d.ts +4 -0
  92. package/dist/core/patch-pipeline.js +284 -0
  93. package/dist/core/pipeline.d.ts +22 -0
  94. package/dist/core/pipeline.js +153 -0
  95. package/dist/core/plan-edit.d.ts +15 -0
  96. package/dist/core/plan-edit.js +215 -0
  97. package/dist/core/plan-patch.d.ts +29 -0
  98. package/dist/core/plan-patch.js +169 -0
  99. package/dist/core/plan-write.d.ts +6 -0
  100. package/dist/core/plan-write.js +27 -0
  101. package/dist/core/planned.d.ts +35 -0
  102. package/dist/core/planned.js +36 -0
  103. package/dist/core/precondition.d.ts +10 -0
  104. package/dist/core/precondition.js +65 -0
  105. package/dist/core/record.d.ts +3 -0
  106. package/dist/core/record.js +58 -0
  107. package/dist/core/resolve.d.ts +3 -0
  108. package/dist/core/resolve.js +65 -0
  109. package/dist/core/scope.d.ts +33 -0
  110. package/dist/core/scope.js +109 -0
  111. package/dist/core/snippet.d.ts +4 -0
  112. package/dist/core/snippet.js +36 -0
  113. package/dist/core/splice.d.ts +10 -0
  114. package/dist/core/splice.js +19 -0
  115. package/dist/core/target.d.ts +10 -0
  116. package/dist/core/target.js +46 -0
  117. package/dist/formatters/default.d.ts +22 -0
  118. package/dist/formatters/default.js +140 -0
  119. package/dist/formatters/index.d.ts +2 -0
  120. package/dist/formatters/index.js +1 -0
  121. package/dist/guards/generated-file.d.ts +10 -0
  122. package/dist/guards/generated-file.js +39 -0
  123. package/dist/guards/index.d.ts +19 -0
  124. package/dist/guards/index.js +33 -0
  125. package/dist/guards/non-text.d.ts +7 -0
  126. package/dist/guards/non-text.js +50 -0
  127. package/dist/guards/omission.d.ts +9 -0
  128. package/dist/guards/omission.js +48 -0
  129. package/dist/guards/read-prefix.d.ts +12 -0
  130. package/dist/guards/read-prefix.js +87 -0
  131. package/dist/guards/shared.d.ts +11 -0
  132. package/dist/guards/shared.js +46 -0
  133. package/dist/guards/syntax.d.ts +10 -0
  134. package/dist/guards/syntax.js +54 -0
  135. package/dist/guards/truncation-notice.d.ts +8 -0
  136. package/dist/guards/truncation-notice.js +32 -0
  137. package/dist/hooks/executable-shebang.d.ts +9 -0
  138. package/dist/hooks/executable-shebang.js +46 -0
  139. package/dist/hooks/index.d.ts +2 -0
  140. package/dist/hooks/index.js +2 -0
  141. package/dist/hooks/verify-write.d.ts +9 -0
  142. package/dist/hooks/verify-write.js +63 -0
  143. package/dist/index.d.ts +30 -0
  144. package/dist/index.js +15 -0
  145. package/dist/locks/index.d.ts +1 -0
  146. package/dist/locks/index.js +1 -0
  147. package/dist/locks/memory-locks.d.ts +9 -0
  148. package/dist/locks/memory-locks.js +94 -0
  149. package/dist/matchers/block-anchor.d.ts +9 -0
  150. package/dist/matchers/block-anchor.js +69 -0
  151. package/dist/matchers/escape.d.ts +6 -0
  152. package/dist/matchers/escape.js +32 -0
  153. package/dist/matchers/exact.d.ts +4 -0
  154. package/dist/matchers/exact.js +29 -0
  155. package/dist/matchers/fold.d.ts +11 -0
  156. package/dist/matchers/fold.js +144 -0
  157. package/dist/matchers/indentation.d.ts +3 -0
  158. package/dist/matchers/indentation.js +81 -0
  159. package/dist/matchers/index.d.ts +14 -0
  160. package/dist/matchers/index.js +20 -0
  161. package/dist/matchers/line-trimmed.d.ts +3 -0
  162. package/dist/matchers/line-trimmed.js +32 -0
  163. package/dist/matchers/lines.d.ts +24 -0
  164. package/dist/matchers/lines.js +74 -0
  165. package/dist/matchers/normalized.d.ts +7 -0
  166. package/dist/matchers/normalized.js +69 -0
  167. package/dist/patch/grammar.d.ts +5 -0
  168. package/dist/patch/grammar.js +24 -0
  169. package/dist/patch/index.d.ts +4 -0
  170. package/dist/patch/index.js +3 -0
  171. package/dist/patch/parse.d.ts +11 -0
  172. package/dist/patch/parse.js +209 -0
  173. package/dist/signature/contract.d.ts +8 -0
  174. package/dist/signature/contract.js +0 -0
  175. package/dist/signature/edit.d.ts +19 -0
  176. package/dist/signature/edit.js +158 -0
  177. package/dist/signature/index.d.ts +6 -0
  178. package/dist/signature/index.js +4 -0
  179. package/dist/signature/matching.d.ts +3 -0
  180. package/dist/signature/matching.js +25 -0
  181. package/dist/signature/messages.d.ts +4 -0
  182. package/dist/signature/messages.js +9 -0
  183. package/dist/signature/patch.d.ts +6 -0
  184. package/dist/signature/patch.js +55 -0
  185. package/dist/signature/schema.d.ts +19 -0
  186. package/dist/signature/schema.js +117 -0
  187. package/dist/signature/write.d.ts +9 -0
  188. package/dist/signature/write.js +52 -0
  189. package/dist/state/index.d.ts +2 -0
  190. package/dist/state/index.js +1 -0
  191. package/dist/state/invalidate.d.ts +29 -0
  192. package/dist/state/invalidate.js +37 -0
  193. package/docs/architecture.md +223 -0
  194. package/docs/hosts.md +96 -0
  195. package/docs/result-schema.md +261 -0
  196. package/package.json +53 -2
@@ -0,0 +1,144 @@
1
+ export const FIRST = 1;
2
+ export const LAST = 2;
3
+ const BLANK = 4;
4
+ const LF = 10;
5
+ const MARK = /^\p{M}/u;
6
+ export function fold(text) {
7
+ const out = new FoldedUnitBuffer(text.length);
8
+ let index = 0;
9
+ while (index < text.length) {
10
+ const code = text.charCodeAt(index);
11
+ const next = text.charCodeAt(index + 1);
12
+ if (foldsToItself(code, next)) {
13
+ out.push(code, index, index + 1, FIRST | LAST | (isBlank(code) ? BLANK : 0));
14
+ index += 1;
15
+ continue;
16
+ }
17
+ let end = index + unitLength(text, index);
18
+ while (end < text.length) {
19
+ const point = text.codePointAt(end) ?? 0;
20
+ if (point < 0x300 || !MARK.test(String.fromCodePoint(point)))
21
+ break;
22
+ end += point > 0xffff ? 2 : 1;
23
+ }
24
+ const folded = replaceLookalikes(text.slice(index, end).normalize("NFKC"));
25
+ let blank = BLANK;
26
+ for (let unit = 0; unit < folded.length; unit += 1) {
27
+ if (!isBlank(folded.charCodeAt(unit)))
28
+ blank = 0;
29
+ }
30
+ for (let unit = 0; unit < folded.length; unit += 1) {
31
+ const edges = (unit === 0 ? FIRST : 0) | (unit === folded.length - 1 ? LAST : 0);
32
+ out.push(folded.charCodeAt(unit), index, end, edges | blank);
33
+ }
34
+ index = end;
35
+ }
36
+ return out.dropTrailingBlanks();
37
+ }
38
+ function foldsToItself(code, next) {
39
+ const isAscii = code < 0x80;
40
+ const mayHaveCombiningMark = next >= 0x300;
41
+ return isAscii && !mayHaveCombiningMark;
42
+ }
43
+ function isBlank(code) {
44
+ return code === 32 || code === 9 || code === 13;
45
+ }
46
+ function unitLength(text, index) {
47
+ return (text.codePointAt(index) ?? 0) > 0xffff ? 2 : 1;
48
+ }
49
+ function replaceLookalikes(text) {
50
+ let result = "";
51
+ for (let index = 0; index < text.length; index += 1) {
52
+ const code = text.charCodeAt(index);
53
+ result += code < 0x80 ? text[index] : (MAPPED.get(code) ?? text[index]);
54
+ }
55
+ return result;
56
+ }
57
+ const MAPPED = new Map([
58
+ ...[0x2018, 0x2019, 0x201a, 0x201b].map((code) => [code, "'"]),
59
+ ...[0x201c, 0x201d, 0x201e, 0x201f].map((code) => [code, '"']),
60
+ ...[0x2010, 0x2011, 0x2012, 0x2013, 0x2014, 0x2015, 0x2212].map((code) => [code, "-"]),
61
+ ...[0x00a0, 0x202f, 0x205f, 0x3000].map((code) => [code, " "]),
62
+ ...Array.from({ length: 11 }, (_, offset) => [0x2000 + offset, " "]),
63
+ ]);
64
+ class FoldedUnitBuffer {
65
+ codes;
66
+ starts;
67
+ ends;
68
+ flags;
69
+ length = 0;
70
+ constructor(capacity) {
71
+ const size = Math.max(16, capacity);
72
+ this.codes = new Uint16Array(size);
73
+ this.starts = new Int32Array(size);
74
+ this.ends = new Int32Array(size);
75
+ this.flags = new Uint8Array(size);
76
+ }
77
+ push(code, start, end, flags) {
78
+ if (this.length === this.codes.length)
79
+ this.grow();
80
+ this.codes[this.length] = code;
81
+ this.starts[this.length] = start;
82
+ this.ends[this.length] = end;
83
+ this.flags[this.length] = flags;
84
+ this.length += 1;
85
+ }
86
+ grow() {
87
+ const size = this.codes.length * 2;
88
+ const codes = new Uint16Array(size);
89
+ codes.set(this.codes);
90
+ this.codes = codes;
91
+ const starts = new Int32Array(size);
92
+ starts.set(this.starts);
93
+ this.starts = starts;
94
+ const ends = new Int32Array(size);
95
+ ends.set(this.ends);
96
+ this.ends = ends;
97
+ const flags = new Uint8Array(size);
98
+ flags.set(this.flags);
99
+ this.flags = flags;
100
+ }
101
+ dropTrailingBlanks() {
102
+ const keep = new Uint8Array(this.length).fill(1);
103
+ let run = -1;
104
+ for (let index = 0; index < this.length; index += 1) {
105
+ const flags = this.flags[index] ?? 0;
106
+ if (flags & BLANK) {
107
+ if (run === -1)
108
+ run = index;
109
+ continue;
110
+ }
111
+ if (this.codes[index] === LF && run !== -1)
112
+ keep.fill(0, run, index);
113
+ run = -1;
114
+ }
115
+ const trimmedEnd = run !== -1;
116
+ if (trimmedEnd)
117
+ keep.fill(0, run, this.length);
118
+ let kept = 0;
119
+ for (const flag of keep)
120
+ kept += flag;
121
+ const codes = new Uint16Array(kept);
122
+ const start = new Int32Array(kept);
123
+ const end = new Int32Array(kept);
124
+ const edges = new Uint8Array(kept);
125
+ let at = 0;
126
+ for (let index = 0; index < this.length; index += 1) {
127
+ if (keep[index] === 0)
128
+ continue;
129
+ codes[at] = this.codes[index] ?? 0;
130
+ start[at] = this.starts[index] ?? 0;
131
+ end[at] = this.ends[index] ?? 0;
132
+ edges[at] = (this.flags[index] ?? 0) & (FIRST | LAST);
133
+ at += 1;
134
+ }
135
+ return { text: fromCodes(codes), sourceStart: start, sourceEnd: end, edges, trimmedEnd };
136
+ }
137
+ }
138
+ function fromCodes(codes) {
139
+ const parts = [];
140
+ for (let index = 0; index < codes.length; index += 8_192) {
141
+ parts.push(String.fromCharCode(...codes.subarray(index, index + 8_192)));
142
+ }
143
+ return parts.join("");
144
+ }
@@ -0,0 +1,3 @@
1
+ import type { Matcher } from "../contract/matcher.ts";
2
+ /** Matches lines that differ only in their common indentation, and re-indents the new text to fit. */
3
+ export declare function indentationMatcher(): Matcher;
@@ -0,0 +1,81 @@
1
+ import { LineTable, memoizeLast, needleLines } from "./lines.js";
2
+ /** Matches lines that differ only in their common indentation, and re-indents the new text to fit. */
3
+ export function indentationMatcher() {
4
+ const table = memoizeLast((text) => new LineTable(text));
5
+ return Object.freeze({
6
+ id: "indentation",
7
+ fuzzy: true,
8
+ describe: "a different indentation level",
9
+ find(haystack, needle, ctx) {
10
+ const { lines, withBreak } = needleLines(needle);
11
+ if (lines.every(isBlank))
12
+ return [];
13
+ const indent = commonIndent(lines);
14
+ const wanted = lines.map((line) => (isBlank(line) ? "" : line.slice(indent.length)));
15
+ const wantedTrimmed = lines.map((line) => line.trim());
16
+ const hay = table(haystack);
17
+ const trimmed = hay.trimmed;
18
+ const ranges = [];
19
+ const last = hay.count - lines.length;
20
+ for (let first = hay.firstLineAtOrAfter(ctx.from); first <= last; first += 1) {
21
+ if (ranges.length >= ctx.maxMatches)
22
+ break;
23
+ if (!wantedTrimmed.every((line, offset) => trimmed[first + offset] === line))
24
+ continue;
25
+ const window = wanted.map((_, offset) => hay.line(first + offset));
26
+ const shift = commonIndent(window);
27
+ const same = window.every((line, offset) => isBlank(line) ? wanted[offset] === "" : line.slice(shift.length) === wanted[offset]);
28
+ if (!same)
29
+ continue;
30
+ const range = hay.range(first, first + lines.length - 1, withBreak);
31
+ if (range === null)
32
+ continue;
33
+ ranges.push(range);
34
+ first += lines.length - 1;
35
+ }
36
+ return ranges;
37
+ },
38
+ adapt(newText, hit) {
39
+ const from = commonIndent(needleLines(hit.needle).lines);
40
+ const to = commonIndent(needleLines(hit.haystack.slice(hit.range.start, hit.range.end)).lines);
41
+ if (from === to)
42
+ return newText;
43
+ return newText
44
+ .split("\n")
45
+ .map((line) => reindent(line, from, to))
46
+ .join("\n");
47
+ },
48
+ });
49
+ }
50
+ function isBlank(line) {
51
+ return line.trim() === "";
52
+ }
53
+ function commonIndent(lines) {
54
+ let common = null;
55
+ for (const line of lines) {
56
+ if (isBlank(line))
57
+ continue;
58
+ const indent = /^[ \t]*/u.exec(line)?.[0] ?? "";
59
+ if (common === null) {
60
+ common = indent;
61
+ continue;
62
+ }
63
+ let length = 0;
64
+ while (length < common.length && length < indent.length && common[length] === indent[length]) {
65
+ length += 1;
66
+ }
67
+ common = common.slice(0, length);
68
+ }
69
+ return common ?? "";
70
+ }
71
+ function reindent(line, from, to) {
72
+ if (isBlank(line))
73
+ return line;
74
+ if (line.startsWith(from))
75
+ return to + line.slice(from.length);
76
+ if (to.startsWith(from))
77
+ return to.slice(from.length) + line;
78
+ const extra = from.length - to.length;
79
+ const leading = /^[ \t]*/u.exec(line)?.[0].length ?? 0;
80
+ return extra > 0 ? line.slice(Math.min(extra, leading)) : line;
81
+ }
@@ -0,0 +1,14 @@
1
+ import type { Matcher } from "../contract/matcher.ts";
2
+ export { blockAnchorMatcher } from "./block-anchor.ts";
3
+ export { escapeMatcher } from "./escape.ts";
4
+ export { exactMatcher } from "./exact.ts";
5
+ export { indentationMatcher } from "./indentation.ts";
6
+ export { lineTrimmedMatcher } from "./line-trimmed.ts";
7
+ export { normalizedMatcher } from "./normalized.ts";
8
+ /**
9
+ * The matchers the edit tool tries by default, in order: exact, normalized.
10
+ * escapeMatcher() and the other matchers are opt-in through `matchers`.
11
+ */
12
+ export declare function defaultEditMatchers(): readonly Matcher[];
13
+ /** The matchers the patch tool tries by default, in order: exact, normalized, line-trimmed. */
14
+ export declare function defaultPatchMatchers(): readonly Matcher[];
@@ -0,0 +1,20 @@
1
+ import { exactMatcher } from "./exact.js";
2
+ import { lineTrimmedMatcher } from "./line-trimmed.js";
3
+ import { normalizedMatcher } from "./normalized.js";
4
+ export { blockAnchorMatcher } from "./block-anchor.js";
5
+ export { escapeMatcher } from "./escape.js";
6
+ export { exactMatcher } from "./exact.js";
7
+ export { indentationMatcher } from "./indentation.js";
8
+ export { lineTrimmedMatcher } from "./line-trimmed.js";
9
+ export { normalizedMatcher } from "./normalized.js";
10
+ /**
11
+ * The matchers the edit tool tries by default, in order: exact, normalized.
12
+ * escapeMatcher() and the other matchers are opt-in through `matchers`.
13
+ */
14
+ export function defaultEditMatchers() {
15
+ return Object.freeze([exactMatcher(), normalizedMatcher()]);
16
+ }
17
+ /** The matchers the patch tool tries by default, in order: exact, normalized, line-trimmed. */
18
+ export function defaultPatchMatchers() {
19
+ return Object.freeze([exactMatcher(), normalizedMatcher(), lineTrimmedMatcher()]);
20
+ }
@@ -0,0 +1,3 @@
1
+ import type { Matcher } from "../contract/matcher.ts";
2
+ /** Matches whole lines, ignoring leading and trailing whitespace on each line. */
3
+ export declare function lineTrimmedMatcher(): Matcher;
@@ -0,0 +1,32 @@
1
+ import { LineTable, memoizeLast, needleLines } from "./lines.js";
2
+ /** Matches whole lines, ignoring leading and trailing whitespace on each line. */
3
+ export function lineTrimmedMatcher() {
4
+ const table = memoizeLast((text) => new LineTable(text));
5
+ return Object.freeze({
6
+ id: "line-trimmed",
7
+ fuzzy: true,
8
+ describe: "leading and trailing whitespace on each line",
9
+ find(haystack, needle, ctx) {
10
+ const { lines, withBreak } = needleLines(needle);
11
+ const wanted = lines.map((line) => line.trim());
12
+ if (wanted.every((line) => line === ""))
13
+ return [];
14
+ const hay = table(haystack);
15
+ const trimmed = hay.trimmed;
16
+ const ranges = [];
17
+ const last = hay.count - wanted.length;
18
+ for (let first = hay.firstLineAtOrAfter(ctx.from); first <= last; first += 1) {
19
+ if (ranges.length >= ctx.maxMatches)
20
+ break;
21
+ if (!wanted.every((line, offset) => trimmed[first + offset] === line))
22
+ continue;
23
+ const range = hay.range(first, first + wanted.length - 1, withBreak);
24
+ if (range === null)
25
+ continue;
26
+ ranges.push(range);
27
+ first += wanted.length - 1;
28
+ }
29
+ return ranges;
30
+ },
31
+ });
32
+ }
@@ -0,0 +1,24 @@
1
+ import type { MatchContext } from "../contract/matcher.ts";
2
+ export declare function atLineStart(text: string, index: number): boolean;
3
+ export declare function atLineEnd(text: string, end: number): boolean;
4
+ export declare function fitsMode(text: string, start: number, end: number, ctx: MatchContext): boolean;
5
+ export declare class LineTable {
6
+ readonly text: string;
7
+ readonly starts: number[];
8
+ readonly ends: number[];
9
+ private trimmedLines;
10
+ constructor(text: string);
11
+ get count(): number;
12
+ line(index: number): string;
13
+ get trimmed(): readonly string[];
14
+ firstLineAtOrAfter(from: number): number;
15
+ range(first: number, last: number, withBreak: boolean): {
16
+ start: number;
17
+ end: number;
18
+ } | null;
19
+ }
20
+ export declare function needleLines(needle: string): {
21
+ lines: string[];
22
+ withBreak: boolean;
23
+ };
24
+ export declare function memoizeLast<T>(build: (text: string) => T): (text: string) => T;
@@ -0,0 +1,74 @@
1
+ const LF = 10;
2
+ export function atLineStart(text, index) {
3
+ return index === 0 || text.charCodeAt(index - 1) === LF;
4
+ }
5
+ export function atLineEnd(text, end) {
6
+ return end === text.length || text.charCodeAt(end) === LF || text.charCodeAt(end - 1) === LF;
7
+ }
8
+ export function fitsMode(text, start, end, ctx) {
9
+ return ctx.mode === "text" || (atLineStart(text, start) && atLineEnd(text, end));
10
+ }
11
+ export class LineTable {
12
+ text;
13
+ starts = [];
14
+ ends = [];
15
+ trimmedLines = null;
16
+ constructor(text) {
17
+ this.text = text;
18
+ let start = 0;
19
+ while (start < text.length) {
20
+ const newline = text.indexOf("\n", start);
21
+ const end = newline === -1 ? text.length : newline;
22
+ this.starts.push(start);
23
+ this.ends.push(end);
24
+ start = end + 1;
25
+ }
26
+ }
27
+ get count() {
28
+ return this.starts.length;
29
+ }
30
+ line(index) {
31
+ return this.text.slice(this.starts[index] ?? 0, this.ends[index] ?? 0);
32
+ }
33
+ get trimmed() {
34
+ this.trimmedLines ??= this.starts.map((_, index) => this.line(index).trim());
35
+ return this.trimmedLines;
36
+ }
37
+ firstLineAtOrAfter(from) {
38
+ let low = 0;
39
+ let high = this.starts.length;
40
+ while (low < high) {
41
+ const middle = (low + high) >>> 1;
42
+ if ((this.starts[middle] ?? 0) < from)
43
+ low = middle + 1;
44
+ else
45
+ high = middle;
46
+ }
47
+ return low;
48
+ }
49
+ range(first, last, withBreak) {
50
+ const start = this.starts[first] ?? 0;
51
+ const end = this.ends[last] ?? 0;
52
+ if (!withBreak)
53
+ return { start, end };
54
+ return end < this.text.length ? { start, end: end + 1 } : null;
55
+ }
56
+ }
57
+ export function needleLines(needle) {
58
+ const lines = needle.split("\n");
59
+ const withBreak = lines.length > 1 && lines.at(-1) === "";
60
+ if (withBreak)
61
+ lines.pop();
62
+ return { lines, withBreak };
63
+ }
64
+ export function memoizeLast(build) {
65
+ let key = null;
66
+ let value = null;
67
+ return (text) => {
68
+ if (key !== text || value === null) {
69
+ value = build(text);
70
+ key = text;
71
+ }
72
+ return value;
73
+ };
74
+ }
@@ -0,0 +1,7 @@
1
+ import type { Matcher } from "../contract/matcher.ts";
2
+ /**
3
+ * Matches while ignoring trailing whitespace, Unicode normalization, curly
4
+ * quotes, dashes, and special spaces. A match that starts or ends inside one
5
+ * folded character is refused.
6
+ */
7
+ export declare function normalizedMatcher(): Matcher;
@@ -0,0 +1,69 @@
1
+ import { FIRST, LAST, fold } from "./fold.js";
2
+ import { memoizeLast } from "./lines.js";
3
+ const LF = 10;
4
+ /**
5
+ * Matches while ignoring trailing whitespace, Unicode normalization, curly
6
+ * quotes, dashes, and special spaces. A match that starts or ends inside one
7
+ * folded character is refused.
8
+ */
9
+ export function normalizedMatcher() {
10
+ const folded = memoizeLast(fold);
11
+ return Object.freeze({
12
+ id: "normalized",
13
+ fuzzy: true,
14
+ describe: "trailing whitespace, Unicode normalization, curly quotes, dashes, and special spaces",
15
+ find: (haystack, needle, ctx) => findFolded(haystack, folded(haystack), fold(needle), ctx),
16
+ });
17
+ }
18
+ function findFolded(haystack, hay, needle, ctx) {
19
+ const ranges = [];
20
+ const text = hay.text;
21
+ if (needle.text === "")
22
+ return ranges;
23
+ let from = firstUnitAtOrAfter(hay, ctx.from);
24
+ while (ranges.length < ctx.maxMatches) {
25
+ const first = text.indexOf(needle.text, from);
26
+ if (first === -1)
27
+ break;
28
+ const end = first + needle.text.length;
29
+ const range = originalRange(haystack, hay, first, end, needle.trimmedEnd, ctx);
30
+ if (range === null || range.start < ctx.from) {
31
+ from = first + 1;
32
+ continue;
33
+ }
34
+ ranges.push(range);
35
+ from = end;
36
+ }
37
+ return ranges;
38
+ }
39
+ function originalRange(haystack, hay, first, end, trimmedEnd, ctx) {
40
+ const text = hay.text;
41
+ const lineEnd = end === text.length || text.charCodeAt(end) === LF;
42
+ if (trimmedEnd && !lineEnd)
43
+ return null;
44
+ let start = hay.sourceStart[first] ?? 0;
45
+ let stop = hay.sourceEnd[end - 1] ?? 0;
46
+ if (ctx.mode === "lines") {
47
+ const endsLine = lineEnd || text.charCodeAt(end - 1) === LF;
48
+ if (!(first === 0 || text.charCodeAt(first - 1) === LF) || !endsLine)
49
+ return null;
50
+ start = first === 0 ? 0 : (hay.sourceEnd[first - 1] ?? 0);
51
+ }
52
+ if ((ctx.mode === "lines" || trimmedEnd) && text.charCodeAt(end - 1) !== LF) {
53
+ stop = end === text.length ? haystack.length : (hay.sourceStart[end] ?? 0);
54
+ }
55
+ const inside = !((hay.edges[first] ?? 0) & FIRST) || !((hay.edges[end - 1] ?? 0) & LAST);
56
+ return inside ? { start, end: stop, refused: "boundary" } : { start, end: stop };
57
+ }
58
+ function firstUnitAtOrAfter(hay, from) {
59
+ let low = 0;
60
+ let high = hay.sourceStart.length;
61
+ while (low < high) {
62
+ const middle = (low + high) >>> 1;
63
+ if ((hay.sourceStart[middle] ?? 0) < from)
64
+ low = middle + 1;
65
+ else
66
+ high = middle;
67
+ }
68
+ return low;
69
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * The Codex apply_patch grammar in Lark (codex-rs/core, apply_patch.lark,
3
+ * Apache-2.0). Hosts with grammar tools can use it to constrain patch text.
4
+ */
5
+ export declare const CODEX_PATCH_GRAMMAR = "start: begin_patch hunk+ end_patch\nbegin_patch: \"*** Begin Patch\" LF\nend_patch: \"*** End Patch\" LF?\n\nhunk: add_hunk | delete_hunk | update_hunk\nadd_hunk: \"*** Add File: \" filename LF add_line+\ndelete_hunk: \"*** Delete File: \" filename LF\nupdate_hunk: \"*** Update File: \" filename LF change_move? change?\n\nfilename: /(.+)/\nadd_line: \"+\" /(.*)/ LF -> line\n\nchange_move: \"*** Move to: \" filename LF\nchange: (change_context | change_line)+ eof_line?\nchange_context: (\"@@\" | \"@@ \" /(.+)/) LF\nchange_line: (\"+\" | \"-\" | \" \") /(.*)/ LF\neof_line: \"*** End of File\" LF\n\n%import common.LF\n";
@@ -0,0 +1,24 @@
1
+ /**
2
+ * The Codex apply_patch grammar in Lark (codex-rs/core, apply_patch.lark,
3
+ * Apache-2.0). Hosts with grammar tools can use it to constrain patch text.
4
+ */
5
+ export const CODEX_PATCH_GRAMMAR = `start: begin_patch hunk+ end_patch
6
+ begin_patch: "*** Begin Patch" LF
7
+ end_patch: "*** End Patch" LF?
8
+
9
+ hunk: add_hunk | delete_hunk | update_hunk
10
+ add_hunk: "*** Add File: " filename LF add_line+
11
+ delete_hunk: "*** Delete File: " filename LF
12
+ update_hunk: "*** Update File: " filename LF change_move? change?
13
+
14
+ filename: /(.+)/
15
+ add_line: "+" /(.*)/ LF -> line
16
+
17
+ change_move: "*** Move to: " filename LF
18
+ change: (change_context | change_line)+ eof_line?
19
+ change_context: ("@@" | "@@ " /(.+)/) LF
20
+ change_line: ("+" | "-" | " ") /(.*)/ LF
21
+ eof_line: "*** End of File" LF
22
+
23
+ %import common.LF
24
+ `;
@@ -0,0 +1,4 @@
1
+ /** The `./patch` subpath: the Codex patch parser, its Lark grammar, and the patch types. */
2
+ export { CODEX_PATCH_GRAMMAR } from "./grammar.ts";
3
+ export { codexPatchParser, parsePatch } from "./parse.ts";
4
+ export type { PatchHunk, PatchLine, PatchOperation, PatchParseOutcome, PatchParser, PatchPlan, } from "../contract/patch.ts";
@@ -0,0 +1,3 @@
1
+ /** The `./patch` subpath: the Codex patch parser, its Lark grammar, and the patch types. */
2
+ export { CODEX_PATCH_GRAMMAR } from "./grammar.js";
3
+ export { codexPatchParser, parsePatch } from "./parse.js";
@@ -0,0 +1,11 @@
1
+ import type { PatchParseOutcome, PatchParser } from "../contract/patch.ts";
2
+ /**
3
+ * Parses the Codex patch format. Lenient at the edges: CRLF line breaks,
4
+ * surrounding blank lines, one fenced block, and one heredoc wrapper
5
+ * (`apply_patch <<'EOF'`) are stripped, and a missing final newline is fine.
6
+ * Line numbers in errors and in the plan count lines of the text as given.
7
+ * Never throws.
8
+ */
9
+ export declare function parsePatch(text: string): PatchParseOutcome;
10
+ /** The default patch parser: parsePatch as a PatchParser. */
11
+ export declare function codexPatchParser(): PatchParser;