function setState(code) {
state.clear();
+ let position = 0;
let strict = false;
const fns = code.split('\n'); // Assume each function is on its own line
for (let fn of fns) {
+ // Track character index in code block for lookahead
+ const originalFnLength = fn.length;
+ position += originalFnLength;
+
// Clean up empty lines
fn = fn.trim();
if (fn === '') {
// Basic lookahead for function definitions that span multiple lines
if ((fn.match(/\(/g)?.length ?? 0) > (fn.match(/\)/g)?.length ?? 0)) {
- const start = code.indexOf(fn);
+ const start = code.indexOf(fn, position - originalFnLength - 1);
const end = code.indexOf(')', start);
fn = code.substring(start, end + 1).replaceAll('\n', '');
}