Add more fns to update selection ranges (#38)

* feat: add push-range & set-primary-index fns

* feat: add remove-range fn

---------

Co-authored-by: meepleek <ergocrab@gmail.com>
pull/8675/merge^2
meepleek 2025-07-09 16:05:45 +02:00 committed by GitHub
parent 8218db65e8
commit 46abe80bdd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 34 additions and 0 deletions

View File

@ -384,6 +384,21 @@ fn load_static_commands(engine: &mut Engine, generate_sources: bool) {
set_selection, set_selection,
"Update the selection object to the current selection within the editor" "Update the selection object to the current selection within the editor"
); );
function1!(
"push-range-to-selection!",
push_range_to_selection,
"Push a new range to a selection. The new selection will be the primary one"
);
function1!(
"set-current-selection-primary-index!",
set_selection_primary_index,
"Set the primary index of the current selection"
);
function1!(
"remove-current-selection-range!",
remove_selection_range,
"Remove a range from the current selection"
);
function1!( function1!(
"regex-selection", "regex-selection",
@ -4429,6 +4444,25 @@ fn set_selection(cx: &mut Context, selection: Selection) {
doc.set_selection(view.id, selection) doc.set_selection(view.id, selection)
} }
fn push_range_to_selection(cx: &mut Context, range: Range) {
let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id).clone();
doc.set_selection(view.id, selection.push(range))
}
fn set_selection_primary_index(cx: &mut Context, primary_index: usize) {
let (view, doc) = current!(cx.editor);
let mut selection = doc.selection(view.id).clone();
selection.set_primary_index(primary_index);
doc.set_selection(view.id, selection)
}
fn remove_selection_range(cx: &mut Context, index: usize) {
let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id).clone();
doc.set_selection(view.id, selection.remove(index))
}
fn current_line_number(cx: &mut Context) -> usize { fn current_line_number(cx: &mut Context) -> usize {
let (view, doc) = current_ref!(cx.editor); let (view, doc) = current_ref!(cx.editor);
helix_core::coords_at_pos( helix_core::coords_at_pos(