From 550f1b44b2c9f89b72b0aabed992ed6b8190997a Mon Sep 17 00:00:00 2001 From: dylan <> Date: Sat, 6 May 2023 11:45:59 -0700 Subject: [PATCH] Make enter play nicer with indentation --- cart_unpacked.json | 2 +- codetab.ts | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cart_unpacked.json b/cart_unpacked.json index 010d45c..972d159 100644 --- a/cart_unpacked.json +++ b/cart_unpacked.json @@ -1,7 +1,7 @@ [ { "sheet_type": "code", - "value": "x = code_sheet(1);\nreturn {\n\tinit: () => {y = 0},\n\tupdate: () => {\n\t\ty += speed;\n\t\tif (y > 127) {\n\t\t\ty = -6\n\t\t}\n\t},\n\tdraw: () => {\n\t\tcls();\n\t\ttxt(x, y, 'hello world')\n\t}\n}" + "value": "x = code(1);\nreturn {\n\tinit: () => {y = 0},\n\tupdate: () => {\n\t\ty += speed;\n\t\tif (y > 127) {\n\t\t\ty = -6\n\t\t}\n\t},\n\tdraw: () => {\n\t\tcls();\n\t\ttxt(x, y, 'hello world')\n\t}\n}" }, { "sheet_type": "code", diff --git a/codetab.ts b/codetab.ts index 44520cf..0e28aab 100644 --- a/codetab.ts +++ b/codetab.ts @@ -163,6 +163,18 @@ const state = { this.scrollX = focusX*fw-128+fw; } }, + currentIndentation() { + const lines = this.code.slice(0, this.focus).split("\n"); + const line = lines[lines.length-1]; + const match = line.match(/^\s*/); + if (!match) { + return ""; + } + console.log(lines); + console.log(line); + console.log(match); + return match[0]; + }, get code() { return getCodeSheet(0); }, @@ -401,7 +413,7 @@ const update = async () => { if (keyPressed(K.ENTER)) { // TODO: Make this play nicely with indentation - state.insertText("\n"); + state.insertText("\n"+state.currentIndentation()); state.scrollToCursor(); } if (keyPressed(K.TAB)) {