Skip to content

Commit

Permalink
Fixed further selection issues (and cleaned up unclear indices in ren…
Browse files Browse the repository at this point in the history
…dering)
  • Loading branch information
curlpipe committed Nov 15, 2024
1 parent 49d9f93 commit 58bc7dc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
21 changes: 18 additions & 3 deletions kaolinite/src/document/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,26 @@ impl Document {

/// Will return the bounds of the current active selection
#[must_use]
pub fn selection_loc_bound(&self) -> (Loc, Loc) {
pub fn selection_loc_bound_disp(&self) -> (Loc, Loc) {
let mut left = self.cursor.loc;
let mut right = self.cursor.selection_end;
// Convert into character indices
left.x = self.character_idx(&left);
right.x = self.character_idx(&right);
if left > right {
std::mem::swap(&mut left, &mut right);
}
(left, right)
}

/// Will return the bounds of the current active selection
#[must_use]
pub fn selection_loc_bound(&self) -> (Loc, Loc) {
let (mut left, mut right) = self.selection_loc_bound_disp();
// Convert into character indices
left.x = self.character_idx(&left);
right.x = self.character_idx(&right);
(left, right)
}

/// Returns true if the provided location is within the current active selection
#[must_use]
pub fn is_loc_selected(&self, loc: Loc) -> bool {
Expand All @@ -370,6 +378,13 @@ impl Document {
left <= loc && loc < right
}

/// Returns true if the provided location is within the provided selection argument
#[must_use]
pub fn is_this_loc_selected_disp(&self, loc: Loc, selection_bound: (Loc, Loc)) -> bool {
let (left, right) = selection_bound;
left <= loc && loc < right
}

/// Will return the current active selection as a range over file characters
#[must_use]
pub fn selection_range(&self) -> Range<usize> {
Expand Down
15 changes: 9 additions & 6 deletions src/editor/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Editor {
let start = u16::try_from(first_line).unwrap_or(u16::MAX);
let end = start + u16::try_from(message.len()).unwrap_or(u16::MAX);
// Get other information
let selection = self.doc().selection_loc_bound();
let selection = self.doc().selection_loc_bound_disp();
// Render each line of the document
for y in 0..u16::try_from(h).unwrap_or(0) {
// Work out how long the line should be (accounting for help message if necessary)
Expand Down Expand Up @@ -124,7 +124,8 @@ impl Editor {
// Gather the tokens
let tokens = self.highlighter().line(idx, &line);
let tokens = trim_fit(&tokens, self.doc().offset.x, required_width, tab_width);
let mut x_pos = self.doc().character_idx(&self.doc().offset);
let mut x_disp = self.doc().offset.x;
let mut x_char = self.doc().character_idx(&self.doc().offset);
for token in tokens {
// Find out the text (and colour of that text)
let (text, colour) = match token {
Expand All @@ -149,8 +150,9 @@ impl Editor {
let underline = SetAttribute(Attribute::Underlined);
let no_underline = SetAttribute(Attribute::NoUnderline);
for c in text.chars() {
let at_loc = Loc { y: idx, x: x_pos };
let is_selected = self.doc().is_this_loc_selected(at_loc, selection);
let disp_loc = Loc { y: idx, x: x_disp };
let char_loc = Loc { y: idx, x: x_char };
let is_selected = self.doc().is_this_loc_selected_disp(disp_loc, selection);
// Render the correct colour
if is_selected {
if cache_bg != selection_bg {
Expand All @@ -172,7 +174,7 @@ impl Editor {
}
}
// Render multi-cursors
let multi_cursor_here = self.doc().has_cursor(at_loc).is_some();
let multi_cursor_here = self.doc().has_cursor(char_loc).is_some();
if multi_cursor_here {
display!(self, underline, Bg(Color::White), Fg(Color::Black));
}
Expand All @@ -182,7 +184,8 @@ impl Editor {
if multi_cursor_here {
display!(self, no_underline, cache_bg, cache_fg);
}
x_pos += 1;
x_char += 1;
x_disp += width(&c.to_string(), tab_width);
}
}
display!(self, editor_fg, editor_bg);
Expand Down
1 change: 1 addition & 0 deletions src/editor/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl Editor {
}

/// Handles a mouse event (dragging / clicking)
#[allow(clippy::too_many_lines)]
pub fn handle_mouse_event(&mut self, lua: &Lua, event: MouseEvent) {
match event.modifiers {
KeyModifiers::NONE => match event.kind {
Expand Down

0 comments on commit 58bc7dc

Please sign in to comment.