Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for extend_file_{start,end}. #11767

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions book/src/generated/static-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@
| `add_newline_below` | Add newline below | normal: `` ]<space> ``, select: `` ]<space> `` |
| `goto_type_definition` | Goto type definition | normal: `` gy ``, select: `` gy `` |
| `goto_implementation` | Goto implementation | normal: `` gi ``, select: `` gi `` |
| `goto_file_start` | Goto line number <n> else file start | normal: `` gg ``, select: `` gg `` |
| `goto_file_start` | Goto line number <n> else file start | normal: `` gg `` |
| `goto_file_end` | Goto file end | |
| `extend_to_file_start` | Extend to line number<n> else file start | select: `` gg `` |
| `extend_to_file_end` | Extend to file end | |
| `goto_file` | Goto files/URLs in selections | normal: `` gf ``, select: `` gf `` |
| `goto_file_hsplit` | Goto files in selections (hsplit) | normal: `` <C-w>f ``, `` <space>wf ``, select: `` <C-w>f ``, `` <space>wf `` |
| `goto_file_vsplit` | Goto files in selections (vsplit) | normal: `` <C-w>F ``, `` <space>wF ``, select: `` <C-w>F ``, `` <space>wF `` |
Expand All @@ -139,7 +141,8 @@
| `goto_last_modified_file` | Goto last modified file | normal: `` gm ``, select: `` gm `` |
| `goto_last_modification` | Goto last modification | normal: `` g. ``, select: `` g. `` |
| `goto_line` | Goto line | normal: `` G ``, select: `` G `` |
| `goto_last_line` | Goto last line | normal: `` ge ``, select: `` ge `` |
| `goto_last_line` | Goto last line | normal: `` ge `` |
| `extend_to_last_line` | Extend to last line | select: `` ge `` |
| `goto_first_diag` | Goto first diagnostic | normal: `` [D ``, select: `` [D `` |
| `goto_last_diag` | Goto last diagnostic | normal: `` ]D ``, select: `` ]D `` |
| `goto_next_diag` | Goto next diagnostic | normal: `` ]d ``, select: `` ]d `` |
Expand Down
49 changes: 42 additions & 7 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ impl MappableCommand {
goto_implementation, "Goto implementation",
goto_file_start, "Goto line number <n> else file start",
goto_file_end, "Goto file end",
extend_to_file_start, "Extend to line number<n> else file start",
extend_to_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 All @@ -439,6 +441,7 @@ impl MappableCommand {
goto_last_modification, "Goto last modification",
goto_line, "Goto line",
goto_last_line, "Goto last line",
extend_to_last_line, "Extend to last line",
goto_first_diag, "Goto first diagnostic",
goto_last_diag, "Goto last diagnostic",
goto_next_diag, "Goto next diagnostic",
Expand Down Expand Up @@ -1254,28 +1257,44 @@ fn goto_next_paragraph(cx: &mut Context) {
}

fn goto_file_start(cx: &mut Context) {
goto_file_start_impl(cx, Movement::Move);
}

fn extend_to_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, Movement::Move);
}

fn extend_to_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 @@ -3747,15 +3766,23 @@ fn push_jump(view: &mut View, doc: &Document) {
}

fn goto_line(cx: &mut Context) {
goto_line_impl(cx, 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 @@ -3770,13 +3797,21 @@ 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);
}
}

fn goto_last_line(cx: &mut Context) {
goto_last_line_impl(cx, Movement::Move)
}

fn extend_to_last_line(cx: &mut Context) {
goto_last_line_impl(cx, Movement::Extend)
}

fn goto_last_line_impl(cx: &mut Context, movement: Movement) {
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let line_idx = if text.line(text.len_lines() - 1).len_chars() == 0 {
Expand All @@ -3789,7 +3824,7 @@ fn goto_last_line(cx: &mut Context) {
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
10 changes: 9 additions & 1 deletion helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1819,7 +1819,15 @@ fn update_goto_line_number_preview(cx: &mut compositor::Context, args: Args) ->

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
},
Comment on lines +1825 to +1829
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what to do there since I made goto_line_without_jumplist take the Movement.

);

let (view, doc) = current!(cx.editor);
view.ensure_cursor_in_view(doc, scrolloff);
Expand Down
2 changes: 2 additions & 0 deletions helix-term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ pub fn default() -> HashMap<Mode, KeyTrie> {

"v" => normal_mode,
"g" => { "Goto"
"g" => extend_to_file_start,
"e" => extend_to_last_line,
"k" => extend_line_up,
"j" => extend_line_down,
"w" => extend_to_word,
Expand Down