Refactor so engines use 0-based document ranges

This commit is contained in:
Nilay Majorwar 2022-01-20 15:29:21 +05:30
parent dbfacece2c
commit d8481c097b
5 changed files with 28 additions and 21 deletions

View File

@ -85,7 +85,7 @@ export default class BrainfuckLanguageEngine implements LanguageEngine<BFRS> {
// Add instruction to AST
ast.push({
instr: { type: char as BF_OP, param: jumpTarget },
location: { line: lIdx + 1, char: cIdx + 1 },
location: { line: lIdx, char: cIdx },
});
});
});

View File

@ -98,7 +98,7 @@ export default class ChefLanguageEngine implements LanguageEngine<T.ChefRS> {
} else {
// Next step is a regular method instruction
const nextOp = currFrame.recipe.method[currFrame.pc];
nextStepLocation = this.convertTo1Index(nextOp.location);
nextStepLocation = nextOp.location;
}
}
@ -191,19 +191,6 @@ export default class ChefLanguageEngine implements LanguageEngine<T.ChefRS> {
if (opOutput) return opOutput;
}
private convertTo1Index(location: DocumentRange): DocumentRange {
const lineNum = location.line + 1;
const charRange = location.charRange
? {
start: location.charRange.start
? location.charRange.start + 1
: undefined,
end: location.charRange.end ? location.charRange.end + 1 : undefined,
}
: undefined;
return { line: lineNum, charRange };
}
/**
* Empty the first N dishes of given kitchen into text output.
* @param numDishes Number of dishes to empty as output

View File

@ -57,7 +57,7 @@ export default class DeadfishLanguageEngine implements LanguageEngine<DFRS> {
if (OP_CHARS.includes(char as DF_OP)) {
ast.push({
instr: char as DF_OP,
location: { line: lIdx + 1, char: cIdx + 1 },
location: { line: lIdx, char: cIdx },
});
}
});

View File

@ -21,11 +21,12 @@ export const createHighlightRange = (
monacoInstance: MonacoInstance,
highlights: DocumentRange
): MonacoDecoration => {
const lineNum = highlights.line;
const startChar = highlights.charRange?.start || 0;
const endChar = highlights.charRange?.end || 1000;
const location = get1IndexedLocation(highlights);
const lineNum = location.line;
const startChar = location.charRange?.start || 0;
const endChar = location.charRange?.end || 1e5;
const range = new monacoInstance.Range(lineNum, startChar, lineNum, endChar);
const isWholeLine = !highlights.charRange;
const isWholeLine = !location.charRange;
return { range, options: { isWholeLine, inlineClassName: "code-highlight" } };
};
@ -39,3 +40,20 @@ export const createBreakpointRange = (
const className = "breakpoint-glyph " + (hint ? "hint" : "solid");
return { range, options: { glyphMarginClassName: className } };
};
/**
* Convert a DocumentRange to use 1-indexed values. Used since language engines
* use 0-indexed ranges but Monaco requires 1-indexed ranges.
* @param range DocumentRange to convert to 1-indexed
* @returns DocumentRange that uses 1-indexed values
*/
const get1IndexedLocation = (range: DocumentRange): DocumentRange => {
const lineNum = range.line + 1;
const charRange = range.charRange
? {
start: range.charRange.start ? range.charRange.start + 1 : undefined,
end: range.charRange.end ? range.charRange.end + 1 : undefined,
}
: undefined;
return { line: lineNum, charRange };
};

View File

@ -51,7 +51,9 @@ export const useEditorBreakpoints = ({
// Update breakpoints to parent
const bpLineNumStrs = Object.keys(breakpoints.current);
const bpLineNums = bpLineNumStrs.map((numStr) => parseInt(numStr, 10));
const bpLineNums = bpLineNumStrs.map(
(numStr) => parseInt(numStr, 10) - 1
);
onUpdateBreakpoints(bpLineNums);
});
return () => disposer.dispose();