on master help sign in
let extend-line respect range direction (#3046)
411c5e4Bob
book/src/keymap.md+1−1
@@ -125,7 +125,7 @@
125| `Alt-(` | Rotate selection contents backward | `rotate_selection_contents_backward` |
125| `Alt-(` | Rotate selection contents backward | `rotate_selection_contents_backward` |
126| `Alt-)` | Rotate selection contents forward | `rotate_selection_contents_forward` |
126| `Alt-)` | Rotate selection contents forward | `rotate_selection_contents_forward` |
127| `%` | Select entire file | `select_all` |
127| `%` | 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` |
129| `X` | Extend selection to line bounds (line-wise selection) | `extend_to_line_bounds` |
129| `X` | Extend selection to line bounds (line-wise selection) | `extend_to_line_bounds` |
130| `Alt-x` | Shrink selection to line bounds (line-wise selection) | `shrink_to_line_bounds` |
130| `Alt-x` | Shrink selection to line bounds (line-wise selection) | `shrink_to_line_bounds` |
131| `J` | Join lines inside selection | `join_selections` |
131| `J` | Join lines inside selection | `join_selections` |
helix-term/src/commands.rs+27−5
@@ -247,7 +247,8 @@ impl MappableCommand {
247 extend_search_prev, "Add previous search match to selection",
247 extend_search_prev, "Add previous search match to selection",
248 search_selection, "Use current selection as search pattern",
248 search_selection, "Use current selection as search pattern",
249 global_search, "Global search in workspace folder",
249 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",
251 extend_line_above, "Select current line, if already selected, extend to previous line",
252 extend_line_above, "Select current line, if already selected, extend to previous line",
252 extend_to_line_bounds, "Extend selection to line bounds",
253 extend_to_line_bounds, "Extend selection to line bounds",
253 shrink_to_line_bounds, "Shrink selection to line bounds",
254 shrink_to_line_bounds, "Shrink selection to line bounds",
@@ -1945,6 +1946,15 @@ enum Extend {
1945}
1946}
1946
1947
1947fn extend_line(cx: &mut Context) {
1948fn 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) {
1948 extend_line_impl(cx, Extend::Below);
1958 extend_line_impl(cx, Extend::Below);
1949}
1959}
1950
1960
@@ -1960,20 +1970,32 @@ fn extend_line_impl(cx: &mut Context, extend: Extend) {
1960 let selection = doc.selection(view.id).clone().transform(|range| {
1970 let selection = doc.selection(view.id).clone().transform(|range| {
1961 let (start_line, end_line) = range.line_range(text.slice(..));
1971 let (start_line, end_line) = range.line_range(text.slice(..));
1962
1972
1963− let start = text.line_to_char(start_line);
1973+ let start = text.line_to_char(match extend {
1974+ Extend::Above => start_line.saturating_sub(count),
1975+ Extend::Below => start_line,
1976+ });
1964− let end = text.line_to_char((end_line + count).min(text.len_lines()));
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+ );
1965
1984
1966 // extend to previous/next line if current line is selected
1985 // extend to previous/next line if current line is selected
1967 let (anchor, head) = if range.from() == start && range.to() == end {
1986 let (anchor, head) = if range.from() == start && range.to() == end {
1968 match extend {
1987 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))),
1970 Extend::Below => (
1989 Extend::Below => (
1971 start,
1990 start,
1972 text.line_to_char((end_line + count + 1).min(text.len_lines())),
1991 text.line_to_char((end_line + count + 1).min(text.len_lines())),
1973 ),
1992 ),
1974 }
1993 }
1975 } else {
1994 } else {
1995+ match extend {
1996+ Extend::Above => (end, start),
1976− (start, end)
1997+ Extend::Below => (start, end),
1998+ }
1977 };
1999 };
1978
2000
1979 Range::new(anchor, head)
2001 Range::new(anchor, head)
helix-term/src/keymap/default.rs+1−1
@@ -85,7 +85,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
85 "A-n" | "A-right" => select_next_sibling,
85 "A-n" | "A-right" => select_next_sibling,
86
86
87 "%" => select_all,
87 "%" => select_all,
88− "x" => extend_line,
88+ "x" => extend_line_below,
89 "X" => extend_to_line_bounds,
89 "X" => extend_to_line_bounds,
90 "A-x" => shrink_to_line_bounds,
90 "A-x" => shrink_to_line_bounds,
91
91