Skip to content

Commit

Permalink
Add support for extend_file_{start,end}.
Browse files Browse the repository at this point in the history
  • Loading branch information
hadronized committed Sep 24, 2024
1 parent 73deaba commit 617e800
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
59 changes: 53 additions & 6 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ impl MappableCommand {
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)",
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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(..);
Expand All @@ -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);
}
Expand Down
10 changes: 9 additions & 1 deletion helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 617e800

Please sign in to comment.