Repository: microsoft/edit
File: src/buffer/mod.rs (Rust)

Code before:
            {
                let mut cursor = cursor_beg;

                // By default, we assume the entire line is selected.
                selection_off.start = cursor_beg.offset;
                selection_off.end = cursor_end.offset;
                selection_pos.start = 0;
                selection_pos.end = COORD_TYPE_SAFE_MAX;

                // The start of the selection is within this line. We need to update selection_beg.
                if selection_beg <= cursor_end.logical_pos
                    && selection_beg >= cursor_beg.logical_pos
                {
                    cursor = self.cursor_move_to_logical_internal(
                        cursor,
                        Point { x: selection_beg.x.max(origin.x), y: selection_beg.y },
                    );
                    selection_off.start = cursor.offset;
                    selection_pos.start = cursor.visual_pos.x;
                }

                // The end of the selection is within this line. We need to update selection_end.
                if selection_end <= cursor_end.logical_pos
                    && selection_end >= cursor_beg.logical_pos
                {
                    cursor = self.cursor_move_to_logical_internal(
                        cursor,
                        Point { x: selection_end.x.min(origin.x + text_width), y: selection_end.y },
                    );
                    selection_off.end = cursor.offset;
                    selection_pos.end = cursor.visual_pos.x;
                }

                let left = destination.left + self.margin_width - origin.x;
                let top = destination.top + y;
                let rect = Rect {
                    left: left + selection_pos.start,
                    top,
                    right: left + selection_pos.end,
                    bottom: top + 1,
                };

                let mut bg = oklab_blend(
                    fb.indexed(IndexedColor::Foreground),
                    fb.indexed_alpha(IndexedColor::BrightBlue, 1, 2),
                );
                if !focused {
                    bg = oklab_blend(bg, fb.indexed_alpha(IndexedColor::Background, 1, 2))
                };
                let fg = fb.contrasted(bg);
                fb.blend_bg(rect, bg);

Review comment (suggestion):
```suggestion

                selection_pos.start = selection_pos.start.max(origin.x);
                selection_pos.end = selection_pos.end.min(origin.x + text_width);

```

This is old lines 1877-1878. If the selection_pos.start is not clamped with origin.x, on long lines the selection coloring goes into the line number margin.

Code after:
                let mut cursor = cursor_beg;

                // By default, we assume the entire line is selected.
                let mut selection_pos_beg = 0;
                let mut selection_pos_end = COORD_TYPE_SAFE_MAX;
                selection_off.start = cursor_beg.offset;
                selection_off.end = cursor_end.offset;

                // The start of the selection is within this line. We need to update selection_beg.
                if selection_beg <= cursor_end.logical_pos
                    && selection_beg >= cursor_beg.logical_pos
                {
                    cursor = self.cursor_move_to_logical_internal(cursor, selection_beg);
                    selection_off.start = cursor.offset;
                    selection_pos_beg = cursor.visual_pos.x;
                }

                // The end of the selection is within this line. We need to update selection_end.
                if selection_end <= cursor_end.logical_pos
                    && selection_end >= cursor_beg.logical_pos
                {
                    cursor = self.cursor_move_to_logical_internal(cursor, selection_end);
                    selection_off.end = cursor.offset;
                    selection_pos_end = cursor.visual_pos.x;
                }

                let left = destination.left + self.margin_width - origin.x;
                let top = destination.top + y;
                let rect = Rect {
                    left: left + selection_pos_beg.max(origin.x),
                    top,
                    right: left + selection_pos_end.min(origin.x + text_width),
                    bottom: top + 1,
                };

                let mut bg = oklab_blend(
                    fb.indexed(IndexedColor::Foreground),
                    fb.indexed_alpha(IndexedColor::BrightBlue, 1, 2),
                );
                if !focused {
                    bg = oklab_blend(bg, fb.indexed_alpha(IndexedColor::Background, 1, 2))
                };
                let fg = fb.contrasted(bg);
                fb.blend_bg(rect, bg);
                fb.blend_fg(rect, fg);
            }

            // Nothing to do if the entire line is empty.
            if cursor_beg.offset != cursor_end.offset {
                // If we couldn't reach the left edge, we may have stopped short due to a wide glyph.
                // In that case we'll try to find the next character and then compute by how many