Trying to do better but it's hard

This commit is contained in:
2025-10-15 12:42:56 +02:00
parent 1dc9d1114e
commit ea8ecd4b2a
9 changed files with 200 additions and 163 deletions

View File

@ -80,14 +80,57 @@ export function getLine(state: EditorState): EvalBlock {
return { text, from, to };
}
type BlockType = 'instr' | 'endin' | 'opcode' | 'endop' | null;
function startsWithBlockMarker(text: string): BlockType {
const trimmed = text.trim();
if (/^\s*instr\b/.test(text)) return 'instr';
if (/^\s*endin\b/.test(text)) return 'endin';
if (/^\s*opcode\b/.test(text)) return 'opcode';
if (/^\s*endop\b/.test(text)) return 'endop';
return null;
}
function findBlockMarker(
doc: any,
startLine: number,
direction: number,
limit: number
): [number, BlockType] | null {
for (let i = startLine; direction > 0 ? i <= limit : i >= limit; i += direction) {
if (i < 1 || i > doc.lines) break;
const lineText = doc.line(i).text;
const marker = startsWithBlockMarker(lineText);
if (marker) {
return [i, marker];
}
}
return null;
}
export function getBlock(state: EditorState): EvalBlock {
let { doc, selection } = state;
let { text, number } = state.doc.lineAt(selection.main.from);
if (text.trim().length === 0) return { text: '', from: null, to: null };
let fromL, toL;
fromL = toL = number;
const prevBlockMark = findBlockMarker(doc, number, -1, 1);
const nextBlockMark = findBlockMarker(doc, number, 1, doc.lines);
if (
prevBlockMark &&
nextBlockMark &&
((prevBlockMark[1] === 'instr' && nextBlockMark[1] === 'endin') ||
(prevBlockMark[1] === 'opcode' && nextBlockMark[1] === 'endop'))
) {
const { from } = doc.line(prevBlockMark[0]);
const { to } = doc.line(nextBlockMark[0]);
text = state.doc.sliceString(from, to);
return { text, from, to };
}
let fromL = number;
let toL = number;
while (fromL > 1 && doc.line(fromL - 1).text.trim().length > 0) {
fromL -= 1;