on master help sign in
Add shrink equivalent of extend_to_line_bounds (#2450)

* Add shrink equivalent of extend_to_line_bounds * Add a check for being past rope end in end position calc * Include the EOL character in calculations * Bind to `A-x` for now * Document new keybind

commit0c05447d49103e96fc21910d336bdc75ab96338d authorDaniel S Poulin <dan.poulin@digitalsurgeons.com> dateSat 21 May 2022, 21:33 (4 years ago) parent5bcb31a
3 files changed+44−1
book/src/keymap.md+1 helix-term/src/commands.rs+42 helix-term/src/keymap/default.rs+1−1
book/src/keymap.md+1
@@ -107,6 +107,7 @@
107107| `%` | Select entire file | `select_all` |
108108| `x` | Select current line, if already selected, extend to next line | `extend_line` |
109109| `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` |
110111| `J` | Join lines inside selection | `join_selections` |
111112| `K` | Keep selections matching the regex | `keep_selections` |
112113| `Alt-K` | Remove selections matching the regex | `remove_selections` |
helix-term/src/commands.rs+42
@@ -247,6 +247,7 @@ impl MappableCommand {
247247 extend_line, "Select current line, if already selected, extend to next line",
248248 extend_line_above, "Select current line, if already selected, extend to previous line",
249249 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)",
250251 delete_selection, "Delete selection",
251252 delete_selection_noyank, "Delete selection, without yanking",
252253 change_selection, "Change selection (delete and enter insert mode)",
@@ -1942,6 +1943,47 @@ fn extend_to_line_bounds(cx: &mut Context) {
19421943 );
19431944}
19441945
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+
19451987enum Operation {
19461988 Delete,
19471989 Change,
helix-term/src/keymap/default.rs+1−1
@@ -87,7 +87,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
8787 "%" => select_all,
8888 "x" => extend_line,
8989 "X" => extend_to_line_bounds,
90− // crop_to_whole_line
90+ "A-x" => shrink_to_line_bounds,
9191
9292 "m" => { "Match"
9393 "m" => match_brackets,