|
|
|
@ -12,8 +12,13 @@ use helix_core::{
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use helix_view::{
|
|
|
|
|
clipboard::ClipboardType, document::Mode, editor::Action, input::KeyEvent, keyboard::KeyCode,
|
|
|
|
|
view::View, Document, DocumentId, Editor, ViewId,
|
|
|
|
|
clipboard::ClipboardType,
|
|
|
|
|
document::Mode,
|
|
|
|
|
editor::{Action, Motion},
|
|
|
|
|
input::KeyEvent,
|
|
|
|
|
keyboard::KeyCode,
|
|
|
|
|
view::View,
|
|
|
|
|
Document, DocumentId, Editor, ViewId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use anyhow::{anyhow, bail, Context as _};
|
|
|
|
@ -198,6 +203,7 @@ impl Command {
|
|
|
|
|
find_prev_char, "Move to previous occurance of char",
|
|
|
|
|
extend_till_prev_char, "Extend till previous occurance of char",
|
|
|
|
|
extend_prev_char, "Extend to previous occurance of char",
|
|
|
|
|
repeat_last_motion, "repeat last motion(extend_next_char, extend_till_char, find_next_char, find_till_char...)",
|
|
|
|
|
replace, "Replace with new char",
|
|
|
|
|
switch_case, "Switch (toggle) case",
|
|
|
|
|
switch_to_uppercase, "Switch to uppercase",
|
|
|
|
@ -666,8 +672,7 @@ fn extend_next_long_word_end(cx: &mut Context) {
|
|
|
|
|
extend_word_impl(cx, movement::move_next_long_word_end)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn find_char_impl<F>(cx: &mut Context, search_fn: F, inclusive: bool, extend: bool)
|
|
|
|
|
fn will_find_char<F>(cx: &mut Context, search_fn: F, inclusive: bool, extend: bool)
|
|
|
|
|
where
|
|
|
|
|
F: Fn(RopeSlice, char, usize, usize, bool) -> Option<usize> + 'static,
|
|
|
|
|
{
|
|
|
|
@ -705,7 +710,27 @@ where
|
|
|
|
|
_ => return,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let (view, doc) = current!(cx.editor);
|
|
|
|
|
find_char_impl(cx.editor, &search_fn, inclusive, extend, ch, count);
|
|
|
|
|
cx.editor.last_motion = Some(Motion(Box::new(move |editor: &mut Editor| {
|
|
|
|
|
find_char_impl(editor, &search_fn, inclusive, true, ch, 1);
|
|
|
|
|
})));
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn find_char_impl<F>(
|
|
|
|
|
editor: &mut Editor,
|
|
|
|
|
search_fn: &F,
|
|
|
|
|
inclusive: bool,
|
|
|
|
|
extend: bool,
|
|
|
|
|
ch: char,
|
|
|
|
|
count: usize,
|
|
|
|
|
) where
|
|
|
|
|
F: Fn(RopeSlice, char, usize, usize, bool) -> Option<usize> + 'static,
|
|
|
|
|
{
|
|
|
|
|
let (view, doc) = current!(editor);
|
|
|
|
|
let text = doc.text().slice(..);
|
|
|
|
|
|
|
|
|
|
let selection = doc.selection(view.id).clone().transform(|range| {
|
|
|
|
@ -727,7 +752,6 @@ where
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
doc.set_selection(view.id, selection);
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn find_next_char_impl(
|
|
|
|
@ -741,6 +765,10 @@ fn find_next_char_impl(
|
|
|
|
|
if inclusive {
|
|
|
|
|
search::find_nth_next(text, ch, pos, n)
|
|
|
|
|
} else {
|
|
|
|
|
let n = match text.get_char(pos) {
|
|
|
|
|
Some(next_ch) if next_ch == ch => n + 1,
|
|
|
|
|
_ => n,
|
|
|
|
|
};
|
|
|
|
|
search::find_nth_next(text, ch, pos, n).map(|n| n.saturating_sub(1))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -755,80 +783,52 @@ fn find_prev_char_impl(
|
|
|
|
|
if inclusive {
|
|
|
|
|
search::find_nth_prev(text, ch, pos, n)
|
|
|
|
|
} else {
|
|
|
|
|
let n = match text.get_char(pos.saturating_sub(1)) {
|
|
|
|
|
Some(next_ch) if next_ch == ch => n + 1,
|
|
|
|
|
_ => n,
|
|
|
|
|
};
|
|
|
|
|
search::find_nth_prev(text, ch, pos, n).map(|n| (n + 1).min(text.len_chars()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn find_till_char(cx: &mut Context) {
|
|
|
|
|
find_char_impl(
|
|
|
|
|
cx,
|
|
|
|
|
find_next_char_impl,
|
|
|
|
|
false, /* inclusive */
|
|
|
|
|
false, /* extend */
|
|
|
|
|
)
|
|
|
|
|
will_find_char(cx, find_next_char_impl, false, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn find_next_char(cx: &mut Context) {
|
|
|
|
|
find_char_impl(
|
|
|
|
|
cx,
|
|
|
|
|
find_next_char_impl,
|
|
|
|
|
true, /* inclusive */
|
|
|
|
|
false, /* extend */
|
|
|
|
|
)
|
|
|
|
|
will_find_char(cx, find_next_char_impl, true, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn extend_till_char(cx: &mut Context) {
|
|
|
|
|
find_char_impl(
|
|
|
|
|
cx,
|
|
|
|
|
find_next_char_impl,
|
|
|
|
|
false, /* inclusive */
|
|
|
|
|
true, /* extend */
|
|
|
|
|
)
|
|
|
|
|
will_find_char(cx, find_next_char_impl, false, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn extend_next_char(cx: &mut Context) {
|
|
|
|
|
find_char_impl(
|
|
|
|
|
cx,
|
|
|
|
|
find_next_char_impl,
|
|
|
|
|
true, /* inclusive */
|
|
|
|
|
true, /* extend */
|
|
|
|
|
)
|
|
|
|
|
will_find_char(cx, find_next_char_impl, true, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn till_prev_char(cx: &mut Context) {
|
|
|
|
|
find_char_impl(
|
|
|
|
|
cx,
|
|
|
|
|
find_prev_char_impl,
|
|
|
|
|
false, /* inclusive */
|
|
|
|
|
false, /* extend */
|
|
|
|
|
)
|
|
|
|
|
will_find_char(cx, find_prev_char_impl, false, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn find_prev_char(cx: &mut Context) {
|
|
|
|
|
find_char_impl(
|
|
|
|
|
cx,
|
|
|
|
|
find_prev_char_impl,
|
|
|
|
|
true, /* inclusive */
|
|
|
|
|
false, /* extend */
|
|
|
|
|
)
|
|
|
|
|
will_find_char(cx, find_prev_char_impl, true, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn extend_till_prev_char(cx: &mut Context) {
|
|
|
|
|
find_char_impl(
|
|
|
|
|
cx,
|
|
|
|
|
find_prev_char_impl,
|
|
|
|
|
false, /* inclusive */
|
|
|
|
|
true, /* extend */
|
|
|
|
|
)
|
|
|
|
|
will_find_char(cx, find_prev_char_impl, false, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn extend_prev_char(cx: &mut Context) {
|
|
|
|
|
find_char_impl(
|
|
|
|
|
cx,
|
|
|
|
|
find_prev_char_impl,
|
|
|
|
|
true, /* inclusive */
|
|
|
|
|
true, /* extend */
|
|
|
|
|
)
|
|
|
|
|
will_find_char(cx, find_prev_char_impl, true, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn repeat_last_motion(cx: &mut Context) {
|
|
|
|
|
let last_motion = cx.editor.last_motion.take();
|
|
|
|
|
if let Some(m) = &last_motion {
|
|
|
|
|
m.run(cx.editor);
|
|
|
|
|
cx.editor.last_motion = last_motion;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn replace(cx: &mut Context) {
|
|
|
|
@ -4495,7 +4495,8 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
|
|
|
|
|
let count = cx.count();
|
|
|
|
|
cx.on_next_key(move |cx, event| {
|
|
|
|
|
if let Some(ch) = event.char() {
|
|
|
|
|
let (view, doc) = current!(cx.editor);
|
|
|
|
|
let textobject = move |editor: &mut Editor| {
|
|
|
|
|
let (view, doc) = current!(editor);
|
|
|
|
|
let text = doc.text().slice(..);
|
|
|
|
|
|
|
|
|
|
let textobject_treesitter = |obj_name: &str, range: Range| -> Range {
|
|
|
|
@ -4528,6 +4529,9 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
doc.set_selection(view.id, selection);
|
|
|
|
|
};
|
|
|
|
|
textobject(&mut cx.editor);
|
|
|
|
|
cx.editor.last_motion = Some(Motion(Box::new(textobject)));
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|