on master help sign in
Add shrink equivalent of extend_to_line_bounds (#2450)
0c05447Daniel S Poulin
book/src/keymap.md+1
@@ -107,6 +107,7 @@
107| `%` | Select entire file | `select_all` |
107| `%` | Select entire file | `select_all` |
108| `x` | Select current line, if already selected, extend to next line | `extend_line` |
108| `x` | Select current line, if already selected, extend to next line | `extend_line` |
109| `X` | Extend selection to line bounds (line-wise selection) | `extend_to_line_bounds` |
109| `X` | Extend selection to line bounds (line-wise selection) | `extend_to_line_bounds` |
110+| `Alt-x` | Shrink selection to line bounds (line-wise selection) | `shrink_to_line_bounds` |
110| `J` | Join lines inside selection | `join_selections` |
111| `J` | Join lines inside selection | `join_selections` |
111| `K` | Keep selections matching the regex | `keep_selections` |
112| `K` | Keep selections matching the regex | `keep_selections` |
112| `Alt-K` | Remove selections matching the regex | `remove_selections` |
113| `Alt-K` | Remove selections matching the regex | `remove_selections` |
helix-term/src/commands.rs+42
@@ -247,6 +247,7 @@ impl MappableCommand {
247 extend_line, "Select current line, if already selected, extend to next line",
247 extend_line, "Select current line, if already selected, extend to next line",
248 extend_line_above, "Select current line, if already selected, extend to previous line",
248 extend_line_above, "Select current line, if already selected, extend to previous line",
249 extend_to_line_bounds, "Extend selection to line bounds (line-wise selection)",
249 extend_to_line_bounds, "Extend selection to line bounds (line-wise selection)",
250+ shrink_to_line_bounds, "Shrink selection to line bounds (line-wise selection)",
250 delete_selection, "Delete selection",
251 delete_selection, "Delete selection",
251 delete_selection_noyank, "Delete selection, without yanking",
252 delete_selection_noyank, "Delete selection, without yanking",
252 change_selection, "Change selection (delete and enter insert mode)",
253 change_selection, "Change selection (delete and enter insert mode)",
@@ -1942,6 +1943,47 @@ fn extend_to_line_bounds(cx: &mut Context) {
1942 );
1943 );
1943}
1944}
1944
1945
1946+fn shrink_to_line_bounds(cx: &mut Context) {
1947+ let (view, doc) = current!(cx.editor);
1948+
1949+ doc.set_selection(
1950+ view.id,
1951+ doc.selection(view.id).clone().transform(|range| {
1952+ let text = doc.text();
1953+
1954+ let (start_line, end_line) = range.line_range(text.slice(..));
1955+
1956+ // Do nothing if the selection is within one line to prevent
1957+ // conditional logic for the behavior of this command
1958+ if start_line == end_line {
1959+ return range;
1960+ }
1961+
1962+ let mut start = text.line_to_char(start_line);
1963+
1964+ // line_to_char gives us the start position of the line, so
1965+ // we need to get the start position of the next line. In
1966+ // the editor, this will correspond to the cursor being on
1967+ // the EOL whitespace charactor, which is what we want.
1968+ let mut end = text.line_to_char((end_line + 1).min(text.len_lines()));
1969+
1970+ if start != range.from() {
1971+ start = text.line_to_char((start_line + 1).min(text.len_lines()));
1972+ }
1973+
1974+ if end != range.to() {
1975+ end = text.line_to_char(end_line);
1976+ }
1977+
1978+ if range.anchor <= range.head {
1979+ Range::new(start, end)
1980+ } else {
1981+ Range::new(end, start)
1982+ }
1983+ }),
1984+ );
1985+}
1986+
1945enum Operation {
1987enum Operation {
1946 Delete,
1988 Delete,
1947 Change,
1989 Change,
helix-term/src/keymap/default.rs+1−1
@@ -87,7 +87,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
87 "%" => select_all,
87 "%" => select_all,
88 "x" => extend_line,
88 "x" => extend_line,
89 "X" => extend_to_line_bounds,
89 "X" => extend_to_line_bounds,
90− // crop_to_whole_line
90+ "A-x" => shrink_to_line_bounds,
91
91
92 "m" => { "Match"
92 "m" => { "Match"
93 "m" => match_brackets,
93 "m" => match_brackets,