WIP: start search2

This commit is contained in:
Piotr Osiewicz
2023-11-13 20:38:37 +01:00
parent 7a454bed22
commit dfd68d4cb8
18 changed files with 5115 additions and 43 deletions
File diff suppressed because it is too large Load Diff
+184
View File
@@ -0,0 +1,184 @@
use smallvec::SmallVec;
const SEARCH_HISTORY_LIMIT: usize = 20;
#[derive(Default, Debug, Clone)]
pub struct SearchHistory {
history: SmallVec<[String; SEARCH_HISTORY_LIMIT]>,
selected: Option<usize>,
}
impl SearchHistory {
pub fn add(&mut self, search_string: String) {
if let Some(i) = self.selected {
if search_string == self.history[i] {
return;
}
}
if let Some(previously_searched) = self.history.last_mut() {
if search_string.find(previously_searched.as_str()).is_some() {
*previously_searched = search_string;
self.selected = Some(self.history.len() - 1);
return;
}
}
self.history.push(search_string);
if self.history.len() > SEARCH_HISTORY_LIMIT {
self.history.remove(0);
}
self.selected = Some(self.history.len() - 1);
}
pub fn next(&mut self) -> Option<&str> {
let history_size = self.history.len();
if history_size == 0 {
return None;
}
let selected = self.selected?;
if selected == history_size - 1 {
return None;
}
let next_index = selected + 1;
self.selected = Some(next_index);
Some(&self.history[next_index])
}
pub fn current(&self) -> Option<&str> {
Some(&self.history[self.selected?])
}
pub fn previous(&mut self) -> Option<&str> {
let history_size = self.history.len();
if history_size == 0 {
return None;
}
let prev_index = match self.selected {
Some(selected_index) => {
if selected_index == 0 {
return None;
} else {
selected_index - 1
}
}
None => history_size - 1,
};
self.selected = Some(prev_index);
Some(&self.history[prev_index])
}
pub fn reset_selection(&mut self) {
self.selected = None;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() {
let mut search_history = SearchHistory::default();
assert_eq!(
search_history.current(),
None,
"No current selection should be set fo the default search history"
);
search_history.add("rust".to_string());
assert_eq!(
search_history.current(),
Some("rust"),
"Newly added item should be selected"
);
// check if duplicates are not added
search_history.add("rust".to_string());
assert_eq!(
search_history.history.len(),
1,
"Should not add a duplicate"
);
assert_eq!(search_history.current(), Some("rust"));
// check if new string containing the previous string replaces it
search_history.add("rustlang".to_string());
assert_eq!(
search_history.history.len(),
1,
"Should replace previous item if it's a substring"
);
assert_eq!(search_history.current(), Some("rustlang"));
// push enough items to test SEARCH_HISTORY_LIMIT
for i in 0..SEARCH_HISTORY_LIMIT * 2 {
search_history.add(format!("item{i}"));
}
assert!(search_history.history.len() <= SEARCH_HISTORY_LIMIT);
}
#[test]
fn test_next_and_previous() {
let mut search_history = SearchHistory::default();
assert_eq!(
search_history.next(),
None,
"Default search history should not have a next item"
);
search_history.add("Rust".to_string());
assert_eq!(search_history.next(), None);
search_history.add("JavaScript".to_string());
assert_eq!(search_history.next(), None);
search_history.add("TypeScript".to_string());
assert_eq!(search_history.next(), None);
assert_eq!(search_history.current(), Some("TypeScript"));
assert_eq!(search_history.previous(), Some("JavaScript"));
assert_eq!(search_history.current(), Some("JavaScript"));
assert_eq!(search_history.previous(), Some("Rust"));
assert_eq!(search_history.current(), Some("Rust"));
assert_eq!(search_history.previous(), None);
assert_eq!(search_history.current(), Some("Rust"));
assert_eq!(search_history.next(), Some("JavaScript"));
assert_eq!(search_history.current(), Some("JavaScript"));
assert_eq!(search_history.next(), Some("TypeScript"));
assert_eq!(search_history.current(), Some("TypeScript"));
assert_eq!(search_history.next(), None);
assert_eq!(search_history.current(), Some("TypeScript"));
}
#[test]
fn test_reset_selection() {
let mut search_history = SearchHistory::default();
search_history.add("Rust".to_string());
search_history.add("JavaScript".to_string());
search_history.add("TypeScript".to_string());
assert_eq!(search_history.current(), Some("TypeScript"));
search_history.reset_selection();
assert_eq!(search_history.current(), None);
assert_eq!(
search_history.previous(),
Some("TypeScript"),
"Should start from the end after reset on previous item query"
);
search_history.previous();
assert_eq!(search_history.current(), Some("JavaScript"));
search_history.previous();
assert_eq!(search_history.current(), Some("Rust"));
search_history.reset_selection();
assert_eq!(search_history.current(), None);
}
}
+65
View File
@@ -0,0 +1,65 @@
use gpui::Action;
use crate::{ActivateRegexMode, ActivateSemanticMode, ActivateTextMode};
// TODO: Update the default search mode to get from config
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub enum SearchMode {
#[default]
Text,
Semantic,
Regex,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub(crate) enum Side {
Left,
Right,
}
impl SearchMode {
pub(crate) fn label(&self) -> &'static str {
match self {
SearchMode::Text => "Text",
SearchMode::Semantic => "Semantic",
SearchMode::Regex => "Regex",
}
}
pub(crate) fn region_id(&self) -> usize {
match self {
SearchMode::Text => 3,
SearchMode::Semantic => 4,
SearchMode::Regex => 5,
}
}
pub(crate) fn tooltip_text(&self) -> &'static str {
match self {
SearchMode::Text => "Activate Text Search",
SearchMode::Semantic => "Activate Semantic Search",
SearchMode::Regex => "Activate Regex Search",
}
}
pub(crate) fn activate_action(&self) -> Box<dyn Action> {
match self {
SearchMode::Text => Box::new(ActivateTextMode),
SearchMode::Semantic => Box::new(ActivateSemanticMode),
SearchMode::Regex => Box::new(ActivateRegexMode),
}
}
}
pub(crate) fn next_mode(mode: &SearchMode, semantic_enabled: bool) -> SearchMode {
match mode {
SearchMode::Text => SearchMode::Regex,
SearchMode::Regex => {
if semantic_enabled {
SearchMode::Semantic
} else {
SearchMode::Text
}
}
SearchMode::Semantic => SearchMode::Text,
}
}
File diff suppressed because it is too large Load Diff
+115
View File
@@ -0,0 +1,115 @@
use bitflags::bitflags;
pub use buffer_search::BufferSearchBar;
use gpui::{actions, Action, AnyElement, AppContext, Component, Element, Svg, View};
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<V: 'static>(&self, active: bool) -> impl Component<V> {
ui::IconButton::new(0, self.icon())
.on_click({
let action = self.to_toggle_action();
move |_: &mut V, cx| {
cx.dispatch_action(action.boxed_clone());
}
})
.variant(ui::ButtonVariant::Ghost)
.when(active, |button| button.variant(ButtonVariant::Filled))
}
}
fn toggle_replace_button<V: 'static>(active: bool) -> impl Component<V> {
// todo: add toggle_replace button
ui::IconButton::new(0, ui::Icon::Replace)
.on_click(|_: &mut V, cx| {
cx.dispatch_action(Box::new(ToggleReplace));
})
.variant(ui::ButtonVariant::Ghost)
.when(active, |button| button.variant(ButtonVariant::Filled))
}
fn replace_action<V: 'static>(
action: impl Action + 'static + Send + Sync,
name: &'static str,
) -> impl Component<V> {
ui::IconButton::new(0, ui::Icon::Replace).on_click(move |_: &mut V, cx| {
cx.dispatch_action(action.boxed_clone());
})
}
+177
View File
@@ -0,0 +1,177 @@
use std::borrow::Cow;
use gpui::{
div, Action, AnyElement, Component, CursorStyle, Element, MouseButton, MouseDownEvent, Svg,
View, ViewContext,
};
use theme::ActiveTheme;
use ui::Label;
use workspace::searchable::Direction;
use crate::{
mode::{SearchMode, Side},
SelectNextMatch, SelectPrevMatch,
};
pub(super) fn render_nav_button<V: 'static>(
icon: &'static str,
direction: Direction,
active: bool,
on_click: impl Fn(MouseDownEvent, &mut V, &mut ViewContext<V>) + 'static,
cx: &mut ViewContext<V>,
) -> impl Component<V> {
let action: Box<dyn Action>;
let tooltip;
match direction {
Direction::Prev => {
action = Box::new(SelectPrevMatch);
tooltip = "Select Previous Match";
}
Direction::Next => {
action = Box::new(SelectNextMatch);
tooltip = "Select Next Match";
}
};
// let tooltip_style = cx.theme().tooltip.clone();
// let cursor_style = if active {
// CursorStyle::PointingHand
// } else {
// CursorStyle::default()
// };
// enum NavButton {}
div()
// MouseEventHandler::new::<NavButton, _>(direction as usize, cx, |state, cx| {
// let theme = cx.theme();
// let style = theme
// .search
// .nav_button
// .in_state(active)
// .style_for(state)
// .clone();
// let mut container_style = style.container.clone();
// let label = Label::new(icon, style.label.clone()).aligned().contained();
// container_style.corner_radii = match direction {
// Direction::Prev => CornerRadii {
// bottom_right: 0.,
// top_right: 0.,
// ..container_style.corner_radii
// },
// Direction::Next => CornerRadii {
// bottom_left: 0.,
// top_left: 0.,
// ..container_style.corner_radii
// },
// };
// if direction == Direction::Prev {
// // Remove right border so that when both Next and Prev buttons are
// // next to one another, there's no double border between them.
// container_style.border.right = false;
// }
// label.with_style(container_style)
// })
// .on_click(MouseButton::Left, on_click)
// .with_cursor_style(cursor_style)
// .with_tooltip::<NavButton>(
// direction as usize,
// tooltip.to_string(),
// Some(action),
// tooltip_style,
// cx,
// )
// .into_any()
}
pub(crate) fn render_search_mode_button<V: 'static>(
mode: SearchMode,
side: Option<Side>,
is_active: bool,
//on_click: impl Fn(MouseClick, &mut V, &mut ViewContext<V>) + 'static,
cx: &mut ViewContext<V>,
) -> impl Component<V> {
//let tooltip_style = cx.theme().tooltip.clone();
enum SearchModeButton {}
div()
// MouseEventHandler::new::<SearchModeButton, _>(mode.region_id(), cx, |state, cx| {
// let theme = cx.theme();
// let style = theme
// .search
// .mode_button
// .in_state(is_active)
// .style_for(state)
// .clone();
// let mut container_style = style.container;
// if let Some(button_side) = side {
// if button_side == Side::Left {
// container_style.border.left = true;
// container_style.corner_radii = CornerRadii {
// bottom_right: 0.,
// top_right: 0.,
// ..container_style.corner_radii
// };
// } else {
// container_style.border.left = false;
// container_style.corner_radii = CornerRadii {
// bottom_left: 0.,
// top_left: 0.,
// ..container_style.corner_radii
// };
// }
// } else {
// container_style.border.left = false;
// container_style.corner_radii = CornerRadii::default();
// }
// Label::new(mode.label(), style.text)
// .aligned()
// .contained()
// .with_style(container_style)
// .constrained()
// .with_height(theme.search.search_bar_row_height)
// })
// .on_click(MouseButton::Left, on_click)
// .with_cursor_style(CursorStyle::PointingHand)
// .with_tooltip::<SearchModeButton>(
// mode.region_id(),
// mode.tooltip_text().to_owned(),
// Some(mode.activate_action()),
// tooltip_style,
// cx,
// )
// .into_any()
}
pub(crate) fn render_option_button_icon<V: 'static>(
is_active: bool,
icon: &'static str,
id: usize,
label: impl Into<Cow<'static, str>>,
action: Box<dyn Action>,
//on_click: impl Fn(MouseClick, &mut V, &mut EventContext<V>) + 'static,
cx: &mut ViewContext<V>,
) -> impl Component<V> {
//let tooltip_style = cx.theme().tooltip.clone();
div()
// MouseEventHandler::new::<V, _>(id, cx, |state, cx| {
// let theme = cx.theme();
// let style = theme
// .search
// .option_button
// .in_state(is_active)
// .style_for(state);
// Svg::new(icon)
// .with_color(style.color.clone())
// .constrained()
// .with_width(style.icon_width)
// .contained()
// .with_style(style.container)
// .constrained()
// .with_height(theme.search.option_button_height)
// .with_width(style.button_width)
// })
// .on_click(MouseButton::Left, on_click)
// .with_cursor_style(CursorStyle::PointingHand)
// .with_tooltip::<V>(id, label, Some(action), tooltip_style, cx)
// .into_any()
}