From ea8cc51eefe6543ca6074b5ba65c854f0cbc0321 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Mon, 26 Jan 2026 19:19:58 +0200 Subject: [PATCH] Prevented auto pair insertion of '', "", [], () blocks in excluded ranges (comments,strings) --- .../client/gui/util/GuiScriptTextArea.java | 71 ++++++++++--------- .../script/interpreter/ScriptDocument.java | 3 +- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/main/java/noppes/npcs/client/gui/util/GuiScriptTextArea.java b/src/main/java/noppes/npcs/client/gui/util/GuiScriptTextArea.java index 8489d5753..977b213b9 100644 --- a/src/main/java/noppes/npcs/client/gui/util/GuiScriptTextArea.java +++ b/src/main/java/noppes/npcs/client/gui/util/GuiScriptTextArea.java @@ -2357,6 +2357,7 @@ public class GuiScriptTextArea extends GuiNpcTextField { if (ChatAllowedCharacters.isAllowedCharacter(c)) { String before = getSelectionBeforeText(); String after = getSelectionAfterText(); + int cursorPos = selection.getCursorPosition(); // If the user types a closing character and that same closer is // already immediately after the caret, move caret past it instead @@ -2367,50 +2368,52 @@ public class GuiScriptTextArea extends GuiNpcTextField { selection.reset(before.length() + 1); scrollToCursor(); // Notify autocomplete of the character - autocompleteManager.onCharTyped(c, text, selection.getCursorPosition()); + autocompleteManager.onCharTyped(c, text, cursorPos); return true; } // Auto-pair insertion: when opening a quote/brace/bracket is typed, // insert a matching closer and place caret between the pair. - if (c == '"') { - setText(before + "\"\"" + after, true); - selection.reset(before.length() + 1); - scrollToCursor(); - // Notify autocomplete of the character - autocompleteManager.onCharTyped(c, text, selection.getCursorPosition()); - return true; + // But only if the current position is not excluded (e.g., inside a comment or string) + if (!container.getDocument().isExcluded(cursorPos)) { + if (c == '"') { + setText(before + "\"\"" + after, true); + selection.reset(before.length() + 1); + scrollToCursor(); + // Notify autocomplete of the character + autocompleteManager.onCharTyped(c, text, cursorPos); + return true; + } + if (c == '\'') { + setText(before + "''" + after, true); + selection.reset(before.length() + 1); + scrollToCursor(); + // Notify autocomplete of the character + autocompleteManager.onCharTyped(c, text, cursorPos); + return true; + } + if (c == '[') { + setText(before + "[]" + after, true); + selection.reset(before.length() + 1); + scrollToCursor(); + // Notify autocomplete of the character + autocompleteManager.onCharTyped(c, text, cursorPos); + return true; + } + if (c == '(') { + setText(before + "()" + after, true); + selection.reset(before.length() + 1); + scrollToCursor(); + // Notify autocomplete of the character + autocompleteManager.onCharTyped(c, text, cursorPos); + return true; + } } - if (c == '\'') { - setText(before + "''" + after, true); - selection.reset(before.length() + 1); - scrollToCursor(); - // Notify autocomplete of the character - autocompleteManager.onCharTyped(c, text, selection.getCursorPosition()); - return true; - } - if (c == '[') { - setText(before + "[]" + after, true); - selection.reset(before.length() + 1); - scrollToCursor(); - // Notify autocomplete of the character - autocompleteManager.onCharTyped(c, text, selection.getCursorPosition()); - return true; - } - if (c == '(') { - setText(before + "()" + after, true); - selection.reset(before.length() + 1); - scrollToCursor(); - // Notify autocomplete of the character - autocompleteManager.onCharTyped(c, text, selection.getCursorPosition()); - return true; - } - // Default insertion for printable characters: insert at caret (replacing selection) addText(Character.toString(c)); scrollToCursor(); // Notify autocomplete of the character - autocompleteManager.onCharTyped(c, text, selection.getCursorPosition()); + autocompleteManager.onCharTyped(c, text, cursorPos); return true; } return false; diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java index 8f8002e56..74f69a175 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java @@ -442,9 +442,10 @@ public class ScriptDocument { excludedRanges.addAll(merged); } + public boolean isExcluded(int position) { for (int[] range : excludedRanges) { - if (position >= range[0] && position < range[1]) { + if (position >= range[0] && position <= range[1]) { return true; } }