diff options
author | Wojciech Kępka | 2021-06-08 06:25:55 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-06-08 08:22:37 +0000 |
commit | 48df05b16dba362366b63284c2e551c51995fb66 (patch) | |
tree | 9f8d6442c74c50c46ac7ad1979fea8a3fa0bb731 /helix-term | |
parent | b873fb9897bb5b24a60cca3d9fa69285446a857f (diff) |
commands: Add goto first non-whitespace char of line
Diffstat (limited to 'helix-term')
-rw-r--r-- | helix-term/src/commands.rs | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 2412e55d..fc1b363c 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1,5 +1,6 @@ use helix_core::{ - comment, coords_at_pos, find_root, graphemes, indent, match_brackets, + comment, coords_at_pos, find_first_non_whitespace_char2, find_root, graphemes, indent, + match_brackets, movement::{self, Direction}, object, pos_at_coords, regex::{self, Regex}, @@ -216,6 +217,24 @@ pub fn move_line_start(cx: &mut Context) { doc.set_selection(view.id, selection); } +pub fn move_first_nonwhitespace(cx: &mut Context) { + let (view, doc) = cx.current(); + + let selection = doc.selection(view.id).transform(|range| { + let text = doc.text(); + let line_idx = text.char_to_line(range.head); + + if let Some(pos) = find_first_non_whitespace_char2(text.line(line_idx)) { + let pos = pos + text.line_to_char(line_idx); + Range::new(pos, pos) + } else { + range + } + }); + + doc.set_selection(view.id, selection); +} + // TODO: move vs extend could take an extra type Extend/Move that would // Range::new(if Move { pos } if Extend { range.anchor }, pos) // since these all really do the same thing @@ -421,6 +440,24 @@ pub fn extend_prev_char(cx: &mut Context) { ) } +pub fn extend_first_nonwhitespace(cx: &mut Context) { + let (view, doc) = cx.current(); + + let selection = doc.selection(view.id).transform(|range| { + let text = doc.text(); + let line_idx = text.char_to_line(range.head); + + if let Some(pos) = find_first_non_whitespace_char2(text.line(line_idx)) { + let pos = pos + text.line_to_char(line_idx); + Range::new(range.anchor, pos) + } else { + range + } + }); + + doc.set_selection(view.id, selection); +} + pub fn replace(cx: &mut Context) { // need to wait for next key cx.on_next_key(move |cx, event| { @@ -1288,6 +1325,8 @@ pub fn goto_mode(cx: &mut Context) { (_, 'y') => goto_type_definition(cx), (_, 'r') => goto_reference(cx), (_, 'i') => goto_implementation(cx), + (Mode::Normal, 's') => move_first_nonwhitespace(cx), + (Mode::Select, 's') => extend_first_nonwhitespace(cx), (_, 't') | (_, 'm') | (_, 'b') => { let (view, doc) = cx.current(); |