@criogaid/pi-hashline-edit 0.3.0 → 0.4.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.
- package/README.md +59 -50
- package/package.json +8 -3
- package/src/core/apply.test.ts +366 -310
- package/src/core/apply.ts +269 -202
- package/src/core/hash.test.ts +61 -23
- package/src/core/hash.ts +45 -16
- package/src/core/lines.test.ts +46 -27
- package/src/core/lines.ts +36 -28
- package/src/core/ranges.test.ts +73 -19
- package/src/core/ranges.ts +15 -15
- package/src/core/text.test.ts +3 -3
- package/src/core/text.ts +28 -9
- package/src/core/types.ts +54 -36
- package/src/index.ts +22 -16
- package/src/integration/grep-rg.test.ts +834 -219
- package/src/integration/large-text.test.ts +62 -0
- package/src/integration/replace-package.test.ts +37 -0
- package/src/pi/action-fusion.test.ts +535 -206
- package/src/pi/action-fusion.ts +469 -244
- package/src/pi/anchor-format.test.ts +30 -19
- package/src/pi/anchor-format.ts +27 -14
- package/src/pi/budgets.ts +20 -0
- package/src/pi/config.test.ts +60 -30
- package/src/pi/config.ts +45 -32
- package/src/pi/directory-sync.test.ts +55 -0
- package/src/pi/e2e-scenarios.test.ts +359 -0
- package/src/pi/edit-tool.ts +426 -262
- package/src/pi/error-text.ts +21 -0
- package/src/pi/execute.test.ts +1594 -694
- package/src/pi/failure-context.test.ts +105 -83
- package/src/pi/failure-context.ts +83 -71
- package/src/pi/failure-path.test.ts +171 -64
- package/src/pi/file-commit.test.ts +446 -233
- package/src/pi/file-commit.ts +347 -203
- package/src/pi/fusion-card.test.ts +118 -83
- package/src/pi/fusion-card.ts +120 -85
- package/src/pi/grep-output.ts +208 -0
- package/src/pi/grep-render.ts +58 -0
- package/src/pi/grep-scope.ts +152 -0
- package/src/pi/grep-search.ts +217 -0
- package/src/pi/grep-tool.ts +191 -766
- package/src/pi/grep.test.ts +562 -150
- package/src/pi/mutation-result.test.ts +37 -0
- package/src/pi/mutation-result.ts +107 -59
- package/src/pi/mutation-runner.ts +168 -0
- package/src/pi/path.ts +20 -12
- package/src/pi/pi.test.ts +95 -52
- package/src/pi/read-tool.ts +219 -130
- package/src/pi/registration.test.ts +274 -113
- package/src/pi/render.test.ts +218 -83
- package/src/pi/render.ts +152 -95
- package/src/pi/replace-apply.ts +109 -0
- package/src/pi/replace-tool.ts +280 -240
- package/src/pi/replace-worker.mjs +4 -0
- package/src/pi/replace-worker.ts +10 -0
- package/src/pi/replace.test.ts +773 -389
- package/src/pi/result-safety.test.ts +779 -342
- package/src/pi/rg-line-filter.test.ts +64 -36
- package/src/pi/rg-line-filter.ts +40 -124
- package/src/pi/rg-line-ranges.test.ts +57 -31
- package/src/pi/rg-line-ranges.ts +44 -82
- package/src/pi/rg-text-view.ts +106 -36
- package/src/pi/schema.ts +27 -0
- package/src/pi/state.ts +7 -7
- package/src/pi/text-stream.test.ts +118 -0
- package/src/pi/text-stream.ts +136 -0
- package/src/pi/tool-call.testing.ts +37 -0
- package/src/pi/write-tool.ts +115 -52
- package/src/pi/write.test.ts +248 -96
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { escapeRegex } from "../core/text.ts";
|
|
2
|
+
import {
|
|
3
|
+
createLfTextView,
|
|
4
|
+
detectLineEnding,
|
|
5
|
+
normalizeLineEndings,
|
|
6
|
+
restoreLineEndings,
|
|
7
|
+
} from "../core/lines.ts";
|
|
8
|
+
import { findSortedRangeConflict } from "../core/ranges.ts";
|
|
9
|
+
import { errorMessage } from "./error-text.ts";
|
|
10
|
+
|
|
11
|
+
export interface Replacement {
|
|
12
|
+
find: string;
|
|
13
|
+
replace: string;
|
|
14
|
+
regex?: boolean;
|
|
15
|
+
flags?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Both matcher modes operate on the shared LF view. Flag characters are validated by the tool
|
|
20
|
+
* schema; replace's prepareArguments also compiles regex rules here before any file is read.
|
|
21
|
+
*/
|
|
22
|
+
export function buildRegex(find: string, isRegex: boolean, flagsRaw: string | undefined): RegExp {
|
|
23
|
+
const set = new Set((flagsRaw ?? "").split(""));
|
|
24
|
+
set.add("g");
|
|
25
|
+
const flagStr = [...set].join("");
|
|
26
|
+
const logical = normalizeLineEndings(find);
|
|
27
|
+
const source = isRegex ? logical : escapeRegex(logical);
|
|
28
|
+
try {
|
|
29
|
+
return new RegExp(source, flagStr);
|
|
30
|
+
} catch (e) {
|
|
31
|
+
const msg = errorMessage(e);
|
|
32
|
+
throw new Error(`invalid regex /${source}/${flagStr}: ${msg}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Expand JS replacement tokens against the original match, including prefix/suffix context. */
|
|
37
|
+
function expandReplacement(template: string, match: RegExpMatchArray, source: string): string {
|
|
38
|
+
// Without named captures, $<...> is ordinary text and may contain other $ tokens.
|
|
39
|
+
const tokens =
|
|
40
|
+
match.groups === undefined ? /\$(\$|&|`|'|\d{1,2})/g : /\$(\$|&|`|'|<[^>]*>|\d{1,2})/g;
|
|
41
|
+
return template.replace(tokens, (token, key: string) => {
|
|
42
|
+
if (key === "$") return "$";
|
|
43
|
+
if (key === "&") return match[0];
|
|
44
|
+
if (key === "`") return source.slice(0, match.index);
|
|
45
|
+
if (key === "'") return source.slice(match.index! + match[0].length);
|
|
46
|
+
if (key.startsWith("<"))
|
|
47
|
+
return match.groups === undefined ? token : (match.groups[key.slice(1, -1)] ?? "");
|
|
48
|
+
const index = Number(key);
|
|
49
|
+
if (index > 0 && index < match.length) return match[index] ?? "";
|
|
50
|
+
// $12 falls back to capture 1 plus literal 2 when capture 12 does not exist.
|
|
51
|
+
const first = Number(key[0]);
|
|
52
|
+
if (key.length === 2 && first > 0 && first < match.length) return (match[first] ?? "") + key[1];
|
|
53
|
+
return token;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function applyReplacements(
|
|
58
|
+
source: string,
|
|
59
|
+
rules: readonly Replacement[],
|
|
60
|
+
): { text: string; count: number } {
|
|
61
|
+
const changes: { start: number; end: number; text: string; rule: number }[] = [];
|
|
62
|
+
const view = createLfTextView(source);
|
|
63
|
+
const fallbackEnding = detectLineEnding(source) === "crlf" ? "\r\n" : "\n";
|
|
64
|
+
for (const [index, rule] of rules.entries()) {
|
|
65
|
+
try {
|
|
66
|
+
const regex = buildRegex(rule.find, rule.regex === true, rule.flags);
|
|
67
|
+
let count = 0;
|
|
68
|
+
const replacement = normalizeLineEndings(rule.replace);
|
|
69
|
+
for (const match of view.text.matchAll(regex)) {
|
|
70
|
+
count++;
|
|
71
|
+
const start = view.sourceOffset(match.index!);
|
|
72
|
+
const end = view.sourceOffset(match.index! + match[0].length);
|
|
73
|
+
changes.push({
|
|
74
|
+
start,
|
|
75
|
+
end,
|
|
76
|
+
rule: index,
|
|
77
|
+
text: restoreLineEndings(
|
|
78
|
+
rule.regex ? expandReplacement(replacement, match, view.text) : replacement,
|
|
79
|
+
source.slice(start, end),
|
|
80
|
+
fallbackEnding,
|
|
81
|
+
),
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (count === 0)
|
|
85
|
+
throw new Error(
|
|
86
|
+
`no matches for ${rule.regex ? `/${rule.find}/` : JSON.stringify(rule.find)}. Verify the target text with read or grep; check case sensitivity or regex flags if applicable.`,
|
|
87
|
+
);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
throw new Error(`rule ${index}: ${errorMessage(error)}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
changes.sort((a, b) => a.start - b.start || a.end - b.end);
|
|
93
|
+
const conflict = findSortedRangeConflict(changes.map((change) => [change.start, change.end]));
|
|
94
|
+
if (conflict !== undefined) {
|
|
95
|
+
const previous = changes[conflict - 1];
|
|
96
|
+
const current = changes[conflict];
|
|
97
|
+
throw new Error(
|
|
98
|
+
`rules ${previous.rule} and ${current.rule} overlap at offset ${current.start}; no replacements applied`,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
const parts: string[] = [];
|
|
102
|
+
let cursor = 0;
|
|
103
|
+
for (const change of changes) {
|
|
104
|
+
parts.push(source.slice(cursor, change.start), change.text);
|
|
105
|
+
cursor = change.end;
|
|
106
|
+
}
|
|
107
|
+
parts.push(source.slice(cursor));
|
|
108
|
+
return { text: parts.join(""), count: changes.length };
|
|
109
|
+
}
|