aboutsummaryrefslogtreecommitdiff
path: root/helix-term
diff options
context:
space:
mode:
authorath32021-11-14 15:16:20 +0000
committerGitHub2021-11-14 15:16:20 +0000
commit6fa76d9fe77d43ebc18cc78a6a1c1957d72cf59b (patch)
tree8998e33a6b7b19e87c79dae0b8c1edc824ab4e3e /helix-term
parentedc976b6bb36c6017bf59691abbde5c086267bfd (diff)
Add trim_selections command (#1092)
Diffstat (limited to 'helix-term')
-rw-r--r--helix-term/src/commands.rs37
-rw-r--r--helix-term/src/keymap.rs2
2 files changed, 38 insertions, 1 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index ebacb377..56cc02a2 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -272,6 +272,7 @@ impl Command {
// TODO: different description ?
goto_line_end_newline, "Goto line end",
goto_first_nonwhitespace, "Goto first non-blank in line",
+ trim_selections, "Trim whitespace from selections",
extend_to_line_start, "Extend to line start",
extend_to_line_end, "Extend to line end",
extend_to_line_end_newline, "Extend to line end",
@@ -584,6 +585,42 @@ fn goto_first_nonwhitespace(cx: &mut Context) {
doc.set_selection(view.id, selection);
}
+fn trim_selections(cx: &mut Context) {
+ let (view, doc) = current!(cx.editor);
+ let text = doc.text().slice(..);
+
+ let ranges: SmallVec<[Range; 1]> = doc
+ .selection(view.id)
+ .iter()
+ .filter_map(|range| {
+ if range.is_empty() || range.fragment(text).chars().all(|ch| ch.is_whitespace()) {
+ return None;
+ }
+ let mut start = range.from();
+ let mut end = range.to();
+ start = movement::skip_while(text, start, |x| x.is_whitespace()).unwrap_or(start);
+ end = movement::backwards_skip_while(text, end, |x| x.is_whitespace()).unwrap_or(end);
+ if range.anchor < range.head {
+ Some(Range::new(start, end))
+ } else {
+ Some(Range::new(end, start))
+ }
+ })
+ .collect();
+
+ if !ranges.is_empty() {
+ let primary = doc.selection(view.id).primary();
+ let idx = ranges
+ .iter()
+ .position(|range| range.overlaps(&primary))
+ .unwrap_or(ranges.len() - 1);
+ doc.set_selection(view.id, Selection::new(ranges, idx));
+ } else {
+ collapse_selection(cx);
+ keep_primary_selection(cx);
+ };
+}
+
fn goto_window(cx: &mut Context, align: Align) {
let (view, doc) = current!(cx.editor);
diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs
index 3280f0f8..fc9fd590 100644
--- a/helix-term/src/keymap.rs
+++ b/helix-term/src/keymap.rs
@@ -603,7 +603,7 @@ impl Default for Keymaps {
// "Q" => replay_macro,
// & align selections
- // _ trim selections
+ "_" => trim_selections,
"(" => rotate_selections_backward,
")" => rotate_selections_forward,