1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use bitflags::bitflags;
pub use buffer_search::BufferSearchBar;
use gpui::{actions, Action, AppContext, RenderOnce};
pub use mode::SearchMode;
use project::search::SearchQuery;
use ui::ButtonVariant;
//pub use project_search::{ProjectSearchBar, ProjectSearchView};
// use theme::components::{
//     action_button::Button, svg::Svg, ComponentExt, IconButtonStyle, ToggleIconButtonStyle,
// };

pub mod buffer_search;
mod history;
mod mode;
//pub mod project_search;
pub(crate) mod search_bar;

pub fn init(cx: &mut AppContext) {
    buffer_search::init(cx);
    //project_search::init(cx);
}

actions!(
    CycleMode,
    ToggleWholeWord,
    ToggleCaseSensitive,
    ToggleReplace,
    SelectNextMatch,
    SelectPrevMatch,
    SelectAllMatches,
    NextHistoryQuery,
    PreviousHistoryQuery,
    ActivateTextMode,
    ActivateSemanticMode,
    ActivateRegexMode,
    ReplaceAll,
    ReplaceNext,
);

bitflags! {
    #[derive(Default)]
    pub struct SearchOptions: u8 {
        const NONE = 0b000;
        const WHOLE_WORD = 0b001;
        const CASE_SENSITIVE = 0b010;
    }
}

impl SearchOptions {
    pub fn label(&self) -> &'static str {
        match *self {
            SearchOptions::WHOLE_WORD => "Match Whole Word",
            SearchOptions::CASE_SENSITIVE => "Match Case",
            _ => panic!("{:?} is not a named SearchOption", self),
        }
    }

    pub fn icon(&self) -> ui::Icon {
        match *self {
            SearchOptions::WHOLE_WORD => ui::Icon::WholeWord,
            SearchOptions::CASE_SENSITIVE => ui::Icon::CaseSensitive,
            _ => panic!("{:?} is not a named SearchOption", self),
        }
    }

    pub fn to_toggle_action(&self) -> Box<dyn Action + Sync + Send + 'static> {
        match *self {
            SearchOptions::WHOLE_WORD => Box::new(ToggleWholeWord),
            SearchOptions::CASE_SENSITIVE => Box::new(ToggleCaseSensitive),
            _ => panic!("{:?} is not a named SearchOption", self),
        }
    }

    pub fn none() -> SearchOptions {
        SearchOptions::NONE
    }

    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
    }

    pub fn as_button(&self, active: bool) -> impl RenderOnce {
        ui::IconButton::new(0, self.icon())
            .on_click({
                let action = self.to_toggle_action();
                move |_, cx| {
                    cx.dispatch_action(action.boxed_clone());
                }
            })
            .variant(ui::ButtonVariant::Ghost)
            .when(active, |button| button.variant(ButtonVariant::Filled))
    }
}

fn toggle_replace_button(active: bool) -> impl RenderOnce {
    // todo: add toggle_replace button
    ui::IconButton::new(0, ui::Icon::Replace)
        .on_click(|_, cx| {
            cx.dispatch_action(Box::new(ToggleReplace));
            cx.notify();
        })
        .variant(ui::ButtonVariant::Ghost)
        .when(active, |button| button.variant(ButtonVariant::Filled))
}

fn render_replace_button(
    action: impl Action + 'static + Send + Sync,
    icon: ui::Icon,
) -> impl RenderOnce {
    // todo: add tooltip
    ui::IconButton::new(0, icon).on_click(move |_, cx| {
        cx.dispatch_action(action.boxed_clone());
    })
}