Put selection in separate variable in commands code.

pull/376/head
Nathan Vegdahl 3 years ago
parent ad814b8c2e
commit 427ae6ac6c

@ -325,77 +325,52 @@ impl PartialEq for Command {
fn move_char_left(cx: &mut Context) { fn move_char_left(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
movement::move_horizontally( movement::move_horizontally(text, range, Direction::Backward, count, Movement::Move)
doc.text().slice(..), });
range, doc.set_selection(view.id, selection);
Direction::Backward,
count,
Movement::Move,
)
}),
);
} }
fn move_char_right(cx: &mut Context) { fn move_char_right(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
movement::move_horizontally( movement::move_horizontally(text, range, Direction::Forward, count, Movement::Move)
doc.text().slice(..), });
range, doc.set_selection(view.id, selection);
Direction::Forward,
count,
Movement::Move,
)
}),
);
} }
fn move_line_up(cx: &mut Context) { fn move_line_up(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
movement::move_vertically( movement::move_vertically(text, range, Direction::Backward, count, Movement::Move)
doc.text().slice(..), });
range, doc.set_selection(view.id, selection);
Direction::Backward,
count,
Movement::Move,
)
}),
);
} }
fn move_line_down(cx: &mut Context) { fn move_line_down(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
movement::move_vertically( movement::move_vertically(text, range, Direction::Forward, count, Movement::Move)
doc.text().slice(..), });
range, doc.set_selection(view.id, selection);
Direction::Forward,
count,
Movement::Move,
)
}),
);
} }
fn goto_line_end(cx: &mut Context) { fn goto_line_end(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection(
view.id,
doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = doc.selection(view.id).clone().transform(|range| {
let line = range.head_line(text); let line = range.head_line(text);
let mut pos = line_end_char_index(&text, line); let mut pos = line_end_char_index(&text, line);
@ -406,17 +381,15 @@ fn goto_line_end(cx: &mut Context) {
pos = range.head.max(pos).max(text.line_to_char(line)); pos = range.head.max(pos).max(text.line_to_char(line));
range.put(text, pos, doc.mode == Mode::Select) range.put(text, pos, doc.mode == Mode::Select)
}), });
); doc.set_selection(view.id, selection);
} }
fn goto_line_end_newline(cx: &mut Context) { fn goto_line_end_newline(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection(
view.id,
doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = doc.selection(view.id).clone().transform(|range| {
let line = range.head_line(text); let line = range.head_line(text);
let mut pos = text.line_to_char((line + 1).min(text.len_lines())); let mut pos = text.line_to_char((line + 1).min(text.len_lines()));
@ -424,31 +397,29 @@ fn goto_line_end_newline(cx: &mut Context) {
pos = graphemes::prev_grapheme_boundary(text, pos); pos = graphemes::prev_grapheme_boundary(text, pos);
} }
range.put(text, pos, doc.mode == Mode::Select) range.put(text, pos, doc.mode == Mode::Select)
}), });
); doc.set_selection(view.id, selection);
} }
fn goto_line_start(cx: &mut Context) { fn goto_line_start(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection(
view.id,
doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = doc.selection(view.id).clone().transform(|range| {
let line = range.head_line(text); let line = range.head_line(text);
// adjust to start of the line // adjust to start of the line
let pos = text.line_to_char(line); let pos = text.line_to_char(line);
range.put(text, pos, doc.mode == Mode::Select) range.put(text, pos, doc.mode == Mode::Select)
}), });
); doc.set_selection(view.id, selection);
} }
fn goto_first_nonwhitespace(cx: &mut Context) { fn goto_first_nonwhitespace(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection(
view.id,
doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = doc.selection(view.id).clone().transform(|range| {
let line = range.head_line(text); let line = range.head_line(text);
if let Some(pos) = find_first_non_whitespace_char(text.line(line)) { if let Some(pos) = find_first_non_whitespace_char(text.line(line)) {
@ -457,8 +428,8 @@ fn goto_first_nonwhitespace(cx: &mut Context) {
} else { } else {
range range
} }
}), });
); doc.set_selection(view.id, selection);
} }
fn goto_window(cx: &mut Context, align: Align) { fn goto_window(cx: &mut Context, align: Align) {
@ -499,80 +470,79 @@ fn goto_window_bottom(cx: &mut Context) {
fn move_next_word_start(cx: &mut Context) { fn move_next_word_start(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
doc.set_selection( let selection = doc
view.id, .selection(view.id)
doc.selection(view.id)
.clone() .clone()
.min_width_1(doc.text().slice(..)) .min_width_1(text)
.transform(|range| movement::move_next_word_start(doc.text().slice(..), range, count)), .transform(|range| movement::move_next_word_start(text, range, count));
); doc.set_selection(view.id, selection);
} }
fn move_prev_word_start(cx: &mut Context) { fn move_prev_word_start(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id) let selection = doc
.selection(view.id)
.clone() .clone()
.min_width_1(doc.text().slice(..)) .min_width_1(text)
.transform(|range| movement::move_prev_word_start(doc.text().slice(..), range, count)), .transform(|range| movement::move_prev_word_start(text, range, count));
); doc.set_selection(view.id, selection);
} }
fn move_next_word_end(cx: &mut Context) { fn move_next_word_end(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id) let selection = doc
.selection(view.id)
.clone() .clone()
.min_width_1(doc.text().slice(..)) .min_width_1(text)
.transform(|range| movement::move_next_word_end(doc.text().slice(..), range, count)), .transform(|range| movement::move_next_word_end(text, range, count));
); doc.set_selection(view.id, selection);
} }
fn move_next_long_word_start(cx: &mut Context) { fn move_next_long_word_start(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id) let selection = doc
.selection(view.id)
.clone() .clone()
.min_width_1(doc.text().slice(..)) .min_width_1(text)
.transform(|range| { .transform(|range| movement::move_next_long_word_start(text, range, count));
movement::move_next_long_word_start(doc.text().slice(..), range, count) doc.set_selection(view.id, selection);
}),
);
} }
fn move_prev_long_word_start(cx: &mut Context) { fn move_prev_long_word_start(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id) let selection = doc
.selection(view.id)
.clone() .clone()
.min_width_1(doc.text().slice(..)) .min_width_1(text)
.transform(|range| { .transform(|range| movement::move_prev_long_word_start(text, range, count));
movement::move_prev_long_word_start(doc.text().slice(..), range, count) doc.set_selection(view.id, selection);
}),
);
} }
fn move_next_long_word_end(cx: &mut Context) { fn move_next_long_word_end(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id) let selection = doc
.selection(view.id)
.clone() .clone()
.min_width_1(doc.text().slice(..)) .min_width_1(text)
.transform(|range| { .transform(|range| movement::move_next_long_word_end(text, range, count));
movement::move_next_long_word_end(doc.text().slice(..), range, count) doc.set_selection(view.id, selection);
}),
);
} }
fn goto_file_start(cx: &mut Context) { fn goto_file_start(cx: &mut Context) {
@ -590,52 +560,52 @@ fn goto_file_end(cx: &mut Context) {
fn extend_next_word_start(cx: &mut Context) { fn extend_next_word_start(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id) let selection = doc
.selection(view.id)
.clone() .clone()
.min_width_1(doc.text().slice(..)) .min_width_1(text)
.transform(|range| { .transform(|range| {
let text = doc.text().slice(..);
let word = movement::move_next_word_start(text, range, count); let word = movement::move_next_word_start(text, range, count);
let pos = word.head; let pos = word.head;
range.put(text, pos, true) range.put(text, pos, true)
}), });
); doc.set_selection(view.id, selection);
} }
fn extend_prev_word_start(cx: &mut Context) { fn extend_prev_word_start(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id) let selection = doc
.selection(view.id)
.clone() .clone()
.min_width_1(doc.text().slice(..)) .min_width_1(text)
.transform(|range| { .transform(|range| {
let text = doc.text().slice(..);
let word = movement::move_prev_word_start(text, range, count); let word = movement::move_prev_word_start(text, range, count);
let pos = word.head; let pos = word.head;
range.put(text, pos, true) range.put(text, pos, true)
}), });
); doc.set_selection(view.id, selection);
} }
fn extend_next_word_end(cx: &mut Context) { fn extend_next_word_end(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id) let selection = doc
.selection(view.id)
.clone() .clone()
.min_width_1(doc.text().slice(..)) .min_width_1(text)
.transform(|range| { .transform(|range| {
let text = doc.text().slice(..);
let word = movement::move_next_word_end(text, range, count); let word = movement::move_next_word_end(text, range, count);
let pos = word.head; let pos = word.head;
range.put(text, pos, true) range.put(text, pos, true)
}), });
); doc.set_selection(view.id, selection);
} }
#[inline] #[inline]
@ -678,15 +648,13 @@ where
}; };
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection(
view.id,
doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = doc.selection(view.id).clone().transform(|range| {
search_fn(text, ch, range.head, count, inclusive) search_fn(text, ch, range.head, count, inclusive)
.map_or(range, |pos| range.put(text, pos, extend)) .map_or(range, |pos| range.put(text, pos, extend))
}), });
); doc.set_selection(view.id, selection);
}) })
} }
@ -939,69 +907,45 @@ fn half_page_down(cx: &mut Context) {
fn extend_char_left(cx: &mut Context) { fn extend_char_left(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
movement::move_horizontally( movement::move_horizontally(text, range, Direction::Backward, count, Movement::Extend)
doc.text().slice(..), });
range, doc.set_selection(view.id, selection);
Direction::Backward,
count,
Movement::Extend,
)
}),
);
} }
fn extend_char_right(cx: &mut Context) { fn extend_char_right(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
movement::move_horizontally( movement::move_horizontally(text, range, Direction::Forward, count, Movement::Extend)
doc.text().slice(..), });
range, doc.set_selection(view.id, selection);
Direction::Forward,
count,
Movement::Extend,
)
}),
);
} }
fn extend_line_up(cx: &mut Context) { fn extend_line_up(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
movement::move_vertically( movement::move_vertically(text, range, Direction::Backward, count, Movement::Extend)
doc.text().slice(..), });
range, doc.set_selection(view.id, selection);
Direction::Backward,
count,
Movement::Extend,
)
}),
);
} }
fn extend_line_down(cx: &mut Context) { fn extend_line_down(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let text = doc.text().slice(..);
view.id,
doc.selection(view.id).clone().transform(|range| { let selection = doc.selection(view.id).clone().transform(|range| {
movement::move_vertically( movement::move_vertically(text, range, Direction::Forward, count, Movement::Extend)
doc.text().slice(..), });
range, doc.set_selection(view.id, selection);
Direction::Forward,
count,
Movement::Extend,
)
}),
);
} }
fn select_all(cx: &mut Context) { fn select_all(cx: &mut Context) {
@ -1222,30 +1166,28 @@ fn change_selection(cx: &mut Context) {
fn collapse_selection(cx: &mut Context) { fn collapse_selection(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
doc.set_selection( let selection = doc.selection(view.id).clone().transform(|range| {
view.id,
doc.selection(view.id).clone().transform(|range| {
let pos = if range.head > range.anchor { let pos = if range.head > range.anchor {
// For 1-width cursor semantics. // For 1-width cursor semantics.
graphemes::prev_grapheme_boundary(doc.text().slice(..), range.head) graphemes::prev_grapheme_boundary(text, range.head)
} else { } else {
range.head range.head
}; };
Range::new(pos, pos) Range::new(pos, pos)
}), });
); doc.set_selection(view.id, selection);
} }
fn flip_selections(cx: &mut Context) { fn flip_selections(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection( let selection = doc
view.id, .selection(view.id)
doc.selection(view.id)
.clone() .clone()
.transform(|range| Range::new(range.head, range.anchor)), .transform(|range| Range::new(range.head, range.anchor));
); doc.set_selection(view.id, selection);
} }
fn enter_insert_mode(doc: &mut Document) { fn enter_insert_mode(doc: &mut Document) {
@ -1257,12 +1199,11 @@ fn insert_mode(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
enter_insert_mode(doc); enter_insert_mode(doc);
doc.set_selection( let selection = doc
view.id, .selection(view.id)
doc.selection(view.id)
.clone() .clone()
.transform(|range| Range::new(range.to(), range.from())), .transform(|range| Range::new(range.to(), range.from()));
); doc.set_selection(view.id, selection);
} }
// inserts at the end of each selection // inserts at the end of each selection
@ -1286,15 +1227,13 @@ fn append_mode(cx: &mut Context) {
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
} }
doc.set_selection( let selection = selection.transform(|range| {
view.id,
selection.clone().transform(|range| {
Range::new( Range::new(
range.from(), range.from(),
graphemes::next_grapheme_boundary(doc.text().slice(..), range.to()), graphemes::next_grapheme_boundary(doc.text().slice(..), range.to()),
) )
}), });
); doc.set_selection(view.id, selection);
} }
mod cmd { mod cmd {
@ -2200,15 +2139,13 @@ fn append_to_line(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
enter_insert_mode(doc); enter_insert_mode(doc);
doc.set_selection( let selection = doc.selection(view.id).clone().transform(|range| {
view.id,
doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let line = range.head_line(text); let line = range.head_line(text);
let pos = line_end_char_index(&text, line); let pos = line_end_char_index(&text, line);
Range::new(pos, pos) Range::new(pos, pos)
}), });
); doc.set_selection(view.id, selection);
} }
/// Sometimes when applying formatting changes we want to mark the buffer as unmodified, for /// Sometimes when applying formatting changes we want to mark the buffer as unmodified, for
@ -2338,15 +2275,14 @@ fn normal_mode(cx: &mut Context) {
// if leaving append mode, move cursor back by 1 // if leaving append mode, move cursor back by 1
if doc.restore_cursor { if doc.restore_cursor {
doc.set_selection( let text = doc.text().slice(..);
view.id, let selection = doc.selection(view.id).clone().transform(|range| {
doc.selection(view.id).clone().transform(|range| {
Range::new( Range::new(
range.from(), range.from(),
graphemes::prev_grapheme_boundary(doc.text().slice(..), range.to()), graphemes::prev_grapheme_boundary(text, range.to()),
) )
}), });
); doc.set_selection(view.id, selection);
doc.restore_cursor = false; doc.restore_cursor = false;
} }
@ -2370,22 +2306,21 @@ fn goto_last_accessed_file(cx: &mut Context) {
fn select_mode(cx: &mut Context) { fn select_mode(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
// Make sure all selections are at least 1-wide. // Make sure all selections are at least 1-wide.
// (With the exception of being in an empty document, of course.) // (With the exception of being in an empty document, of course.)
doc.set_selection( let selection = doc.selection(view.id).clone().transform(|range| {
view.id, if range.is_empty() && range.head == text.len_chars() {
doc.selection(view.id).clone().transform(|range| {
if range.is_empty() && range.head == doc.text().len_chars() {
Range::new( Range::new(
graphemes::prev_grapheme_boundary(doc.text().slice(..), range.anchor), graphemes::prev_grapheme_boundary(text, range.anchor),
range.head, range.head,
) )
} else { } else {
range.min_width_1(doc.text().slice(..)) range.min_width_1(text)
} }
}), });
); doc.set_selection(view.id, selection);
doc_mut!(cx.editor).mode = Mode::Select; doc_mut!(cx.editor).mode = Mode::Select;
} }
@ -2964,13 +2899,13 @@ pub mod insert {
pub fn delete_word_backward(cx: &mut Context) { pub fn delete_word_backward(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
doc.set_selection( let selection = doc
view.id, .selection(view.id)
doc.selection(view.id).clone().transform(|range| { .clone()
movement::move_prev_word_start(doc.text().slice(..), range, count) .transform(|range| movement::move_prev_word_start(text, range, count));
}), doc.set_selection(view.id, selection);
);
delete_selection(cx) delete_selection(cx)
} }
} }
@ -3721,11 +3656,9 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
} = event } = event
{ {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
doc.set_selection(
view.id,
doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = doc.selection(view.id).clone().transform(|range| {
match ch { match ch {
'w' => textobject::textobject_word(text, range, objtype, count), 'w' => textobject::textobject_word(text, range, objtype, count),
// TODO: cancel new ranges if inconsistent surround matches across lines // TODO: cancel new ranges if inconsistent surround matches across lines
@ -3734,8 +3667,8 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
} }
_ => range, _ => range,
} }
}), });
); doc.set_selection(view.id, selection);
} }
}) })
} }

Loading…
Cancel
Save