Add support for extend_file_{start,end}.

Solves #11766.
This commit is contained in:
Dimitri Sabadie 2024-09-24 17:49:15 +02:00
parent 73deabaa40
commit 617e800833
No known key found for this signature in database
GPG Key ID: A94386A8A6252ECB
2 changed files with 62 additions and 7 deletions

View File

@ -400,6 +400,8 @@ pub fn doc(&self) -> &str {
goto_implementation, "Goto implementation",
goto_file_start, "Goto line number <n> else file start",
goto_file_end, "Goto file end",
extend_file_start, "Extend to line number<n> else file start",
extend_file_end, "Extend to file end",
goto_file, "Goto files/URLs in selections",
goto_file_hsplit, "Goto files in selections (hsplit)",
goto_file_vsplit, "Goto files in selections (vsplit)",
@ -1222,28 +1224,58 @@ fn goto_next_paragraph(cx: &mut Context) {
}
fn goto_file_start(cx: &mut Context) {
goto_file_start_impl(
cx,
if cx.editor.mode == Mode::Select {
Movement::Extend
} else {
Movement::Move
},
);
}
fn extend_file_start(cx: &mut Context) {
goto_file_start_impl(cx, Movement::Extend);
}
fn goto_file_start_impl(cx: &mut Context, movement: Movement) {
if cx.count.is_some() {
goto_line(cx);
goto_line_impl(cx, movement);
} else {
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let selection = doc
.selection(view.id)
.clone()
.transform(|range| range.put_cursor(text, 0, cx.editor.mode == Mode::Select));
.transform(|range| range.put_cursor(text, 0, movement == Movement::Extend));
push_jump(view, doc);
doc.set_selection(view.id, selection);
}
}
fn goto_file_end(cx: &mut Context) {
goto_file_end_impl(
cx,
if cx.editor.mode == Mode::Select {
Movement::Extend
} else {
Movement::Move
},
);
}
fn extend_file_end(cx: &mut Context) {
goto_file_end_impl(cx, Movement::Extend)
}
fn goto_file_end_impl(cx: &mut Context, movement: Movement) {
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let pos = doc.text().len_chars();
let selection = doc
.selection(view.id)
.clone()
.transform(|range| range.put_cursor(text, pos, cx.editor.mode == Mode::Select));
.transform(|range| range.put_cursor(text, pos, movement == Movement::Extend));
push_jump(view, doc);
doc.set_selection(view.id, selection);
}
@ -3525,15 +3557,30 @@ fn push_jump(view: &mut View, doc: &Document) {
}
fn goto_line(cx: &mut Context) {
goto_line_impl(
cx,
if cx.editor.mode == Mode::Select {
Movement::Extend
} else {
Movement::Move
},
);
}
fn goto_line_impl(cx: &mut Context, movement: Movement) {
if cx.count.is_some() {
let (view, doc) = current!(cx.editor);
push_jump(view, doc);
goto_line_without_jumplist(cx.editor, cx.count);
goto_line_without_jumplist(cx.editor, cx.count, movement);
}
}
fn goto_line_without_jumplist(editor: &mut Editor, count: Option<NonZeroUsize>) {
fn goto_line_without_jumplist(
editor: &mut Editor,
count: Option<NonZeroUsize>,
movement: Movement,
) {
if let Some(count) = count {
let (view, doc) = current!(editor);
let text = doc.text().slice(..);
@ -3548,7 +3595,7 @@ fn goto_line_without_jumplist(editor: &mut Editor, count: Option<NonZeroUsize>)
let selection = doc
.selection(view.id)
.clone()
.transform(|range| range.put_cursor(text, pos, editor.mode == Mode::Select));
.transform(|range| range.put_cursor(text, pos, movement == Movement::Extend));
doc.set_selection(view.id, selection);
}

View File

@ -1814,7 +1814,15 @@ fn update_goto_line_number_preview(
let scrolloff = cx.editor.config().scrolloff;
let line = args[0].parse::<usize>()?;
goto_line_without_jumplist(cx.editor, NonZeroUsize::new(line));
goto_line_without_jumplist(
cx.editor,
NonZeroUsize::new(line),
if cx.editor.mode == Mode::Select {
Movement::Extend
} else {
Movement::Move
},
);
let (view, doc) = current!(cx.editor);
view.ensure_cursor_in_view(doc, scrolloff);