search: Allow running a search with different options
Refactor search options to use bitflags so that we can represent the entire set of settings in one place.
This commit is contained in:
parent
20d8a2a1ec
commit
75fe77c11d
5 changed files with 196 additions and 100 deletions
|
@ -1,5 +1,7 @@
|
|||
use bitflags::bitflags;
|
||||
pub use buffer_search::BufferSearchBar;
|
||||
use gpui::{actions, Action, AppContext};
|
||||
use project::search::SearchQuery;
|
||||
pub use project_search::{ProjectSearchBar, ProjectSearchView};
|
||||
|
||||
pub mod buffer_search;
|
||||
|
@ -21,27 +23,40 @@ actions!(
|
|||
]
|
||||
);
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub enum SearchOption {
|
||||
WholeWord,
|
||||
CaseSensitive,
|
||||
Regex,
|
||||
bitflags! {
|
||||
#[derive(Default)]
|
||||
pub struct SearchOptions: u8 {
|
||||
const NONE = 0b000;
|
||||
const WHOLE_WORD = 0b001;
|
||||
const CASE_SENSITIVE = 0b010;
|
||||
const REGEX = 0b100;
|
||||
}
|
||||
}
|
||||
|
||||
impl SearchOption {
|
||||
impl SearchOptions {
|
||||
pub fn label(&self) -> &'static str {
|
||||
match self {
|
||||
SearchOption::WholeWord => "Match Whole Word",
|
||||
SearchOption::CaseSensitive => "Match Case",
|
||||
SearchOption::Regex => "Use Regular Expression",
|
||||
match *self {
|
||||
SearchOptions::WHOLE_WORD => "Match Whole Word",
|
||||
SearchOptions::CASE_SENSITIVE => "Match Case",
|
||||
SearchOptions::REGEX => "Use Regular Expression",
|
||||
_ => panic!("{:?} is not a named SearchOption", self),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_toggle_action(&self) -> Box<dyn Action> {
|
||||
match self {
|
||||
SearchOption::WholeWord => Box::new(ToggleWholeWord),
|
||||
SearchOption::CaseSensitive => Box::new(ToggleCaseSensitive),
|
||||
SearchOption::Regex => Box::new(ToggleRegex),
|
||||
match *self {
|
||||
SearchOptions::WHOLE_WORD => Box::new(ToggleWholeWord),
|
||||
SearchOptions::CASE_SENSITIVE => Box::new(ToggleCaseSensitive),
|
||||
SearchOptions::REGEX => Box::new(ToggleRegex),
|
||||
_ => panic!("{:?} is not a named SearchOption", self),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_query(query: &SearchQuery) -> SearchOptions {
|
||||
let mut options = SearchOptions::NONE;
|
||||
options.set(SearchOptions::WHOLE_WORD, query.whole_word());
|
||||
options.set(SearchOptions::CASE_SENSITIVE, query.case_sensitive());
|
||||
options.set(SearchOptions::REGEX, query.is_regex());
|
||||
options
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue