From ff7fbd411d32e596d9183ea7fff08d9ac72c47b4 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Sun, 4 Jan 2026 02:39:27 +0200 Subject: [PATCH] Smart tab completion: replace till next separator --- .../client/gui/util/GuiScriptTextArea.java | 10 ++++++ .../autocomplete/AutocompleteManager.java | 35 ++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) 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 23b5369e4..de4ef92b6 100644 --- a/src/main/java/noppes/npcs/client/gui/util/GuiScriptTextArea.java +++ b/src/main/java/noppes/npcs/client/gui/util/GuiScriptTextArea.java @@ -489,6 +489,16 @@ public class GuiScriptTextArea extends GuiNpcTextField { scrollToCursor(); } + @Override + public void replaceTextRange(String text, int startPosition, int endPosition) { + // Replace text from startPosition to endPosition + String before = GuiScriptTextArea.this.text.substring(0, startPosition); + String after = GuiScriptTextArea.this.text.substring(endPosition); + setText(before + text + after); + selection.reset(startPosition + text.length()); + scrollToCursor(); + } + @Override public int getCursorPosition() { return selection.getCursorPosition(); diff --git a/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteManager.java b/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteManager.java index 630bf8c7a..d2c4aa016 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteManager.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteManager.java @@ -67,6 +67,14 @@ public class AutocompleteManager { */ void insertText(String text, int startPosition); + /** + * Replace text in a specific range. + * @param text Text to insert + * @param startPosition Position to start replacing from + * @param endPosition Position to end replacing at + */ + void replaceTextRange(String text, int startPosition, int endPosition); + /** * Get current cursor position. */ @@ -478,7 +486,32 @@ public class AutocompleteManager { } String insertText = item.getInsertText(); - insertCallback.insertText(insertText, prefixStartPosition); + + // Smart tab completion: replace till next separator + // Find the end position (current word till next separator) + String text = insertCallback.getText(); + int cursorPos = insertCallback.getCursorPosition(); + int endPos = cursorPos; + + // Extend to consume rest of current word + while (endPos < text.length() && Character.isJavaIdentifierPart(text.charAt(endPos))) { + endPos++; + } + + // Also consume following parentheses and their content if present + if (endPos < text.length() && text.charAt(endPos) == '(') { + int parenDepth = 1; + endPos++; // Skip opening paren + while (endPos < text.length() && parenDepth > 0) { + char c = text.charAt(endPos); + if (c == '(') parenDepth++; + else if (c == ')') parenDepth--; + endPos++; + } + } + + // Replace from prefixStart to endPos using the new method + insertCallback.replaceTextRange(insertText, prefixStartPosition, endPos); active = false; }