Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Helix Support (WIP) #19175

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
16 changes: 16 additions & 0 deletions assets/keymaps/vim.json
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,22 @@
"ctrl-r": ["vim::PushOperator", "Register"]
}
},
{
"context": "vim_mode == helix_normal",
"bindings": {
"i": "vim::InsertBefore",
"a": "vim::InsertAfter",
"w": "vim::NextWordStart",
"e": "vim::NextWordEnd",
"b": "vim::PreviousWordStart",

"h": "vim::Left",
"j": "vim::Down",
"k": "vim::Up",
"l": "vim::Right"
}
},

{
"context": "vim_mode == insert && !(showing_code_actions || showing_completions)",
"bindings": {
Expand Down
93 changes: 93 additions & 0 deletions crates/editor/src/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,99 @@ pub fn find_boundary_point(
map.clip_point(offset.to_display_point(map), Bias::Right)
}


pub fn find_preceding_boundary_trail(
map: &DisplaySnapshot,
head: DisplayPoint,
trail: DisplayPoint,
mut is_boundary: impl FnMut(char, char) -> bool,
) -> (DisplayPoint, DisplayPoint) {
let mut offset = head.to_offset(map, Bias::Left);
let mut trail_offset = None;

let mut prev_ch = map.buffer_snapshot.chars_at(offset).next();
let mut forward = map.buffer_snapshot.reversed_chars_at(offset).peekable();

// Skip newlines
while let Some(&ch) = forward.peek() {
if ch == '\n' {
prev_ch = forward.next();
offset -= ch.len_utf8();
trail_offset = Some(offset);
} else {
break;
}
}

// Find the boundary
let start_offset = offset;
for ch in forward {
if let Some(prev_ch) = prev_ch {
if is_boundary(prev_ch, ch) {
if start_offset == offset {
trail_offset = Some(offset);
} else {
break;
}
}
}
offset -= ch.len_utf8();
prev_ch = Some(ch);
}

let trail = trail_offset.map_or(trail, |trail_offset: usize| {
map.clip_point(trail_offset.to_display_point(map), Bias::Left)
});

(trail, map.clip_point(offset.to_display_point(map), Bias::Left))
}
/// Finds the location of a bonudary
pub fn find_boundary_trail(
map: &DisplaySnapshot,
head: DisplayPoint,
trail: DisplayPoint,
mut is_boundary: impl FnMut(char, char) -> bool,
) -> (DisplayPoint, DisplayPoint) {
let mut offset = head.to_offset(map, Bias::Right);
let mut trail_offset = None;

let mut prev_ch = map.buffer_snapshot.reversed_chars_at(offset).next();
let mut forward = map.buffer_snapshot.chars_at(offset).peekable();

// Skip newlines
while let Some(&ch) = forward.peek() {
if ch == '\n' {
prev_ch = forward.next();
offset += ch.len_utf8();
trail_offset = Some(offset);
} else {
break;
}
}

// Find the boundary
let start_offset = offset;
for ch in forward {
if let Some(prev_ch) = prev_ch {
if is_boundary(prev_ch, ch) {
if start_offset == offset {
trail_offset = Some(offset);
} else {
break;
}
}
}
offset += ch.len_utf8();
prev_ch = Some(ch);
}

let trail = trail_offset.map_or(trail, |trail_offset: usize| {
map.clip_point(trail_offset.to_display_point(map), Bias::Right)
});

(trail, map.clip_point(offset.to_display_point(map), Bias::Right))
}

pub fn find_boundary(
map: &DisplaySnapshot,
from: DisplayPoint,
Expand Down
1 change: 1 addition & 0 deletions crates/editor/src/selections_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ impl<'a> MutableSelectionsCollection<'a> {
selection.set_head(new_head, new_goal);
});
}


pub fn move_cursors_with(
&mut self,
Expand Down
25 changes: 25 additions & 0 deletions crates/text/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ impl<T: Copy + Ord> Selection<T> {
}
self.goal = new_goal;
}

pub fn set_tail(&mut self, tail: T, new_goal: SelectionGoal) {
if tail.cmp(&self.head()) <= Ordering::Equal {
if self.reversed {
self.end = self.start;
self.reversed = false;
}
self.start = tail;
} else {
if !self.reversed {
self.start = self.end;
self.reversed = true;
}
self.end = tail;
}
self.goal = new_goal;
}

pub fn swap_head_tail(&mut self) {
if self.reversed {
self.reversed = false;
} else {
std::mem::swap(&mut self.start, &mut self.end);
}
}
}

impl<T: Copy> Selection<T> {
Expand Down
Loading
Loading