Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions assets/keymaps/vim.json
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,22 @@
"ctrl-o": "vim::TemporaryNormal"
}
},
{
"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)",
"use_layout_keys": true,
Expand Down
95 changes: 95 additions & 0 deletions crates/editor/src/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,101 @@ 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,
mut is_boundary: impl FnMut(char, char) -> bool,
) -> (Option<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(|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 boundary
pub fn find_boundary_trail(
map: &DisplaySnapshot,
head: DisplayPoint,
mut is_boundary: impl FnMut(char, char) -> bool,
) -> (Option<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(|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
10 changes: 7 additions & 3 deletions crates/language/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4493,7 +4493,7 @@ impl CharClassifier {
self.kind(c) == CharKind::Punctuation
}

pub fn kind(&self, c: char) -> CharKind {
pub fn kind_with(&self, c: char, ignore_punctuation: bool) -> CharKind {
if c.is_whitespace() {
return CharKind::Whitespace;
} else if c.is_alphanumeric() || c == '_' {
Expand All @@ -4503,20 +4503,24 @@ impl CharClassifier {
if let Some(scope) = &self.scope {
if let Some(characters) = scope.word_characters() {
if characters.contains(&c) {
if c == '-' && !self.for_completion && !self.ignore_punctuation {
if c == '-' && !self.for_completion && !ignore_punctuation {
return CharKind::Punctuation;
}
return CharKind::Word;
}
}
}

if self.ignore_punctuation {
if ignore_punctuation {
CharKind::Word
} else {
CharKind::Punctuation
}
}

pub fn kind(&self, c: char) -> CharKind {
self.kind_with(c, self.ignore_punctuation)
}
}

/// Find all of the ranges of whitespace that occur at the ends of lines
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