minor: Simplify some command code

This commit is contained in:
Blaž Hrastnik 2022-11-16 18:13:36 +09:00
parent a3173c2280
commit fe11ae2218
No known key found for this signature in database
GPG key ID: 1238B9C4AD889640

View file

@ -780,11 +780,7 @@ fn trim_selections(cx: &mut Context) {
let mut end = range.to();
start = movement::skip_while(text, start, |x| x.is_whitespace()).unwrap_or(start);
end = movement::backwards_skip_while(text, end, |x| x.is_whitespace()).unwrap_or(end);
if range.anchor < range.head {
Some(Range::new(start, end))
} else {
Some(Range::new(end, start))
}
Some(Range::new(start, end).with_direction(range.direction()))
})
.collect();
@ -1656,11 +1652,7 @@ fn search_impl(
// Determine range direction based on the primary range
let primary = selection.primary();
let range = if primary.head < primary.anchor {
Range::new(end, start)
} else {
Range::new(start, end)
};
let range = Range::new(start, end).with_direction(primary.direction());
let selection = match movement {
Movement::Extend => selection.clone().push(range),
@ -2093,11 +2085,7 @@ fn extend_to_line_bounds(cx: &mut Context) {
let start = text.line_to_char(start_line);
let end = text.line_to_char((end_line + 1).min(text.len_lines()));
if range.anchor <= range.head {
Range::new(start, end)
} else {
Range::new(end, start)
}
Range::new(start, end).with_direction(range.direction())
}),
);
}
@ -2134,11 +2122,7 @@ fn shrink_to_line_bounds(cx: &mut Context) {
end = text.line_to_char(end_line);
}
if range.anchor <= range.head {
Range::new(start, end)
} else {
Range::new(end, start)
}
Range::new(start, end).with_direction(range.direction())
}),
);
}
@ -2753,15 +2737,15 @@ fn goto_line(cx: &mut Context) {
fn goto_line_impl(editor: &mut Editor, count: Option<NonZeroUsize>) {
if let Some(count) = count {
let (view, doc) = current!(editor);
let max_line = if doc.text().line(doc.text().len_lines() - 1).len_chars() == 0 {
let text = doc.text().slice(..);
let max_line = if text.line(text.len_lines() - 1).len_chars() == 0 {
// If the last line is blank, don't jump to it.
doc.text().len_lines().saturating_sub(2)
text.len_lines().saturating_sub(2)
} else {
doc.text().len_lines() - 1
text.len_lines() - 1
};
let line_idx = std::cmp::min(count.get() - 1, max_line);
let text = doc.text().slice(..);
let pos = doc.text().line_to_char(line_idx);
let pos = text.line_to_char(line_idx);
let selection = doc
.selection(view.id)
.clone()
@ -2774,14 +2758,14 @@ fn goto_line_impl(editor: &mut Editor, count: Option<NonZeroUsize>) {
fn goto_last_line(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
let line_idx = if doc.text().line(doc.text().len_lines() - 1).len_chars() == 0 {
// If the last line is blank, don't jump to it.
doc.text().len_lines().saturating_sub(2)
} else {
doc.text().len_lines() - 1
};
let text = doc.text().slice(..);
let pos = doc.text().line_to_char(line_idx);
let line_idx = if text.line(text.len_lines() - 1).len_chars() == 0 {
// If the last line is blank, don't jump to it.
text.len_lines().saturating_sub(2)
} else {
text.len_lines() - 1
};
let pos = text.line_to_char(line_idx);
let selection = doc
.selection(view.id)
.clone()
@ -3823,7 +3807,7 @@ fn format_selections(cx: &mut Context) {
apply_transaction(&transaction, doc, view);
}
fn join_selections_inner(cx: &mut Context, select_space: bool) {
fn join_selections_impl(cx: &mut Context, select_space: bool) {
use movement::skip_while;
let (view, doc) = current!(cx.editor);
let text = doc.text();
@ -3902,11 +3886,11 @@ fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
}
fn join_selections(cx: &mut Context) {
join_selections_inner(cx, false)
join_selections_impl(cx, false)
}
fn join_selections_space(cx: &mut Context) {
join_selections_inner(cx, true)
join_selections_impl(cx, true)
}
fn keep_selections(cx: &mut Context) {