Skip to content
Merged
98 changes: 98 additions & 0 deletions src/explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ pub fn object_row(catalog: &Catalog, schema: &str, name: &str, kind: ObjectKind)
}
}

/// Whether the row `id` is the row `front`, or a folder's or a pin's
/// shortcut to it.
pub fn stands_for(id: &str, front: &str, shortcuts: &HashMap<String, Shortcut>) -> bool {
id == front
|| shortcuts
.get(id)
.and_then(Shortcut::repeats)
.is_some_and(|row| row == front)
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ExplorerTarget {
Relation {
Expand Down Expand Up @@ -862,6 +872,21 @@ impl Shortcut {
_ => None,
}
}

/// The row in the schema tree or the Queries group it repeats.
fn repeats(&self) -> Option<String> {
match self {
Shortcut::Folder(_) => None,
Shortcut::Member {
member: Member::Query(name),
..
} => Some(query_row(name)),
Shortcut::Member {
member: Member::Object(object),
..
} => Some(object.row()),
}
}
}

/// The Pinned group, when anything is pinned, then every folder not pinned,
Expand Down Expand Up @@ -1936,6 +1961,79 @@ mod tests {
);
}

#[test]
fn a_shortcut_stands_for_the_row_of_what_it_repeats() {
let catalog = catalog();
let shortcuts = Shortcuts {
folders: vec![folder(
"Hot",
vec![
relation("public", "accounts"),
Member::Query("daily".into()),
],
)],
pins: vec![Pin::Member(Member::Object(ObjectRef {
database: None,
schema: "public".into(),
name: "account_name(account_id bigint)".into(),
kind: ObjectKind::Routine(RoutineKind::Function),
}))],
};
let names = ["daily".to_string()];
let mut explorer = super::shortcuts(
&shortcuts,
Some(&catalog),
&names,
&ShortcutDatabase::Unnamed,
"",
);
explorer.extend(queries(&names, ""));
explorer.extend(tree(&catalog, ""));
let mut ids = Vec::new();
every_id(&explorer.items, &mut ids);

let accounts = object_row(
&catalog,
"public",
"accounts",
ObjectKind::Relation(RelationKind::Table),
)
.expect("the table is listed");
let function = object_row(
&catalog,
"public",
"account_name(account_id bigint)",
ObjectKind::Routine(RoutineKind::Function),
)
.expect("the function is listed");
let daily = query_row("daily");
for front in [&accounts, &function, &daily] {
assert!(ids.contains(front), "{front:?} is a row of the tree");
}

let standing = |front: &str| -> Vec<&str> {
ids.iter()
.map(String::as_str)
.filter(|id| stands_for(id, front, &explorer.shortcuts))
.collect()
};
let hot = &explorer.items[1];
let pinned = &explorer.items[0];
assert_eq!(
standing(&accounts),
[hot.children[0].id.as_ref(), accounts.as_str()]
);
assert_eq!(
standing(&daily),
[hot.children[1].id.as_ref(), daily.as_str()]
);
assert_eq!(
standing(&function),
[pinned.children[0].id.as_ref(), function.as_str()]
);
assert!(!stands_for(hot.id.as_ref(), &accounts, &explorer.shortcuts));
}

#[test]
fn pins_come_first_in_the_order_pinned_and_a_pinned_folder_moves_into_them() {
let shortcuts = Shortcuts {
Expand Down
52 changes: 50 additions & 2 deletions src/icons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ use gpui::{AssetSource, Result, SharedString};
use gpui_component::Icon;
use icondata_core::IconData;

use crate::db::Syntax;

/// The icons dbdelve can draw, by the path GPUI asks for.
///
/// The gpui-component widgets ask for their own paths — those are Lucide names
/// too, so they resolve here as well. Add a row when something asks for one;
/// an unlisted path simply draws nothing.
const ICONS: [(&str, &IconData); 74] = [
const ICONS: [(&str, &IconData); 80] = [
("icons/git-fork.svg", icondata_lu::LuGitFork),
("icons/chevron-down.svg", icondata_lu::LuChevronDown),
("icons/chevron-right.svg", icondata_lu::LuChevronRight),
Expand Down Expand Up @@ -80,6 +82,12 @@ const ICONS: [(&str, &IconData); 74] = [
("icons/folder-minus.svg", icondata_lu::LuFolderMinus),
("icons/folder-plus.svg", icondata_lu::LuFolderPlus),
("icons/pin.svg", icondata_lu::LuPin),
("icons/package.svg", icondata_lu::LuPackage),
("icons/code.svg", icondata_lu::LuCode),
("icons/braces.svg", icondata_lu::LuBraces),
("icons/terminal.svg", icondata_lu::LuTerminal),
("icons/file-json.svg", icondata_lu::LuFileJson),
("icons/file-terminal.svg", icondata_lu::LuFileTerminal),
(
"icons/sliders-horizontal.svg",
icondata_lu::LuSlidersHorizontal,
Expand All @@ -105,6 +113,24 @@ const ICONS: [(&str, &IconData); 74] = [
("icons/unplug.svg", icondata_lu::LuUnplug),
];

/// The Queries group's icon for a connection's query language.
pub fn queries_group_icon(syntax: Syntax) -> &'static str {
match syntax {
Syntax::Sql => icon::QUERIES_SQL,
Syntax::Mongo => icon::QUERIES_MONGO,
Syntax::Redis => icon::QUERIES_REDIS,
}
}

/// A saved query's icon for a connection's query language.
pub fn saved_query_icon(syntax: Syntax) -> &'static str {
match syntax {
Syntax::Sql => icon::SAVED_QUERY,
Syntax::Mongo => icon::SAVED_QUERY_MONGO,
Syntax::Redis => icon::SAVED_QUERY_REDIS,
}
}

/// dbdelve's own names for the icons it draws, so a call site names a thing
/// rather than a file.
pub mod icon {
Expand Down Expand Up @@ -137,16 +163,24 @@ pub mod icon {
/// No connection colour.
pub const NO_COLOR: &str = "icons/ban.svg";
pub const SAVED_QUERY: &str = "icons/file-code.svg";
pub const SAVED_QUERY_MONGO: &str = "icons/file-json.svg";
pub const SAVED_QUERY_REDIS: &str = "icons/file-terminal.svg";
/// The Queries group, in the glyph of the language its queries are in.
pub const QUERIES_SQL: &str = "icons/code.svg";
pub const QUERIES_MONGO: &str = "icons/braces.svg";
pub const QUERIES_REDIS: &str = "icons/terminal.svg";
pub const DELETE: &str = "icons/trash.svg";
pub const SEARCH: &str = "icons/search.svg";
pub const DATABASE: &str = "icons/database.svg";
pub const DISCONNECT: &str = "icons/unplug.svg";
pub const PROJECT: &str = "icons/folder.svg";
/// A folder of shortcuts in the explorer, closed and open.
pub const FOLDER: &str = "icons/folder.svg";
pub const FOLDER_OPEN: &str = "icons/folder-open.svg";
/// The Pinned group's header, which is the icon alone.
pub const PIN: &str = "icons/pin.svg";
/// A schema: a box rather than a folder, since the folders beside it are
/// the user's own.
pub const SCHEMA: &str = "icons/package.svg";
pub const BROWSE: &str = "icons/folder-open.svg";
pub const LEAVE_PROJECT: &str = "icons/folder-minus.svg";
pub const ADD_TO_PROJECT: &str = "icons/folder-plus.svg";
Expand Down Expand Up @@ -276,6 +310,20 @@ mod tests {
use super::*;
use crate::db::Engine;

#[test]
fn query_icons_follow_the_query_language() {
assert_eq!(queries_group_icon(Syntax::Sql), icon::QUERIES_SQL);
assert_eq!(queries_group_icon(Syntax::Mongo), icon::QUERIES_MONGO);
assert_eq!(queries_group_icon(Syntax::Redis), icon::QUERIES_REDIS);
assert_eq!(saved_query_icon(Syntax::Sql), icon::SAVED_QUERY);
assert_eq!(saved_query_icon(Syntax::Mongo), icon::SAVED_QUERY_MONGO);
assert_eq!(saved_query_icon(Syntax::Redis), icon::SAVED_QUERY_REDIS);
for syntax in [Syntax::Sql, Syntax::Mongo, Syntax::Redis] {
assert_draws(queries_group_icon(syntax));
assert_draws(saved_query_icon(syntax));
}
}

#[test]
fn the_brand_mark_is_declared_near_the_size_it_is_drawn() {
let document = brand_document();
Expand Down
29 changes: 12 additions & 17 deletions src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use crate::{
explorer::{ExplorerTarget, ObjectKind},
export::{ClipboardFormat, FileAs, Format, RowsAs},
fuzzy::{Fuzzy, Query},
icons::icon,
icons::{icon, saved_query_icon},
import::Source,
session::{
CatalogState, ObjectBody, Profile, QueryState, RowCount, Session, StructureState, Tab,
Expand Down Expand Up @@ -2281,38 +2281,33 @@ fn search_items(workspace: &Workspace, profile: &Profile, cx: &App) -> Vec<Item>

/// The scratch buffer and every saved query.
fn query_items(profile: &Profile) -> Vec<Item> {
let saved = saved_query_icon(profile.config.engine().syntax());
let scratch = Item::new("Scratch query", icon::SCRATCH_QUERY, Command::OpenScratch)
.hint("scratch")
.scope(Scope::Queries);
std::iter::once(scratch)
.chain(profile.session.saved_queries.iter().map(|name| {
Item::new(
name.clone(),
icon::SAVED_QUERY,
Command::OpenQuery(name.clone()),
)
.hint("query")
.scope(Scope::Queries)
Item::new(name.clone(), saved, Command::OpenQuery(name.clone()))
.hint("query")
.scope(Scope::Queries)
}))
.collect()
}

/// Every saved query, open or not, each raising the delete dialog.
fn delete_query_items(profile: &Profile) -> Vec<Item> {
let saved = saved_query_icon(profile.config.engine().syntax());
profile
.session
.saved_queries
.iter()
.map(|name| {
Item::new(
name.clone(),
icon::SAVED_QUERY,
Command::DeleteQuery(name.clone()),
Item::new(name.clone(), saved, Command::DeleteQuery(name.clone())).hint(
match profile.session.tab_holding(name) {
Some(_) => "open",
None => "query",
},
)
.hint(match profile.session.tab_holding(name) {
Some(_) => "open",
None => "query",
})
})
.collect()
}
Expand Down Expand Up @@ -3234,7 +3229,7 @@ fn schema_items(profile: &Profile) -> Vec<Item> {
};
Item::new(
schema.name.clone(),
icon::PROJECT,
icon::SCHEMA,
Command::SetSchema(schema.name.clone()),
)
.hint(hint)
Expand Down
Loading
Loading