Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto indent change if selection is linewise #7316

Merged
merged 9 commits into from
Jul 11, 2023
34 changes: 34 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ impl MappableCommand {
delete_selection, "Delete selection",
delete_selection_noyank, "Delete selection without yanking",
change_selection, "Change selection",
change_selection_with_indent, "Change selection and place the cursor at the appropriated indent",
pascalkuthe marked this conversation as resolved.
Show resolved Hide resolved
change_selection_with_indent_noyank, "Change selection without yanking and place the cursor at the appropriated indent",
change_selection_noyank, "Change selection without yanking",
collapse_selection, "Collapse selection into single cursor",
flip_selections, "Flip selection cursor and anchor",
Expand Down Expand Up @@ -2313,12 +2315,28 @@ fn shrink_to_line_bounds(cx: &mut Context) {
enum Operation {
Delete,
Change,
ChangeWithIndent,
}

fn only_whole_lines(selection: &Selection, text: &Rope) -> bool {
pascalkuthe marked this conversation as resolved.
Show resolved Hide resolved
selection.ranges().iter().all(|range| {
// If not at least a full line is selected (strange require 2).
if range.slice(text.slice(..)).len_lines() < 2 {
return false;
}
pascalkuthe marked this conversation as resolved.
Show resolved Hide resolved
// If the start of the selection is at the start of a line and the end at the end of a line.
let (start_line, end_line) = range.line_range(text.slice(..));
let start = text.line_to_char(start_line);
let end = text.line_to_char((end_line + 1).min(text.len_lines()));
start == range.from() && end == range.to()
pascalkuthe marked this conversation as resolved.
Show resolved Hide resolved
})
}

fn delete_selection_impl(cx: &mut Context, op: Operation) {
let (view, doc) = current!(cx.editor);

let selection = doc.selection(view.id);
let only_whole_lines = only_whole_lines(selection, doc.text());

if cx.register != Some('_') {
// first yank the selection
Expand All @@ -2341,6 +2359,13 @@ fn delete_selection_impl(cx: &mut Context, op: Operation) {
Operation::Change => {
enter_insert_mode(cx);
}
Operation::ChangeWithIndent => {
if only_whole_lines {
open_above(cx);
} else {
enter_insert_mode(cx);
}
}
}
}

Expand Down Expand Up @@ -2408,6 +2433,15 @@ fn change_selection_noyank(cx: &mut Context) {
delete_selection_impl(cx, Operation::Change);
}

fn change_selection_with_indent(cx: &mut Context) {
delete_selection_impl(cx, Operation::ChangeWithIndent);
}

fn change_selection_with_indent_noyank(cx: &mut Context) {
cx.register = Some('_');
delete_selection_impl(cx, Operation::ChangeWithIndent);
}

fn collapse_selection(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
Expand Down