hookloom 1.2.0 → 1.3.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/dist/cli.mjs
CHANGED
|
@@ -14,7 +14,7 @@ import { execFile, spawn } from "node:child_process";
|
|
|
14
14
|
import { createInterface } from "node:readline";
|
|
15
15
|
import { homedir, tmpdir } from "node:os";
|
|
16
16
|
import { once } from "node:events";
|
|
17
|
-
import { n as invocation, r as version, t as claudeUsesPowerShell } from "./command-line-
|
|
17
|
+
import { n as invocation, r as version, t as claudeUsesPowerShell } from "./command-line-CxFCE3W7.mjs";
|
|
18
18
|
// src/examples/catalogue.ts
|
|
19
19
|
const BOTH = ["claude", "codex"];
|
|
20
20
|
const LOCAL_SECONDS = 10;
|
|
@@ -87,6 +87,22 @@ const examples = [
|
|
|
87
87
|
timeout: LOCAL_SECONDS
|
|
88
88
|
}]
|
|
89
89
|
},
|
|
90
|
+
{
|
|
91
|
+
name: "dewrap",
|
|
92
|
+
summary: "After an edit, join hard-wrapped Markdown paragraphs and comments into single lines. Changes your files.",
|
|
93
|
+
needs: [],
|
|
94
|
+
registrations: [{
|
|
95
|
+
event: "PostToolUse",
|
|
96
|
+
harnesses: ["claude"],
|
|
97
|
+
matcher: "Edit|Write",
|
|
98
|
+
timeout: LOCAL_SECONDS
|
|
99
|
+
}, {
|
|
100
|
+
event: "PostToolUse",
|
|
101
|
+
harnesses: ["codex"],
|
|
102
|
+
matcher: "apply_patch",
|
|
103
|
+
timeout: LOCAL_SECONDS
|
|
104
|
+
}]
|
|
105
|
+
},
|
|
90
106
|
{
|
|
91
107
|
name: "private-reference-guard",
|
|
92
108
|
summary: "Refuse to name a private GitHub repository in a public one.",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
2
|
import { delimiter, join } from "node:path";
|
|
3
3
|
// src/version.ts
|
|
4
|
-
const version = "1.
|
|
4
|
+
const version = "1.3.0";
|
|
5
5
|
// src/dispatch/command-line.ts
|
|
6
6
|
/**
|
|
7
7
|
* The program and arguments with which each harness runs a command hook on each platform.
|
package/dist/dispatcher.mjs
CHANGED
|
@@ -6,7 +6,7 @@ import { dirname, join, resolve } from "node:path";
|
|
|
6
6
|
import { a as registryPath, n as cacheDirectory, r as claudeDirectory, t as atomicWrite } from "./files-CBn-xAo2.mjs";
|
|
7
7
|
import { createHash } from "node:crypto";
|
|
8
8
|
import { a as stringField, i as parsePayload, n as isRecord, o as toolInput, r as namedField } from "./shell-4WSaabze.mjs";
|
|
9
|
-
import { n as invocation, r as version } from "./command-line-
|
|
9
|
+
import { n as invocation, r as version } from "./command-line-CxFCE3W7.mjs";
|
|
10
10
|
import { a as sessionKey, i as removeOlderThan, n as readRecord, o as writeRecord, r as records, t as digest } from "./state-Db3zojdV.mjs";
|
|
11
11
|
import { spawn } from "node:child_process";
|
|
12
12
|
import { t as withoutHarnessMessages } from "./prompt-text-A_jEvJei.mjs";
|
|
@@ -0,0 +1,973 @@
|
|
|
1
|
+
import { basename, resolve, sep } from "node:path";
|
|
2
|
+
import { readFileSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { a as stringField, o as toolInput } from "../shell-4WSaabze.mjs";
|
|
4
|
+
import { a as shellCommand, i as main } from "../hook-DIS0wk_5.mjs";
|
|
5
|
+
// src/examples/dewrap/lines.ts
|
|
6
|
+
/**
|
|
7
|
+
* Recognising what a line is, shared by the prose, comment and docstring rejoiners.
|
|
8
|
+
*/
|
|
9
|
+
const FENCE = /^(\s*)(```+|~~~+)/u;
|
|
10
|
+
const HEADING = /^\s{0,3}#{1,6}(\s|$)/u;
|
|
11
|
+
const RULE = /^\s{0,3}([-*_=])(\s*\1){2,}\s*$/u;
|
|
12
|
+
const LIST_ITEM = /^(\s*)([-*+]|\d+[.)])\s+/u;
|
|
13
|
+
/**
|
|
14
|
+
* A footnote definition (`[^1]: text`) or a reference link definition (`[label]: url`).
|
|
15
|
+
*/
|
|
16
|
+
const LABEL_DEFINITION = /^\[\^?[^\]]+\]:\s*/u;
|
|
17
|
+
const SPEAKER_LABEL = /^\*\*[^*\n]{1,60}:\*\*/u;
|
|
18
|
+
const BLOCKQUOTE = /^\s{0,3}>/u;
|
|
19
|
+
const HTML_TAG = /^\s*<\/?[a-zA-Z!]/u;
|
|
20
|
+
const HTML_COMMENT_CLOSE = /^\s*-->/u;
|
|
21
|
+
const MATH_FENCE = /^\s*(\$\$|\\\[|\\\]|\\(?:begin|end)\{(?:align|alignat|equation|gather|multline|eqnarray|cases)\*?\})\s*$/u;
|
|
22
|
+
/**
|
|
23
|
+
* Two or more trailing spaces, or a backslash, force a line break inside a paragraph, which has to survive.
|
|
24
|
+
*/
|
|
25
|
+
const HARD_BREAK = /(?: {2,}|\\)$/u;
|
|
26
|
+
const TAB_WIDTH = 4;
|
|
27
|
+
/** Whether a line stands alone and never joins its neighbours. */
|
|
28
|
+
function isStructural(line) {
|
|
29
|
+
return line.trim() === "" || HEADING.test(line) || RULE.test(line) || BLOCKQUOTE.test(line) || HTML_TAG.test(line) || HTML_COMMENT_CLOSE.test(line) || line.includes("|");
|
|
30
|
+
}
|
|
31
|
+
/** Whether a line begins an entry that may continue onto later lines but never joins the entry before it. */
|
|
32
|
+
function startsEntry(line) {
|
|
33
|
+
return LIST_ITEM.test(line) || LABEL_DEFINITION.test(line) || SPEAKER_LABEL.test(line);
|
|
34
|
+
}
|
|
35
|
+
function isFenceMarker(line) {
|
|
36
|
+
return FENCE.test(line) || MATH_FENCE.test(line);
|
|
37
|
+
}
|
|
38
|
+
/** Follows fenced regions line by line. A code fence is closed only by a run of the same character at least as long; a math fence opens only outside a code fence. */
|
|
39
|
+
var Fences = class {
|
|
40
|
+
code;
|
|
41
|
+
math = false;
|
|
42
|
+
get inside() {
|
|
43
|
+
return this.math || this.code !== void 0;
|
|
44
|
+
}
|
|
45
|
+
/** Takes the next line and says whether it opened or closed a fence. */
|
|
46
|
+
step(line) {
|
|
47
|
+
if (this.code !== void 0) {
|
|
48
|
+
const run = /^\s*([`~]+)\s*$/u.exec(line)?.[1] ?? "";
|
|
49
|
+
const { character, length } = this.code;
|
|
50
|
+
if (run.length < length || run !== character.repeat(run.length)) return false;
|
|
51
|
+
this.code = void 0;
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
if (this.math) {
|
|
55
|
+
this.math = !MATH_FENCE.test(line);
|
|
56
|
+
return !this.math;
|
|
57
|
+
}
|
|
58
|
+
const run = FENCE.exec(line)?.[2];
|
|
59
|
+
if (run !== void 0) {
|
|
60
|
+
this.code = {
|
|
61
|
+
character: run.charAt(0),
|
|
62
|
+
length: run.length
|
|
63
|
+
};
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
this.math = MATH_FENCE.test(line);
|
|
67
|
+
return this.math;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
/** The columns of a line's leading white space, a tab advancing to the next multiple of four. */
|
|
71
|
+
function indentColumns(line) {
|
|
72
|
+
let columns = 0;
|
|
73
|
+
for (const character of /^[ \t]*/u.exec(line)?.[0] ?? "") columns += character === " " ? TAB_WIDTH - columns % TAB_WIDTH : 1;
|
|
74
|
+
return columns;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Recognises Markdown's indented code: indented four or more columns past the body's base, and not interrupting a paragraph. Inside a list item the threshold moves to the item's content column plus four. Every line outside fences is fed in order.
|
|
78
|
+
*/
|
|
79
|
+
var IndentedCode = class {
|
|
80
|
+
afterBreak = true;
|
|
81
|
+
listColumn;
|
|
82
|
+
/** `text` is the line without indentation, empty for a blank line, and `columns` its indentation relative to the body. */
|
|
83
|
+
feed(text, columns) {
|
|
84
|
+
if (text === "") {
|
|
85
|
+
this.afterBreak = true;
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
if (this.afterBreak && columns >= (this.listColumn ?? 0) + 4) return true;
|
|
89
|
+
const item = LIST_ITEM.exec(text);
|
|
90
|
+
if (item !== null) this.listColumn = columns + item[0].length;
|
|
91
|
+
else if (columns === 0 && this.afterBreak) this.listColumn = void 0;
|
|
92
|
+
this.afterBreak = isStructural(text) || isFenceMarker(text);
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const CODE_END = /(?:[{([\\]|=>)$/u;
|
|
97
|
+
const CODE_START = /^(?:[})\]]|\$ |(?:const|let|var)\s+[\p{L}\p{N}_$]+\s*[=:;,]|def\s+[\p{L}\p{N}_]+\(.*\):$|class\s+[\p{L}\p{N}_]+\(.*\):$|from\s+[\p{L}\p{N}_.]+\s+import\b|import\s+(?:.+\s+from\s+)?["'])/u;
|
|
98
|
+
const CODE_ASSIGNMENT = /^[\p{L}\p{N}_$.[\]'"-]+\s=\s?\S/u;
|
|
99
|
+
const CODE_CALL = /^[\p{L}\p{N}_$.]+\(/u;
|
|
100
|
+
const CODE_CALL_EVIDENCE = /[;,{"'`]|=>|\)$|^[^)]*$/u;
|
|
101
|
+
const SHELL_COMMAND = /^(?:sudo\s+)?(?:git|docker|npm|pnpm|npx|cd|echo|curl|ssh|kubectl|python3?|node|cp|mv|rm|ls|cat|mkdir|chmod|export)\s+(?:-\S|\$\S|[./~]\S*\/|["'])/u;
|
|
102
|
+
/** Whether a comment's text reads as commented-out code. Deliberately broad: a missed join leaves a wrapped line alone, while a wrong one corrupts code. */
|
|
103
|
+
function looksLikeCode(text) {
|
|
104
|
+
if (CODE_END.test(text) || CODE_START.test(text) || CODE_ASSIGNMENT.test(text) || text.includes("=>") || SHELL_COMMAND.test(text)) return true;
|
|
105
|
+
return CODE_CALL.test(text) && CODE_CALL_EVIDENCE.test(text);
|
|
106
|
+
}
|
|
107
|
+
const LEADER = /^\s*(?:#{1,6}(?=\s|$)|\/\/|--(?=\s|$)|;|%|#|[-*+](?=\s|$)|\d+[.)](?=\s|$)|>)\s*/u;
|
|
108
|
+
/** The words of a text without each line's leading marker: the same before and after a rejoining that lost nothing. */
|
|
109
|
+
function contentWords(text) {
|
|
110
|
+
return text.split("\n").flatMap((line) => line.replace(LEADER, "").split(/\s+/u).filter((word) => word !== ""));
|
|
111
|
+
}
|
|
112
|
+
// src/examples/dewrap/comments.ts
|
|
113
|
+
/**
|
|
114
|
+
* Joins wrapped prose in line comments and in `*`-led block comments.
|
|
115
|
+
*/
|
|
116
|
+
const TAG = /^@[\p{L}\p{N}_]/u;
|
|
117
|
+
const SEPARATOR = /^[-=*_#]{3,}$/u;
|
|
118
|
+
const CLOUD_CONFIG = /^#cloud-config\b/u;
|
|
119
|
+
const SCRIPT_METADATA_FENCE = /^#\s*\/\/\//u;
|
|
120
|
+
const DIRECTIVE = /^[A-Z][A-Z0-9_]+\s+(?:-{1,2}\S|\$)/u;
|
|
121
|
+
const BLOCK_OPEN = /^(\s*)(\/\*\*?)(?:\s+(\S.*))?$/u;
|
|
122
|
+
const BLOCK_CLOSE = /^\s*\*\/\s*$/u;
|
|
123
|
+
const STAR_LINE = /^(\s*)\*(\s?)(.*)$/u;
|
|
124
|
+
const starred = (indent, content) => content === "" ? `${indent}*` : `${indent}* ${content}`;
|
|
125
|
+
function endsRun(content) {
|
|
126
|
+
return isStructural(content) || SEPARATOR.test(content) || TAG.test(content) || startsEntry(content) || isFenceMarker(content) || looksLikeCode(content);
|
|
127
|
+
}
|
|
128
|
+
function rejoinBody(body) {
|
|
129
|
+
const lines = [];
|
|
130
|
+
let changed = 0;
|
|
131
|
+
const fences = new Fences();
|
|
132
|
+
const code = new IndentedCode();
|
|
133
|
+
let example = false;
|
|
134
|
+
for (let index = 0; index < body.length;) {
|
|
135
|
+
const { indent, content, raw } = body[index] ?? {
|
|
136
|
+
indent: "",
|
|
137
|
+
content: "",
|
|
138
|
+
raw: ""
|
|
139
|
+
};
|
|
140
|
+
index += 1;
|
|
141
|
+
if (content.startsWith("@example")) example = true;
|
|
142
|
+
else if (example && TAG.test(content)) example = false;
|
|
143
|
+
if (example) lines.push(starred(indent, raw));
|
|
144
|
+
else if (fences.step(content)) {
|
|
145
|
+
code.feed(content, indentColumns(raw));
|
|
146
|
+
lines.push(starred(indent, content));
|
|
147
|
+
} else if (fences.inside || isStructural(content) || SEPARATOR.test(content)) {
|
|
148
|
+
if (!fences.inside) code.feed(content, indentColumns(raw));
|
|
149
|
+
lines.push(starred(indent, content));
|
|
150
|
+
} else if (code.feed(content, indentColumns(raw)) || looksLikeCode(content)) lines.push(starred(indent, raw));
|
|
151
|
+
else {
|
|
152
|
+
const block = [content];
|
|
153
|
+
for (let next = body[index]; next?.indent === indent && !endsRun(next.content); next = body[index]) {
|
|
154
|
+
block.push(next.content);
|
|
155
|
+
index += 1;
|
|
156
|
+
}
|
|
157
|
+
lines.push(starred(indent, block.length > 1 ? block.join(" ") : content));
|
|
158
|
+
changed += block.length - 1;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
lines,
|
|
163
|
+
changed
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function blockBody(lines, start) {
|
|
167
|
+
const body = [];
|
|
168
|
+
for (let index = start; index < lines.length; index += 1) {
|
|
169
|
+
const line = lines[index] ?? "";
|
|
170
|
+
if (BLOCK_CLOSE.test(line)) return {
|
|
171
|
+
body,
|
|
172
|
+
close: index
|
|
173
|
+
};
|
|
174
|
+
const star = STAR_LINE.exec(line);
|
|
175
|
+
if (star === null) return void 0;
|
|
176
|
+
const [, indent = "", , text = ""] = star;
|
|
177
|
+
body.push({
|
|
178
|
+
indent,
|
|
179
|
+
content: text.trim(),
|
|
180
|
+
raw: text.trimEnd()
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function rejoinBlockComments(text, verbatim = /* @__PURE__ */ new Set()) {
|
|
185
|
+
const lines = text.split("\n");
|
|
186
|
+
const output = [];
|
|
187
|
+
let changed = 0;
|
|
188
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
189
|
+
const line = lines[index] ?? "";
|
|
190
|
+
const open = BLOCK_OPEN.exec(line);
|
|
191
|
+
const block = open === null || line.includes("*/") || verbatim.has(index) ? void 0 : blockBody(lines, index + 1);
|
|
192
|
+
if (open === null || block === void 0) {
|
|
193
|
+
output.push(line);
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
const [, indent = "", marker = "", first = ""] = open;
|
|
197
|
+
const opening = first.trim();
|
|
198
|
+
if (opening === "") {
|
|
199
|
+
const merged = rejoinBody(block.body);
|
|
200
|
+
output.push(line, ...merged.lines);
|
|
201
|
+
changed += merged.changed;
|
|
202
|
+
} else {
|
|
203
|
+
const merged = rejoinBody([{
|
|
204
|
+
indent: block.body[0]?.indent ?? `${indent} `,
|
|
205
|
+
content: opening,
|
|
206
|
+
raw: opening
|
|
207
|
+
}, ...block.body]);
|
|
208
|
+
const [head = "", ...rest] = merged.lines;
|
|
209
|
+
const content = STAR_LINE.exec(head)?.[3] ?? head;
|
|
210
|
+
output.push(content === "" ? `${indent}${marker}` : `${indent}${marker} ${content}`, ...rest);
|
|
211
|
+
changed += merged.changed + 1;
|
|
212
|
+
}
|
|
213
|
+
output.push(lines[block.close] ?? "");
|
|
214
|
+
index = block.close;
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
text: output.join("\n"),
|
|
218
|
+
changed
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
function splitMarker(content, prefix, space) {
|
|
222
|
+
const trimmed = content.trim();
|
|
223
|
+
if (prefix === "#" || space !== "") return {
|
|
224
|
+
words: trimmed,
|
|
225
|
+
extra: ""
|
|
226
|
+
};
|
|
227
|
+
if (prefix === "//" && trimmed.startsWith("!")) return {
|
|
228
|
+
words: trimmed.slice(1).trimStart(),
|
|
229
|
+
extra: "!"
|
|
230
|
+
};
|
|
231
|
+
const repeated = prefix.charAt(prefix.length - 1);
|
|
232
|
+
let count = 0;
|
|
233
|
+
while (trimmed[count] === repeated) count += 1;
|
|
234
|
+
return count === 0 ? {
|
|
235
|
+
words: trimmed,
|
|
236
|
+
extra: ""
|
|
237
|
+
} : {
|
|
238
|
+
words: trimmed.slice(count).trimStart(),
|
|
239
|
+
extra: repeated.repeat(count)
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
function isIndentedCode({ content, space, extra }) {
|
|
243
|
+
if (space === " ") return true;
|
|
244
|
+
let rest = content.slice(extra.length);
|
|
245
|
+
if (extra !== "" && rest.startsWith(" ")) rest = rest.slice(1);
|
|
246
|
+
return rest.trim() !== "" && indentColumns(rest) >= 4;
|
|
247
|
+
}
|
|
248
|
+
function rejoinLineComments(text, prefix, verbatim = /* @__PURE__ */ new Set()) {
|
|
249
|
+
const pattern = new RegExp(`^(\\s*)${prefix.replaceAll(/[.*+?^$()|[\]{}\\/]/gu, "\\$&")}(\\s?)(.*)$`, "u");
|
|
250
|
+
const lines = text.split("\n");
|
|
251
|
+
const output = [];
|
|
252
|
+
let changed = 0;
|
|
253
|
+
let metadata = false;
|
|
254
|
+
const fences = new Fences();
|
|
255
|
+
const parse = (line) => {
|
|
256
|
+
const match = pattern.exec(line);
|
|
257
|
+
if (match === null) return void 0;
|
|
258
|
+
const [, indent = "", space = "", content = ""] = match;
|
|
259
|
+
return {
|
|
260
|
+
indent,
|
|
261
|
+
space,
|
|
262
|
+
content,
|
|
263
|
+
...splitMarker(content, prefix, space)
|
|
264
|
+
};
|
|
265
|
+
};
|
|
266
|
+
const special = (line) => line.startsWith("#!") || CLOUD_CONFIG.test(line) || SCRIPT_METADATA_FENCE.test(line);
|
|
267
|
+
const isDirective = (words) => prefix === "#" && DIRECTIVE.test(words);
|
|
268
|
+
const continuation = (index, first) => {
|
|
269
|
+
const line = lines[index];
|
|
270
|
+
if (line === void 0 || verbatim.has(index) || special(line.trim())) return void 0;
|
|
271
|
+
const next = parse(line);
|
|
272
|
+
if (next?.indent !== first.indent || next.extra !== first.extra) return void 0;
|
|
273
|
+
const { words } = next;
|
|
274
|
+
return isIndentedCode(next) || looksLikeCode(words) || isStructural(words) || TAG.test(words) || SEPARATOR.test(words) || startsEntry(words) || isFenceMarker(words) || isDirective(words) ? void 0 : words;
|
|
275
|
+
};
|
|
276
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
277
|
+
const line = lines[index] ?? "";
|
|
278
|
+
const stripped = line.trim();
|
|
279
|
+
if (!verbatim.has(index) && SCRIPT_METADATA_FENCE.test(stripped)) {
|
|
280
|
+
metadata = !metadata;
|
|
281
|
+
output.push(line);
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
const comment = verbatim.has(index) || metadata || stripped.startsWith("#!") || CLOUD_CONFIG.test(stripped) ? void 0 : parse(line);
|
|
285
|
+
if (comment === void 0 || fences.step(comment.words) || fences.inside) {
|
|
286
|
+
output.push(line);
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
const { words, content, indent, extra } = comment;
|
|
290
|
+
if (isIndentedCode(comment) || looksLikeCode(words) || isStructural(words) || TAG.test(words) || SEPARATOR.test(words) || LIST_ITEM.test(content) || isDirective(words)) {
|
|
291
|
+
output.push(line);
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
const block = [words];
|
|
295
|
+
for (let next = continuation(index + 1, comment); next !== void 0; next = continuation(index + block.length, comment)) block.push(next);
|
|
296
|
+
output.push(block.length > 1 ? `${indent}${prefix}${extra} ${block.join(" ")}` : line);
|
|
297
|
+
changed += block.length - 1;
|
|
298
|
+
index += block.length - 1;
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
text: output.join("\n"),
|
|
302
|
+
changed
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
// src/examples/dewrap/docstrings.ts
|
|
306
|
+
/**
|
|
307
|
+
* Joins wrapped prose inside Python docstrings. A triple-quoted region is touched only when it opens its line (with an optional `r`, `b` or `u` prefix) where a docstring can stand, so other multi-line strings are left alone.
|
|
308
|
+
*/
|
|
309
|
+
const DOCTEST = /^\s*(>>>|\.\.\.)/u;
|
|
310
|
+
const OPENER = /^(\s*)([rRbBuU]{0,2})("""|''')(.*)$/u;
|
|
311
|
+
/**
|
|
312
|
+
* A docstring is the first statement of a module, class or function: nothing precedes it, or a header ending in a colon, or a comment.
|
|
313
|
+
*/
|
|
314
|
+
function isDocstringPosition(previous) {
|
|
315
|
+
if (previous === void 0) return true;
|
|
316
|
+
const trimmed = previous.trimEnd();
|
|
317
|
+
return trimmed.endsWith(":") || trimmed.trimStart().startsWith("#");
|
|
318
|
+
}
|
|
319
|
+
function standsAlone(raw) {
|
|
320
|
+
const text = raw.trim();
|
|
321
|
+
return DOCTEST.test(raw) || looksLikeCode(text) || isFenceMarker(raw) || startsEntry(text) || isStructural(text);
|
|
322
|
+
}
|
|
323
|
+
function merge(items, base) {
|
|
324
|
+
const output = [];
|
|
325
|
+
let run;
|
|
326
|
+
const flush = () => {
|
|
327
|
+
if (run !== void 0) output.push({
|
|
328
|
+
kind: "text",
|
|
329
|
+
indent: run.indent,
|
|
330
|
+
text: run.words.join(" ")
|
|
331
|
+
});
|
|
332
|
+
run = void 0;
|
|
333
|
+
};
|
|
334
|
+
const fences = new Fences();
|
|
335
|
+
const code = new IndentedCode();
|
|
336
|
+
for (const item of items) {
|
|
337
|
+
if (item.kind === "blank") {
|
|
338
|
+
code.feed("", 0);
|
|
339
|
+
flush();
|
|
340
|
+
output.push(item);
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
const { indent, content, raw } = item;
|
|
344
|
+
const columns = Math.max(0, indentColumns(raw) - base);
|
|
345
|
+
const marker = fences.step(raw);
|
|
346
|
+
if (marker) code.feed(content, columns);
|
|
347
|
+
if (marker || fences.inside || code.feed(content, columns) || standsAlone(raw)) {
|
|
348
|
+
flush();
|
|
349
|
+
output.push({
|
|
350
|
+
kind: "as-is",
|
|
351
|
+
raw
|
|
352
|
+
});
|
|
353
|
+
} else if (run?.indent === indent) run.words.push(content);
|
|
354
|
+
else {
|
|
355
|
+
flush();
|
|
356
|
+
run = {
|
|
357
|
+
indent,
|
|
358
|
+
words: [content]
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
flush();
|
|
363
|
+
return output;
|
|
364
|
+
}
|
|
365
|
+
function render(item) {
|
|
366
|
+
if (item.kind === "blank") return "";
|
|
367
|
+
return item.kind === "as-is" ? item.raw : `${item.indent}${item.text}`;
|
|
368
|
+
}
|
|
369
|
+
const indentOf = (line) => /^\s*/u.exec(line)?.[0] ?? "";
|
|
370
|
+
function rewrite(opener, inside, closing) {
|
|
371
|
+
const [, indent = "", prefix = "", quote = "", trailing = ""] = opener;
|
|
372
|
+
const before = closing.slice(0, closing.indexOf(quote));
|
|
373
|
+
const closingIndent = indentOf(closing);
|
|
374
|
+
const [openerText, closerText] = [trailing.trim() !== "", before.trim() !== ""];
|
|
375
|
+
const body = merge(inside.map((raw) => raw.trim() === "" ? { kind: "blank" } : {
|
|
376
|
+
kind: "line",
|
|
377
|
+
indent: indentOf(raw),
|
|
378
|
+
content: raw.trim(),
|
|
379
|
+
raw
|
|
380
|
+
}), indentColumns(indent));
|
|
381
|
+
const start = `${indent}${prefix}${quote}`;
|
|
382
|
+
if (openerText && closerText && body.length === 0) return [`${start}${trailing.trimEnd()} ${before.trim()}${quote}`];
|
|
383
|
+
const output = [];
|
|
384
|
+
const [first] = body;
|
|
385
|
+
if (openerText && first?.kind === "text" && first.indent === indent) {
|
|
386
|
+
output.push(`${start}${trailing.trimEnd()} ${first.text}`);
|
|
387
|
+
body.shift();
|
|
388
|
+
} else output.push(openerText ? `${start}${trailing.trimEnd()}` : start);
|
|
389
|
+
const last = body.at(-1);
|
|
390
|
+
if (closerText && last?.kind === "text" && last.indent === closingIndent) {
|
|
391
|
+
body.pop();
|
|
392
|
+
return [
|
|
393
|
+
...output,
|
|
394
|
+
...body.map(render),
|
|
395
|
+
`${closingIndent}${last.text} ${before.trim()}${quote}`
|
|
396
|
+
];
|
|
397
|
+
}
|
|
398
|
+
return [
|
|
399
|
+
...output,
|
|
400
|
+
...body.map(render),
|
|
401
|
+
`${closingIndent}${closerText ? before.trim() : ""}${quote}`
|
|
402
|
+
];
|
|
403
|
+
}
|
|
404
|
+
function rejoinDocstrings(text) {
|
|
405
|
+
const lines = text.split("\n");
|
|
406
|
+
const output = [];
|
|
407
|
+
let changed = 0;
|
|
408
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
409
|
+
const line = lines[index] ?? "";
|
|
410
|
+
const opener = OPENER.exec(line);
|
|
411
|
+
const quote = opener?.[3] ?? "";
|
|
412
|
+
const close = opener !== null && !(opener[4] ?? "").trimEnd().endsWith(quote) && isDocstringPosition(lines.slice(0, index).findLast((previous) => previous.trim() !== "")) ? lines.findIndex((candidate, at) => at > index && candidate.includes(quote)) : -1;
|
|
413
|
+
if (opener === null || close === -1) {
|
|
414
|
+
output.push(line);
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
const rewritten = rewrite(opener, lines.slice(index + 1, close), lines[close] ?? "");
|
|
418
|
+
output.push(...rewritten);
|
|
419
|
+
changed += close - index + 1 - rewritten.length;
|
|
420
|
+
index = close;
|
|
421
|
+
}
|
|
422
|
+
return {
|
|
423
|
+
text: output.join("\n"),
|
|
424
|
+
changed
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
// src/examples/dewrap/markdown.ts
|
|
428
|
+
/**
|
|
429
|
+
* Joins the hard-wrapped paragraphs of a Markdown or plain-text document, leaving frontmatter, fences, raw HTML, code and structural lines alone.
|
|
430
|
+
*/
|
|
431
|
+
const RAW_OPEN = /^\s*<(pre|script|style|textarea)\b/iu;
|
|
432
|
+
const RAW_CLOSE = /<\/(pre|script|style|textarea)\s*>/iu;
|
|
433
|
+
/** The number of leading lines that are YAML frontmatter, delimiters included. */
|
|
434
|
+
function frontmatterLines(lines) {
|
|
435
|
+
if (lines[0]?.trim() !== "---") return 0;
|
|
436
|
+
const close = lines.findIndex((line, index) => index > 0 && line.trim() === "---");
|
|
437
|
+
return close === -1 ? lines.length : close + 1;
|
|
438
|
+
}
|
|
439
|
+
function joined(block) {
|
|
440
|
+
const [first = ""] = block;
|
|
441
|
+
const marker = LIST_ITEM.exec(first) ?? LABEL_DEFINITION.exec(first);
|
|
442
|
+
const prefix = marker === null ? /^\s*/u.exec(first)?.[0] ?? "" : first.slice(0, marker[0].length);
|
|
443
|
+
const parts = [marker === null ? first.trim() : first.slice(marker[0].length).trim(), ...block.slice(1).map((line) => line.trim())];
|
|
444
|
+
const hardBreak = HARD_BREAK.exec(block.at(-1) ?? "")?.[0] ?? "";
|
|
445
|
+
return prefix + parts.filter((part) => part !== "").join(" ") + hardBreak;
|
|
446
|
+
}
|
|
447
|
+
function rejoinMarkdown(text) {
|
|
448
|
+
const lines = text.split("\n");
|
|
449
|
+
let index = frontmatterLines(lines);
|
|
450
|
+
const output = lines.slice(0, index);
|
|
451
|
+
let changed = 0;
|
|
452
|
+
const fences = new Fences();
|
|
453
|
+
const code = new IndentedCode();
|
|
454
|
+
let raw = false;
|
|
455
|
+
while (index < lines.length) {
|
|
456
|
+
const line = lines[index] ?? "";
|
|
457
|
+
index += 1;
|
|
458
|
+
if (raw) {
|
|
459
|
+
output.push(line);
|
|
460
|
+
raw = !RAW_CLOSE.test(line);
|
|
461
|
+
} else if (RAW_OPEN.test(line) && !RAW_CLOSE.test(line)) {
|
|
462
|
+
raw = true;
|
|
463
|
+
output.push(line);
|
|
464
|
+
} else if (fences.step(line)) {
|
|
465
|
+
code.feed(line.trim(), indentColumns(line));
|
|
466
|
+
output.push(line);
|
|
467
|
+
} else if (fences.inside || code.feed(line.trim(), indentColumns(line)) || isStructural(line)) output.push(line);
|
|
468
|
+
else {
|
|
469
|
+
const block = [line];
|
|
470
|
+
while (index < lines.length && !HARD_BREAK.test(block.at(-1) ?? "")) {
|
|
471
|
+
const next = lines[index] ?? "";
|
|
472
|
+
if (isFenceMarker(next) || isStructural(next) || startsEntry(next)) break;
|
|
473
|
+
block.push(next);
|
|
474
|
+
index += 1;
|
|
475
|
+
}
|
|
476
|
+
output.push(block.length > 1 ? joined(block) : line);
|
|
477
|
+
changed += block.length - 1;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
return {
|
|
481
|
+
text: output.join("\n"),
|
|
482
|
+
changed
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
// src/examples/dewrap/verbatim.ts
|
|
486
|
+
const HEREDOC = /<<([~-]?)\s*(?:'([A-Za-z_]\w*)'|"([A-Za-z_]\w*)"|\\?([A-Za-z_]\w*))/uy;
|
|
487
|
+
const RUST_CHARACTER = /'(?:\\.[^']*|[^'\\])'/uy;
|
|
488
|
+
const RUST_RAW = /r(#*)"/uy;
|
|
489
|
+
const BLOCK_SCALAR = /^(\s*)(?:-\s+)*(?:[^\s#][^#]*?:\s+)?[|>][+-]?\d*\s*(?:#.*)?$/u;
|
|
490
|
+
const slashes = {
|
|
491
|
+
line: "//",
|
|
492
|
+
block: ["/*", "*/"]
|
|
493
|
+
};
|
|
494
|
+
const javascript = {
|
|
495
|
+
...slashes,
|
|
496
|
+
single: "\"'",
|
|
497
|
+
multi: "",
|
|
498
|
+
triple: [],
|
|
499
|
+
template: true
|
|
500
|
+
};
|
|
501
|
+
const c = {
|
|
502
|
+
...slashes,
|
|
503
|
+
single: "\"'",
|
|
504
|
+
multi: "",
|
|
505
|
+
triple: []
|
|
506
|
+
};
|
|
507
|
+
const tripleC = {
|
|
508
|
+
...c,
|
|
509
|
+
triple: ["\"\"\""]
|
|
510
|
+
};
|
|
511
|
+
const hash = {
|
|
512
|
+
line: "#",
|
|
513
|
+
single: "",
|
|
514
|
+
multi: "\"'",
|
|
515
|
+
triple: [],
|
|
516
|
+
heredoc: true
|
|
517
|
+
};
|
|
518
|
+
const lisp = {
|
|
519
|
+
line: ";",
|
|
520
|
+
single: "",
|
|
521
|
+
multi: "\"",
|
|
522
|
+
triple: []
|
|
523
|
+
};
|
|
524
|
+
function group$1(rules, extensions) {
|
|
525
|
+
return Object.fromEntries(extensions.split(" ").map((extension) => [extension, rules]));
|
|
526
|
+
}
|
|
527
|
+
const RULES = {
|
|
528
|
+
...group$1(javascript, ".ts .tsx .js .jsx .mjs .cjs"),
|
|
529
|
+
...group$1(c, ".c .h .cpp .hpp .cc .zig"),
|
|
530
|
+
...group$1(tripleC, ".java .kt .kts .scala .swift"),
|
|
531
|
+
...group$1({
|
|
532
|
+
...c,
|
|
533
|
+
triple: ["\"\"\"", "'''"]
|
|
534
|
+
}, ".dart"),
|
|
535
|
+
...group$1({
|
|
536
|
+
...c,
|
|
537
|
+
multi: "`",
|
|
538
|
+
literal: ["`"]
|
|
539
|
+
}, ".go"),
|
|
540
|
+
...group$1({
|
|
541
|
+
...slashes,
|
|
542
|
+
single: "",
|
|
543
|
+
multi: "\"",
|
|
544
|
+
triple: [],
|
|
545
|
+
rust: true
|
|
546
|
+
}, ".rs"),
|
|
547
|
+
...group$1({
|
|
548
|
+
line: "#",
|
|
549
|
+
single: "\"'",
|
|
550
|
+
multi: "",
|
|
551
|
+
triple: ["\"\"\"", "'''"]
|
|
552
|
+
}, ".py"),
|
|
553
|
+
...group$1({
|
|
554
|
+
...hash,
|
|
555
|
+
literal: ["'"]
|
|
556
|
+
}, ".sh .bash .zsh"),
|
|
557
|
+
...group$1(hash, ".rb .pl .tf .hcl"),
|
|
558
|
+
...group$1({
|
|
559
|
+
line: "#",
|
|
560
|
+
single: "\"'",
|
|
561
|
+
multi: "",
|
|
562
|
+
triple: ["\"\"\"", "'''"],
|
|
563
|
+
literal: ["'", "'''"]
|
|
564
|
+
}, ".toml"),
|
|
565
|
+
...group$1({
|
|
566
|
+
line: "--",
|
|
567
|
+
block: ["/*", "*/"],
|
|
568
|
+
single: "",
|
|
569
|
+
multi: "'",
|
|
570
|
+
triple: [],
|
|
571
|
+
literal: ["'"]
|
|
572
|
+
}, ".sql"),
|
|
573
|
+
...group$1({
|
|
574
|
+
line: "--",
|
|
575
|
+
single: "\"'",
|
|
576
|
+
multi: "",
|
|
577
|
+
triple: ["[["],
|
|
578
|
+
luaClose: true
|
|
579
|
+
}, ".lua"),
|
|
580
|
+
...group$1(lisp, ".lisp .clj .cljs .el")
|
|
581
|
+
};
|
|
582
|
+
function blockScalarLines(lines) {
|
|
583
|
+
const verbatim = /* @__PURE__ */ new Set();
|
|
584
|
+
for (let index = 0; index < lines.length;) {
|
|
585
|
+
const match = BLOCK_SCALAR.exec(lines[index] ?? "");
|
|
586
|
+
index += 1;
|
|
587
|
+
if (match === null) continue;
|
|
588
|
+
const base = (match[1] ?? "").length;
|
|
589
|
+
for (let line = lines[index]; line !== void 0 && (line.trim() === "" || line.length - line.trimStart().length > base); line = lines[index]) {
|
|
590
|
+
verbatim.add(index);
|
|
591
|
+
index += 1;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return verbatim;
|
|
595
|
+
}
|
|
596
|
+
function closes(line, index, closer, escapes) {
|
|
597
|
+
for (let position = index; position < line.length;) if (escapes && line[position] === "\\") position += 2;
|
|
598
|
+
else if (line.startsWith(closer, position)) return position + closer.length;
|
|
599
|
+
else position += 1;
|
|
600
|
+
}
|
|
601
|
+
function sticky(pattern, line, index) {
|
|
602
|
+
const positioned = new RegExp(pattern);
|
|
603
|
+
positioned.lastIndex = index;
|
|
604
|
+
return positioned.exec(line);
|
|
605
|
+
}
|
|
606
|
+
var Lexer = class {
|
|
607
|
+
pending = [];
|
|
608
|
+
stack = [];
|
|
609
|
+
rules;
|
|
610
|
+
constructor(rules) {
|
|
611
|
+
this.rules = rules;
|
|
612
|
+
}
|
|
613
|
+
/** Reads one line, then opens the here-documents it started. */
|
|
614
|
+
line(line) {
|
|
615
|
+
for (let index = 0; index !== void 0 && index < line.length;) index = this.token(line, index);
|
|
616
|
+
this.stack.push(...this.pending.splice(0).toReversed());
|
|
617
|
+
}
|
|
618
|
+
token(line, index) {
|
|
619
|
+
const top = this.stack.at(-1);
|
|
620
|
+
if (top?.kind === "block") {
|
|
621
|
+
const end = line.indexOf(top.closer, index);
|
|
622
|
+
if (end === -1) return void 0;
|
|
623
|
+
this.stack.pop();
|
|
624
|
+
return end + top.closer.length;
|
|
625
|
+
}
|
|
626
|
+
if (top?.kind === "string") {
|
|
627
|
+
const end = closes(line, index, top.closer, !top.literal && this.rules.literal?.includes(top.closer) !== true);
|
|
628
|
+
if (end !== void 0) this.stack.pop();
|
|
629
|
+
return end;
|
|
630
|
+
}
|
|
631
|
+
if (top?.kind === "template") {
|
|
632
|
+
if (line[index] === "\\") return index + 2;
|
|
633
|
+
if (line[index] === "`") {
|
|
634
|
+
this.stack.pop();
|
|
635
|
+
return index + 1;
|
|
636
|
+
}
|
|
637
|
+
if (!line.startsWith("${", index)) return index + 1;
|
|
638
|
+
this.stack.push({
|
|
639
|
+
kind: "expression",
|
|
640
|
+
depth: 0
|
|
641
|
+
});
|
|
642
|
+
return index + 2;
|
|
643
|
+
}
|
|
644
|
+
return this.code(line, index);
|
|
645
|
+
}
|
|
646
|
+
code(line, index) {
|
|
647
|
+
const { rules, stack } = this;
|
|
648
|
+
const character = line.charAt(index);
|
|
649
|
+
const top = stack.at(-1);
|
|
650
|
+
if (line.startsWith(rules.line, index) && (rules.line !== "#" || index === 0 || " ;|&(".includes(line.charAt(index - 1)) || rules.heredoc !== true)) return void 0;
|
|
651
|
+
if (rules.block !== void 0 && line.startsWith(rules.block[0], index)) {
|
|
652
|
+
stack.push({
|
|
653
|
+
kind: "block",
|
|
654
|
+
closer: rules.block[1]
|
|
655
|
+
});
|
|
656
|
+
return index + rules.block[0].length;
|
|
657
|
+
}
|
|
658
|
+
if (rules.rust === true) {
|
|
659
|
+
const raw = sticky(RUST_RAW, line, index);
|
|
660
|
+
if (raw !== null && (index === 0 || !/[\p{L}\p{N}_]/u.test(line.charAt(index - 1)))) {
|
|
661
|
+
stack.push({
|
|
662
|
+
kind: "string",
|
|
663
|
+
closer: `"${raw[1] ?? ""}`,
|
|
664
|
+
literal: true
|
|
665
|
+
});
|
|
666
|
+
return index + raw[0].length;
|
|
667
|
+
}
|
|
668
|
+
const literal = sticky(RUST_CHARACTER, line, index);
|
|
669
|
+
if (literal !== null) return index + literal[0].length;
|
|
670
|
+
}
|
|
671
|
+
const triple = rules.triple.find((candidate) => line.startsWith(candidate, index));
|
|
672
|
+
if (triple !== void 0) {
|
|
673
|
+
stack.push({
|
|
674
|
+
kind: "string",
|
|
675
|
+
closer: rules.luaClose === true ? "]]" : triple,
|
|
676
|
+
literal: false
|
|
677
|
+
});
|
|
678
|
+
return index + triple.length;
|
|
679
|
+
}
|
|
680
|
+
if (top?.kind === "expression") {
|
|
681
|
+
if (character === "{") stack[stack.length - 1] = {
|
|
682
|
+
kind: "expression",
|
|
683
|
+
depth: top.depth + 1
|
|
684
|
+
};
|
|
685
|
+
else if (character === "}") {
|
|
686
|
+
if (top.depth === 0) stack.pop();
|
|
687
|
+
else stack[stack.length - 1] = {
|
|
688
|
+
kind: "expression",
|
|
689
|
+
depth: top.depth - 1
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
if (character === "`" && rules.template === true) {
|
|
694
|
+
stack.push({ kind: "template" });
|
|
695
|
+
return index + 1;
|
|
696
|
+
}
|
|
697
|
+
if (rules.multi.includes(character)) {
|
|
698
|
+
stack.push({
|
|
699
|
+
kind: "string",
|
|
700
|
+
closer: character,
|
|
701
|
+
literal: false
|
|
702
|
+
});
|
|
703
|
+
return index + 1;
|
|
704
|
+
}
|
|
705
|
+
if (rules.single.includes(character)) return closes(line, index + 1, character, true) ?? line.length;
|
|
706
|
+
if (character === "\\") return index + 2;
|
|
707
|
+
if (rules.heredoc !== true || !line.startsWith("<<", index) || line.startsWith("<<<", index)) return index + 1;
|
|
708
|
+
const heredoc = sticky(HEREDOC, line, index);
|
|
709
|
+
if (heredoc === null) return index + 2;
|
|
710
|
+
this.pending.push({
|
|
711
|
+
kind: "heredoc",
|
|
712
|
+
terminator: heredoc[2] ?? heredoc[3] ?? heredoc[4] ?? "",
|
|
713
|
+
style: heredoc[1] ?? ""
|
|
714
|
+
});
|
|
715
|
+
return index + heredoc[0].length;
|
|
716
|
+
}
|
|
717
|
+
};
|
|
718
|
+
/** The zero-based indexes of the verbatim lines; none for a language without rules. An unterminated construct protects every later line. */
|
|
719
|
+
function verbatimLines(text, extension) {
|
|
720
|
+
const lines = text.split("\n");
|
|
721
|
+
if (extension === ".yaml" || extension === ".yml") return blockScalarLines(lines);
|
|
722
|
+
const rules = RULES[extension];
|
|
723
|
+
const verbatim = /* @__PURE__ */ new Set();
|
|
724
|
+
if (rules === void 0) return verbatim;
|
|
725
|
+
const lexer = new Lexer(rules);
|
|
726
|
+
for (const [index, line] of lines.entries()) {
|
|
727
|
+
const top = lexer.stack.at(-1);
|
|
728
|
+
if (top?.kind === "string" || top?.kind === "template" || top?.kind === "heredoc") verbatim.add(index);
|
|
729
|
+
if (top?.kind === "heredoc") {
|
|
730
|
+
if ((top.style === "~" ? line.trim() : top.style === "" ? line : line.replace(/^\t+/u, "")) === top.terminator) lexer.stack.pop();
|
|
731
|
+
} else lexer.line(line);
|
|
732
|
+
}
|
|
733
|
+
return verbatim;
|
|
734
|
+
}
|
|
735
|
+
// src/examples/dewrap/file.ts
|
|
736
|
+
/**
|
|
737
|
+
* Rejoins one file, wholly or only where an edit added text, choosing the rules from its name.
|
|
738
|
+
*/
|
|
739
|
+
const MARKDOWN = /* @__PURE__ */ new Set([
|
|
740
|
+
".md",
|
|
741
|
+
".markdown",
|
|
742
|
+
".mdx",
|
|
743
|
+
".txt"
|
|
744
|
+
]);
|
|
745
|
+
function group(prefix, extensions) {
|
|
746
|
+
return Object.fromEntries(extensions.split(" ").map((extension) => [extension, prefix]));
|
|
747
|
+
}
|
|
748
|
+
const COMMENT_PREFIXES = {
|
|
749
|
+
...group("//", ".ts .tsx .js .jsx .mjs .cjs .go .rs .java .c .h .cpp .hpp .cc .swift .kt .kts .scala .dart .zig"),
|
|
750
|
+
...group("#", ".py .rb .sh .bash .zsh .yaml .yml .toml .r .pl .tf .hcl"),
|
|
751
|
+
...group("--", ".sql .lua .hs"),
|
|
752
|
+
...group(";", ".lisp .clj .cljs .el .asm .s"),
|
|
753
|
+
...group("%", ".tex .erl")
|
|
754
|
+
};
|
|
755
|
+
const REQUIREMENTS = /^(requirements|constraints)(-[\p{L}\p{N}_.]+)?\.txt$/iu;
|
|
756
|
+
const TEMPLATES = /* @__PURE__ */ new Set([
|
|
757
|
+
".tpl",
|
|
758
|
+
".j2",
|
|
759
|
+
".jinja",
|
|
760
|
+
".jinja2",
|
|
761
|
+
".mustache",
|
|
762
|
+
".hbs",
|
|
763
|
+
".ejs",
|
|
764
|
+
".tmpl"
|
|
765
|
+
]);
|
|
766
|
+
const SKIPPED_DIRECTORIES = /* @__PURE__ */ new Set([
|
|
767
|
+
"node_modules",
|
|
768
|
+
".git",
|
|
769
|
+
"vendor",
|
|
770
|
+
"dist",
|
|
771
|
+
"build",
|
|
772
|
+
".venv",
|
|
773
|
+
"__pycache__"
|
|
774
|
+
]);
|
|
775
|
+
const CONVENTIONAL_SUBJECT = /^(?:(?:feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\([^()\n]+\))?!?: \S.*|(?:Merge|Revert) \S.*)$/u;
|
|
776
|
+
const MESSAGE_WORDS = /* @__PURE__ */ new Set([
|
|
777
|
+
"commit",
|
|
778
|
+
"msg",
|
|
779
|
+
"message",
|
|
780
|
+
"editmsg",
|
|
781
|
+
"body",
|
|
782
|
+
"draft",
|
|
783
|
+
"final",
|
|
784
|
+
"merge",
|
|
785
|
+
"tag"
|
|
786
|
+
]);
|
|
787
|
+
const MESSAGE_NAMES = /* @__PURE__ */ new Set([
|
|
788
|
+
"msg",
|
|
789
|
+
"merge msg",
|
|
790
|
+
"tag editmsg",
|
|
791
|
+
"commit editmsg"
|
|
792
|
+
]);
|
|
793
|
+
/** A name's suffixes, each with its dot; a leading dot starts a name, not a suffix. */
|
|
794
|
+
function suffixes(name) {
|
|
795
|
+
return name.replace(/^\.+/u, "").split(".").slice(1).map((part) => `.${part}`);
|
|
796
|
+
}
|
|
797
|
+
/** Whether a file is a commit message, whose body breaks lines on purpose: by its shape, or by a name git or a person gives one. */
|
|
798
|
+
function isCommitMessage(name, text) {
|
|
799
|
+
const words = name.slice(0, name.length - (suffixes(name).at(-1) ?? "").length).toLowerCase().split(/[-_.\s]+/u).filter((word) => word !== "");
|
|
800
|
+
const named = words.filter((word) => !/^\d+$/u.test(word));
|
|
801
|
+
if (MESSAGE_NAMES.has(words.join(" ")) || named.includes("commit") && named.every((word) => MESSAGE_WORDS.has(word))) return true;
|
|
802
|
+
const [subject = "", second] = text.split("\n");
|
|
803
|
+
return CONVENTIONAL_SUBJECT.test(subject) && (second === void 0 || second.trim() === "");
|
|
804
|
+
}
|
|
805
|
+
/** Rejoins a text as the extension requires; one with no rules is unchanged. */
|
|
806
|
+
function rejoin(text, extension) {
|
|
807
|
+
if (MARKDOWN.has(extension)) return rejoinMarkdown(text);
|
|
808
|
+
const prefix = COMMENT_PREFIXES[extension];
|
|
809
|
+
if (prefix === void 0) return {
|
|
810
|
+
text,
|
|
811
|
+
changed: 0
|
|
812
|
+
};
|
|
813
|
+
return [
|
|
814
|
+
...extension === ".py" ? [rejoinDocstrings] : [],
|
|
815
|
+
...prefix === "//" ? [(current) => rejoinBlockComments(current, verbatimLines(current, extension))] : [],
|
|
816
|
+
(current) => rejoinLineComments(current, prefix, verbatimLines(current, extension))
|
|
817
|
+
].reduce((done, step) => {
|
|
818
|
+
const next = step(done.text);
|
|
819
|
+
return {
|
|
820
|
+
text: next.text,
|
|
821
|
+
changed: done.changed + next.changed
|
|
822
|
+
};
|
|
823
|
+
}, {
|
|
824
|
+
text,
|
|
825
|
+
changed: 0
|
|
826
|
+
});
|
|
827
|
+
}
|
|
828
|
+
function occurrences(text, needle) {
|
|
829
|
+
const found = [];
|
|
830
|
+
for (let index = text.indexOf(needle); index !== -1; index = text.indexOf(needle, index + 1)) found.push(index);
|
|
831
|
+
return found;
|
|
832
|
+
}
|
|
833
|
+
/** Whether any occurrence of a fragment starts inside an open fence, which the fragment alone cannot show. */
|
|
834
|
+
function insideFence(text, needle) {
|
|
835
|
+
return occurrences(text, needle).some((index) => {
|
|
836
|
+
const fences = new Fences();
|
|
837
|
+
for (const line of text.slice(0, index).split("\n")) fences.step(line);
|
|
838
|
+
return fences.inside;
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
/** The offset just past a leading frontmatter block, or `undefined` when there is none. */
|
|
842
|
+
function frontmatterEnd(text) {
|
|
843
|
+
const lines = text.split("\n");
|
|
844
|
+
if (lines[0]?.trim() !== "---") return void 0;
|
|
845
|
+
const close = lines.findIndex((line, index) => index > 0 && line.trim() === "---");
|
|
846
|
+
return close === -1 ? void 0 : lines.slice(0, close + 1).reduce((offset, line) => offset + line.length + 1, 0);
|
|
847
|
+
}
|
|
848
|
+
function insideFrontmatter(text, needle) {
|
|
849
|
+
const end = frontmatterEnd(text);
|
|
850
|
+
return end !== void 0 && occurrences(text, needle).some((index) => index < end);
|
|
851
|
+
}
|
|
852
|
+
function insideVerbatim(text, needle, extension) {
|
|
853
|
+
const verbatim = verbatimLines(text, extension);
|
|
854
|
+
const span = needle.split("\n").length;
|
|
855
|
+
return occurrences(text, needle).some((index) => {
|
|
856
|
+
const first = text.slice(0, index).split("\n").length - 1;
|
|
857
|
+
return Array.from({ length: span }, (_value, offset) => first + offset).some((line) => verbatim.has(line));
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
const BEFORE_LAST = -2;
|
|
861
|
+
const sameWords = (before, after) => JSON.stringify(contentWords(before)) === JSON.stringify(contentWords(after));
|
|
862
|
+
function rejoinRegions(text, extension, edit) {
|
|
863
|
+
return edit.regions.reduce((done, region) => {
|
|
864
|
+
const current = done.text;
|
|
865
|
+
if (!current.includes(region)) return done;
|
|
866
|
+
if (MARKDOWN.has(extension) && (insideFence(current, region) || insideFrontmatter(current, region))) return done;
|
|
867
|
+
if (COMMENT_PREFIXES[extension] !== void 0 && insideVerbatim(current, region, extension)) return done;
|
|
868
|
+
const result = rejoin(region, extension);
|
|
869
|
+
if (result.changed === 0 || result.text === region) return done;
|
|
870
|
+
return {
|
|
871
|
+
text: edit.everywhere ? current.replaceAll(region, () => result.text) : current.replace(region, () => result.text),
|
|
872
|
+
changed: done.changed + result.changed
|
|
873
|
+
};
|
|
874
|
+
}, {
|
|
875
|
+
text,
|
|
876
|
+
changed: 0
|
|
877
|
+
});
|
|
878
|
+
}
|
|
879
|
+
/** Rejoins a file in place and says what happened, or `undefined` when nothing did. A change that would lose or alter a word is not written. */
|
|
880
|
+
function rejoinFile(path, edit) {
|
|
881
|
+
if (statSync(path, { throwIfNoEntry: false })?.isFile() !== true) return void 0;
|
|
882
|
+
if (path.split(sep).some((part) => SKIPPED_DIRECTORIES.has(part))) return void 0;
|
|
883
|
+
const name = basename(path);
|
|
884
|
+
const all = suffixes(name).map((suffix) => suffix.toLowerCase());
|
|
885
|
+
if (all.includes(".snippet") || REQUIREMENTS.test(name)) return void 0;
|
|
886
|
+
let extension = all.at(-1) ?? "";
|
|
887
|
+
if (TEMPLATES.has(extension)) extension = all.at(BEFORE_LAST) ?? extension;
|
|
888
|
+
const bytes = readFileSync(path, "utf8");
|
|
889
|
+
if (bytes.includes("�") || bytes.includes("\0")) return void 0;
|
|
890
|
+
const windowsEndings = bytes.includes("\r\n");
|
|
891
|
+
const text = bytes.replaceAll("\r\n", "\n");
|
|
892
|
+
if (MARKDOWN.has(extension) && isCommitMessage(name, text)) return void 0;
|
|
893
|
+
if (edit.kind === "regions" && edit.regions.length === 0) return void 0;
|
|
894
|
+
const result = edit.kind === "whole" ? rejoin(text, extension) : rejoinRegions(text, extension, edit);
|
|
895
|
+
if (result.changed === 0 || result.text === text) return void 0;
|
|
896
|
+
if (!sameWords(text, result.text)) return edit.kind === "whole" ? `dewrap: skipped ${path} (content safety check failed, no changes applied)` : void 0;
|
|
897
|
+
writeFileSync(path, windowsEndings ? result.text.replaceAll("\n", "\r\n") : result.text);
|
|
898
|
+
const where = edit.kind === "whole" ? "" : "the edited region of ";
|
|
899
|
+
return `dewrap: joined ${String(result.changed)} hard-wrapped line(s) in ${where}${name}`;
|
|
900
|
+
}
|
|
901
|
+
// src/examples/dewrap.ts
|
|
902
|
+
/**
|
|
903
|
+
* After an edit, joins hard-wrapped Markdown paragraphs and comment blocks into single lines, only where the edit added text. A heuristic, so it skips whatever it cannot be sure of and never writes a change that would alter a word.
|
|
904
|
+
*/
|
|
905
|
+
/** The consecutive added lines of each file a Codex patch writes. */
|
|
906
|
+
function addedRegions(patch) {
|
|
907
|
+
const regions = /* @__PURE__ */ new Map();
|
|
908
|
+
let name;
|
|
909
|
+
let added = [];
|
|
910
|
+
const flush = () => {
|
|
911
|
+
if (name !== void 0 && added.length > 0) regions.set(name, [...regions.get(name) ?? [], added.join("\n")]);
|
|
912
|
+
added = [];
|
|
913
|
+
};
|
|
914
|
+
for (const line of patch.split(/\r?\n/u).slice(1, -1)) {
|
|
915
|
+
const header = /^\*\*\* (Add File|Update File|Delete File|Move to): (.*)$/u.exec(line);
|
|
916
|
+
if (header !== null) {
|
|
917
|
+
flush();
|
|
918
|
+
name = header[1] === "Delete File" ? void 0 : header[2];
|
|
919
|
+
} else if (line.startsWith("+") && name !== void 0) added.push(line.slice(1));
|
|
920
|
+
else flush();
|
|
921
|
+
}
|
|
922
|
+
flush();
|
|
923
|
+
return regions;
|
|
924
|
+
}
|
|
925
|
+
function afterPatch(input, patch) {
|
|
926
|
+
const response = input["tool_response"];
|
|
927
|
+
if (typeof response !== "string") return ["dewrap: cannot determine whether the patch succeeded; inspect its added prose manually"];
|
|
928
|
+
if (!response.startsWith("Success") && !response.startsWith("Exit code: 0\n")) return [];
|
|
929
|
+
const cwd = stringField(input, "cwd") ?? ".";
|
|
930
|
+
return [...addedRegions(patch)].flatMap(([name, regions]) => {
|
|
931
|
+
const path = resolve(cwd, name);
|
|
932
|
+
if (statSync(path, { throwIfNoEntry: false })?.isFile() !== true) return [`dewrap: ${name} is unavailable after the patch; inspect its added prose manually`];
|
|
933
|
+
const content = readFileSync(path, "utf8").replaceAll("\r\n", "\n");
|
|
934
|
+
const times = (region) => content.split(region).length - 1;
|
|
935
|
+
const present = regions.filter((region) => region !== "");
|
|
936
|
+
const unique = present.filter((region) => times(region) === 1);
|
|
937
|
+
const messages = [...present.some((region) => times(region) > 1) ? [`dewrap: could not identify the edited occurrence in ${name}; inspect the added prose manually`] : [], ...present.some((region) => times(region) === 0) ? [`dewrap: could not find an added region in ${name}; inspect the added prose manually`] : []];
|
|
938
|
+
const done = unique.length === 0 ? void 0 : rejoinFile(path, {
|
|
939
|
+
kind: "regions",
|
|
940
|
+
regions: unique,
|
|
941
|
+
everywhere: false
|
|
942
|
+
});
|
|
943
|
+
return done === void 0 ? messages : [...messages, done];
|
|
944
|
+
});
|
|
945
|
+
}
|
|
946
|
+
function editOf(tool, input) {
|
|
947
|
+
const added = (value) => typeof value === "string" && value !== "" ? [value] : [];
|
|
948
|
+
if (tool === "Edit") return {
|
|
949
|
+
kind: "regions",
|
|
950
|
+
regions: added(input["new_string"]),
|
|
951
|
+
everywhere: input["replace_all"] === true
|
|
952
|
+
};
|
|
953
|
+
if (tool !== "MultiEdit") return { kind: "whole" };
|
|
954
|
+
return {
|
|
955
|
+
kind: "regions",
|
|
956
|
+
regions: (Array.isArray(input["edits"]) ? input["edits"] : []).flatMap((edit) => typeof edit === "object" && edit !== null && "new_string" in edit ? added(edit.new_string) : []),
|
|
957
|
+
everywhere: input["replace_all"] === true
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
function report(messages) {
|
|
961
|
+
return messages.length === 0 ? void 0 : { systemMessage: messages.join("\n") };
|
|
962
|
+
}
|
|
963
|
+
const hook = (input) => {
|
|
964
|
+
const tool = stringField(input, "tool_name") ?? "";
|
|
965
|
+
if (tool === "apply_patch") return report(afterPatch(input, shellCommand(input) ?? ""));
|
|
966
|
+
const { file_path: path } = toolInput(input);
|
|
967
|
+
if (typeof path !== "string") return void 0;
|
|
968
|
+
const message = rejoinFile(resolve(stringField(input, "cwd") ?? ".", path), editOf(tool, toolInput(input)));
|
|
969
|
+
return report(message === void 0 ? [] : [message]);
|
|
970
|
+
};
|
|
971
|
+
// src/examples/bin/dewrap.ts
|
|
972
|
+
await main(hook);
|
|
973
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hookloom",
|
|
3
3
|
"description": "One hook registry for Claude Code and Codex: a plugin that dispatches every lifecycle event to your hooks, with a CLI to set up, migrate and roll back",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.0",
|
|
5
5
|
"author": "Joseph Mearman <joseph@mearman.co.uk>",
|
|
6
6
|
"bin": {
|
|
7
7
|
"hookloom": "./dist/cli.mjs"
|