From dc89679cc81cd519d4e98f0d1a536ede4377f0a0 Mon Sep 17 00:00:00 2001 From: metiftikci Date: Sat, 28 Oct 2023 17:17:06 +0300 Subject: [PATCH] feat: add selected text highlight --- src/buffer.rs | 2 +- src/buffer/highlight.rs | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index 37dec98..3731bbc 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -75,7 +75,7 @@ impl Buffer { finds: vec![], clipboard: None, }; - buffer.set_static_highlights(); + buffer.set_default_styles(); buffer.set_size(area); buffer } diff --git a/src/buffer/highlight.rs b/src/buffer/highlight.rs index aa2a302..7882445 100644 --- a/src/buffer/highlight.rs +++ b/src/buffer/highlight.rs @@ -1,6 +1,6 @@ use crate::{ - buffer::Buffer, - core::{rectangle::Rectangle, style::Style}, + buffer::{mode::BufferMode, Buffer}, + core::{rectangle::Rectangle, style::Style, text_reader::TextReader}, theme::THEME_ONE as T, }; @@ -14,6 +14,7 @@ pub struct Highlight { pub const HL_CURRENT_LINE: &str = "CurrentLine"; pub const HL_CURRENT_LINE_TEXT: &str = "CurrentLineText"; pub const HL_FIND_TEXT: &str = "FindText"; +pub const HL_SELECTED_TEXT: &str = "SelectedText"; impl Highlight { pub fn is_visible_in_area(&self, area: Rectangle) -> bool { @@ -25,9 +26,13 @@ impl Highlight { } impl Buffer { - pub fn set_static_highlights(&mut self) { + pub fn set_default_styles(&mut self) { self.styles .insert(HL_FIND_TEXT, Style::new(T.text_finded_fg, T.text_finded_bg)); + self.styles.insert( + HL_SELECTED_TEXT, + Style::new(T.text_selected_fg, T.text_selected_bg), + ); } pub fn get_dynamic_highlights(&self) -> Vec { @@ -49,6 +54,31 @@ 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 { + name: HL_SELECTED_TEXT, + row: reader.get_cursor_y(), + start: reader.get_cursor_x(), + end: reader.get_cursor_x(), + }; + while reader.get_cursor() <= to && !reader.is_line_last_x() { + let _ = reader.next(); + highlight.end += 1; + } + list.push(highlight); + let _ = reader.next(); + } + } + list }