on master help sign in
scroll: change only main selection, only when needed (#1420)
dbaed0bMathis Brossier
helix-term/src/commands.rs+15−8
@@ -1282,16 +1282,23 @@ pub fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
1282 .max(view.offset.row + scrolloff)
1282 .max(view.offset.row + scrolloff)
1283 .min(last_line.saturating_sub(scrolloff));
1283 .min(last_line.saturating_sub(scrolloff));
1284
1284
1285+ // If cursor needs moving, replace primary selection
1286+ if line != cursor.row {
1285− let head = pos_at_coords(text, Position::new(line, cursor.col), true); // this func will properly truncate to line end
1287+ let head = pos_at_coords(text, Position::new(line, cursor.col), true); // this func will properly truncate to line end
1286
1288
1287− let anchor = if doc.mode == Mode::Select {
1289+ let anchor = if doc.mode == Mode::Select {
1288− range.anchor
1290+ range.anchor
1289− } else {
1291+ } else {
1290− head
1292+ head
1291− };
1293+ };
1292
1294
1293− // TODO: only manipulate main selection
1295+ // replace primary selection with an empty selection at cursor pos
1294− doc.set_selection(view.id, Selection::single(anchor, head));
1296+ let prim_sel = Range::new(anchor, head);
1297+ let mut sel = doc.selection(view.id).clone();
1298+ let idx = sel.primary_index();
1299+ sel = sel.replace(idx, prim_sel);
1300+ doc.set_selection(view.id, sel);
1301+ }
1295}
1302}
1296
1303
1297fn page_up(cx: &mut Context) {
1304fn page_up(cx: &mut Context) {