Order prompts by default (#12268)

Prompts in the prompt library will be in A->Z order by default.

Release Notes:

- N/A
This commit is contained in:
Nate Butler 2024-05-24 22:59:37 -04:00 committed by GitHub
parent f7a86967fd
commit d5fe2c85d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 2 deletions

View File

@ -15,6 +15,11 @@ use super::prompt::StaticPrompt;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct PromptId(pub Uuid); pub struct PromptId(pub Uuid);
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum SortOrder {
Alphabetical,
}
#[allow(unused)] #[allow(unused)]
impl PromptId { impl PromptId {
pub fn new() -> Self { pub fn new() -> Self {
@ -60,6 +65,22 @@ impl PromptLibrary {
.collect() .collect()
} }
pub fn sorted_prompts(&self, sort_order: SortOrder) -> Vec<(PromptId, StaticPrompt)> {
let state = self.state.read();
let mut prompts = state
.prompts
.iter()
.map(|(id, prompt)| (*id, prompt.clone()))
.collect::<Vec<_>>();
match sort_order {
SortOrder::Alphabetical => prompts.sort_by(|(_, a), (_, b)| a.title().cmp(&b.title())),
};
prompts
}
pub fn first_prompt_id(&self) -> Option<PromptId> { pub fn first_prompt_id(&self) -> Option<PromptId> {
let state = self.state.read(); let state = self.state.read();
state.prompts.keys().next().cloned() state.prompts.keys().next().cloned()

View File

@ -9,7 +9,7 @@ use ui::{prelude::*, IconButtonShape, ListItem, ListItemSpacing};
use util::{ResultExt, TryFutureExt}; use util::{ResultExt, TryFutureExt};
use workspace::ModalView; use workspace::ModalView;
use super::prompt_library::{PromptId, PromptLibrary}; use super::prompt_library::{PromptId, PromptLibrary, SortOrder};
use crate::prompts::prompt::StaticPrompt; use crate::prompts::prompt::StaticPrompt;
pub struct PromptManager { pub struct PromptManager {
@ -264,7 +264,7 @@ impl PickerDelegate for PromptManagerDelegate {
let prompt_library = self.prompt_library.clone(); let prompt_library = self.prompt_library.clone();
cx.spawn(|picker, mut cx| async move { cx.spawn(|picker, mut cx| async move {
async { async {
let prompts = prompt_library.prompts(); let prompts = prompt_library.sorted_prompts(SortOrder::Alphabetical);
let matching_prompts = prompts let matching_prompts = prompts
.into_iter() .into_iter()
.filter(|(_, prompt)| { .filter(|(_, prompt)| {