Skip to content

Commit

Permalink
ViEditor: update modit and improve put implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Dec 5, 2023
1 parent de6f2c7 commit b7bdd62
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fontdb = { version = "0.16.0", default-features = false }
hashbrown = { version = "0.14.1", optional = true, default-features = false }
libm = "0.2.8"
log = "0.4.20"
modit = { version = "0.1.2", optional = true }
modit = { version = "0.1.3", optional = true }
rangemap = "1.4.0"
rustc-hash = { version = "1.1.0", default-features = false }
rustybuzz = { version = "0.11.0", default-features = false, features = ["libm"] }
Expand Down
66 changes: 63 additions & 3 deletions src/edit/vi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,14 @@ impl<'a> Edit for ViEditor<'a> {
return;
}
Event::Backspace => Action::Backspace,
Event::BackspaceInLine => {
let cursor = editor.cursor();
if cursor.index > 0 {
Action::Backspace
} else {
return;
}
}
Event::ChangeStart => {
editor.start_change();
return;
Expand All @@ -385,15 +393,67 @@ impl<'a> Edit for ViEditor<'a> {
return;
}
Event::Delete => Action::Delete,
Event::DeleteInLine => {
let cursor = editor.cursor();
let buffer = editor.buffer();
if cursor.index < buffer.lines[cursor.line].text().len() {
Action::Delete
} else {
return;
}
}
Event::Escape => Action::Escape,
Event::Insert(c) => Action::Insert(c),
Event::NewLine => Action::Enter,
Event::Put { register, after } => {
if let Some((selection, data)) = self.registers.get(&register) {
editor.start_change();
editor.delete_selection();
//TODO: handle after/before and select by line
editor.insert_string(data, None);
if editor.delete_selection() {
editor.insert_string(data, None);
} else {
match selection {
Selection::Normal(_) | Selection::None => {
let mut cursor = editor.cursor();
if after {
let buffer = editor.buffer();
let text = buffer.lines[cursor.line].text();
if let Some(c) = text[cursor.index..].chars().next() {
cursor.index += c.len_utf8();
}
editor.set_cursor(cursor);
}
editor.insert_at(cursor, data, None);
}
Selection::Line(_) => {
let mut cursor = editor.cursor();
if after {
// Insert at next line
cursor.line += 1;
} else {
// Previous line will be moved down, so set cursor to next line
cursor.line += 1;
editor.set_cursor(cursor);
cursor.line -= 1;
}
// Insert at start of line
cursor.index = 0;

// Insert text
editor.insert_at(cursor, "\n", None);
editor.insert_at(cursor, data, None);

// Hack to allow immediate up/down
editor.shape_as_needed(font_system);

// Move to inserted line, preserving cursor x position
if after {
editor.action(font_system, Action::Down);
} else {
editor.action(font_system, Action::Up);
}
}
}
}
finish_change(editor, &mut self.commands, &mut self.changed);
}
return;
Expand Down

0 comments on commit b7bdd62

Please sign in to comment.