@@ -186,37 +186,31 @@ pub fn move_line_down(cx: &mut Context) {
187pub fn move_line_end(cx: &mut Context) {
187pub fn move_line_end(cx: &mut Context) {
188 let (view, doc) = cx.current();
188 let (view, doc) = cx.current();
189− let lines = selection_lines(doc.text(), doc.selection(view.id));
191− let positions = lines
190+ let selection = doc.selection(view.id).transform(|range| {
192− .into_iter()
191+ let text = doc.text();
193− .map(|index| {
192+ let line = text.char_to_line(range.head);
194− // adjust all positions to the end of the line.
196− // Line end is pos at the start of next line - 1
197− // subtract another 1 because the line ends with \n
198− doc.text().line_to_char(index + 1).saturating_sub(2)
200− .map(|pos| Range::new(pos, pos));
202− let selection = Selection::new(positions.collect(), 0);
194+ // Line end is pos at the start of next line - 1
195+ // subtract another 1 because the line ends with \n
196+ let pos = text.line_to_char(line + 1).saturating_sub(2);
197+ Range::new(pos, pos)
204 doc.set_selection(view.id, selection);
200 doc.set_selection(view.id, selection);
207pub fn move_line_start(cx: &mut Context) {
203pub fn move_line_start(cx: &mut Context) {
208 let (view, doc) = cx.current();
204 let (view, doc) = cx.current();
209− let lines = selection_lines(doc.text(), doc.selection(view.id));
211− let positions = lines
206+ let selection = doc.selection(view.id).transform(|range| {
212− .into_iter()
207+ let text = doc.text();
213− .map(|index| {
208+ let line = text.char_to_line(range.head);
214− // adjust all positions to the start of the line.
215− doc.text().line_to_char(index)
217− .map(|pos| Range::new(pos, pos));
219− let selection = Selection::new(positions.collect(), 0);
210+ // adjust to start of the line
211+ let pos = text.line_to_char(line);
212+ Range::new(pos, pos)
221 doc.set_selection(view.id, selection);
215 doc.set_selection(view.id, selection);
@@ -1123,19 +1117,6 @@ pub fn buffer_picker(cx: &mut Context) {
1123 cx.push_layer(Box::new(picker));
1117 cx.push_layer(Box::new(picker));
1126−// calculate line numbers for each selection range
1127−fn selection_lines(doc: &Rope, selection: &Selection) -> Vec<usize> {
1128− let mut lines = selection
1130− .map(|range| doc.char_to_line(range.head))
1131− .collect::<Vec<_>>();
1133− lines.sort_unstable(); // sorting by usize so _unstable is preferred
1139// I inserts at the start of each line with a selection
1120// I inserts at the start of each line with a selection
1140pub fn prepend_to_line(cx: &mut Context) {
1121pub fn prepend_to_line(cx: &mut Context) {
1141 move_line_start(cx);
1122 move_line_start(cx);
@@ -1169,13 +1150,15 @@ fn open(cx: &mut Context, open: Open) {
1169 enter_insert_mode(doc);
1150 enter_insert_mode(doc);
1171 let text = doc.text().slice(..);
1152 let text = doc.text().slice(..);
1172− let lines = selection_lines(doc.text(), doc.selection(view.id));
1153+ let selection = doc.selection(view.id);
1155+ let mut ranges = SmallVec::with_capacity(selection.len());
1174− let mut ranges = SmallVec::with_capacity(lines.len());
1157+ let changes: Vec<Change> = selection
1160+ let line = text.char_to_line(range.head);
1176− let changes: Vec<Change> = lines
1179 let line = match open {
1162 let line = match open {
1180 // adjust position to the end of the line (next line - 1)
1163 // adjust position to the end of the line (next line - 1)
1181 Open::Below => line + 1,
1164 Open::Below => line + 1,