From 0e5c160a475ccb7ec2bf3b257c31a84fa255274c Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Sat, 11 Jan 2025 15:33:45 +0000 Subject: [PATCH] fix: add back unnecesserily removed content --- helix-term/src/commands.rs | 69 ++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index e9fbebd69..06d892adc 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -5627,16 +5627,73 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) { let (view, doc) = current!(editor); let text = doc.text().slice(..); - let selection = doc.selection(view.id).clone().transform(|range| match ch { - ch if !ch.is_ascii_alphanumeric() => textobject::textobject_pair_surround( - doc.syntax(), + let textobject_treesitter = |obj_name: &str, range: Range| -> Range { + let (lang_config, syntax) = match doc.language_config().zip(doc.syntax()) { + Some(t) => t, + None => return range, + }; + textobject::textobject_treesitter( text, range, objtype, - ch, + obj_name, + syntax.tree().root_node(), + lang_config, count, - ), - _ => range, + ) + }; + + 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 diff = diff_handle.load(); + let line = range.cursor_line(text); + let hunk_idx = if let Some(hunk_idx) = diff.hunk_at(line as u32, false) { + hunk_idx + } else { + return range; + }; + let hunk = diff.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), + 'W' => textobject::textobject_word(text, range, objtype, count, true), + 't' => textobject_treesitter("class", range), + 'f' => textobject_treesitter("function", range), + 'a' => textobject_treesitter("parameter", range), + 'c' => textobject_treesitter("comment", range), + 'T' => textobject_treesitter("test", range), + 'e' => textobject_treesitter("entry", range), + 'p' => textobject::textobject_paragraph(text, range, objtype, count), + 'm' => textobject::textobject_pair_surround_closest( + doc.syntax(), + 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( + doc.syntax(), + text, + range, + objtype, + ch, + count, + ), + _ => range, + } }); doc.set_selection(view.id, selection); };