aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel S Poulin2022-05-22 01:33:11 +0000
committerGitHub2022-05-22 01:33:11 +0000
commit0c05447d49103e96fc21910d336bdc75ab96338d (patch)
tree2763895357e870ed766c039617ef6830a9e09b44
parent5bcb31a6dfb007b31b6ba38c5a1455727904ba27 (diff)
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
-rw-r--r--book/src/keymap.md1
-rw-r--r--helix-term/src/commands.rs42
-rw-r--r--helix-term/src/keymap/default.rs2
3 files changed, 44 insertions, 1 deletions
diff --git a/book/src/keymap.md b/book/src/keymap.md
index e56eeefc..25280d71 100644
--- a/book/src/keymap.md
+++ b/book/src/keymap.md
@@ -107,6 +107,7 @@
| `%` | Select entire file | `select_all` |
| `x` | Select current line, if already selected, extend to next line | `extend_line` |
| `X` | Extend selection to line bounds (line-wise selection) | `extend_to_line_bounds` |
+| `Alt-x` | Shrink selection to line bounds (line-wise selection) | `shrink_to_line_bounds` |
| `J` | Join lines inside selection | `join_selections` |
| `K` | Keep selections matching the regex | `keep_selections` |
| `Alt-K` | Remove selections matching the regex | `remove_selections` |
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 2839f495..8024e3f0 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -247,6 +247,7 @@ impl MappableCommand {
extend_line, "Select current line, if already selected, extend to next line",
extend_line_above, "Select current line, if already selected, extend to previous line",
extend_to_line_bounds, "Extend selection to line bounds (line-wise selection)",
+ shrink_to_line_bounds, "Shrink selection to line bounds (line-wise selection)",
delete_selection, "Delete selection",
delete_selection_noyank, "Delete selection, without yanking",
change_selection, "Change selection (delete and enter insert mode)",
@@ -1942,6 +1943,47 @@ fn extend_to_line_bounds(cx: &mut Context) {
);
}
+fn shrink_to_line_bounds(cx: &mut Context) {
+ let (view, doc) = current!(cx.editor);
+
+ doc.set_selection(
+ view.id,
+ doc.selection(view.id).clone().transform(|range| {
+ let text = doc.text();
+
+ let (start_line, end_line) = range.line_range(text.slice(..));
+
+ // Do nothing if the selection is within one line to prevent
+ // conditional logic for the behavior of this command
+ if start_line == end_line {
+ return range;
+ }
+
+ let mut start = text.line_to_char(start_line);
+
+ // line_to_char gives us the start position of the line, so
+ // we need to get the start position of the next line. In
+ // the editor, this will correspond to the cursor being on
+ // the EOL whitespace charactor, which is what we want.
+ let mut end = text.line_to_char((end_line + 1).min(text.len_lines()));
+
+ if start != range.from() {
+ start = text.line_to_char((start_line + 1).min(text.len_lines()));
+ }
+
+ if end != range.to() {
+ end = text.line_to_char(end_line);
+ }
+
+ if range.anchor <= range.head {
+ Range::new(start, end)
+ } else {
+ Range::new(end, start)
+ }
+ }),
+ );
+}
+
enum Operation {
Delete,
Change,
diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs
index a887f4b3..0f0b09dd 100644
--- a/helix-term/src/keymap/default.rs
+++ b/helix-term/src/keymap/default.rs
@@ -87,7 +87,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
"%" => select_all,
"x" => extend_line,
"X" => extend_to_line_bounds,
- // crop_to_whole_line
+ "A-x" => shrink_to_line_bounds,
"m" => { "Match"
"m" => match_brackets,