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 Include Ignored Toggle to File Finder and implement support for SearchOption flags on other Pickers #9760

Closed
Closed
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
2 changes: 2 additions & 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/file_finder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ itertools = "0.11"
menu.workspace = true
picker.workspace = true
project.workspace = true
search.workspace = true
text.workspace = true
theme.workspace = true
ui.workspace = true
Expand Down
28 changes: 25 additions & 3 deletions crates/file_finder/src/file_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ use gpui::{
ViewContext, VisualContext, WeakView,
};
use itertools::Itertools;
use picker::SupportedSearchOptions;
use picker::{Picker, PickerDelegate};
use project::{PathMatchCandidateSet, Project, ProjectPath, WorktreeId};
use search::SearchOptions;
use std::{
cmp,
path::{Path, PathBuf},
Expand Down Expand Up @@ -149,6 +151,8 @@ pub struct FileFinderDelegate {
has_changed_selected_index: bool,
cancel_flag: Arc<AtomicBool>,
history_items: Vec<FoundPath>,
search_options: SearchOptions,
supported_search_options: SupportedSearchOptions,
}

/// Use a custom ordering for file finder: the regular one
Expand Down Expand Up @@ -404,6 +408,8 @@ impl FileFinderDelegate {
selected_index: 0,
cancel_flag: Arc::new(AtomicBool::new(false)),
history_items,
search_options: SearchOptions::NONE,
supported_search_options: SupportedSearchOptions::new(true),
}
}

Expand Down Expand Up @@ -442,9 +448,13 @@ impl FileFinderDelegate {
let worktree = worktree.read(cx);
PathMatchCandidateSet {
snapshot: worktree.snapshot(),
include_ignored: worktree
.root_entry()
.map_or(false, |entry| entry.is_ignored),
include_ignored: worktree.root_entry().map_or(false, |entry| {
if self.search_options.contains(SearchOptions::INCLUDE_IGNORED) {
true
} else {
entry.is_ignored
}
}),
include_root_name,
}
})
Expand Down Expand Up @@ -695,6 +705,18 @@ impl FileFinderDelegate {
impl PickerDelegate for FileFinderDelegate {
type ListItem = ListItem;

fn supported_search_options(&self) -> SupportedSearchOptions {
self.supported_search_options
}

fn search_options(&self) -> SearchOptions {
self.search_options
}

fn toggle_include_ignored(&mut self) {
self.search_options.toggle(SearchOptions::INCLUDE_IGNORED);
}

fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
"Search project files...".into()
}
Expand Down
1 change: 1 addition & 0 deletions crates/picker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ anyhow.workspace = true
editor.workspace = true
gpui.workspace = true
menu.workspace = true
search.workspace = true
ui.workspace = true
workspace.workspace = true

Expand Down
47 changes: 46 additions & 1 deletion crates/picker/src/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use gpui::{
UniformListScrollHandle, View, ViewContext, WindowContext,
};
use head::Head;
use search::SearchOptions;
use std::{sync::Arc, time::Duration};
use ui::{prelude::*, v_flex, Color, Divider, Label, ListItem, ListItemSpacing};
use workspace::ModalView;
Expand Down Expand Up @@ -38,9 +39,33 @@ pub struct Picker<D: PickerDelegate> {
is_modal: bool,
}

#[derive(Copy, Clone)]
pub struct SupportedSearchOptions {
include_ignored: bool,
}

impl SupportedSearchOptions {
pub fn new(include_ignored: bool) -> Self {
Self { include_ignored }
}

pub fn default() -> Self {
Self::new(false)
}
}

pub trait PickerDelegate: Sized + 'static {
type ListItem: IntoElement;

fn search_options(&self) -> SearchOptions {
SearchOptions::NONE
}
fn supported_search_options(&self) -> SupportedSearchOptions {
SupportedSearchOptions::default()
}

fn toggle_include_ignored(&mut self) {}

fn match_count(&self) -> usize;
fn selected_index(&self) -> usize;
fn separators_after_indices(&self) -> Vec<usize> {
Expand Down Expand Up @@ -430,6 +455,25 @@ impl<D: PickerDelegate> Picker<D> {
.into_any_element(),
}
}

fn render_search_buttons(&self, cx: &mut ViewContext<Self>) -> Vec<impl IntoElement> {
let mut buttons = vec![];
if self.delegate.supported_search_options().include_ignored {
buttons.push(
SearchOptions::INCLUDE_IGNORED.as_button(
self.delegate
.search_options()
.contains(SearchOptions::INCLUDE_IGNORED),
cx.listener(|this, _, cx| {
this.delegate.toggle_include_ignored();
cx.notify();
this.update_matches(this.query(cx).to_string(), cx);
}),
),
);
}
buttons
}
}

impl<D: PickerDelegate> EventEmitter<DismissEvent> for Picker<D> {}
Expand Down Expand Up @@ -463,7 +507,8 @@ impl<D: PickerDelegate> Render for Picker<D> {
.flex_none()
.h_9()
.px_4()
.child(editor.clone()),
.child(editor.clone())
.children(self.render_search_buttons(cx)),
)
.child(Divider::horizontal()),
Head::Empty(empty_head) => div().child(empty_head.clone()),
Expand Down
Loading