Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

ui: Don't panic on invalid highlight indices in `HighlightedLabel` (#57291)

Oleksiy Syvokon7f7c21536e08
diff --git a/Cargo.lock b/Cargo.lock
index ad760e911e2..73206015b40 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -19197,6 +19197,7 @@ dependencies = [
"gpui_util",
"icons",
"itertools 0.14.0",
+ "log",
"menu",
"num-format",
"schemars 1.0.4",
diff --git a/crates/ui/Cargo.toml b/crates/ui/Cargo.toml
index 97469cae36c..7999a300648 100644
--- a/crates/ui/Cargo.toml
+++ b/crates/ui/Cargo.toml
@@ -20,6 +20,7 @@ gpui.workspace = true
gpui_macros.workspace = true
icons.workspace = true
itertools.workspace = true
+log.workspace = true
menu.workspace = true
num-format.workspace = true
schemars.workspace = true
@@ -36,5 +37,8 @@ windows.workspace = true
[dev-dependencies]
gpui = { workspace = true, features = ["test-support"] }
+[package.metadata.cargo-machete]
+ignored = ["log"]
+
[features]
default = []
diff --git a/crates/ui/src/components/label/highlighted_label.rs b/crates/ui/src/components/label/highlighted_label.rs
index 73e03f82dfd..6d5b1d5645a 100644
--- a/crates/ui/src/components/label/highlighted_label.rs
+++ b/crates/ui/src/components/label/highlighted_label.rs
@@ -1,6 +1,7 @@
use std::ops::Range;
use gpui::{FontWeight, HighlightStyle, StyleRefinement, StyledText};
+use gpui_util::debug_panic;
use crate::{LabelCommon, LabelLike, LabelSize, LineHeightStyle, prelude::*};
@@ -14,14 +15,21 @@ pub struct HighlightedLabel {
impl HighlightedLabel {
/// Constructs a label with the given characters highlighted.
/// Characters are identified by UTF-8 byte position.
- pub fn new(label: impl Into<SharedString>, highlight_indices: Vec<usize>) -> Self {
+ #[track_caller]
+ pub fn new(label: impl Into<SharedString>, mut highlight_indices: Vec<usize>) -> Self {
let label = label.into();
- for &run in &highlight_indices {
- assert!(
- label.is_char_boundary(run),
- "highlight index {run} is not a valid UTF-8 boundary"
+
+ if let Some(index) = highlight_indices
+ .iter()
+ .find(|&i| !label.is_char_boundary(*i))
+ {
+ let location = std::panic::Location::caller();
+ debug_panic!(
+ "highlight index {index} is not a valid UTF-8 boundary (called from {location})"
);
+ highlight_indices.clear();
}
+
Self {
base: LabelLike::new(),
label,