From 5d4db0a9141cd4718bba73a23f42ec1e99040158 Mon Sep 17 00:00:00 2001 From: dylan <> Date: Sat, 6 May 2023 12:18:37 -0700 Subject: [PATCH] toggle comments with ctrl+slash --- codetab.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/codetab.ts b/codetab.ts index fe95847..7ffcc96 100644 --- a/codetab.ts +++ b/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 = () => {