Skip to content
Open
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
23 changes: 23 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ members = [
"crates/scheduler",
"crates/schema_generator",
"crates/search",
"crates/search_everywhere",
"crates/session",
"crates/settings",
"crates/settings_content",
Expand Down Expand Up @@ -432,6 +433,7 @@ rpc = { path = "crates/rpc" }
rules_library = { path = "crates/rules_library" }
scheduler = { path = "crates/scheduler" }
search = { path = "crates/search" }
search_everywhere = { path = "crates/search_everywhere" }
session = { path = "crates/session" }
sidebar = { path = "crates/sidebar" }
settings = { path = "crates/settings" }
Expand Down
6 changes: 5 additions & 1 deletion assets/keymaps/linux/jetbrains.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
"ctrl-alt-n": "file_finder::Toggle",
"ctrl-n": "project_symbols::Toggle",
"ctrl-shift-a": "command_palette::Toggle",
"shift shift": "command_palette::Toggle",
"shift shift": "search_everywhere::Toggle",
"ctrl-alt-shift-n": "project_symbols::Toggle",
"alt-0": "git_panel::ToggleFocus",
"alt-1": "project_panel::ToggleFocus",
Expand Down Expand Up @@ -192,6 +192,10 @@
},
},
{
"context": "SearchEverywhere",
"bindings": {
"tab": "search_everywhere::NextTab",
"shift-tab": "search_everywhere::PreviousTab",
"context": "Editor && mode == auto_height",
"bindings": {
"escape": "editor::Cancel",
Expand Down
6 changes: 5 additions & 1 deletion assets/keymaps/macos/jetbrains.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
"cmd-shift-n": "file_finder::Toggle",
"cmd-n": "project_symbols::Toggle",
"cmd-shift-a": "command_palette::Toggle",
"shift shift": "command_palette::Toggle",
"shift shift": "search_everywhere::Toggle",
"cmd-alt-o": "project_symbols::Toggle", // JetBrains: Go to Symbol
"cmd-o": "project_symbols::Toggle", // JetBrains: Go to Class
"cmd-1": "project_panel::ToggleFocus",
Expand Down Expand Up @@ -196,6 +196,10 @@
},
},
{
"context": "SearchEverywhere",
"bindings": {
"tab": "search_everywhere::NextTab",
"shift-tab": "search_everywhere::PreviousTab",
"context": "Editor && mode == auto_height",
"bindings": {
"escape": "editor::Cancel",
Expand Down
38 changes: 38 additions & 0 deletions crates/search_everywhere/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "search_everywhere"
version = "0.1.0"
edition.workspace = true
publish.workspace = true
license = "GPL-3.0-or-later"

[lints]
workspace = true

[lib]
path = "src/search_everywhere.rs"
doctest = false

[dependencies]
anyhow.workspace = true
collections.workspace = true
command_palette_hooks.workspace = true
editor.workspace = true
futures.workspace = true
fuzzy.workspace = true
gpui.workspace = true
language.workspace = true
log.workspace = true
parking_lot.workspace = true
picker.workspace = true
project.workspace = true
ui.workspace = true
util.workspace = true
workspace.workspace = true

[dev-dependencies]
ctor.workspace = true
editor = { workspace = true, features = ["test-support"] }
gpui = { workspace = true, features = ["test-support"] }
language = { workspace = true, features = ["test-support"] }
picker = { workspace = true, features = ["test-support"] }
workspace = { workspace = true, features = ["test-support"] }
1 change: 1 addition & 0 deletions crates/search_everywhere/LICENSE-GPL
136 changes: 136 additions & 0 deletions crates/search_everywhere/src/actions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use command_palette_hooks::CommandPaletteFilter;
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{Action, SharedString, Task, Window};

use crate::SearchEverywhereDelegate;
use crate::providers::{SearchResult, SearchResultCategory};

pub struct ActionProvider {
commands: Vec<Command>,
}

struct Command {
name: String,
action: Box<dyn Action>,
}

impl ActionProvider {
pub fn new<T: 'static>(window: &mut Window, cx: &mut gpui::Context<T>) -> Self {
let filter = CommandPaletteFilter::try_global(cx);

let commands = window
.available_actions(cx)
.into_iter()
.filter_map(|action| {
if filter.is_some_and(|filter| filter.is_hidden(&*action)) {
return None;
}

Some(Command {
name: humanize_action_name(action.name()),
action,
})
})
.collect();

Self { commands }
}

pub fn search(
&self,
query: &str,
_window: &mut Window,
cx: &mut gpui::Context<picker::Picker<SearchEverywhereDelegate>>,
) -> Task<Vec<(SearchResult, StringMatch)>> {
if query.is_empty() {
return Task::ready(Vec::new());
}

let candidates: Vec<StringMatchCandidate> = self
.commands
.iter()
.enumerate()
.map(|(id, c)| StringMatchCandidate::new(id, &c.name))
.collect();

let query = query.to_string();
let commands: Vec<_> = self
.commands
.iter()
.map(|c| (c.name.clone(), c.action.boxed_clone()))
.collect();

cx.spawn(async move |_, cx| {
let matches = fuzzy::match_strings(
&candidates,
&query,
true,
true,
100,
&Default::default(),
cx.background_executor().clone(),
)
.await;

matches
.into_iter()
.filter_map(|m| {
let (name, action) = commands.get(m.candidate_id)?;

let result = SearchResult {
label: SharedString::from(name.clone()),
detail: None,
category: SearchResultCategory::Action,
path: None,
action: Some(action.boxed_clone()),
symbol: None,
document_symbol: None,
};

Some((result, m))
})
.collect()
})
}
}

fn humanize_action_name(name: &str) -> String {
let capacity = name.len() + name.chars().filter(|c| c.is_uppercase()).count();
let mut result = String::with_capacity(capacity);

for char in name.chars() {
if char == ':' {
if result.ends_with(':') {
result.push(' ');
} else {
result.push(':');
}
} else if char == '_' {
result.push(' ');
} else if char.is_uppercase() {
if !result.ends_with(' ') && !result.ends_with(':') {
result.push(' ');
}
result.extend(char.to_lowercase());
} else {
result.push(char);
}
}

let mut title_cased = String::with_capacity(result.len());
let mut should_capitalize = true;

for char in result.chars() {
if should_capitalize && char.is_alphabetic() {
title_cased.extend(char.to_uppercase());
should_capitalize = false;
} else {
title_cased.push(char);
if char == ' ' || char == ':' {
should_capitalize = true;
}
}
}

title_cased
}
109 changes: 109 additions & 0 deletions crates/search_everywhere/src/files.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{App, Entity, SharedString, Task};
use project::{Project, ProjectPath, WorktreeId};

use crate::SearchEverywhereDelegate;
use crate::providers::{SearchResult, SearchResultCategory};

pub struct FileProvider {
project: Entity<Project>,
}

impl FileProvider {
pub fn new(project: Entity<Project>, _cx: &App) -> Self {
Self { project }
}

pub fn search(
&self,
query: &str,
cx: &mut gpui::Context<picker::Picker<SearchEverywhereDelegate>>,
) -> Task<Vec<(SearchResult, StringMatch)>> {
if query.is_empty() {
return Task::ready(Vec::new());
}

let project = self.project.clone();
let query = query.to_string();

cx.spawn(async move |_, cx| {
let Some(candidates) = cx
.update(|cx| {
let project = project.read(cx);
let worktrees = project.visible_worktrees(cx).collect::<Vec<_>>();

let mut candidates = Vec::new();
for worktree in worktrees {
let worktree = worktree.read(cx);
let worktree_id = worktree.id();

for entry in worktree.files(false, 0) {
let path = entry.path.as_unix_str().to_string();
candidates.push(FileCandidate {
path: path.clone(),
worktree_id,
project_path: ProjectPath {
worktree_id,
path: entry.path.clone(),
},
});
}
}
candidates
})
.ok()
else {
return Vec::new();
};

let string_candidates: Vec<StringMatchCandidate> = candidates
.iter()
.enumerate()
.map(|(id, c)| StringMatchCandidate::new(id, &c.path))
.collect();

let matches = fuzzy::match_strings(
&string_candidates,
&query,
true,
true,
100,
&Default::default(),
cx.background_executor().clone(),
)
.await;

matches
.into_iter()
.filter_map(|m| {
let candidate = candidates.get(m.candidate_id)?;
let file_name = candidate
.project_path
.path
.file_name()
.map(|n| n.to_string())
.unwrap_or_else(|| candidate.path.clone());

let result = SearchResult {
label: SharedString::from(file_name),
detail: Some(SharedString::from(candidate.path.clone())),
category: SearchResultCategory::File,
path: Some(candidate.project_path.clone()),
action: None,
symbol: None,
document_symbol: None,
};

Some((result, m))
})
.collect()
})
}
}

struct FileCandidate {
path: String,
#[allow(dead_code)]
worktree_id: WorktreeId,
project_path: ProjectPath,
}
Loading
Loading