vim: Add smartcase search (#16932)
Closes #16878 Release Notes: - Added a vim-style smart case option for search patterns --------- Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
parent
b0045b9324
commit
65bc1ea7c8
5 changed files with 78 additions and 8 deletions
|
@ -567,6 +567,7 @@ impl BufferSearchBar {
|
|||
active_item.toggle_filtered_search_ranges(deploy.selection_search_enabled, cx);
|
||||
}
|
||||
self.search_suggested(cx);
|
||||
self.smartcase(cx);
|
||||
self.replace_enabled = deploy.replace_enabled;
|
||||
self.selection_search_enabled = deploy.selection_search_enabled;
|
||||
if deploy.focus {
|
||||
|
@ -718,13 +719,21 @@ impl BufferSearchBar {
|
|||
}
|
||||
}
|
||||
|
||||
fn toggle_search_option(&mut self, search_option: SearchOptions, cx: &mut ViewContext<Self>) {
|
||||
pub fn toggle_search_option(
|
||||
&mut self,
|
||||
search_option: SearchOptions,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
self.search_options.toggle(search_option);
|
||||
self.default_options = self.search_options;
|
||||
drop(self.update_matches(cx));
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
pub fn has_search_option(&mut self, search_option: SearchOptions) -> bool {
|
||||
self.search_options.contains(search_option)
|
||||
}
|
||||
|
||||
pub fn enable_search_option(
|
||||
&mut self,
|
||||
search_option: SearchOptions,
|
||||
|
@ -819,6 +828,7 @@ impl BufferSearchBar {
|
|||
editor::EditorEvent::Focused => self.query_editor_focused = true,
|
||||
editor::EditorEvent::Blurred => self.query_editor_focused = false,
|
||||
editor::EditorEvent::Edited { .. } => {
|
||||
self.smartcase(cx);
|
||||
self.clear_matches(cx);
|
||||
let search = self.update_matches(cx);
|
||||
|
||||
|
@ -1151,6 +1161,26 @@ impl BufferSearchBar {
|
|||
self.update_match_index(cx);
|
||||
self.active_match_index.is_some()
|
||||
}
|
||||
|
||||
pub fn should_use_smartcase_search(&mut self, cx: &mut ViewContext<Self>) -> bool {
|
||||
EditorSettings::get_global(cx).use_smartcase_search
|
||||
}
|
||||
|
||||
pub fn is_contains_uppercase(&mut self, str: &String) -> bool {
|
||||
str.chars().any(|c| c.is_uppercase())
|
||||
}
|
||||
|
||||
fn smartcase(&mut self, cx: &mut ViewContext<Self>) {
|
||||
if self.should_use_smartcase_search(cx) {
|
||||
let query = self.query(cx);
|
||||
if !query.is_empty() {
|
||||
let is_case = self.is_contains_uppercase(&query);
|
||||
if self.has_search_option(SearchOptions::CASE_SENSITIVE) != is_case {
|
||||
self.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -114,6 +114,10 @@ pub fn init(cx: &mut AppContext) {
|
|||
.detach();
|
||||
}
|
||||
|
||||
fn is_contains_uppercase(str: &str) -> bool {
|
||||
str.chars().any(|c| c.is_uppercase())
|
||||
}
|
||||
|
||||
pub struct ProjectSearch {
|
||||
project: Model<Project>,
|
||||
excerpts: Model<MultiBuffer>,
|
||||
|
@ -651,7 +655,22 @@ impl ProjectSearchView {
|
|||
});
|
||||
// Subscribe to query_editor in order to reraise editor events for workspace item activation purposes
|
||||
subscriptions.push(
|
||||
cx.subscribe(&query_editor, |_, _, event: &EditorEvent, cx| {
|
||||
cx.subscribe(&query_editor, |this, _, event: &EditorEvent, cx| {
|
||||
match event {
|
||||
EditorEvent::Edited { .. } => {
|
||||
if EditorSettings::get_global(cx).use_smartcase_search {
|
||||
let query = this.search_query_text(cx);
|
||||
if !query.is_empty() {
|
||||
if this.search_options.contains(SearchOptions::CASE_SENSITIVE)
|
||||
!= is_contains_uppercase(&query)
|
||||
{
|
||||
this.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
cx.emit(ViewEvent::EditorEvent(event.clone()))
|
||||
}),
|
||||
);
|
||||
|
@ -1055,6 +1074,15 @@ impl ProjectSearchView {
|
|||
fn set_query(&mut self, query: &str, cx: &mut ViewContext<Self>) {
|
||||
self.query_editor
|
||||
.update(cx, |query_editor, cx| query_editor.set_text(query, cx));
|
||||
if EditorSettings::get_global(cx).use_smartcase_search {
|
||||
if !query.is_empty() {
|
||||
if self.search_options.contains(SearchOptions::CASE_SENSITIVE)
|
||||
!= is_contains_uppercase(query)
|
||||
{
|
||||
self.toggle_search_option(SearchOptions::CASE_SENSITIVE, cx)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn focus_results_editor(&mut self, cx: &mut ViewContext<Self>) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue