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.

Respect max line limit for invisible character replacements (#57137)

Oleksiy Syvokond33a3186aab6
diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs
index 97380794178..7e03201b278 100644
--- a/crates/editor/src/element.rs
+++ b/crates/editor/src/element.rs
@@ -9001,6 +9001,15 @@ impl LineWithInvisibles {
replacement: None,
}]) {
if let Some(replacement) = highlighted_chunk.replacement {
+ if line_exceeded_max_len {
+ continue;
+ }
+
+ if len + line.len() + highlighted_chunk.text.len() > max_line_len {
+ line_exceeded_max_len = true;
+ continue;
+ }
+
if !line.is_empty() {
let segments = bg_segments_per_row.get(row).map(|v| &v[..]).unwrap_or(&[]);
let text_runs: &[TextRun] = if segments.is_empty() {
@@ -9132,8 +9141,9 @@ impl LineWithInvisibles {
Cow::Borrowed(text_style)
};
- if line.len() + line_chunk.len() > max_line_len {
- let mut chunk_len = max_line_len - line.len();
+ let current_line_len = len + line.len();
+ if current_line_len + line_chunk.len() > max_line_len {
+ let mut chunk_len = max_line_len - current_line_len;
while !line_chunk.is_char_boundary(chunk_len) {
chunk_len -= 1;
}
@@ -9141,6 +9151,10 @@ impl LineWithInvisibles {
line_exceeded_max_len = true;
}
+ if line_chunk.is_empty() {
+ continue;
+ }
+
styles.push(TextRun {
len: line_chunk.len(),
font: text_style.font(),
@@ -13573,6 +13587,67 @@ mod tests {
);
}
+ #[gpui::test]
+ fn test_replacement_chunks_are_clipped_to_max_line_len(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let window = cx.add_window(|window, cx| {
+ let buffer = MultiBuffer::build_simple("", cx);
+ Editor::new(EditorMode::full(), buffer, None, window, cx)
+ });
+ let cx = &mut VisualTestContext::from_window(*window, cx);
+ let editor = window.root(cx).unwrap();
+ let style = cx.update(|_, cx| editor.update(cx, |editor, cx| editor.style(cx).clone()));
+ let editor_mode = EditorMode::full();
+ let max_line_len = "\u{00a0}abcdef".len();
+
+ window
+ .update(cx, |_, window, cx| {
+ let chunks = std::iter::once(HighlightedChunk {
+ text: "\u{00a0}",
+ style: None,
+ is_tab: false,
+ is_inlay: false,
+ replacement: Some(ChunkReplacement::Str("\u{2007}".into())),
+ })
+ .chain(std::iter::once(HighlightedChunk {
+ text: "abcdefghi",
+ style: None,
+ is_tab: false,
+ is_inlay: false,
+ replacement: None,
+ }))
+ .chain(
+ std::iter::repeat_with(|| HighlightedChunk {
+ text: "\u{00a0}",
+ style: None,
+ is_tab: false,
+ is_inlay: false,
+ replacement: Some(ChunkReplacement::Str("\u{2007}".into())),
+ })
+ .take(8),
+ );
+
+ let layouts = LineWithInvisibles::from_chunks(
+ chunks,
+ &style,
+ max_line_len,
+ 1,
+ &editor_mode,
+ px(500.),
+ |_| false,
+ &[],
+ window,
+ cx,
+ );
+
+ assert_eq!(layouts.len(), 1);
+ assert_eq!(layouts[0].len, max_line_len);
+ assert!(layouts[0].fragments.len() <= max_line_len);
+ })
+ .unwrap();
+ }
+
#[gpui::test]
fn test_invisibles_dont_appear_in_certain_editors(cx: &mut TestAppContext) {
init_test(cx, |s| {