From 18ee4f774f5d9ba305e955c56a94f5fba669ff0f Mon Sep 17 00:00:00 2001 From: mindolph Date: Mon, 10 Jun 2024 12:45:08 +0800 Subject: [PATCH] 1.7.7improvement: optimize the searching reach the beginning in text editors --- .../base/control/SearchableCodeArea.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/code/mindolph-base/src/main/java/com/mindolph/base/control/SearchableCodeArea.java b/code/mindolph-base/src/main/java/com/mindolph/base/control/SearchableCodeArea.java index fbab324c..7f1ce0cf 100644 --- a/code/mindolph-base/src/main/java/com/mindolph/base/control/SearchableCodeArea.java +++ b/code/mindolph-base/src/main/java/com/mindolph/base/control/SearchableCodeArea.java @@ -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, String, Collection> extraSelection = new SelectionImpl<>("main selection", this, @@ -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(); @@ -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(); @@ -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 + } } }