Skip to content

Commit 7103808

Browse files
QiBaobinpickfire
authored andcommitted
let extend-line respect range direction (helix-editor#3046)
* let extend-line respect range direction * fix extend above logic * keep `x` existing binding * Update book/src/keymap.md Co-authored-by: Ivan Tham <pickfire@riseup.net> Co-authored-by: Ivan Tham <pickfire@riseup.net>
1 parent 9501d8f commit 7103808

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

book/src/keymap.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
| `Alt-(` | Rotate selection contents backward | `rotate_selection_contents_backward` |
126126
| `Alt-)` | Rotate selection contents forward | `rotate_selection_contents_forward` |
127127
| `%` | Select entire file | `select_all` |
128-
| `x` | Select current line, if already selected, extend to next line | `extend_line` |
128+
| `x` | Select current line, if already selected, extend to next line | `extend_line_below` |
129129
| `X` | Extend selection to line bounds (line-wise selection) | `extend_to_line_bounds` |
130130
| `Alt-x` | Shrink selection to line bounds (line-wise selection) | `shrink_to_line_bounds` |
131131
| `J` | Join lines inside selection | `join_selections` |

helix-term/src/commands.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ impl MappableCommand {
247247
extend_search_prev, "Add previous search match to selection",
248248
search_selection, "Use current selection as search pattern",
249249
global_search, "Global search in workspace folder",
250-
extend_line, "Select current line, if already selected, extend to next line",
250+
extend_line, "Select current line, if already selected, extend to another line based on the anchor",
251+
extend_line_below, "Select current line, if already selected, extend to next line",
251252
extend_line_above, "Select current line, if already selected, extend to previous line",
252253
extend_to_line_bounds, "Extend selection to line bounds",
253254
shrink_to_line_bounds, "Shrink selection to line bounds",
@@ -1945,6 +1946,15 @@ enum Extend {
19451946
}
19461947

19471948
fn extend_line(cx: &mut Context) {
1949+
let (view, doc) = current_ref!(cx.editor);
1950+
let extend = match doc.selection(view.id).primary().direction() {
1951+
Direction::Forward => Extend::Below,
1952+
Direction::Backward => Extend::Above,
1953+
};
1954+
extend_line_impl(cx, extend);
1955+
}
1956+
1957+
fn extend_line_below(cx: &mut Context) {
19481958
extend_line_impl(cx, Extend::Below);
19491959
}
19501960

@@ -1960,20 +1970,32 @@ fn extend_line_impl(cx: &mut Context, extend: Extend) {
19601970
let selection = doc.selection(view.id).clone().transform(|range| {
19611971
let (start_line, end_line) = range.line_range(text.slice(..));
19621972

1963-
let start = text.line_to_char(start_line);
1964-
let end = text.line_to_char((end_line + count).min(text.len_lines()));
1973+
let start = text.line_to_char(match extend {
1974+
Extend::Above => start_line.saturating_sub(count),
1975+
Extend::Below => start_line,
1976+
});
1977+
let end = text.line_to_char(
1978+
match extend {
1979+
Extend::Above => end_line + 1, // the start of next line
1980+
Extend::Below => (end_line + count),
1981+
}
1982+
.min(text.len_lines()),
1983+
);
19651984

19661985
// extend to previous/next line if current line is selected
19671986
let (anchor, head) = if range.from() == start && range.to() == end {
19681987
match extend {
1969-
Extend::Above => (end, text.line_to_char(start_line.saturating_sub(1))),
1988+
Extend::Above => (end, text.line_to_char(start_line.saturating_sub(count + 1))),
19701989
Extend::Below => (
19711990
start,
19721991
text.line_to_char((end_line + count + 1).min(text.len_lines())),
19731992
),
19741993
}
19751994
} else {
1976-
(start, end)
1995+
match extend {
1996+
Extend::Above => (end, start),
1997+
Extend::Below => (start, end),
1998+
}
19771999
};
19782000

19792001
Range::new(anchor, head)

helix-term/src/keymap/default.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
8585
"A-n" | "A-right" => select_next_sibling,
8686

8787
"%" => select_all,
88-
"x" => extend_line,
88+
"x" => extend_line_below,
8989
"X" => extend_to_line_bounds,
9090
"A-x" => shrink_to_line_bounds,
9191

0 commit comments

Comments
 (0)