sync commit

This commit is contained in:
Joe Bellus 2022-10-15 20:19:35 -04:00
parent 1acc998d7a
commit f590338805
2 changed files with 70 additions and 23 deletions

View File

@ -43,12 +43,8 @@ impl EditorData {
pub fn move_cursor(&mut self, idx: usize) {
let has_selection = self.cursor_pos != self.selection_pos;
if idx <= self.content.len_chars() {
self.cursor_pos = idx;
}
if self.mode == EditMode::Normal && self.cursor_pos == 0 {
self.cursor_pos = 1;
}
self.cursor_pos = idx;
if !has_selection {
self.deselect();
@ -116,8 +112,17 @@ impl EditorData {
}
pub fn cursor_left(&mut self) {
if self.cursor_pos > 0 {
self.move_cursor(self.cursor_pos - 1);
match self.mode {
EditMode::Insert => {
if self.cursor_pos > 0 {
self.move_cursor(self.cursor_pos - 1);
}
}
EditMode::Normal => {
if self.cursor_pos > self.current_line_start() {
self.move_cursor(self.cursor_pos - 1);
}
}
}
}
@ -155,13 +160,7 @@ impl EditorData {
let new_pos = start_of_next_line + line_pos.min(next_line_len);
if self.cursor_pos == new_pos {
self.move_cursor(new_pos + 1);
} else {
self.move_cursor(new_pos);
}
println!(" ");
dbg!(line_idx, new_pos, start_of_next_line);
self.move_cursor(new_pos);
}
}
@ -182,8 +181,21 @@ impl EditorData {
pub fn cursor_right(&mut self) {
let has_selection = self.cursor_pos != self.selection_pos;
if self.cursor_pos < self.content.len_chars() {
self.move_cursor(self.cursor_pos + 1);
match self.mode {
EditMode::Insert => {
if self.cursor_pos < self.content.len_chars() {
self.move_cursor(self.cursor_pos + 1);
}
}
EditMode::Normal => {
if self.current_line_index() == self.content.len_lines() - 1 {
if self.cursor_pos < self.content.len_chars() - 1 {
self.move_cursor(self.cursor_pos + 1);
}
} else if self.current_column() < (self.current_line().len_chars().max(2) - 2) {
self.move_cursor(self.cursor_pos + 1);
}
}
}
if !has_selection {
@ -246,11 +258,32 @@ impl EditorData {
}
}
fn current_line(&self) -> usize {
pub fn current_line(&self) -> ropey::RopeSlice {
self.content
.get_slice(
self.current_line_start()
..self
.content
.line_to_char(self.current_line_index() + 1)
.min(self.content.len_chars()),
)
.unwrap()
}
pub fn current_line_index(&self) -> usize {
self.content.char_to_line(self.cursor_pos)
}
fn current_char(&self) -> Option<char> {
pub fn current_line_start(&self) -> usize {
self.content
.line_to_char(self.content.char_to_line(self.cursor_pos))
}
pub fn current_column(&self) -> usize {
self.cursor_pos - self.current_line_start()
}
pub fn current_char(&self) -> Option<char> {
self.content.get_char(self.cursor_pos)
}
}

View File

@ -68,10 +68,13 @@ impl AbacusEditor {
}
if data.mode == EditMode::Normal {
if let Some(char_rect) = layout
.rects_for_range(data.cursor_pos..(data.cursor_pos + 1))
.last()
{
let range = if data.current_line() == "\n" {
(data.cursor_pos.max(1) - 1)..data.cursor_pos
} else {
data.cursor_pos..(data.cursor_pos + 1)
};
if let Some(char_rect) = layout.rects_for_range(range).last() {
if char_rect.width() == 0. {
let rect = Rect::new(
char_rect.min_x(),
@ -434,6 +437,17 @@ pub fn editor_header() -> impl Widget<Block> {
ctx.submit_command(crate::commands::RENAME_BLOCK.with(data.index));
}),
)
.with_child(
Label::dynamic(|data: &Block, _| {
format!(
"{}:{}",
data.editor_data.current_line_index() + 1,
data.editor_data.current_column() + 1,
)
})
.with_font(FontDescriptor::new(FontFamily::SANS_SERIF).with_size(14.0))
.padding(5.0),
)
.with_flex_spacer(1.0)
.with_child(
Container::new(Padding::new(10.0, Svg::new(run_svg).fix_width(10.0)))