Put selection in separate variable in commands code.

imgbot
Nathan Vegdahl 3 years ago
parent ad814b8c2e
commit 427ae6ac6c

@ -325,140 +325,111 @@ 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( let text = doc.text().slice(..);
view.id,
doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..);
let line = range.head_line(text);
let mut pos = line_end_char_index(&text, line); let selection = doc.selection(view.id).clone().transform(|range| {
if doc.mode != Mode::Select { let line = range.head_line(text);
pos = graphemes::prev_grapheme_boundary(text, pos);
}
pos = range.head.max(pos).max(text.line_to_char(line)); let mut pos = line_end_char_index(&text, line);
if doc.mode != Mode::Select {
pos = graphemes::prev_grapheme_boundary(text, pos);
}
range.put(text, pos, doc.mode == Mode::Select) pos = range.head.max(pos).max(text.line_to_char(line));
}),
); 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);
let text = doc.text().slice(..);
doc.set_selection( let selection = doc.selection(view.id).clone().transform(|range| {
view.id, let line = range.head_line(text);
doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..);
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()));
if doc.mode != Mode::Select { if doc.mode != Mode::Select {
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( let text = doc.text().slice(..);
view.id,
doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..);
let line = range.head_line(text);
// adjust to start of the line let selection = doc.selection(view.id).clone().transform(|range| {
let pos = text.line_to_char(line); let line = range.head_line(text);
range.put(text, pos, doc.mode == Mode::Select)
}), // adjust to start of the line
); let pos = text.line_to_char(line);
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( let text = doc.text().slice(..);
view.id,
doc.selection(view.id).clone().transform(|range| {
let text = doc.text().slice(..);
let line = range.head_line(text);
if let Some(pos) = find_first_non_whitespace_char(text.line(line)) { let selection = doc.selection(view.id).clone().transform(|range| {
let pos = pos + text.line_to_char(line); let line = range.head_line(text);
range.put(text, pos, doc.mode == Mode::Select)
} else { if let Some(pos) = find_first_non_whitespace_char(text.line(line)) {
range let pos = pos + text.line_to_char(line);
} range.put(text, pos, doc.mode == Mode::Select)
}), } else {
); 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(text)
.min_width_1(doc.text().slice(..)) .transform(|range| movement::move_next_word_start(text, range, count));
.transform(|range| movement::move_next_word_start(doc.text().slice(..), 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
.clone() .selection(view.id)
.min_width_1(doc.text().slice(..)) .clone()
.transform(|range| movement::move_prev_word_start(doc.text().slice(..), range, count)), .min_width_1(text)
); .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
.clone() .selection(view.id)
.min_width_1(doc.text().slice(..)) .clone()
.transform(|range| movement::move_next_word_end(doc.text().slice(..), range, count)), .min_width_1(text)
); .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
.clone() .selection(view.id)
.min_width_1(doc.text().slice(..)) .clone()
.transform(|range| { .min_width_1(text)
movement::move_next_long_word_start(doc.text().slice(..), range, count) .transform(|range| movement::move_next_long_word_start(text, 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
.clone() .selection(view.id)
.min_width_1(doc.text().slice(..)) .clone()
.transform(|range| { .min_width_1(text)
movement::move_prev_long_word_start(doc.text().slice(..), range, count) .transform(|range| movement::move_prev_long_word_start(text, 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
.clone() .selection(view.id)
.min_width_1(doc.text().slice(..)) .clone()
.transform(|range| { .min_width_1(text)
movement::move_next_long_word_end(doc.text().slice(..), range, count) .transform(|range| movement::move_next_long_word_end(text, 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
.clone() .selection(view.id)
.min_width_1(doc.text().slice(..)) .clone()
.transform(|range| { .min_width_1(text)
let text = doc.text().slice(..); .transform(|range| {
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
.clone() .selection(view.id)
.min_width_1(doc.text().slice(..)) .clone()
.transform(|range| { .min_width_1(text)
let text = doc.text().slice(..); .transform(|range| {
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
.clone() .selection(view.id)
.min_width_1(doc.text().slice(..)) .clone()
.transform(|range| { .min_width_1(text)
let text = doc.text().slice(..); .transform(|range| {
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);
let text = doc.text().slice(..);
doc.set_selection( let selection = doc.selection(view.id).clone().transform(|range| {
view.id, search_fn(text, ch, range.head, count, inclusive)
doc.selection(view.id).clone().transform(|range| { .map_or(range, |pos| range.put(text, pos, extend))
let text = doc.text().slice(..); });
search_fn(text, ch, range.head, count, inclusive) doc.set_selection(view.id, selection);
.map_or(range, |pos| range.put(text, pos, extend))
}),
);
}) })
} }
@ -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, let pos = if range.head > range.anchor {
doc.selection(view.id).clone().transform(|range| { // For 1-width cursor semantics.
let pos = if range.head > range.anchor { graphemes::prev_grapheme_boundary(text, range.head)
// For 1-width cursor semantics. } else {
graphemes::prev_grapheme_boundary(doc.text().slice(..), range.head) range.head
} else { };
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, Range::new(
selection.clone().transform(|range| { range.from(),
Range::new( graphemes::next_grapheme_boundary(doc.text().slice(..), range.to()),
range.from(), )
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, let text = doc.text().slice(..);
doc.selection(view.id).clone().transform(|range| { let line = range.head_line(text);
let text = doc.text().slice(..); let pos = line_end_char_index(&text, line);
let line = range.head_line(text); Range::new(pos, pos)
let pos = line_end_char_index(&text, line); });
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(text, range.to()),
graphemes::prev_grapheme_boundary(doc.text().slice(..), 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| { Range::new(
if range.is_empty() && range.head == doc.text().len_chars() { graphemes::prev_grapheme_boundary(text, range.anchor),
Range::new( range.head,
graphemes::prev_grapheme_boundary(doc.text().slice(..), range.anchor), )
range.head, } else {
) range.min_width_1(text)
} else { }
range.min_width_1(doc.text().slice(..)) });
} 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,21 +3656,19 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
} = event } = event
{ {
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, match ch {
doc.selection(view.id).clone().transform(|range| { 'w' => textobject::textobject_word(text, range, objtype, count),
let text = doc.text().slice(..); // TODO: cancel new ranges if inconsistent surround matches across lines
match ch { ch if !ch.is_ascii_alphanumeric() => {
'w' => textobject::textobject_word(text, range, objtype, count), textobject::textobject_surround(text, range, objtype, ch, count)
// TODO: cancel new ranges if inconsistent surround matches across lines
ch if !ch.is_ascii_alphanumeric() => {
textobject::textobject_surround(text, range, objtype, ch, count)
}
_ => range,
} }
}), _ => range,
); }
});
doc.set_selection(view.id, selection);
} }
}) })
} }

Loading…
Cancel
Save