Repository: akvelon/flutter-code-editor (dart)

Issue:
Search selection jumps when change search pattern that also fit to current selection

Pull request:
- Resolves #242

issue 242 fixed bug: Search selection jumped when change search pattern that also fit to current selection

Patch:
diff --git a/lib/src/code_field/search_result_highlighted_builder.dart b/lib/src/code_field/search_result_highlighted_builder.dart
index f5b9c271..2a6217d3 100644
--- a/lib/src/code_field/search_result_highlighted_builder.dart
+++ b/lib/src/code_field/search_result_highlighted_builder.dart
@@ -4,13 +4,13 @@ import '../search/result.dart';
 import '../search/search_navigation_state.dart';
 
 @visibleForTesting
-const matchBackgroundColor = Color.fromARGB(141, 255, 235, 59);
+const matchBackgroundColor = Color.fromARGB(255, 255, 235, 0);
 
 @visibleForTesting
 const searchTextColor = Colors.black;
 
 @visibleForTesting
-const currentMatchBackgroundColor = Colors.yellow;
+const currentMatchBackgroundColor = Color.fromARGB(255, 255, 150, 50);
 
 class SearchResultHighlightedBuilder {
   final SearchResult searchResult;
diff --git a/lib/src/search/search_navigation_controller.dart b/lib/src/search/search_navigation_controller.dart
index 020ab2cf..70cc1d15 100644
--- a/lib/src/search/search_navigation_controller.dart
+++ b/lib/src/search/search_navigation_controller.dart
@@ -104,14 +104,14 @@ class SearchNavigationController extends ValueNotifier<SearchNavigationState> {
       return null;
     }
 
-    final visibleSelectionEnd = codeController.selection.end;
-    final fullSelectionEnd = codeController.code.hiddenRanges.recoverPosition(
-      visibleSelectionEnd,
+    final visibleSelectionStart = codeController.selection.start;
+    final fullSelectionStart = codeController.code.hiddenRanges.recoverPosition(
+      visibleSelectionStart,
       placeHiddenRanges: TextAffinity.downstream,
     );
 
     var closestMatchIndex = _lastFullSearchResult.matches.indexWhere(
-      (element) => element.start >= fullSelectionEnd,
+      (element) => element.start >= fullSelectionStart,
     );
 
     if (closestMatchIndex == -1) {


Test patch:
diff --git a/test/src/search/search_navigation_test.dart b/test/src/search/search_navigation_test.dart
index def2942d..0a59a91d 100644
--- a/test/src/search/search_navigation_test.dart
+++ b/test/src/search/search_navigation_test.dart
@@ -262,6 +262,42 @@ d
         null,
       );
     });
+
+    testWidgets(
+        'Search selection should stay at current match when search pattern '
+        'changes with match at current', (wt) async {
+      const text = 'abcabc';
+      final controller = await pumpController(wt, text);
+      controller.selection = const TextSelection.collapsed(offset: 0);
+
+      controller.showSearch();
+
+      controller.searchController.settingsController.value =
+          const SearchSettings(
+        isCaseSensitive: false,
+        isRegExp: false,
+        pattern: 'a',
+      );
+
+      expect(
+        controller
+            .searchController.navigationController.value.currentMatchIndex,
+        0,
+      );
+
+      controller.searchController.settingsController.value =
+          const SearchSettings(
+        isCaseSensitive: false,
+        isRegExp: false,
+        pattern: 'ab',
+      );
+
+      expect(
+        controller
+            .searchController.navigationController.value.currentMatchIndex,
+        0,
+      );
+    });
   });
 }