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

feat: autohelp for delete, replace and add surrounds #12262

Merged
merged 4 commits into from
Dec 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5671,8 +5671,17 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
cx.editor.autoinfo = Some(Info::new(title, &help_text));
}

static SURROUND_HELP_TEXT: [(&str, &str); 5] = [
("( or )", "Parentheses"),
("{ or }", "Curly braces"),
("< or >", "Angled brackets"),
("[ or ]", "Square brackets"),
(" ", "... or any character"),
];

fn surround_add(cx: &mut Context) {
cx.on_next_key(move |cx, event| {
cx.editor.autoinfo = None;
let (view, doc) = current!(cx.editor);
// surround_len is the number of new characters being added.
let (open, close, surround_len) = match event.char() {
Expand Down Expand Up @@ -5713,7 +5722,9 @@ fn surround_add(cx: &mut Context) {
.with_selection(Selection::new(ranges, selection.primary_index()));
doc.apply(&transaction, view.id);
exit_select_mode(cx);
})
});

cx.editor.autoinfo = Some(Info::new("Surround selections with", &SURROUND_HELP_TEXT));
}

fn surround_replace(cx: &mut Context) {
Expand Down Expand Up @@ -5745,6 +5756,7 @@ fn surround_replace(cx: &mut Context) {
);

cx.on_next_key(move |cx, event| {
cx.editor.autoinfo = None;
let (view, doc) = current!(cx.editor);
let to = match event.char() {
Some(to) => to,
Expand Down Expand Up @@ -5772,12 +5784,20 @@ fn surround_replace(cx: &mut Context) {
doc.apply(&transaction, view.id);
exit_select_mode(cx);
});
})

cx.editor.autoinfo = Some(Info::new("Replace with a pair of", &SURROUND_HELP_TEXT));
});

cx.editor.autoinfo = Some(Info::new(
"Replace surrounding pair of",
&SURROUND_HELP_TEXT,
));
}

fn surround_delete(cx: &mut Context) {
let count = cx.count();
cx.on_next_key(move |cx, event| {
cx.editor.autoinfo = None;
let surround_ch = match event.char() {
Some('m') => None, // m selects the closest surround pair
Some(ch) => Some(ch),
Expand All @@ -5800,7 +5820,9 @@ fn surround_delete(cx: &mut Context) {
Transaction::change(doc.text(), change_pos.into_iter().map(|p| (p, p + 1, None)));
doc.apply(&transaction, view.id);
exit_select_mode(cx);
})
});

cx.editor.autoinfo = Some(Info::new("Delete surrounding pair of", &SURROUND_HELP_TEXT));
}

#[derive(Eq, PartialEq)]
Expand Down
Loading