Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file filter to buffer search #22910

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/language_tools/src/lsp_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,7 @@ impl SearchableItem for LspLogView {
// LSP log is read-only.
replacement: false,
selection: false,
filters: false,
}
}
fn active_match_index(
Expand Down
1 change: 1 addition & 0 deletions crates/search/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ editor.workspace = true
futures.workspace = true
gpui.workspace = true
language.workspace = true
log.workspace = true
menu.workspace = true
project.workspace = true
serde.workspace = true
Expand Down
156 changes: 151 additions & 5 deletions crates/search/src/buffer_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ mod registrar;
use crate::{
search_bar::render_nav_button, FocusSearch, NextHistoryQuery, PreviousHistoryQuery, ReplaceAll,
ReplaceNext, SearchOptions, SelectAllMatches, SelectNextMatch, SelectPrevMatch,
ToggleCaseSensitive, ToggleRegex, ToggleReplace, ToggleSelection, ToggleWholeWord,
ToggleCaseSensitive, ToggleFilters, ToggleRegex, ToggleReplace, ToggleSelection,
ToggleWholeWord,
};
use any_vec::AnyVec;
use collections::HashMap;
Expand All @@ -18,6 +19,8 @@ use gpui::{
Render, ScrollHandle, Styled, Subscription, Task, TextStyle, View, ViewContext,
VisualContext as _, WindowContext,
};
use util::paths::PathMatcher;

use project::{
search::SearchQuery,
search_history::{SearchHistory, SearchHistoryCursor},
Expand Down Expand Up @@ -81,6 +84,10 @@ pub struct BufferSearchBar {
query_editor_focused: bool,
replacement_editor: View<Editor>,
replacement_editor_focused: bool,
included_files_editor: View<Editor>,
included_files_editor_focused: bool,
excluded_files_editor: View<Editor>,
excluded_files_editor_focused: bool,
active_searchable_item: Option<Box<dyn SearchableItemHandle>>,
active_match_index: Option<usize>,
active_searchable_item_subscription: Option<Subscription>,
Expand All @@ -96,6 +103,7 @@ pub struct BufferSearchBar {
search_history_cursor: SearchHistoryCursor,
replace_enabled: bool,
selection_search_enabled: bool,
filters_enabled: bool,
scroll_handle: ScrollHandle,
editor_scroll_handle: ScrollHandle,
editor_needed_width: Pixels,
Expand Down Expand Up @@ -193,6 +201,8 @@ impl Render for BufferSearchBar {
let should_show_replace_input = self.replace_enabled && supported_options.replacement;
let in_replace = self.replacement_editor.focus_handle(cx).is_focused(cx);

let should_show_filters = self.filters_enabled && supported_options.filters;

let mut key_context = KeyContext::new_with_defaults();
key_context.add("BufferSearchBar");
if in_replace {
Expand Down Expand Up @@ -265,6 +275,30 @@ impl Render for BufferSearchBar {
h_flex()
.gap_1()
.min_w_64()
.when(supported_options.filters, |this| {
this.child(
IconButton::new("project-search-filter-button", IconName::Filter)
.shape(IconButtonShape::Square)
.tooltip(|cx| {
Tooltip::for_action("Toggle Filters", &ToggleFilters, cx)
})
.on_click(cx.listener(|this, _, cx| {
this.toggle_filters(cx);
}))
.toggle_state(self.filters_enabled)
.tooltip({
let focus_handle = focus_handle.clone();
move |cx| {
Tooltip::for_action_in(
"Toggle Filters",
&ToggleFilters,
&focus_handle,
cx,
)
}
}),
)
})
.when(supported_options.replacement, |this| {
this.child(
IconButton::new(
Expand Down Expand Up @@ -422,6 +456,41 @@ impl Render for BufferSearchBar {
)
});

// TODO: replace with filter line
let filter_line = should_show_filters.then(|| {
h_flex()
.w_full()
.gap_2()
.child(
input_base_styles()
.on_action(
cx.listener(|this, action, cx| this.previous_history_query(action, cx)),
)
.on_action(
cx.listener(|this, action, cx| this.next_history_query(action, cx)),
)
.child(self.render_text_input(
&self.included_files_editor,
cx.theme().colors().text,
cx,
)),
)
.child(
input_base_styles()
.on_action(
cx.listener(|this, action, cx| this.previous_history_query(action, cx)),
)
.on_action(
cx.listener(|this, action, cx| this.next_history_query(action, cx)),
)
.child(self.render_text_input(
&self.excluded_files_editor,
cx.theme().colors().text,
cx,
)),
)
});

v_flex()
.id("buffer_search")
.gap_2()
Expand Down Expand Up @@ -474,6 +543,7 @@ impl Render for BufferSearchBar {
}),
)
.children(replace_line)
.children(filter_line)
}
}

Expand Down Expand Up @@ -586,6 +656,22 @@ impl BufferSearchBar {
let replacement_editor = cx.new_view(Editor::single_line);
cx.subscribe(&replacement_editor, Self::on_replacement_editor_event)
.detach();
let included_files_editor = cx.new_view(|cx| {
let mut editor = Editor::single_line(cx);
editor.set_placeholder_text("Include: crates/**/*.toml", cx);

editor
});
cx.subscribe(&included_files_editor, Self::on_included_files_editor_event)
.detach();
let excluded_files_editor = cx.new_view(|cx| {
let mut editor = Editor::single_line(cx);
editor.set_placeholder_text("Exclude: vendor/*, *.lock", cx);

editor
});
cx.subscribe(&excluded_files_editor, Self::on_excluded_files_editor_event)
.detach();

let search_options = SearchOptions::from_settings(&EditorSettings::get_global(cx).search);

Expand All @@ -594,6 +680,10 @@ impl BufferSearchBar {
query_editor_focused: false,
replacement_editor,
replacement_editor_focused: false,
included_files_editor,
included_files_editor_focused: false,
excluded_files_editor,
excluded_files_editor_focused: false,
active_searchable_item: None,
active_searchable_item_subscription: None,
active_match_index: None,
Expand All @@ -612,6 +702,7 @@ impl BufferSearchBar {
active_search: None,
replace_enabled: false,
selection_search_enabled: false,
filters_enabled: false,
scroll_handle: ScrollHandle::new(),
editor_scroll_handle: ScrollHandle::new(),
editor_needed_width: px(0.),
Expand Down Expand Up @@ -960,6 +1051,32 @@ impl BufferSearchBar {
}
}

fn on_included_files_editor_event(
&mut self,
_: View<Editor>,
event: &editor::EditorEvent,
_: &mut ViewContext<Self>,
) {
match event {
editor::EditorEvent::Focused => self.included_files_editor_focused = true,
editor::EditorEvent::Blurred => self.included_files_editor_focused = false,
_ => {}
}
}

fn on_excluded_files_editor_event(
&mut self,
_: View<Editor>,
event: &editor::EditorEvent,
_: &mut ViewContext<Self>,
) {
match event {
editor::EditorEvent::Focused => self.excluded_files_editor_focused = true,
editor::EditorEvent::Blurred => self.excluded_files_editor_focused = false,
_ => {}
}
}

fn on_active_searchable_item_event(&mut self, event: &SearchEvent, cx: &mut ViewContext<Self>) {
match event {
SearchEvent::MatchesInvalidated => {
Expand Down Expand Up @@ -1032,6 +1149,18 @@ impl BufferSearchBar {

if let Some(active_searchable_item) = self.active_searchable_item.as_ref() {
self.query_contains_error = false;

let included_files =
match Self::parse_path_matches(&self.included_files_editor.read(cx).text(cx)) {
Ok(included_files) => included_files,
Err(_e) => PathMatcher::default(),
};
let excluded_files =
match Self::parse_path_matches(&self.excluded_files_editor.read(cx).text(cx)) {
Ok(excluded_files) => excluded_files,
Err(_e) => PathMatcher::default(),
};

if query.is_empty() {
self.clear_active_searchable_item_matches(cx);
let _ = done_tx.send(());
Expand All @@ -1048,8 +1177,8 @@ impl BufferSearchBar {
self.search_options.contains(SearchOptions::WHOLE_WORD),
self.search_options.contains(SearchOptions::CASE_SENSITIVE),
false,
Default::default(),
Default::default(),
included_files,
excluded_files,
None,
) {
Ok(query) => query.with_replacement(self.replacement(cx)),
Expand All @@ -1066,8 +1195,8 @@ impl BufferSearchBar {
self.search_options.contains(SearchOptions::WHOLE_WORD),
self.search_options.contains(SearchOptions::CASE_SENSITIVE),
false,
Default::default(),
Default::default(),
included_files,
excluded_files,
None,
) {
Ok(query) => query.with_replacement(self.replacement(cx)),
Expand Down Expand Up @@ -1123,6 +1252,16 @@ impl BufferSearchBar {
done_rx
}

fn parse_path_matches(text: &str) -> anyhow::Result<PathMatcher> {
let queries = text
.split(',')
.map(str::trim)
.filter(|maybe_glob_str| !maybe_glob_str.is_empty())
.map(str::to_owned)
.collect::<Vec<_>>();
Ok(PathMatcher::new(&queries)?)
}

pub fn update_match_index(&mut self, cx: &mut ViewContext<Self>) {
let new_index = self
.active_searchable_item
Expand Down Expand Up @@ -1219,6 +1358,13 @@ impl BufferSearchBar {
}
}

fn toggle_filters(&mut self, cx: &mut ViewContext<Self>) -> bool {
self.filters_enabled = !self.filters_enabled;
cx.notify();

true
}

fn replace_next(&mut self, _: &ReplaceNext, cx: &mut ViewContext<Self>) {
let mut should_propagate = true;
if !self.dismissed && self.active_search.is_some() {
Expand Down
1 change: 1 addition & 0 deletions crates/search/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ actions!(
ToggleRegex,
ToggleReplace,
ToggleSelection,
ToggleFilters,
SelectNextMatch,
SelectPrevMatch,
SelectAllMatches,
Expand Down
1 change: 1 addition & 0 deletions crates/terminal_view/src/terminal_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,7 @@ impl SearchableItem for TerminalView {
regex: true,
replacement: false,
selection: false,
filters: false,
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/workspace/src/searchable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct SearchOptions {
/// Specifies whether the supports search & replace.
pub replacement: bool,
pub selection: bool,
pub filters: bool,
}

pub trait SearchableItem: Item + EventEmitter<SearchEvent> {
Expand All @@ -54,6 +55,7 @@ pub trait SearchableItem: Item + EventEmitter<SearchEvent> {
regex: true,
replacement: true,
selection: true,
filters: true, // TODO: should only be true for multi-buffers
}
}

Expand Down
Loading