From b9ca36914cf2a52673cd48f27e1dab2439b26292 Mon Sep 17 00:00:00 2001
From: Sekedus <sekedus@gmail.com>
Date: Fri, 29 Nov 2024 06:48:25 +0800
Subject: [PATCH] fix: codecov missing coverage

---
 src/editor.js           | 4 ----
 src/ext/searchbox.js    | 8 ++++++++
 src/search.js           | 4 ++--
 src/search_highlight.js | 9 +++++++--
 src/search_test.js      | 2 --
 5 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/src/editor.js b/src/editor.js
index 05fa6db33d..db888515de 100644
--- a/src/editor.js
+++ b/src/editor.js
@@ -617,10 +617,6 @@ class Editor {
 
         // Update cursor because tab characters can influence the cursor position.
         this.$cursorChange();
-
-        // Updates "updateCounter" and "ace_nomatch" in the search box
-        if (this.searchBox && this.searchBox.active === true)
-            this.searchBox.find(false, false, true);
     }
 
     /**
diff --git a/src/ext/searchbox.js b/src/ext/searchbox.js
index 22381028cd..2a34ecce97 100644
--- a/src/ext/searchbox.js
+++ b/src/ext/searchbox.js
@@ -51,6 +51,7 @@ class SearchBox {
         this.element = div.firstChild;
 
         this.setSession = this.setSession.bind(this);
+        this.$onEditorInput = this.onEditorInput.bind(this);
 
         this.$init();
         this.setEditor(editor);
@@ -72,6 +73,11 @@ class SearchBox {
         this.$syncOptions(true);
     }
 
+    // Auto update "updateCounter" and "ace_nomatch"
+    onEditorInput() {
+        this.find(false, false, true);
+    }
+
     /**
      * @param {HTMLElement} sb
      */
@@ -283,6 +289,7 @@ class SearchBox {
         this.active = false;
         this.setSearchRange(null);
         this.editor.off("changeSession", this.setSession);
+        this.editor.off("input", this.$onEditorInput);
 
         this.element.style.display = "none";
         this.editor.keyBinding.removeKeyboardHandler(this.$closeSearchBarKb);
@@ -296,6 +303,7 @@ class SearchBox {
     show(value, isReplace) {
         this.active = true;
         this.editor.on("changeSession", this.setSession);
+        this.editor.on("input", this.$onEditorInput);
         this.element.style.display = "";
         this.replaceOption.checked = isReplace;
 
diff --git a/src/search.js b/src/search.js
index 8903847aea..93bce2a943 100644
--- a/src/search.js
+++ b/src/search.js
@@ -592,8 +592,8 @@ function addWordBoundary(needle, options) {
 }
 
 function multiLineBackwardMatch(line, re, endMargin) {
-    var match,
-        from = 0;
+    var match = null;
+    var from = 0;
     while (from <= line.length) {
         re.lastIndex = from;
         var newMatch = re.exec(line);
diff --git a/src/search_highlight.js b/src/search_highlight.js
index f1a4410e64..cd9f0f643c 100644
--- a/src/search_highlight.js
+++ b/src/search_highlight.js
@@ -15,6 +15,7 @@ class SearchHighlight {
         this.setRegexp(regExp);
         this.clazz = clazz;
         this.type = type;
+        this.docLen = 0;
     }
 
     setRegexp(regExp) {
@@ -33,14 +34,15 @@ class SearchHighlight {
     update(html, markerLayer, session, config) {
         if (!this.regExp)
             return;
-        var start = config.firstRow, end = config.lastRow;
+        var start = config.firstRow;
+        var end = config.lastRow;
         var renderedMarkerRanges = {};
         var _search = session.$editor.$search;
         var mtSearch = _search.$isMultilineSearch(session.$editor.getLastSearchOptions());
 
         for (var i = start; i <= end; i++) {
             var ranges = this.cache[i];
-            if (ranges == null) {
+            if (ranges == null || session.getValue().length != this.docLen) {
                 if (mtSearch) {
                     ranges = [];
                     var match = _search.$multiLineForward(session, this.regExp, i, end);
@@ -64,6 +66,8 @@ class SearchHighlight {
                 this.cache[i] = ranges.length ? ranges : "";
             }
 
+            if (ranges.length === 0) continue;
+
             for (var j = ranges.length; j --; ) {
                 var rangeToAddMarkerTo = ranges[j].toScreenRange(session);
                 var rangeAsString = rangeToAddMarkerTo.toString();
@@ -74,6 +78,7 @@ class SearchHighlight {
                     html, rangeToAddMarkerTo, this.clazz, config);
             }
         }
+        this.docLen = session.getValue().length;
     }
 }
 
diff --git a/src/search_test.js b/src/search_test.js
index f1e7674d3c..de9c6ef238 100644
--- a/src/search_test.js
+++ b/src/search_test.js
@@ -684,8 +684,6 @@ module.exports = {
     },
 
     "test: replace with line breaks (\\n) and TAB (\\t) using regular expression" : function() {
-        var session = new EditSession('\nfunction foo(items, nada) {\n    for (var i=0; i<items.length; i++) {\n        alert(items[i] + "juhu\\n");\n    }\t/* Real Tab */\n\n\n\n\n}\n\n\n// test search/replace line break with regexp\r\n\r\n\t\t\t\t\n');
-
         var search = new Search().set({
             needle: "with",
             regExp: true,