Merge open_above and open_below definitions.

pull/11/head
Blaž Hrastnik 4 years ago
parent cc058ad78f
commit 77cab5a715

@ -945,8 +945,12 @@ pub fn append_to_line(cx: &mut Context) {
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);
} }
// o inserts a new line after each line with a selection enum Open {
pub fn open_below(cx: &mut Context) { Below,
Above,
}
fn open(cx: &mut Context, open: Open) {
let count = cx.count; let count = cx.count;
let (view, doc) = cx.current(); let (view, doc) = cx.current();
enter_insert_mode(doc); enter_insert_mode(doc);
@ -959,8 +963,14 @@ pub fn open_below(cx: &mut Context) {
let changes: Vec<Change> = lines let changes: Vec<Change> = lines
.into_iter() .into_iter()
.map(|line| { .map(|line| {
// adjust all positions to the end of the line (next line minus one) let line = match open {
let index = doc.text().line_to_char(line + 1).saturating_sub(1); // adjust position to the end of the line (next line - 1)
Open::Below => line + 1,
// adjust position to the end of the previous line (current line - 1)
Open::Above => line,
};
let index = doc.text().line_to_char(line).saturating_sub(1);
// TODO: share logic with insert_newline for indentation // TODO: share logic with insert_newline for indentation
let indent_level = indent::suggested_indent_for_pos(doc.syntax(), text, index, true); let indent_level = indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
@ -989,49 +999,14 @@ pub fn open_below(cx: &mut Context) {
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
} }
// o inserts a new line after each line with a selection
pub fn open_below(cx: &mut Context) {
open(cx, Open::Below)
}
// O inserts a new line before each line with a selection // O inserts a new line before each line with a selection
pub fn open_above(cx: &mut Context) { pub fn open_above(cx: &mut Context) {
let count = cx.count; open(cx, Open::Above)
let (view, doc) = cx.current();
enter_insert_mode(doc);
let text = doc.text().slice(..);
let lines = selection_lines(doc.text(), doc.selection(view.id));
let mut ranges = SmallVec::with_capacity(lines.len());
let changes: Vec<Change> = lines
.into_iter()
.map(|line| {
// adjust all positions to the end of the previous line
let index = doc.text().line_to_char(line).saturating_sub(1);
// TODO: share logic with insert_newline for indentation
let indent_level = indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
let indent = doc.indent_unit().repeat(indent_level);
let mut text = String::with_capacity(1 + indent.len());
text.push('\n');
text.push_str(&indent);
let text = text.repeat(count);
// calculate new selection range
let pos = index + text.len();
ranges.push(Range::new(pos, pos));
// generate changes
(index, index, Some(text.into()))
})
.collect();
// TODO: count actually inserts "n" new lines and starts editing on all of them.
// TODO: append "count" newlines and modify cursors to those lines
let selection = Selection::new(ranges, 0);
let transaction =
Transaction::change(doc.text(), changes.into_iter()).with_selection(selection);
doc.apply(&transaction, view.id);
} }
pub fn normal_mode(cx: &mut Context) { pub fn normal_mode(cx: &mut Context) {
@ -1344,14 +1319,14 @@ pub mod insert {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let transaction = let transaction =
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| { Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
let pos = range.head;
// TODO: offset range.head by 1? when calculating? // TODO: offset range.head by 1? when calculating?
let indent_level = let indent_level = indent::suggested_indent_for_pos(doc.syntax(), text, pos, true);
indent::suggested_indent_for_pos(doc.syntax(), text, range.head, true);
let indent = doc.indent_unit().repeat(indent_level); let indent = doc.indent_unit().repeat(indent_level);
let mut text = String::with_capacity(1 + indent.len()); let mut text = String::with_capacity(1 + indent.len());
text.push('\n'); text.push('\n');
text.push_str(&indent); text.push_str(&indent);
(range.head, range.head, Some(text.into())) (pos, pos, Some(text.into()))
}); });
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
} }

Loading…
Cancel
Save