toggle comments with ctrl+slash

This commit is contained in:
dylan 2023-05-06 12:18:37 -07:00
parent 60542b63c0
commit 5d4db0a914

View File

@ -83,6 +83,33 @@ const state = {
this.setSelection(Math.min(anchor, focus) + text.length);
this.startSnapping();
},
toggleComment() {
const lines = this.code.split("\n");
const {focusX, focusY, anchorX, anchorY} = this;
const lineInSelection = (i: number) => i >= Math.min(focusY, anchorY) && i <= Math.max(focusY, anchorY);
const allLinesAreCommented = lines.every((line, i) => {
if (lineInSelection(i) && !line.trim().startsWith("// ")) {
return false;
} else {
return true;
}
});
const newLines = lines.map((line, i) => {
if (lineInSelection(i)) {
if (allLinesAreCommented) {
return line.slice(3);
} else {
return "// "+line;
}
} else {
return line;
}
});
this.code = newLines.join("\n");
const shiftBy = allLinesAreCommented ? -3 : 3;
this.setSelection({x: anchorX+shiftBy, y: anchorY}, {x: focusX+shiftBy, y: focusY});
this.startSnapping();
},
indent(indentString: string) {
const lines = this.code.split("\n");
const {focusX, focusY, anchorX, anchorY} = this;
@ -494,6 +521,9 @@ const update = async () => {
if (keyPressed("Y") && ctrlKeyDown()) {
state.redo();
}
if (keyPressed("/") && ctrlKeyDown()) {
state.toggleComment();
}
}
const draw = () => {