Skip to content
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
19 changes: 19 additions & 0 deletions crates/command_palette/src/command_palette.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod persistence;

use std::{
any::Any,
cmp::{self, Reverse},
collections::{HashMap, VecDeque},
sync::Arc,
Expand Down Expand Up @@ -366,6 +367,9 @@ impl CommandPaletteDelegate {
}
}

#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct CommandPaletteStableId(SharedString);

impl PickerDelegate for CommandPaletteDelegate {
type ListItem = ListItem;

Expand Down Expand Up @@ -518,6 +522,21 @@ impl PickerDelegate for CommandPaletteDelegate {
})
}

fn match_stable_id(&self, ix: usize) -> Option<Box<dyn Any>> {
let candidate_id = self.matches.get(ix)?.candidate_id;
let name = self.commands.get(candidate_id)?.name.clone();
Some(Box::new(CommandPaletteStableId(name.into())))
}

fn find_match_by_stable_id(&self, stable_id: &dyn Any) -> Option<usize> {
let stable_id = stable_id.downcast_ref::<CommandPaletteStableId>()?;
self.matches.iter().position(|m| {
self.commands
.get(m.candidate_id)
.is_some_and(|cmd| cmd.name == stable_id.0.as_ref())
})
}

fn finalize_update_matches(
&mut self,
query: String,
Expand Down
47 changes: 46 additions & 1 deletion crates/git_ui/src/branch_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use picker::{Picker, PickerDelegate, PickerEditorPosition};
use project::git_store::Repository;
use project::project_settings::ProjectSettings;
use settings::Settings;
use std::sync::Arc;
use std::{any::Any, sync::Arc};
use time::OffsetDateTime;
use ui::{
Divider, HighlightedLabel, KeyBinding, ListHeader, ListItem, ListItemSpacing, Tooltip,
Expand Down Expand Up @@ -362,6 +362,16 @@ impl Entry {
}
}

/// Stable identifier for branch picker items, used to preserve manual selections
/// across match updates in the branch picker.
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub enum BranchStableId {
Branch(SharedString),
NewUrl(String),
NewBranch(String),
NewRemoteName { name: String, url: SharedString },
}

#[derive(Clone, Copy, PartialEq)]
enum BranchFilter {
/// Show both local and remote branches.
Expand Down Expand Up @@ -780,6 +790,41 @@ impl PickerDelegate for BranchListDelegate {
})
}

fn match_stable_id(&self, ix: usize) -> Option<Box<dyn Any>> {
Some(Box::new(match self.matches.get(ix)? {
Entry::Branch { branch, .. } => BranchStableId::Branch(branch.ref_name.clone()),
Entry::NewUrl { url } => BranchStableId::NewUrl(url.clone()),
Entry::NewBranch { name } => BranchStableId::NewBranch(name.clone()),
Entry::NewRemoteName { name, url } => BranchStableId::NewRemoteName {
name: name.clone(),
url: url.clone(),
},
}))
}

fn find_match_by_stable_id(&self, stable_id: &dyn Any) -> Option<usize> {
let stable_id = stable_id.downcast_ref::<BranchStableId>()?;
self.matches
.iter()
.position(|entry| match (entry, stable_id) {
(Entry::Branch { branch, .. }, BranchStableId::Branch(ref_name)) => {
&branch.ref_name == ref_name
}
(Entry::NewUrl { url }, BranchStableId::NewUrl(stable_url)) => url == stable_url,
(Entry::NewBranch { name }, BranchStableId::NewBranch(stable_name)) => {
name == stable_name
}
(
Entry::NewRemoteName { name, url },
BranchStableId::NewRemoteName {
name: stable_name,
url: stable_url,
},
) => name == stable_name && url == stable_url,
_ => false,
})
}

fn confirm(&mut self, secondary: bool, window: &mut Window, cx: &mut Context<Picker<Self>>) {
let Some(entry) = self.matches.get(self.selected_index()) else {
return;
Expand Down
43 changes: 41 additions & 2 deletions crates/outline/src/outline.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::any::Any;
use std::ops::Range;
use std::{cmp, sync::Arc};

Expand All @@ -7,8 +8,8 @@ use editor::{MultiBufferOffset, RowHighlightOptions, SelectionEffects};
use fuzzy::StringMatch;
use gpui::{
App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, HighlightStyle,
ParentElement, Point, Render, Styled, StyledText, Task, TextStyle, WeakEntity, Window, div,
rems,
ParentElement, Point, Render, SharedString, Styled, StyledText, Task, TextStyle, WeakEntity,
Window, div, rems,
};
use language::{Outline, OutlineItem};
use ordered_float::OrderedFloat;
Expand All @@ -19,6 +20,23 @@ use ui::{ListItem, ListItemSpacing, prelude::*};
use util::ResultExt;
use workspace::{DismissDecision, ModalView};

/// Stable identifier for outline items, used to preserve manual selections
/// across match updates in the outline picker.
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
struct OutlineStableId {
text: SharedString,
depth: usize,
}

impl OutlineStableId {
fn new(text: impl Into<SharedString>, depth: usize) -> Self {
Self {
text: text.into(),
depth,
}
}
}

pub fn init(cx: &mut App) {
cx.observe_new(OutlineView::register).detach();
zed_actions::outline::TOGGLE_OUTLINE
Expand Down Expand Up @@ -344,6 +362,27 @@ impl PickerDelegate for OutlineViewDelegate {
})
}

fn match_stable_id(&self, ix: usize) -> Option<Box<dyn Any>> {
let mat = self.matches.get(ix)?;
let outline_item = self.outline.items.get(mat.candidate_id)?;
Some(Box::new(OutlineStableId::new(
outline_item.text.clone(),
outline_item.depth,
)))
}

fn find_match_by_stable_id(&self, stable_id: &dyn Any) -> Option<usize> {
let stable_id = stable_id.downcast_ref::<OutlineStableId>()?;
self.matches.iter().position(|mat| {
self.outline
.items
.get(mat.candidate_id)
.is_some_and(|item| {
item.text == stable_id.text.as_ref() && item.depth == stable_id.depth
})
})
}

fn confirm(
&mut self,
_: bool,
Expand Down
Loading
Loading