toggle comments with ctrl+slash
This commit is contained in:
parent
60542b63c0
commit
5d4db0a914
30
codetab.ts
30
codetab.ts
@ -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 = () => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user