Skip to content

Commit

Permalink
Improve StringMatchCandidate::new interface (zed-industries#22011)
Browse files Browse the repository at this point in the history
Release Notes:

- N/A
  • Loading branch information
mgsloan authored Dec 14, 2024
1 parent 9daa426 commit 2597065
Show file tree
Hide file tree
Showing 28 changed files with 92 additions and 184 deletions.
2 changes: 1 addition & 1 deletion crates/assistant/src/context_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ impl ContextStore {
let candidates = metadata
.iter()
.enumerate()
.map(|(id, metadata)| StringMatchCandidate::new(id, metadata.title.clone()))
.map(|(id, metadata)| StringMatchCandidate::new(id, &metadata.title))
.collect::<Vec<_>>();
let matches = fuzzy::match_strings(
&candidates,
Expand Down
5 changes: 1 addition & 4 deletions crates/assistant/src/prompt_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1439,10 +1439,7 @@ impl PromptStore {
.iter()
.enumerate()
.filter_map(|(ix, metadata)| {
Some(StringMatchCandidate::new(
ix,
metadata.title.as_ref()?.to_string(),
))
Some(StringMatchCandidate::new(ix, metadata.title.as_ref()?))
})
.collect::<Vec<_>>();
let matches = fuzzy::match_strings(
Expand Down
6 changes: 1 addition & 5 deletions crates/assistant/src/slash_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ impl SlashCommandCompletionProvider {
.command_names(cx)
.into_iter()
.enumerate()
.map(|(ix, def)| StringMatchCandidate {
id: ix,
string: def.to_string(),
char_bag: def.as_ref().into(),
})
.map(|(ix, def)| StringMatchCandidate::new(ix, &def))
.collect::<Vec<_>>();
let command_name = command_name.to_string();
let editor = self.editor.clone();
Expand Down
5 changes: 1 addition & 4 deletions crates/assistant/src/slash_command/diagnostics_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,7 @@ impl Options {
}

fn match_candidates_for_args() -> [StringMatchCandidate; 1] {
[StringMatchCandidate::new(
0,
INCLUDE_WARNINGS_ARGUMENT.to_string(),
)]
[StringMatchCandidate::new(0, INCLUDE_WARNINGS_ARGUMENT)]
}
}

Expand Down
6 changes: 1 addition & 5 deletions crates/assistant/src/slash_command/tab_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,7 @@ fn tab_items_for_queries(
.enumerate()
.filter_map(|(id, (full_path, ..))| {
let path_string = full_path.as_deref()?.to_string_lossy().to_string();
Some(fuzzy::StringMatchCandidate {
id,
char_bag: path_string.as_str().into(),
string: path_string,
})
Some(fuzzy::StringMatchCandidate::new(id, &path_string))
})
.collect::<Vec<_>>();
let mut processed_matches = HashSet::default();
Expand Down
12 changes: 2 additions & 10 deletions crates/collab_ui/src/chat_panel/message_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,7 @@ impl MessageEditor {

let candidates = names
.into_iter()
.map(|user| StringMatchCandidate {
id: 0,
string: user.clone(),
char_bag: user.chars().collect(),
})
.map(|user| StringMatchCandidate::new(0, &user))
.collect::<Vec<_>>();

Some((start_anchor, query, candidates))
Expand All @@ -401,11 +397,7 @@ impl MessageEditor {
LazyLock::new(|| {
let emojis = emojis::iter()
.flat_map(|s| s.shortcodes())
.map(|emoji| StringMatchCandidate {
id: 0,
string: emoji.to_string(),
char_bag: emoji.chars().collect(),
})
.map(|emoji| StringMatchCandidate::new(0, emoji))
.collect::<Vec<_>>();
emojis
});
Expand Down
104 changes: 38 additions & 66 deletions crates/collab_ui/src/collab_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,8 @@ impl CollabPanel {
// Populate the active user.
if let Some(user) = user_store.current_user() {
self.match_candidates.clear();
self.match_candidates.push(StringMatchCandidate {
id: 0,
string: user.github_login.clone(),
char_bag: user.github_login.chars().collect(),
});
self.match_candidates
.push(StringMatchCandidate::new(0, &user.github_login));
let matches = executor.block(match_strings(
&self.match_candidates,
&query,
Expand Down Expand Up @@ -436,11 +433,10 @@ impl CollabPanel {
self.match_candidates.clear();
self.match_candidates
.extend(room.remote_participants().values().map(|participant| {
StringMatchCandidate {
id: participant.user.id as usize,
string: participant.user.github_login.clone(),
char_bag: participant.user.github_login.chars().collect(),
}
StringMatchCandidate::new(
participant.user.id as usize,
&participant.user.github_login,
)
}));
let mut matches = executor.block(match_strings(
&self.match_candidates,
Expand Down Expand Up @@ -489,10 +485,8 @@ impl CollabPanel {
self.match_candidates.clear();
self.match_candidates
.extend(room.pending_participants().iter().enumerate().map(
|(id, participant)| StringMatchCandidate {
id,
string: participant.github_login.clone(),
char_bag: participant.github_login.chars().collect(),
|(id, participant)| {
StringMatchCandidate::new(id, &participant.github_login)
},
));
let matches = executor.block(match_strings(
Expand All @@ -519,17 +513,12 @@ impl CollabPanel {

if channel_store.channel_count() > 0 || self.channel_editing_state.is_some() {
self.match_candidates.clear();
self.match_candidates
.extend(
channel_store
.ordered_channels()
.enumerate()
.map(|(ix, (_, channel))| StringMatchCandidate {
id: ix,
string: channel.name.clone().into(),
char_bag: channel.name.chars().collect(),
}),
);
self.match_candidates.extend(
channel_store
.ordered_channels()
.enumerate()
.map(|(ix, (_, channel))| StringMatchCandidate::new(ix, &channel.name)),
);
let matches = executor.block(match_strings(
&self.match_candidates,
&query,
Expand Down Expand Up @@ -600,14 +589,12 @@ impl CollabPanel {
let channel_invites = channel_store.channel_invitations();
if !channel_invites.is_empty() {
self.match_candidates.clear();
self.match_candidates
.extend(channel_invites.iter().enumerate().map(|(ix, channel)| {
StringMatchCandidate {
id: ix,
string: channel.name.clone().into(),
char_bag: channel.name.chars().collect(),
}
}));
self.match_candidates.extend(
channel_invites
.iter()
.enumerate()
.map(|(ix, channel)| StringMatchCandidate::new(ix, &channel.name)),
);
let matches = executor.block(match_strings(
&self.match_candidates,
&query,
Expand Down Expand Up @@ -637,17 +624,12 @@ impl CollabPanel {
let incoming = user_store.incoming_contact_requests();
if !incoming.is_empty() {
self.match_candidates.clear();
self.match_candidates
.extend(
incoming
.iter()
.enumerate()
.map(|(ix, user)| StringMatchCandidate {
id: ix,
string: user.github_login.clone(),
char_bag: user.github_login.chars().collect(),
}),
);
self.match_candidates.extend(
incoming
.iter()
.enumerate()
.map(|(ix, user)| StringMatchCandidate::new(ix, &user.github_login)),
);
let matches = executor.block(match_strings(
&self.match_candidates,
&query,
Expand All @@ -666,17 +648,12 @@ impl CollabPanel {
let outgoing = user_store.outgoing_contact_requests();
if !outgoing.is_empty() {
self.match_candidates.clear();
self.match_candidates
.extend(
outgoing
.iter()
.enumerate()
.map(|(ix, user)| StringMatchCandidate {
id: ix,
string: user.github_login.clone(),
char_bag: user.github_login.chars().collect(),
}),
);
self.match_candidates.extend(
outgoing
.iter()
.enumerate()
.map(|(ix, user)| StringMatchCandidate::new(ix, &user.github_login)),
);
let matches = executor.block(match_strings(
&self.match_candidates,
&query,
Expand All @@ -703,17 +680,12 @@ impl CollabPanel {
let contacts = user_store.contacts();
if !contacts.is_empty() {
self.match_candidates.clear();
self.match_candidates
.extend(
contacts
.iter()
.enumerate()
.map(|(ix, contact)| StringMatchCandidate {
id: ix,
string: contact.user.github_login.clone(),
char_bag: contact.user.github_login.chars().collect(),
}),
);
self.match_candidates.extend(
contacts
.iter()
.enumerate()
.map(|(ix, contact)| StringMatchCandidate::new(ix, &contact.user.github_login)),
);

let matches = executor.block(match_strings(
&self.match_candidates,
Expand Down
6 changes: 1 addition & 5 deletions crates/collab_ui/src/collab_panel/channel_modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,7 @@ impl PickerDelegate for ChannelModalDelegate {
self.match_candidates.clear();
self.match_candidates
.extend(self.members.iter().enumerate().map(|(id, member)| {
StringMatchCandidate {
id,
string: member.user.github_login.clone(),
char_bag: member.user.github_login.chars().collect(),
}
StringMatchCandidate::new(id, &member.user.github_login)
}));

let matches = cx.background_executor().block(match_strings(
Expand Down
6 changes: 1 addition & 5 deletions crates/command_palette/src/command_palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,7 @@ impl PickerDelegate for CommandPaletteDelegate {
let candidates = commands
.iter()
.enumerate()
.map(|(ix, command)| StringMatchCandidate {
id: ix,
string: command.name.to_string(),
char_bag: command.name.chars().collect(),
})
.map(|(ix, command)| StringMatchCandidate::new(ix, &command.name))
.collect::<Vec<_>>();
let matches = if query.is_empty() {
candidates
Expand Down
4 changes: 2 additions & 2 deletions crates/editor/src/code_context_menus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl CompletionsMenu {
.map(|(id, completion)| {
StringMatchCandidate::new(
id,
completion.label.text[completion.label.filter_range.clone()].into(),
&completion.label.text[completion.label.filter_range.clone()],
)
})
.collect();
Expand Down Expand Up @@ -211,7 +211,7 @@ impl CompletionsMenu {
let match_candidates = choices
.iter()
.enumerate()
.map(|(id, completion)| StringMatchCandidate::new(id, completion.to_string()))
.map(|(id, completion)| StringMatchCandidate::new(id, &completion))
.collect();
let matches = choices
.iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13293,7 +13293,7 @@ fn snippet_completions(
snippet
.prefix
.iter()
.map(move |prefix| StringMatchCandidate::new(ix, prefix.clone()))
.map(move |prefix| StringMatchCandidate::new(ix, &prefix))
})
.collect::<Vec<StringMatchCandidate>>();

Expand Down
8 changes: 1 addition & 7 deletions crates/extensions_ui/src/extension_version_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,7 @@ impl PickerDelegate for ExtensionVersionSelectorDelegate {
.iter()
.enumerate()
.map(|(id, extension)| {
let text = format!("v{}", extension.manifest.version);

StringMatchCandidate {
id,
char_bag: text.as_str().into(),
string: text,
}
StringMatchCandidate::new(id, &format!("v{}", extension.manifest.version))
})
.collect::<Vec<_>>();

Expand Down
6 changes: 1 addition & 5 deletions crates/extensions_ui/src/extensions_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,7 @@ impl ExtensionsPage {
let match_candidates = dev_extensions
.iter()
.enumerate()
.map(|(ix, manifest)| StringMatchCandidate {
id: ix,
string: manifest.name.clone(),
char_bag: manifest.name.as_str().into(),
})
.map(|(ix, manifest)| StringMatchCandidate::new(ix, &manifest.name))
.collect::<Vec<_>>();

let matches = match_strings(
Expand Down
2 changes: 1 addition & 1 deletion crates/file_finder/src/open_path_prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl PickerDelegate for OpenPathDelegate {
.iter()
.enumerate()
.map(|(ix, path)| {
StringMatchCandidate::new(ix, path.to_string_lossy().into())
StringMatchCandidate::new(ix, &path.to_string_lossy())
})
.collect::<Vec<_>>();

Expand Down
26 changes: 13 additions & 13 deletions crates/fuzzy/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,12 @@ pub struct StringMatchCandidate {
pub char_bag: CharBag,
}

impl Match for StringMatch {
fn score(&self) -> f64 {
self.score
}

fn set_positions(&mut self, positions: Vec<usize>) {
self.positions = positions;
}
}

impl StringMatchCandidate {
pub fn new(id: usize, string: String) -> Self {
pub fn new(id: usize, string: &str) -> Self {
Self {
id,
char_bag: CharBag::from(string.as_str()),
string,
string: string.into(),
char_bag: string.into(),
}
}
}
Expand All @@ -56,6 +46,16 @@ pub struct StringMatch {
pub string: String,
}

impl Match for StringMatch {
fn score(&self) -> f64 {
self.score
}

fn set_positions(&mut self, positions: Vec<usize>) {
self.positions = positions;
}
}

impl StringMatch {
pub fn ranges(&self) -> impl '_ + Iterator<Item = Range<usize>> {
let mut positions = self.positions.iter().peekable();
Expand Down
2 changes: 1 addition & 1 deletion crates/indexed_docs/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl IndexedDocsStore {
let candidates = items
.iter()
.enumerate()
.map(|(ix, item_path)| StringMatchCandidate::new(ix, item_path.clone()))
.map(|(ix, item_path)| StringMatchCandidate::new(ix, &item_path))
.collect::<Vec<_>>();

let matches = fuzzy::match_strings(
Expand Down
4 changes: 2 additions & 2 deletions crates/language/src/outline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ impl<T> Outline<T> {
.map(|range| &item.text[range.start..range.end])
.collect::<String>();

path_candidates.push(StringMatchCandidate::new(id, path_text.clone()));
candidates.push(StringMatchCandidate::new(id, candidate_text));
path_candidates.push(StringMatchCandidate::new(id, &path_text));
candidates.push(StringMatchCandidate::new(id, &candidate_text));
}

Self {
Expand Down
Loading

0 comments on commit 2597065

Please sign in to comment.