82 lines
2.2 KiB
TypeScript
82 lines
2.2 KiB
TypeScript
import { getTracerContext } from '../context';
|
|
|
|
export const getLocation = (targetFn?: Function) => {
|
|
const stackObject: { stack: string } = { stack: '' };
|
|
Error.captureStackTrace(stackObject, targetFn ?? getLocation);
|
|
const { stack } = stackObject;
|
|
// D:\Projects\structrail-sdk\tracers.ts\src\index.ts:23:18
|
|
const location = stack.split('\n').at(1)?.trim().split(' ').at(1)?.trim();
|
|
// console.log(location);
|
|
if (!location) return null;
|
|
|
|
const matches = location?.match(/:(\d+):?(\d+)?\)?$/);
|
|
// console.log(matches);
|
|
const file = location?.replace(matches?.at(0) ?? '', '');
|
|
// console.log(file);
|
|
const line = parseInt(matches?.at(1) ?? '0');
|
|
// console.log(line);
|
|
|
|
return {
|
|
file,
|
|
line,
|
|
};
|
|
};
|
|
|
|
interface ControlTracerCreateOptions {
|
|
description?: string;
|
|
}
|
|
|
|
export const createControlTracer = (options: ControlTracerCreateOptions) => {
|
|
const { description = 'ControlTracer' } = options;
|
|
const tracer = crypto.randomUUID();
|
|
|
|
const { command } = getTracerContext();
|
|
|
|
command({
|
|
type: 'ControlTracer',
|
|
tracer: tracer,
|
|
action: 'create',
|
|
params: {
|
|
description: description,
|
|
},
|
|
});
|
|
|
|
const step = (...range: (number | [number, number])[]) => {
|
|
const { line: currentLine } = getLocation(step) ?? {};
|
|
if (!currentLine) return;
|
|
// console.log(currentLine);
|
|
|
|
const linesSet = new Set<number>([currentLine]);
|
|
range.forEach((item) => {
|
|
if (Array.isArray(item) && item.length === 2) {
|
|
const [offsetStart, offsetEnd] = item;
|
|
const lineStart = currentLine - offsetStart;
|
|
const lineEnd = currentLine - offsetEnd;
|
|
const lineMin = Math.min(lineStart, lineEnd);
|
|
const lineMax = Math.max(lineStart, lineEnd);
|
|
for (let line = lineMin; line <= lineMax; line++) {
|
|
linesSet.add(line);
|
|
}
|
|
}
|
|
if (typeof item === 'number') {
|
|
const line = item;
|
|
linesSet.add(currentLine - line);
|
|
}
|
|
});
|
|
const lines = Array.from(linesSet).sort((line1, line2) => line1 - line2);
|
|
|
|
command({
|
|
type: 'ControlTracer',
|
|
tracer: tracer,
|
|
action: 'step',
|
|
params: {
|
|
lines: lines,
|
|
},
|
|
});
|
|
};
|
|
|
|
return {
|
|
step,
|
|
};
|
|
};
|