Refactor search options to use bitflags so that we can represent the entire set of settings in one place.
63 lines
1.7 KiB
Rust
63 lines
1.7 KiB
Rust
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;
|
|
pub mod project_search;
|
|
|
|
pub fn init(cx: &mut AppContext) {
|
|
buffer_search::init(cx);
|
|
project_search::init(cx);
|
|
}
|
|
|
|
actions!(
|
|
search,
|
|
[
|
|
ToggleWholeWord,
|
|
ToggleCaseSensitive,
|
|
ToggleRegex,
|
|
SelectNextMatch,
|
|
SelectPrevMatch
|
|
]
|
|
);
|
|
|
|
bitflags! {
|
|
#[derive(Default)]
|
|
pub struct SearchOptions: u8 {
|
|
const NONE = 0b000;
|
|
const WHOLE_WORD = 0b001;
|
|
const CASE_SENSITIVE = 0b010;
|
|
const REGEX = 0b100;
|
|
}
|
|
}
|
|
|
|
impl SearchOptions {
|
|
pub fn label(&self) -> &'static str {
|
|
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 {
|
|
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
|
|
}
|
|
}
|