refactor: only apply pattern items if regex is enabled

This commit is contained in:
dinocosta 2025-07-29 19:15:50 +01:00
parent aa07594057
commit e53d9e3d01

View file

@ -1483,8 +1483,10 @@ impl BufferSearchBar {
}
// Determines which pattern items are present in the search query and
// updates the search options accordingly.
// updates the search options accordingly, only if the regex search option
// is enabled.
fn apply_pattern_items(&mut self, cx: &mut Context<Self>) {
if self.search_options.contains(SearchOptions::REGEX) {
// Start from the default search options to ensure that any search
// option that is not to be updated does not changed.
// For example, if `\c` was present in the query and the case
@ -1503,6 +1505,7 @@ impl BufferSearchBar {
self.set_search_options(search_options, cx);
}
}
fn adjust_query_regex_language(&self, cx: &mut App) {
let enable = self.search_options.contains(SearchOptions::REGEX);
@ -2921,8 +2924,24 @@ mod tests {
search_bar.update_in(cx, |search_bar, _, _| {
assert_eq!(
search_bar.search_options,
SearchOptions::CASE_SENSITIVE,
"Should have case sensitivity enabled when \\C pattern item is present"
SearchOptions::NONE,
"Should not apply pattern items if regex not enabled"
);
});
cx.simulate_keystrokes("backspace backspace");
search_bar.update_in(cx, |search_bar, window, cx| {
search_bar.toggle_search_option(SearchOptions::REGEX, window, cx);
});
cx.simulate_input("\\C");
search_bar.update_in(cx, |search_bar, _, _| {
assert_eq!(
search_bar.search_options,
SearchOptions::REGEX | SearchOptions::CASE_SENSITIVE,
"Should have case sensitivity enabled when \\C pattern item is present and regex is enabled"
);
});
@ -2933,14 +2952,14 @@ mod tests {
assert_eq!(search_bar.raw_query(cx), "test");
assert_eq!(
search_bar.search_options,
SearchOptions::NONE,
"Should have case sensitivity disable when \\C pattern item is removed"
SearchOptions::REGEX,
"Should have case sensitivity disabled when \\C pattern item is removed"
);
search_bar.toggle_search_option(SearchOptions::CASE_SENSITIVE, window, cx);
assert_eq!(
search_bar.search_options,
SearchOptions::CASE_SENSITIVE,
SearchOptions::REGEX | SearchOptions::CASE_SENSITIVE,
"Should have case sensitivity enabled by default"
);
});
@ -2952,7 +2971,7 @@ mod tests {
assert_eq!(search_bar.raw_query(cx), "test\\c");
assert_eq!(
search_bar.search_options,
SearchOptions::NONE,
SearchOptions::REGEX,
"Should have no case sensitivity enabled when \\c pattern item is present"
);
});
@ -2963,7 +2982,7 @@ mod tests {
assert_eq!(search_bar.raw_query(cx), "test");
assert_eq!(
search_bar.search_options,
SearchOptions::CASE_SENSITIVE,
SearchOptions::REGEX | SearchOptions::CASE_SENSITIVE,
"Should have case sensitivity enabled when \\c pattern item is removed"
);
});