Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Align prefix parsing, smart-select characters (#58267)
Mikayla Makiee74df510f59
diff --git a/Cargo.lock b/Cargo.lockindex c08e373793f..334815a59a5 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -17300,6 +17300,7 @@ dependencies = ["theme","theme_settings","ui",+ "unicode-segmentation","util","workspace","zed_actions",diff --git a/crates/agent_ui/src/terminal_thread_metadata_store.rs b/crates/agent_ui/src/terminal_thread_metadata_store.rsindex 6baedca4289..70bfb8b26e6 100644--- a/crates/agent_ui/src/terminal_thread_metadata_store.rs+++ b/crates/agent_ui/src/terminal_thread_metadata_store.rs@@ -93,7 +93,7 @@ pub(crate) fn terminal_title_without_prefix(title: &str) -> &str {.unwrap_or(title)}-fn terminal_title_prefix(title: &str) -> Option<&str> {+pub fn terminal_title_prefix(title: &str) -> Option<&str> {let mut prefix_byte_len = 0;let mut saw_prefix_character = false;let mut saw_whitespace_after_prefix = false;diff --git a/crates/sidebar/Cargo.toml b/crates/sidebar/Cargo.tomlindex f9ae2ed5241..d479f21af4e 100644--- a/crates/sidebar/Cargo.toml+++ b/crates/sidebar/Cargo.toml@@ -44,6 +44,7 @@ telemetry.workspace = truetheme.workspace = truetheme_settings.workspace = trueui.workspace = true+unicode-segmentation.workspace = trueutil.workspace = trueworkspace.workspace = truezed_actions.workspace = truediff --git a/crates/sidebar/src/sidebar.rs b/crates/sidebar/src/sidebar.rsindex 4dbc96ed70c..9ba8c3b8d93 100644--- a/crates/sidebar/src/sidebar.rs+++ b/crates/sidebar/src/sidebar.rs@@ -5,7 +5,7 @@ use action_log::DiffStats;use agent_client_protocol::schema as acp;use agent_settings::AgentSettings;use agent_ui::terminal_thread_metadata_store::{- TerminalThreadMetadata, TerminalThreadMetadataStore,+ TerminalThreadMetadata, TerminalThreadMetadataStore, terminal_title_prefix,};use agent_ui::thread_metadata_store::{ThreadMetadata, ThreadMetadataStore, WorktreePaths, worktree_info_from_thread_paths,@@ -55,6 +55,7 @@ use ui::{Scrollbars, Tab, ThreadItem, ThreadItemWorktreeInfo, TintColor, Tooltip, WithScrollbar,prelude::*, render_modifiers,};+use unicode_segmentation::UnicodeSegmentation as _;use util::ResultExt as _;use util::path_list::PathList;use workspace::{@@ -226,24 +227,23 @@ impl ThreadEntryWorkspace {}}-/// If the title begins with a non-letter, non-whitespace character (such as a-/// leading emoji or symbol the user prefixed their title with), splits that-/// character out so it can be displayed in place of the entry's icon+/// If the title begins with a decorative prefix (such as a leading emoji,+/// spinner glyph, or symbol the agent prefixed the title with), splits that+/// prefix off so a single representative glyph can be displayed in place of the+/// entry's icon.fn split_leading_icon_char(title: &SharedString,highlight_positions: &[usize],) -> Option<(SharedString, SharedString, Vec<usize>)> {- let first_char = title.chars().next()?;- if first_char.is_alphabetic() || first_char.is_whitespace() {- return None;- }+ let prefix = terminal_title_prefix(title)?;+ let icon_char = pick_icon_glyph(prefix)?;- let trimmed_title = title[first_char.len_utf8()..].trim_start();+ let stripped_len = prefix.len();+ let trimmed_title = &title[stripped_len..];if trimmed_title.is_empty() {return None;}- let stripped_len = title.len() - trimmed_title.len();let adjusted_positions = highlight_positions.iter().filter(|&&position| position >= stripped_len)@@ -251,12 +251,53 @@ fn split_leading_icon_char(.collect();Some((- first_char.to_string().into(),+ icon_char,trimmed_title.to_string().into(),adjusted_positions,))}+/// Picks a single glyph to render as the icon from a detected title prefix.+///+/// We only ever show one glyph, so this makes a best effort to choose a+/// meaningful one by glancing at the leading characters of the prefix:+/// runs of `.` are condensed into a single ellipsis, surrounding ASCII brackets+/// are stripped (so `[!]` yields `!`), and a leading run of the same character+/// is collapsed (so `>>>` yields `>`). The result is the first grapheme cluster+/// of whatever remains, keeping multi-codepoint emoji intact.+fn pick_icon_glyph(prefix: &str) -> Option<SharedString> {+ let prefix = prefix.trim();+ if prefix.is_empty() {+ return None;+ }++ // Condense a leading run of dots (`...`) into a single ellipsis.+ if prefix.starts_with("..") {+ return Some("\u{2026}".into());+ }++ // Strip a single pair of surrounding ASCII brackets, e.g. `[!]` -> `!`.+ let unwrapped = match prefix.chars().next() {+ Some('[') => prefix.strip_prefix('[').and_then(|s| s.strip_suffix(']')),+ Some('(') => prefix.strip_prefix('(').and_then(|s| s.strip_suffix(')')),+ Some('{') => prefix.strip_prefix('{').and_then(|s| s.strip_suffix('}')),+ Some('<') => prefix.strip_prefix('<').and_then(|s| s.strip_suffix('>')),+ _ => None,+ };+ let prefix = unwrapped+ .map(str::trim)+ .filter(|s| !s.is_empty())+ .unwrap_or(prefix);++ // Take the first grapheme cluster so multi-codepoint emoji stay intact.+ let first_grapheme = prefix.graphemes(true).next()?;+ if first_grapheme.trim().is_empty() {+ return None;+ }++ Some(first_grapheme.to_string().into())+}+fn draft_display_label_for_thread_metadata(metadata: &ThreadMetadata,workspace: &ThreadEntryWorkspace,diff --git a/crates/sidebar/src/sidebar_tests.rs b/crates/sidebar/src/sidebar_tests.rsindex 967a61621e0..37b66320732 100644--- a/crates/sidebar/src/sidebar_tests.rs+++ b/crates/sidebar/src/sidebar_tests.rs@@ -14733,26 +14733,48 @@ async fn test_cmd_click_project_header_returns_to_last_active_linked_worktree_wo#[test]fn test_split_leading_icon_char() {- // A leading symbol is pulled out and trimmed from the title.+ // A leading symbol set off by whitespace is pulled out and trimmed from the+ // title.let (icon, title, positions) =split_leading_icon_char(&"✳ Implement separate config".into(), &[]).unwrap();assert_eq!(icon.as_ref(), "✳");assert_eq!(title.as_ref(), "Implement separate config");assert_eq!(positions, Vec::<usize>::new());- // No leading symbol when the title starts with a letter.+ // No prefix when the title starts with a letter.assert!(split_leading_icon_char(&"Implement separate config".into(), &[]).is_none());- // Whitespace is not treated as an icon character.+ // Leading whitespace is not treated as a prefix.assert!(split_leading_icon_char(&" leading space".into(), &[]).is_none());- // Numbers are non-letters, so they are treated as icon characters.- let (icon, title, _) = split_leading_icon_char(&"1 first".into(), &[]).unwrap();- assert_eq!(icon.as_ref(), "1");- assert_eq!(title.as_ref(), "first");+ // An alphanumeric prefix such as a version marker is not treated as an icon.+ assert!(split_leading_icon_char(&"v1 Running".into(), &[]).is_none());+ assert!(split_leading_icon_char(&"1 first".into(), &[]).is_none());- // A title consisting only of a symbol is left untouched.+ // A title consisting only of a symbol (no whitespace separator) is left+ // untouched.assert!(split_leading_icon_char(&"✳".into(), &[]).is_none());+ assert!(split_leading_icon_char(&"✳Thinking".into(), &[]).is_none());++ // A run of the same symbol collapses to a single glyph.+ let (icon, title, _) = split_leading_icon_char(&">>> Thinking".into(), &[]).unwrap();+ assert_eq!(icon.as_ref(), ">");+ assert_eq!(title.as_ref(), "Thinking");++ // Surrounding ASCII brackets are stripped so the inner glyph is used.+ let (icon, title, _) = split_leading_icon_char(&"[!] codex waiting".into(), &[]).unwrap();+ assert_eq!(icon.as_ref(), "!");+ assert_eq!(title.as_ref(), "codex waiting");++ // A run of dots is condensed into an ellipsis.+ let (icon, title, _) = split_leading_icon_char(&"... working".into(), &[]).unwrap();+ assert_eq!(icon.as_ref(), "\u{2026}");+ assert_eq!(title.as_ref(), "working");++ // Multi-codepoint emoji are kept intact rather than sliced mid-cluster.+ let (icon, title, _) = split_leading_icon_char(&"🇺🇸 flag".into(), &[]).unwrap();+ assert_eq!(icon.as_ref(), "🇺🇸");+ assert_eq!(title.as_ref(), "flag");// Highlight positions are shifted to account for the stripped prefix, and// positions that fall inside the stripped prefix are dropped.