From e93c0fa155a47f66f88d86374baf980decf47b8a Mon Sep 17 00:00:00 2001 From: Pascal Kuthe Date: Wed, 30 Nov 2022 19:17:00 +0100 Subject: [PATCH] add textobject for change --- book/src/usage.md | 1 + helix-term/src/commands.rs | 22 ++++++++++++++++++++++ helix-vcs/src/diff.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/book/src/usage.md b/book/src/usage.md index 646bf926d536..a6eb9ec1d4f1 100644 --- a/book/src/usage.md +++ b/book/src/usage.md @@ -143,6 +143,7 @@ though, we climb the syntax tree and then take the previous selection. So | `a` | Argument/parameter | | `o` | Comment | | `t` | Test | +| `g` | Change | > NOTE: `f`, `c`, etc need a tree-sitter grammar active for the current document and a special tree-sitter query file to work properly. [Only diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index a1bbad380ba3..fe043ae401a1 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4608,6 +4608,27 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) { ) }; + if ch == 'g' && doc.diff_handle().is_none() { + editor.set_status("Diff is not available in current buffer"); + return; + } + + let textobject_change = |range: Range| -> Range { + let diff_handle = doc.diff_handle().unwrap(); + let hunks = diff_handle.hunks(); + let line = range.cursor_line(text); + let hunk_idx = if let Some(hunk_idx) = hunks.hunk_at(line as u32, false) { + hunk_idx + } else { + return range; + }; + let hunk = hunks.nth_hunk(hunk_idx).after; + + let start = text.line_to_char(hunk.start as usize); + let end = text.line_to_char(hunk.end as usize); + Range::new(start, end).with_direction(range.direction()) + }; + let selection = doc.selection(view.id).clone().transform(|range| { match ch { 'w' => textobject::textobject_word(text, range, objtype, count, false), @@ -4621,6 +4642,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) { 'm' => textobject::textobject_pair_surround_closest( text, range, objtype, count, ), + 'g' => textobject_change(range), // TODO: cancel new ranges if inconsistent surround matches across lines ch if !ch.is_ascii_alphanumeric() => { textobject::textobject_pair_surround(text, range, objtype, ch, count) diff --git a/helix-vcs/src/diff.rs b/helix-vcs/src/diff.rs index e70293fe7c28..ef42d9cff35d 100644 --- a/helix-vcs/src/diff.rs +++ b/helix-vcs/src/diff.rs @@ -246,4 +246,34 @@ impl FileHunks<'_> { Err(pos) | Ok(pos) => Some(pos as u32 - 1), } } + + pub fn hunk_at(&self, line: u32, include_removal: bool) -> Option { + let hunk_range = if self.inverted { + |hunk: &Hunk| hunk.before.clone() + } else { + |hunk: &Hunk| hunk.after.clone() + }; + + let res = self + .hunks + .binary_search_by_key(&line, |hunk| hunk_range(hunk).start); + + match res { + // Search found a hunk that starts exactly at this line, return it + Ok(pos) => Some(pos as u32), + + // No hunk starts exactly at this line, so the search returns + // the position where a hunk starting at this line should be inserted. + // The previous hunk contains this hunk if it exists and doesn't end before this line + Err(0) => None, + Err(pos) => { + let hunk = hunk_range(&self.hunks[pos - 1]); + if hunk.end > line || include_removal && hunk.start == line && hunk.is_empty() { + Some(pos as u32 - 1) + } else { + None + } + } + } + } }