Smart tab completion: replace till next separator

This commit is contained in:
bigguy345 committed 2026-01-04 02:39:27 +02:00
1 parent 02ce0912c7
commit ff7fbd411d
2 files changed
+44 -1

No files matched your search

@@ -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();
@@ -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;
}