From 814e19774896365d078c9e1506078ddee25423fc Mon Sep 17 00:00:00 2001 From: seji1 <152516748+seji1@users.noreply.github.com> Date: Sun, 22 Sep 2024 19:52:21 +0600 Subject: [PATCH] Refactor: rename some fields and variables in SearchFieldTextModel --- .../config/gui/util/SearchFieldTextModel.java | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/vulkanmod/config/gui/util/SearchFieldTextModel.java b/src/main/java/net/vulkanmod/config/gui/util/SearchFieldTextModel.java index e643b3d7a..c488b875f 100644 --- a/src/main/java/net/vulkanmod/config/gui/util/SearchFieldTextModel.java +++ b/src/main/java/net/vulkanmod/config/gui/util/SearchFieldTextModel.java @@ -15,8 +15,8 @@ public class SearchFieldTextModel { private boolean selecting; private int firstCharacterIndex; private int selectionStart; - private int lastCursorPosition = this.getCursor(); private int selectionEnd; + private int lastCursorPosition = this.getCursor(); public SearchFieldTextModel(Consumer updateConsumer) { this.updateConsumer = updateConsumer; @@ -100,38 +100,37 @@ public int getWordSkipPosition(int wordOffset) { } public int getWordSkipPosition(int wordOffset, int cursorPosition) { - int i = cursorPosition; - boolean backward = wordOffset < 0; - int absOffset = Math.abs(wordOffset); - - for (int k = 0; k < absOffset; k++) { - if (!backward) { - int len = query.length(); - i = query.indexOf(" ", i); - i = (i == -1) ? len : skipSpaces(i); + int currentPosition = cursorPosition; + boolean isMovingBackward = wordOffset < 0; + int numberOfWordsToSkip = Math.abs(wordOffset); + + for (int skippedWords = 0; skippedWords < numberOfWordsToSkip; skippedWords++) { + if (!isMovingBackward) { + currentPosition = query.indexOf(" ", currentPosition); + currentPosition = (currentPosition == -1) ? query.length() : skipSpaces(currentPosition); } else { - i = skipSpacesBackward(i); - while (i > 0 && query.charAt(i - 1) != ' ') { - --i; + currentPosition = skipSpacesBackward(currentPosition); + while (currentPosition > 0 && query.charAt(currentPosition - 1) != ' ') { + --currentPosition; } } } - return i; + return currentPosition; } - private int skipSpaces(int i) { - int len = query.length(); - while (i < len && query.charAt(i) == ' ') { - ++i; + private int skipSpaces(int currentPosition) { + int queryLength = query.length(); + while (currentPosition < queryLength && query.charAt(currentPosition) == ' ') { + ++currentPosition; } - return i; + return currentPosition; } - private int skipSpacesBackward(int i) { - while (i > 0 && query.charAt(i - 1) == ' ') { - --i; + private int skipSpacesBackward(int currentPosition) { + while (currentPosition > 0 && query.charAt(currentPosition - 1) == ' ') { + --currentPosition; } - return i; + return currentPosition; } public int getCursor() { @@ -215,4 +214,4 @@ public void setSelectionEnd(int index) { firstCharacterIndex = Mth.clamp(firstCharacterIndex, 0, query.length()); } -} +} \ No newline at end of file