Skip to content

Commit b9dd82d

Browse files
committed
go_to_line: avoid byte slicing crash in short status labels
Signed-off-by: Xiaobo Liu <[email protected]>
1 parent 6d97598 commit b9dd82d

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

crates/go_to_line/src/cursor_position.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use editor::{Editor, MultiBufferSnapshot};
22
use gpui::{App, Entity, FocusHandle, Focusable, Styled, Subscription, Task, WeakEntity};
33
use settings::Settings;
4-
use std::{fmt::Write, num::NonZeroU32, time::Duration};
4+
use std::{borrow::Cow, fmt::Write, num::NonZeroU32, time::Duration};
55
use text::{Point, Selection};
66
use ui::{
77
Button, ButtonCommon, Clickable, Context, FluentBuilder, IntoElement, LabelSize, ParentElement,
@@ -182,13 +182,20 @@ impl CursorPosition {
182182
if wrote_once {
183183
write!(text, ", ").unwrap();
184184
}
185-
let name = if is_short_format { &name[..1] } else { name };
185+
let display_name: Cow<'_, str> = if is_short_format {
186+
name.chars()
187+
.next()
188+
.map(|c| Cow::Owned(c.to_string()))
189+
.unwrap_or_else(|| Cow::Borrowed(name))
190+
} else {
191+
Cow::Borrowed(name)
192+
};
186193
let plural_suffix = if count > 1 && !is_short_format {
187194
"s"
188195
} else {
189196
""
190197
};
191-
write!(text, "{count} {name}{plural_suffix}").unwrap();
198+
write!(text, "{count} {display_name}{plural_suffix}").unwrap();
192199
wrote_once = true;
193200
}
194201
text.push(')');

0 commit comments

Comments
 (0)