Skip to content

Commit

Permalink
Refactor: rename some fields and variables in SearchFieldTextModel
Browse files Browse the repository at this point in the history
  • Loading branch information
seji1 committed Sep 22, 2024
1 parent 341535c commit 814e197
Showing 1 changed file with 23 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> updateConsumer) {
this.updateConsumer = updateConsumer;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -215,4 +214,4 @@ public void setSelectionEnd(int index) {

firstCharacterIndex = Mth.clamp(firstCharacterIndex, 0, query.length());
}
}
}

0 comments on commit 814e197

Please sign in to comment.