mirror of https://github.com/helix-editor/helix
Merge 77699f1473
into 1315b7e2b1
commit
ee9643e9e1
|
@ -466,6 +466,8 @@ impl MappableCommand {
|
|||
smart_tab, "Insert tab if all cursors have all whitespace to their left; otherwise, run a separate command.",
|
||||
insert_tab, "Insert tab char",
|
||||
insert_newline, "Insert newline char",
|
||||
insert_char_interactive, "Insert an interactively-chosen char",
|
||||
append_char_interactive, "Append an interactively-chosen char",
|
||||
delete_char_backward, "Delete previous char",
|
||||
delete_char_forward, "Delete next char",
|
||||
delete_word_backward, "Delete previous word",
|
||||
|
@ -4094,7 +4096,7 @@ fn hunk_range(hunk: Hunk, text: RopeSlice) -> Range {
|
|||
}
|
||||
|
||||
pub mod insert {
|
||||
use crate::events::PostInsertChar;
|
||||
use crate::{events::PostInsertChar, key};
|
||||
|
||||
use super::*;
|
||||
pub type Hook = fn(&Rope, &Selection, char) -> Option<Transaction>;
|
||||
|
@ -4173,11 +4175,15 @@ pub mod insert {
|
|||
}
|
||||
|
||||
pub fn insert_tab(cx: &mut Context) {
|
||||
insert_tab_impl(cx, 1)
|
||||
}
|
||||
|
||||
fn insert_tab_impl(cx: &mut Context, count: usize) {
|
||||
let (view, doc) = current!(cx.editor);
|
||||
// TODO: round out to nearest indentation level (for example a line with 3 spaces should
|
||||
// indent by one to reach 4 spaces).
|
||||
|
||||
let indent = Tendril::from(doc.indent_style.as_str());
|
||||
let indent = Tendril::from(doc.indent_style.as_str().repeat(count));
|
||||
let transaction = Transaction::insert(
|
||||
doc.text(),
|
||||
&doc.selection(view.id).clone().cursors(doc.text().slice(..)),
|
||||
|
@ -4186,6 +4192,49 @@ pub mod insert {
|
|||
doc.apply(&transaction, view.id);
|
||||
}
|
||||
|
||||
pub fn append_char_interactive(cx: &mut Context) {
|
||||
// Save the current mode, so we can restore it later.
|
||||
let mode = cx.editor.mode;
|
||||
append_mode(cx);
|
||||
insert_selection_interactive(cx, mode);
|
||||
}
|
||||
|
||||
pub fn insert_char_interactive(cx: &mut Context) {
|
||||
let mode = cx.editor.mode;
|
||||
insert_mode(cx);
|
||||
insert_selection_interactive(cx, mode);
|
||||
}
|
||||
|
||||
fn insert_selection_interactive(cx: &mut Context, old_mode: Mode) {
|
||||
let count = cx.count();
|
||||
|
||||
// need to wait for next key
|
||||
cx.on_next_key(move |cx, event| {
|
||||
match event {
|
||||
KeyEvent {
|
||||
code: KeyCode::Char(ch),
|
||||
..
|
||||
} => {
|
||||
for _ in 0..count {
|
||||
insert::insert_char(cx, ch)
|
||||
}
|
||||
}
|
||||
key!(Enter) => {
|
||||
if count != 1 {
|
||||
cx.editor
|
||||
.set_error("inserting multiple newlines not yet supported");
|
||||
return;
|
||||
}
|
||||
insert_newline(cx)
|
||||
}
|
||||
key!(Tab) => insert_tab_impl(cx, count),
|
||||
_ => (),
|
||||
};
|
||||
// Restore the old mode.
|
||||
cx.editor.mode = old_mode;
|
||||
});
|
||||
}
|
||||
|
||||
pub fn insert_newline(cx: &mut Context) {
|
||||
let config = cx.editor.config();
|
||||
let (view, doc) = current_ref!(cx.editor);
|
||||
|
|
Loading…
Reference in New Issue