Skip to content

Commit

Permalink
fix: improve performance for calculate selected lines and fix it
Browse files Browse the repository at this point in the history
  • Loading branch information
metiftikci committed Oct 28, 2023
1 parent e6232b6 commit 49d2da2
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 26 deletions.
29 changes: 9 additions & 20 deletions src/buffer/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,18 @@ impl Buffer {
}

if let BufferMode::Visual = self.mode {
let mut reader = TextReader::new(&self.lines);
let (from, to) = if self.selection.start < self.cursor {
(self.selection.start.clone(), self.cursor.clone())
} else {
(self.cursor.clone(), self.selection.start.clone())
};
let _ = reader.set_cursor(from);

while reader.get_cursor() <= to {
let mut highlight = Highlight {
let reader = TextReader::new(&self.lines);
let selections =
reader.get_text_positions(self.selection.start.clone(), self.cursor.clone());

for selection in selections.iter() {
let highlight = Highlight {
name: HL_SELECTED_TEXT,
row: reader.get_cursor_y(),
start: reader.get_cursor_x(),
end: reader.get_cursor_x(),
row: selection.row,
start: selection.start,
end: selection.end,
};
while !reader.is_line_last_x() && reader.get_cursor() < to {
let _ = reader.next();
if reader.get_cursor() <= to {
highlight.end += 1;
}
}
list.push(highlight);
let _ = reader.next();
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/buffer/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ impl Buffer {
}

pub fn get_text(&self, location1: Point<usize>, location2: Point<usize>) -> String {
let (from, to) = if location1 < location2 {
(location1.clone(), location2.clone())
} else {
(location2.clone(), location1.clone())
};
let (from, to) = Point::order(location1, location2);

let mut result: Vec<String> = Vec::new();

Expand Down
1 change: 1 addition & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod editable_text;
pub mod key;
pub mod point;
pub mod range;
pub mod rectangle;
pub mod size;
pub mod style;
Expand Down
10 changes: 10 additions & 0 deletions src/core/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ impl<T> Point<T> {
}
}

impl<T: PartialOrd> Point<T> {
pub fn order(p1: Point<T>, p2: Point<T>) -> (Self, Self) {
if p1 <= p2 {
(p1, p2)
} else {
(p2, p1)
}
}
}

impl<T: PartialOrd> PartialOrd for Point<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.y < other.y {
Expand Down
12 changes: 12 additions & 0 deletions src/core/range.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::core::point::Point;

pub struct Range<T> {
pub start: Point<T>,
pub end: Point<T>,
}

impl<T> Range<T> {
pub fn new(start: Point<T>, end: Point<T>) -> Self {
Self { start, end }
}
}
24 changes: 23 additions & 1 deletion src/core/text_reader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::core::point::Point;
use crate::core::{point::Point, text_position::TextPosition};

pub struct TextReader<'a> {
cursor: Point<usize>,
Expand Down Expand Up @@ -82,6 +82,28 @@ impl<'a> TextReader<'a> {
pub fn is_text_last_x(&self) -> bool {
self.cursor.y + 1 == self.lines.len() && self.is_line_last_x()
}

pub fn get_text_positions(&self, p1: Point<usize>, p2: Point<usize>) -> Vec<TextPosition> {
let (from, to) = Point::order(p1, p2);

let mut result: Vec<TextPosition> = Vec::new();

for y in from.y..(to.y + 1) {
let x1 = if from.y == y { from.x } else { 0 };
let x2 = if to.y == y {
to.x
} else {
self.get_line_last_x(y)
};
result.push(TextPosition {
row: y,
start: x1,
end: x2,
});
}

result
}
}

#[cfg(test)]
Expand Down

0 comments on commit 49d2da2

Please sign in to comment.