Skip to content

Commit

Permalink
1.7.7improvement: optimize the searching reach the beginning in text …
Browse files Browse the repository at this point in the history
…editors
  • Loading branch information
mindolph committed Jun 10, 2024
1 parent e1aa92b commit 18ee4f7
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class SearchableCodeArea extends SmartCodeArea {
private final TextNavigator textNavigator = new TextNavigator();

private boolean searchAtEnd = false;
private boolean searchAtBeginning = false;

// TODO
Selection<Collection<String>, String, Collection<String>> extraSelection = new SelectionImpl<>("main selection", this,
Expand Down Expand Up @@ -57,6 +58,7 @@ public void setText(String text) {
* @param options
*/
public void searchNext(String keyword, TextSearchOptions options) {
boolean isFromBeginning = super.getCaretPosition() == 0; // to avoid infinite loop when nothing found in whole content.
textNavigator.setText(this.getText(), !options.isForReplacement());
int curRow = this.getCurrentParagraph();
int curCol = this.getCaretColumn();
Expand All @@ -70,19 +72,20 @@ public void searchNext(String keyword, TextSearchOptions options) {
this.requestFollowCaret();
}
else {
if (searchAtEnd) {
if (!isFromBeginning && searchAtEnd) {
this.moveTo(0); // move to the start of doc
searchNext(keyword, options); // and restart
searchNext(keyword, options); // and restart searching.
searchAtEnd = false;
}
else {
searchAtEnd = true;
searchAtEnd = true; // setup flag and skip one search step
}
}
}


public void searchPrev(String keyword, TextSearchOptions options) {
boolean isFromEnd = super.getCaretPosition() == getText().length(); // to avoid infinite loop when nothing found in whole content.
textNavigator.setText(this.getText(), !options.isForReplacement());
IndexRange selection = this.getSelection();
int row = this.getCurrentParagraph();
Expand All @@ -100,7 +103,14 @@ public void searchPrev(String keyword, TextSearchOptions options) {
this.requestFollowCaret();
}
else {
this.moveTo(getText().length()); // move to the end of doc
if (!isFromEnd && searchAtBeginning) {
this.moveTo(getText().length()); // move to the end of doc
this.searchPrev(keyword, options);
searchAtBeginning = false;
}
else {
searchAtBeginning = true; // setup flag and skip one search step
}
}
}

Expand Down

0 comments on commit 18ee4f7

Please sign in to comment.