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.

editor: Fix columnar selection alignment on rows with multi-byte chars (#57097)

João Soares992f395c3d69
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index 09613906743..8c547eff6ff 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/crates/editor/src/editor_tests.rs
@@ -38069,6 +38069,164 @@ async fn test_toggle_diagnostics_persists_across_settings_change(cx: &mut TestAp
});
}
+#[gpui::test]
+async fn test_columnar_selection_with_multibyte_chars(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let mut cx = EditorTestContext::new(cx).await;
+
+ // The middle row contains a 2-byte char (ã) before the dragged column. A
+ // column selection that uses byte columns directly puts the ã row's
+ // selection at a different visual position than the ASCII rows; anchoring
+ // in x pixels keeps all rows at the same character offset.
+ cx.set_state(indoc! {"
+ ˇabcde
+ abcde
+ aãcde
+ abcde
+ abcde
+ "});
+
+ // Drag column-wise from (row 0, col 0) past the ã column on every row.
+ cx.update_editor(|editor, window, cx| {
+ editor.select(
+ SelectPhase::BeginColumnar {
+ position: DisplayPoint::new(DisplayRow(0), 0),
+ goal_column: 0,
+ reset: true,
+ mode: ColumnarMode::FromMouse,
+ },
+ window,
+ cx,
+ );
+ editor.select(
+ SelectPhase::Update {
+ position: DisplayPoint::new(DisplayRow(4), 4),
+ goal_column: 4,
+ scroll_delta: gpui::Point::default(),
+ },
+ window,
+ cx,
+ );
+ });
+
+ cx.assert_editor_state(indoc! {"
+ «abcdˇ»e
+ «abcdˇ»e
+ «aãcdˇ»e
+ «abcdˇ»e
+ «abcdˇ»e
+ "});
+
+ // Control: drag stops before the ã column, where byte columns and x
+ // positions agree.
+ cx.update_editor(|editor, window, cx| {
+ editor.select(
+ SelectPhase::BeginColumnar {
+ position: DisplayPoint::new(DisplayRow(0), 0),
+ goal_column: 0,
+ reset: true,
+ mode: ColumnarMode::FromMouse,
+ },
+ window,
+ cx,
+ );
+ editor.select(
+ SelectPhase::Update {
+ position: DisplayPoint::new(DisplayRow(4), 1),
+ goal_column: 1,
+ scroll_delta: gpui::Point::default(),
+ },
+ window,
+ cx,
+ );
+ });
+
+ cx.assert_editor_state(indoc! {"
+ «aˇ»bcde
+ «aˇ»bcde
+ «aˇ»ãcde
+ «aˇ»bcde
+ «aˇ»bcde
+ "});
+}
+
+#[gpui::test]
+async fn test_columnar_selection_past_end_of_line(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let mut cx = EditorTestContext::new(cx).await;
+
+ cx.set_state(indoc! {"
+ ˇaaaaaaaaaa
+ bb
+ cccccccccc
+ "});
+
+ // Drag from the start of the long first row to a point past the EOL of
+ // the short second row: the mouse handlers encode that as the nearest
+ // valid position (1, 2) plus an unclipped goal column of 8. The rectangle
+ // must keep tracking the mouse x on the long row instead of collapsing to
+ // the short row's width.
+ cx.update_editor(|editor, window, cx| {
+ editor.select(
+ SelectPhase::BeginColumnar {
+ position: DisplayPoint::new(DisplayRow(0), 0),
+ goal_column: 0,
+ reset: true,
+ mode: ColumnarMode::FromMouse,
+ },
+ window,
+ cx,
+ );
+ editor.select(
+ SelectPhase::Update {
+ position: DisplayPoint::new(DisplayRow(1), 2),
+ goal_column: 8,
+ scroll_delta: gpui::Point::default(),
+ },
+ window,
+ cx,
+ );
+ });
+
+ cx.assert_editor_state(indoc! {"
+ «aaaaaaaaˇ»aa
+ «bbˇ»
+ cccccccccc
+ "});
+
+ // Starting the drag past the EOL of the short row must anchor that edge
+ // of the rectangle at the click position, not at the short row's EOL.
+ cx.update_editor(|editor, window, cx| {
+ editor.select(
+ SelectPhase::BeginColumnar {
+ position: DisplayPoint::new(DisplayRow(1), 2),
+ goal_column: 8,
+ reset: true,
+ mode: ColumnarMode::FromMouse,
+ },
+ window,
+ cx,
+ );
+ editor.select(
+ SelectPhase::Update {
+ position: DisplayPoint::new(DisplayRow(2), 4),
+ goal_column: 4,
+ scroll_delta: gpui::Point::default(),
+ },
+ window,
+ cx,
+ );
+ });
+
+ cx.assert_editor_state(indoc! {"
+ aaaaaaaaaa
+ bb
+ cccc«ˇcccc»cc
+ "});
+}
+
#[gpui::test]
async fn test_toggle_markdown_block_quote(cx: &mut TestAppContext) {
init_test(cx, |_| {});
diff --git a/crates/editor/src/selection.rs b/crates/editor/src/selection.rs
index d3a4b80cd1e..11892a39a79 100644
--- a/crates/editor/src/selection.rs
+++ b/crates/editor/src/selection.rs
@@ -1737,30 +1737,73 @@ impl Editor {
let start_row = cmp::min(tail.row(), head.row());
let end_row = cmp::max(tail.row(), head.row());
- let start_column = cmp::min(tail.column(), goal_column);
- let end_column = cmp::max(tail.column(), goal_column);
- let reversed = start_column < tail.column();
+
+ // Anchor the columnar rectangle in x pixels rather than byte columns so
+ // rows with multi-byte characters (e.g. diacritics) stay visually aligned.
+ let text_layout_details = self.text_layout_details(window, cx);
+
+ // The mouse handlers encode drags past a line's end as extra columns
+ // beyond the line length, in em layout widths (see
+ // `PositionMap::point_for_position`). `x_for_display_point` clamps at
+ // the line's width, so convert that overshoot back to pixels with the
+ // same unit to keep the rectangle tracking the mouse past short lines.
+ let font_id = text_layout_details
+ .text_system
+ .resolve_font(&text_layout_details.editor_style.text.font());
+ let font_size = text_layout_details
+ .editor_style
+ .text
+ .font_size
+ .to_pixels(text_layout_details.rem_size);
+ let em_layout_width = text_layout_details
+ .text_system
+ .em_layout_width(font_id, font_size);
+ let x_for_unclipped_point = |point: DisplayPoint| {
+ let line_len = display_map.line_len(point.row());
+ if point.column() > line_len {
+ let eol_x = display_map.x_for_display_point(
+ DisplayPoint::new(point.row(), line_len),
+ &text_layout_details,
+ );
+ eol_x + em_layout_width * (point.column() - line_len) as f32
+ } else {
+ display_map.x_for_display_point(point, &text_layout_details)
+ }
+ };
+
+ let tail_x = x_for_unclipped_point(tail);
+ let head_x = x_for_unclipped_point(DisplayPoint::new(head.row(), goal_column));
+ let start_x = tail_x.min(head_x);
+ let end_x = tail_x.max(head_x);
+ let reversed = head_x < tail_x;
let selection_ranges = (start_row.0..=end_row.0)
.map(DisplayRow)
.filter_map(|row| {
- if (matches!(columnar_state, ColumnarSelectionState::FromMouse { .. })
- || start_column <= display_map.line_len(row))
- && !display_map.is_block_line(row)
+ if display_map.is_block_line(row) {
+ return None;
+ }
+
+ let layout = display_map.layout_row(row, &text_layout_details);
+ if matches!(columnar_state, ColumnarSelectionState::FromSelection { .. })
+ && start_x > layout.width
{
- let start = display_map
- .clip_point(DisplayPoint::new(row, start_column), Bias::Left)
- .to_point(display_map);
- let end = display_map
- .clip_point(DisplayPoint::new(row, end_column), Bias::Right)
- .to_point(display_map);
- if reversed {
- Some(end..start)
- } else {
- Some(start..end)
- }
+ return None;
+ }
+
+ let start_column = layout.closest_index_for_x(start_x) as u32;
+ let end_column = layout.closest_index_for_x(end_x) as u32;
+
+ let start = display_map
+ .clip_point(DisplayPoint::new(row, start_column), Bias::Left)
+ .to_point(display_map);
+ let end = display_map
+ .clip_point(DisplayPoint::new(row, end_column), Bias::Right)
+ .to_point(display_map);
+ if reversed {
+ Some(end..start)
} else {
- None
+ Some(start..end)
}
})
.collect::<Vec<_>>();