From c68fe1f2a3a40c37969c1f5d18e3134320a0c773 Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Sat, 3 Jul 2021 06:37:49 +0530 Subject: Add object selection (textobjects) (#385) * Add textobjects for word * Add textobjects for surround characters * Apply clippy lints * Remove ThisWordPrevBound in favor of PrevWordEnd It's the same as PrevWordEnd except for taking the current char into account, so use a "flag" to capture that usecase * Add tests for PrevWordEnd movement * Remove ThisWord* movements They did not preserve anchor positions and were only used for textobject boundary search anyway so replace them with simple position finding functions * Rewrite tests of word textobject * Add tests for surround textobject * Add textobject docs * Refactor textobject word position functions * Apply clippy lints on textobject * Fix overflow error with textobjects--- book/src/keymap.md | 5 ++++- book/src/usage.md | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) (limited to 'book/src') diff --git a/book/src/keymap.md b/book/src/keymap.md index 0265fe6d..6b7ccd11 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -150,7 +150,8 @@ Jumps to various locations. ## Match mode Enter this mode using `m` from normal mode. See the relavant section -in [Usage](./usage.md#surround) for an explanation about surround usage. +in [Usage](./usage.md) for an explanation about [surround](./usage.md#surround) +and [textobject](./usage.md#textobject) usage. | Key | Description | | ----- | ----------- | @@ -158,6 +159,8 @@ in [Usage](./usage.md#surround) for an explanation about surround usage. | `s` `` | Surround current selection with `` | | `r` `` | Replace surround character `` with `` | | `d` `` | Delete surround character `` | +| `a` `` | Select around textobject | +| `i` `` | Select inside textobject | ## Object mode diff --git a/book/src/usage.md b/book/src/usage.md index e6bd60e2..0458071a 100644 --- a/book/src/usage.md +++ b/book/src/usage.md @@ -24,3 +24,19 @@ It can also act on multiple seletions (yay!). For example, to change every occur - `mr([` to replace the parens with square brackets Multiple characters are currently not supported, but planned. + +## Textobjects + +Currently supported: `word`, `surround`. + +![textobject-demo](https://user-images.githubusercontent.com/23398472/124231131-81a4bb00-db2d-11eb-9d10-8e577ca7b177.gif) + +- `ma` - Select around the object (`va` in vim, `` in kakoune) +- `mi` - Select inside the object (`vi` in vim, `` in kakoune) + +| Key after `mi` or `ma` | Textobject selected | +| --- | --- | +| `w` | Word | +| `(`, `[`, `'`, etc | Specified surround pairs | + +Textobjects based on treesitter, like `function`, `class`, etc are planned. -- cgit v1.2.3-70-g09d2 From 37f0b9ee159c13a8bc225b28219dfc485f4393a2 Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Sat, 3 Jul 2021 10:35:09 +0530 Subject: Add missing linenr.selected key to docs --- book/src/themes.md | 1 + 1 file changed, 1 insertion(+) (limited to 'book/src') diff --git a/book/src/themes.md b/book/src/themes.md index d6ed78ba..e5c461fd 100644 --- a/book/src/themes.md +++ b/book/src/themes.md @@ -81,6 +81,7 @@ Possible keys: | `ui.cursor.match` | Matching bracket etc. | | `ui.cursor.primary` | Cursor with primary selection | | `ui.linenr` | | +| `ui.linenr.selected` | | | `ui.statusline` | | | `ui.statusline.inactive` | | | `ui.popup` | | -- cgit v1.2.3-70-g09d2 From a4e28c6927e59cf08d056e9c9a74be095eae5f29 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Mon, 5 Jul 2021 10:12:34 +0900 Subject: Implement `X` as extend selection to line bounds --- book/src/keymap.md | 1 + helix-term/src/commands.rs | 21 +++++++++++++++++++++ helix-term/src/keymap.rs | 4 +++- 3 files changed, 25 insertions(+), 1 deletion(-) (limited to 'book/src') diff --git a/book/src/keymap.md b/book/src/keymap.md index 6b7ccd11..c0c455d3 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -73,6 +73,7 @@ | `Alt-;` | Flip selection cursor and anchor | | `%` | Select entire file | | `x` | Select current line, if already selected, extend to next line | +| `X` | Extend selection to line bounds (line-wise selection) | | | Expand selection to parent syntax node TODO: pick a key | | `J` | join lines inside selection | | `K` | keep selections matching the regex TODO: overlapped by hover help | diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index d8892c9c..63b91942 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -200,6 +200,7 @@ impl Command { extend_search_next, search_selection, extend_line, + extend_to_line_bounds, delete_selection, change_selection, collapse_selection, @@ -1021,6 +1022,26 @@ fn extend_line(cx: &mut Context) { doc.set_selection(view.id, Selection::single(start, end)); } +fn extend_to_line_bounds(cx: &mut Context) { + let (view, doc) = current!(cx.editor); + + let text = doc.text(); + let selection = doc.selection(view.id).transform(|range| { + let start = text.line_to_char(text.char_to_line(range.from())); + let end = text + .line_to_char(text.char_to_line(range.to()) + 1) + .saturating_sub(1); + + if range.anchor < range.head { + Range::new(start, end) + } else { + Range::new(end, start) + } + }); + + doc.set_selection(view.id, selection); +} + fn delete_selection_impl(reg: &mut Register, doc: &mut Document, view_id: ViewId) { // first yank the selection let values: Vec = doc diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index 6c7a24b1..c340eb2c 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -209,7 +209,9 @@ impl Default for Keymaps { alt!(';') => Command::flip_selections, key!('%') => Command::select_all, key!('x') => Command::extend_line, - // extend_to_whole_line, crop_to_whole_line + key!('x') => Command::extend_line, + key!('X') => Command::extend_to_line_bounds, + // crop_to_whole_line key!('m') => Command::match_mode, -- cgit v1.2.3-70-g09d2