From 3e7560a5658e20ee07edbb40df691c3b23726ab4 Mon Sep 17 00:00:00 2001 From: mcDandy Date: Sat, 21 Feb 2026 13:53:26 +0100 Subject: [PATCH] AI: add autoclose brackets --- web/script_text_input.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/web/script_text_input.js b/web/script_text_input.js index b15441d..e487d59 100644 --- a/web/script_text_input.js +++ b/web/script_text_input.js @@ -339,6 +339,44 @@ function attachLineNumbers(widget) { syntaxLayer.scrollLeft = inputEl.scrollLeft; }; + const handleBracketAutoClose = (e) => { + const key = e.key; + if (!OPENING_BRACKETS.has(key)) { + return; + } + + const selectionStart = inputEl.selectionStart; + const selectionEnd = inputEl.selectionEnd; + + if (selectionStart !== selectionEnd) { + return; + } + + const text = inputEl.value; + const charAfter = text[selectionStart] || ''; + + if (/[a-zA-Z0-9_]/.test(charAfter) || CLOSING_BRACKETS.has(charAfter)) { + return; + } + + const closingBracket = BRACKET_PAIRS[key]; + + e.preventDefault(); + + const before = text.substring(0, selectionStart); + const after = text.substring(selectionStart); + inputEl.value = before + key + closingBracket + after; + inputEl.selectionStart = inputEl.selectionEnd = selectionStart + 1; + + updateNumbers(); + updateHighlight(); + + const event = new Event('input', { bubbles: true }); + inputEl.dispatchEvent(event); + }; + + inputEl.addEventListener("keydown", handleBracketAutoClose); + inputEl.addEventListener("input", () => { updateNumbers(); updateHighlight();