Skip to content
Open
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
13 changes: 13 additions & 0 deletions config/.oxrc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ event_mapping = {
["ctrl_right"] = function()
editor:move_next_word()
end,
["ctrl_shift_up"] = function()
editor:add_cursor_above()
end,
["ctrl_shift_down"] = function()
editor:add_cursor_below()
end,
["alt_shift_up"] = function()
editor:delete_cursor_below()
end,
["alt_shift_down"] = function()
editor:delete_cursor_above()
end,
["home"] = function()
editor:move_home()
end,
Expand Down Expand Up @@ -59,6 +71,7 @@ event_mapping = {
end,
["esc"] = function()
editor:cancel_selection()
editor:cancel_multi_cursors()
end,
["shift_home"] = function()
local n_moves = editor.cursor.x
Expand Down
96 changes: 95 additions & 1 deletion kaolinite/src/document/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::event::Status;
use crate::utils::{tab_boundaries_backward, tab_boundaries_forward, width};
use crate::{Document, Loc};
use std::cmp::{max, min};
use std::ops::Range;

/// Defines a cursor's position and any selection it may be covering
Expand All @@ -21,6 +22,7 @@ impl Document {
/// Select with the cursor up
pub fn select_up(&mut self) -> Status {
// Return if already at start of document
self.secondary_cursors.retain(|loc| loc.y != 0);
if self.loc().y == 0 {
return Status::StartOfFile;
}
Expand All @@ -45,8 +47,11 @@ impl Document {

/// Select with the cursor down
pub fn select_down(&mut self) -> Status {
let len_lines = self.len_lines();
// Return if already on end of document
if self.len_lines() < self.loc().y + 1 {
self.secondary_cursors
.retain(|loc| loc.y != (len_lines - 1));
if len_lines < self.loc().y + 1 {
return Status::EndOfFile;
}
self.cursor.loc.y += 1;
Expand Down Expand Up @@ -171,6 +176,90 @@ impl Document {
self.move_to(&Loc::at(0, last));
}

/// Add a cursor above
pub fn add_cursor_above(&mut self) {
let loc_y_now = self.cursor.loc.y;
let min_y = min(
loc_y_now,
self.secondary_cursors
.iter()
.map(|loc| loc.y)
.min()
.unwrap_or(loc_y_now),
);

if min_y > 0 {
self.secondary_cursors
.push(Loc::at(self.cursor.loc.x, min_y - 1));
}
}

/// Add a cursor below
pub fn add_cursor_below(&mut self) {
let loc_y_now = self.cursor.loc.y;
let max_y = max(
loc_y_now,
self.secondary_cursors
.iter()
.map(|loc| loc.y)
.max()
.unwrap_or(loc_y_now),
);

if max_y < self.len_lines() {
self.secondary_cursors
.push(Loc::at(self.cursor.loc.x, max_y + 1));
}
}

/// Delete a cursor above
pub fn delete_cursor_above(&mut self) {
if self.secondary_cursors.is_empty() {
return;
}

let loc_y_now = self.cursor.loc.y;
let mut min_y = min(
loc_y_now,
self.secondary_cursors
.iter()
.map(|loc| loc.y)
.min()
.unwrap_or(loc_y_now),
);

if min_y == loc_y_now {
self.move_down();
min_y += 1;
}

self.secondary_cursors.retain(|loc| loc.y != min_y);
}

/// Delete a cursor below
pub fn delete_cursor_below(&mut self) {
if self.secondary_cursors.is_empty() {
return;
}

let loc_y_now = self.cursor.loc.y;
let mut max_y = max(
loc_y_now,
self.secondary_cursors
.iter()
.map(|loc| loc.y)
.max()
.unwrap_or(loc_y_now),
);

if max_y == loc_y_now {
self.move_up();
max_y -= 1;
}

self.secondary_cursors.retain(|loc| loc.y != max_y);
}

/// Select to the top of the document
pub fn select_top(&mut self) {
self.select_to(&Loc::at(0, 0));
Expand Down Expand Up @@ -423,6 +512,11 @@ impl Document {
self.cursor.selection_end = self.cursor.loc;
}

/// Cancels the current multi cursors in the buffer
pub fn cancel_multi_cursors(&mut self) {
self.secondary_cursors.clear();
}

/// Create a new alternative cursor
pub fn new_cursor(&mut self, loc: Loc) {
if let Some(idx) = self.has_cursor(loc) {
Expand Down
24 changes: 24 additions & 0 deletions kaolinite/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,30 @@ fn fuzz() {
}
23 => doc.undo(),
24 => doc.redo(),
25 => {
doc.add_cursor_above();
Ok(())
}
26 => {
doc.add_cursor_below();
Ok(())
}
27 => {
doc.delete_cursor_above();
Ok(())
}
28 => {
doc.delete_cursor_below();
Ok(())
}
29 => {
doc.delete_cursor_below();
Ok(())
}
30 => {
doc.delete_cursor_below();
Ok(())
}
_ => Ok(()),
};
println!("{} | {}", doc.loc().x, doc.char_ptr);
Expand Down
4 changes: 2 additions & 2 deletions plugins/quickcomment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ function quickcomment:comment_start()
comment_start = "#"
elseif editor.document_type == "Clojure" then
comment_start = ";"
elseif editor.document_type == "Zsh" then
comment_start = "#"
elseif editor.document_type == "Batch" then
comment_start = "@REM"
else
comment_start = "//"
end
Expand Down
30 changes: 30 additions & 0 deletions src/config/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,30 @@ impl LuaUserData for Editor {
}
Ok(())
});
methods.add_method_mut("add_cursor_above", |_, editor, ()| {
if let Some(doc) = editor.try_doc_mut() {
doc.add_cursor_above();
}
Ok(())
});
methods.add_method_mut("add_cursor_below", |_, editor, ()| {
if let Some(doc) = editor.try_doc_mut() {
doc.add_cursor_below();
}
Ok(())
});
methods.add_method_mut("delete_cursor_above", |_, editor, ()| {
if let Some(doc) = editor.try_doc_mut() {
doc.delete_cursor_above();
}
Ok(())
});
methods.add_method_mut("delete_cursor_below", |_, editor, ()| {
if let Some(doc) = editor.try_doc_mut() {
doc.delete_cursor_below();
}
Ok(())
});
methods.add_method_mut("move_previous_word", |_, editor, ()| {
editor.prev_word();
editor.update_highlighter();
Expand Down Expand Up @@ -350,6 +374,12 @@ impl LuaUserData for Editor {
}
Ok(())
});
methods.add_method_mut("cancel_multi_cursors", |_, editor, ()| {
if let Some(doc) = editor.try_doc_mut() {
doc.cancel_multi_cursors();
}
Ok(())
});
methods.add_method_mut("cursor_to_viewport", |_, editor, ()| {
if let Some(doc) = editor.try_doc_mut() {
doc.bring_cursor_in_viewport();
Expand Down