openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
82 lines (81 loc) • 2.41 kB
JavaScript
import "./src-H3RMXyFL.mjs";
import { t as expectDefined } from "./expect-DDakjvvQ.mjs";
import { t as findMarkdownCodeRegions } from "./markdown-code-regions-CUsfah9M.mjs";
//#region src/shared/text/code-ranges.ts
function isInsideCode(e, t) {
return t.some((t) => e >= t.start && e < t.end);
}
/** Appends cannot revoke block code after its opening line; inline ownership can change. */
function isInsideSettledBlockCode(e, t, n) {
return n.some((n) => n.block && n.start <= e && e < n.end && /[\r\n]/.test(t.slice(n.start, e)));
}
/** Removes control lines while retaining literal code and original line endings. */
function stripLinesOutsideCode(e, t) {
return projectLinesOutsideCode(e, t, 0).text;
}
function projectLinesOutsideCode(e, t, n) {
let r;
let i = n;
return {
text: e.replace(/[^\n]*(?:\n|$)/g, (a, o) => {
if (!t(a.endsWith("\n") ? a.slice(0, -1).replace(/\r$/, "") : a) || isInsideCode(o, r ??= findMarkdownCodeRegions(e))) return a;
if (o < n) i -= a.length;
return "";
}),
retainStart: n,
retainedOutputStart: i
};
}
/** Index nonempty native parts using the visible text owner's newline separator. */
function indexTextParts(e) {
const t = [];
const n = [];
let r = 0;
e.forEach((e, i) => {
if (!e) return;
if (t.length) r += 1;
const a = r;
n.push(e);
r += e.length;
t.push({
index: i,
start: a,
end: r
});
});
return {
text: n.join("\n"),
spans: t
};
}
/** One canonical code scan for a stage's immutable native text parts. */
function createTextPartCodeRegionResolver(e) {
const t = indexTextParts(e);
const n = new Map(t.spans.map((e) => [e.index, e]));
let r;
return (e) => {
const i = n.get(e);
if (!i) return [];
r ??= findMarkdownCodeRegions(t.text);
let a = 0;
let o = r.length;
while (a < o) {
const e = a + o >>> 1;
if (expectDefined(r[e], "indexed code region").end <= i.start) a = e + 1;
else o = e;
}
const s = [];
for (let e = a; e < r.length; e++) {
const t = expectDefined(r[e], "projected code region");
if (t.start >= i.end) break;
s.push({
start: Math.max(0, t.start - i.start),
end: Math.min(i.end, t.end) - i.start,
block: t.block
});
}
return s;
};
}
//#endregion
export { projectLinesOutsideCode as a, isInsideSettledBlockCode as i, indexTextParts as n, stripLinesOutsideCode as o, isInsideCode as r, createTextPartCodeRegionResolver as t };