Skip to content

Commit 29789f2

Browse files
authored
Add support for extend_file_{start,end} (#11767)
1 parent efdcf34 commit 29789f2

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

book/src/generated/static-cmd.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@
126126
| `add_newline_below` | Add newline below | normal: `` ]<space> ``, select: `` ]<space> `` |
127127
| `goto_type_definition` | Goto type definition | normal: `` gy ``, select: `` gy `` |
128128
| `goto_implementation` | Goto implementation | normal: `` gi ``, select: `` gi `` |
129-
| `goto_file_start` | Goto line number <n> else file start | normal: `` gg ``, select: `` gg `` |
129+
| `goto_file_start` | Goto line number <n> else file start | normal: `` gg `` |
130130
| `goto_file_end` | Goto file end | |
131+
| `extend_to_file_start` | Extend to line number<n> else file start | select: `` gg `` |
132+
| `extend_to_file_end` | Extend to file end | |
131133
| `goto_file` | Goto files/URLs in selections | normal: `` gf ``, select: `` gf `` |
132134
| `goto_file_hsplit` | Goto files in selections (hsplit) | normal: `` <C-w>f ``, `` <space>wf ``, select: `` <C-w>f ``, `` <space>wf `` |
133135
| `goto_file_vsplit` | Goto files in selections (vsplit) | normal: `` <C-w>F ``, `` <space>wF ``, select: `` <C-w>F ``, `` <space>wF `` |
@@ -139,7 +141,8 @@
139141
| `goto_last_modified_file` | Goto last modified file | normal: `` gm ``, select: `` gm `` |
140142
| `goto_last_modification` | Goto last modification | normal: `` g. ``, select: `` g. `` |
141143
| `goto_line` | Goto line | normal: `` G ``, select: `` G `` |
142-
| `goto_last_line` | Goto last line | normal: `` ge ``, select: `` ge `` |
144+
| `goto_last_line` | Goto last line | normal: `` ge `` |
145+
| `extend_to_last_line` | Extend to last line | select: `` ge `` |
143146
| `goto_first_diag` | Goto first diagnostic | normal: `` [D ``, select: `` [D `` |
144147
| `goto_last_diag` | Goto last diagnostic | normal: `` ]D ``, select: `` ]D `` |
145148
| `goto_next_diag` | Goto next diagnostic | normal: `` ]d ``, select: `` ]d `` |

helix-term/src/commands.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ impl MappableCommand {
426426
goto_implementation, "Goto implementation",
427427
goto_file_start, "Goto line number <n> else file start",
428428
goto_file_end, "Goto file end",
429+
extend_to_file_start, "Extend to line number<n> else file start",
430+
extend_to_file_end, "Extend to file end",
429431
goto_file, "Goto files/URLs in selections",
430432
goto_file_hsplit, "Goto files in selections (hsplit)",
431433
goto_file_vsplit, "Goto files in selections (vsplit)",
@@ -438,6 +440,7 @@ impl MappableCommand {
438440
goto_last_modification, "Goto last modification",
439441
goto_line, "Goto line",
440442
goto_last_line, "Goto last line",
443+
extend_to_last_line, "Extend to last line",
441444
goto_first_diag, "Goto first diagnostic",
442445
goto_last_diag, "Goto last diagnostic",
443446
goto_next_diag, "Goto next diagnostic",
@@ -1253,28 +1256,44 @@ fn goto_next_paragraph(cx: &mut Context) {
12531256
}
12541257

12551258
fn goto_file_start(cx: &mut Context) {
1259+
goto_file_start_impl(cx, Movement::Move);
1260+
}
1261+
1262+
fn extend_to_file_start(cx: &mut Context) {
1263+
goto_file_start_impl(cx, Movement::Extend);
1264+
}
1265+
1266+
fn goto_file_start_impl(cx: &mut Context, movement: Movement) {
12561267
if cx.count.is_some() {
1257-
goto_line(cx);
1268+
goto_line_impl(cx, movement);
12581269
} else {
12591270
let (view, doc) = current!(cx.editor);
12601271
let text = doc.text().slice(..);
12611272
let selection = doc
12621273
.selection(view.id)
12631274
.clone()
1264-
.transform(|range| range.put_cursor(text, 0, cx.editor.mode == Mode::Select));
1275+
.transform(|range| range.put_cursor(text, 0, movement == Movement::Extend));
12651276
push_jump(view, doc);
12661277
doc.set_selection(view.id, selection);
12671278
}
12681279
}
12691280

12701281
fn goto_file_end(cx: &mut Context) {
1282+
goto_file_end_impl(cx, Movement::Move);
1283+
}
1284+
1285+
fn extend_to_file_end(cx: &mut Context) {
1286+
goto_file_end_impl(cx, Movement::Extend)
1287+
}
1288+
1289+
fn goto_file_end_impl(cx: &mut Context, movement: Movement) {
12711290
let (view, doc) = current!(cx.editor);
12721291
let text = doc.text().slice(..);
12731292
let pos = doc.text().len_chars();
12741293
let selection = doc
12751294
.selection(view.id)
12761295
.clone()
1277-
.transform(|range| range.put_cursor(text, pos, cx.editor.mode == Mode::Select));
1296+
.transform(|range| range.put_cursor(text, pos, movement == Movement::Extend));
12781297
push_jump(view, doc);
12791298
doc.set_selection(view.id, selection);
12801299
}
@@ -3746,15 +3765,23 @@ fn push_jump(view: &mut View, doc: &Document) {
37463765
}
37473766

37483767
fn goto_line(cx: &mut Context) {
3768+
goto_line_impl(cx, Movement::Move);
3769+
}
3770+
3771+
fn goto_line_impl(cx: &mut Context, movement: Movement) {
37493772
if cx.count.is_some() {
37503773
let (view, doc) = current!(cx.editor);
37513774
push_jump(view, doc);
37523775

3753-
goto_line_without_jumplist(cx.editor, cx.count);
3776+
goto_line_without_jumplist(cx.editor, cx.count, movement);
37543777
}
37553778
}
37563779

3757-
fn goto_line_without_jumplist(editor: &mut Editor, count: Option<NonZeroUsize>) {
3780+
fn goto_line_without_jumplist(
3781+
editor: &mut Editor,
3782+
count: Option<NonZeroUsize>,
3783+
movement: Movement,
3784+
) {
37583785
if let Some(count) = count {
37593786
let (view, doc) = current!(editor);
37603787
let text = doc.text().slice(..);
@@ -3769,13 +3796,21 @@ fn goto_line_without_jumplist(editor: &mut Editor, count: Option<NonZeroUsize>)
37693796
let selection = doc
37703797
.selection(view.id)
37713798
.clone()
3772-
.transform(|range| range.put_cursor(text, pos, editor.mode == Mode::Select));
3799+
.transform(|range| range.put_cursor(text, pos, movement == Movement::Extend));
37733800

37743801
doc.set_selection(view.id, selection);
37753802
}
37763803
}
37773804

37783805
fn goto_last_line(cx: &mut Context) {
3806+
goto_last_line_impl(cx, Movement::Move)
3807+
}
3808+
3809+
fn extend_to_last_line(cx: &mut Context) {
3810+
goto_last_line_impl(cx, Movement::Extend)
3811+
}
3812+
3813+
fn goto_last_line_impl(cx: &mut Context, movement: Movement) {
37793814
let (view, doc) = current!(cx.editor);
37803815
let text = doc.text().slice(..);
37813816
let line_idx = if text.line(text.len_lines() - 1).len_chars() == 0 {
@@ -3788,7 +3823,7 @@ fn goto_last_line(cx: &mut Context) {
37883823
let selection = doc
37893824
.selection(view.id)
37903825
.clone()
3791-
.transform(|range| range.put_cursor(text, pos, cx.editor.mode == Mode::Select));
3826+
.transform(|range| range.put_cursor(text, pos, movement == Movement::Extend));
37923827

37933828
push_jump(view, doc);
37943829
doc.set_selection(view.id, selection);

helix-term/src/commands/typed.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,15 @@ fn update_goto_line_number_preview(cx: &mut compositor::Context, args: Args) ->
18801880

18811881
let scrolloff = cx.editor.config().scrolloff;
18821882
let line = args[0].parse::<usize>()?;
1883-
goto_line_without_jumplist(cx.editor, NonZeroUsize::new(line));
1883+
goto_line_without_jumplist(
1884+
cx.editor,
1885+
NonZeroUsize::new(line),
1886+
if cx.editor.mode == Mode::Select {
1887+
Movement::Extend
1888+
} else {
1889+
Movement::Move
1890+
},
1891+
);
18841892

18851893
let (view, doc) = current!(cx.editor);
18861894
view.ensure_cursor_in_view(doc, scrolloff);

helix-term/src/keymap/default.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
367367

368368
"v" => normal_mode,
369369
"g" => { "Goto"
370+
"g" => extend_to_file_start,
371+
"e" => extend_to_last_line,
370372
"k" => extend_line_up,
371373
"j" => extend_line_down,
372374
"w" => extend_to_word,

0 commit comments

Comments
 (0)