2021-03-15 13:53:30 -04:00
|
|
|
use crate::highlighting;
|
2021-03-16 10:38:26 -04:00
|
|
|
use crate::HighlightingOptions;
|
2021-03-12 16:29:19 -05:00
|
|
|
use crate::SearchDirection;
|
2021-03-08 10:43:40 -05:00
|
|
|
use std::cmp;
|
2021-03-12 17:46:31 -05:00
|
|
|
use termion::color;
|
2021-03-08 14:21:24 -05:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
2021-03-08 10:43:40 -05:00
|
|
|
|
2021-03-10 13:48:21 -05:00
|
|
|
#[derive(Default)]
|
2021-03-08 10:21:06 -05:00
|
|
|
pub struct Row {
|
|
|
|
string: String,
|
2021-03-15 13:53:30 -04:00
|
|
|
highlighting: Vec<highlighting::Type>,
|
2021-03-08 14:21:24 -05:00
|
|
|
len: usize,
|
2021-03-08 10:43:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&str> for Row {
|
|
|
|
fn from(slice: &str) -> Self {
|
2021-03-10 16:21:07 -05:00
|
|
|
Self {
|
2021-03-08 10:43:40 -05:00
|
|
|
string: String::from(slice),
|
2021-03-15 13:53:30 -04:00
|
|
|
highlighting: Vec::new(),
|
2021-03-10 16:21:07 -05:00
|
|
|
len: slice.graphemes(true).count(),
|
|
|
|
}
|
2021-03-08 10:43:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Row {
|
|
|
|
pub fn render(&self, start: usize, end: usize) -> String {
|
|
|
|
let end = cmp::min(end, self.string.len());
|
|
|
|
let start = cmp::min(start, end);
|
2021-03-08 14:21:24 -05:00
|
|
|
|
|
|
|
let mut result = String::new();
|
2021-03-15 14:37:41 -04:00
|
|
|
let mut current_highlighting = &highlighting::Type::None;
|
2021-03-08 14:21:24 -05:00
|
|
|
|
2021-03-10 16:09:46 -05:00
|
|
|
#[allow(clippy::integer_arithmetic)]
|
2021-03-15 14:37:41 -04:00
|
|
|
for (index, grapheme) in self.string[..]
|
2021-03-08 14:21:24 -05:00
|
|
|
.graphemes(true)
|
2021-03-15 14:37:41 -04:00
|
|
|
.enumerate()
|
2021-03-08 14:21:24 -05:00
|
|
|
.skip(start)
|
|
|
|
.take(end - start)
|
|
|
|
{
|
2021-03-12 17:46:31 -05:00
|
|
|
if let Some(c) = grapheme.chars().next() {
|
2021-03-15 14:37:41 -04:00
|
|
|
let highlighting_type = self
|
|
|
|
.highlighting
|
|
|
|
.get(index)
|
|
|
|
.unwrap_or(&highlighting::Type::None);
|
|
|
|
|
|
|
|
if highlighting_type != current_highlighting {
|
|
|
|
current_highlighting = highlighting_type;
|
|
|
|
|
|
|
|
let start_highlight =
|
|
|
|
format!("{}", termion::color::Fg(highlighting_type.to_color()));
|
|
|
|
|
|
|
|
result.push_str(&start_highlight[..]);
|
|
|
|
}
|
|
|
|
|
2021-03-12 17:46:31 -05:00
|
|
|
if c == '\t' {
|
|
|
|
result.push_str(" ");
|
|
|
|
} else {
|
|
|
|
result.push(c);
|
|
|
|
}
|
2021-03-08 14:21:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 14:37:41 -04:00
|
|
|
let end_highlight = format!("{}", termion::color::Fg(color::Reset));
|
|
|
|
result.push_str(&end_highlight[..]);
|
|
|
|
|
2021-03-08 14:21:24 -05:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.len
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len == 0
|
|
|
|
}
|
|
|
|
|
2021-03-10 13:48:21 -05:00
|
|
|
pub fn insert(&mut self, at: usize, c: char) {
|
|
|
|
if at >= self.len() {
|
|
|
|
self.string.push(c);
|
2021-03-10 16:21:07 -05:00
|
|
|
self.len += 1;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut result = String::new();
|
|
|
|
let mut length = 0;
|
|
|
|
|
|
|
|
for (index, grapheme) in self.string[..].graphemes(true).enumerate() {
|
|
|
|
length += 1;
|
2021-03-10 13:48:21 -05:00
|
|
|
|
2021-03-10 16:21:07 -05:00
|
|
|
if index == at {
|
|
|
|
length += 1;
|
|
|
|
result.push(c);
|
|
|
|
}
|
2021-03-10 13:48:21 -05:00
|
|
|
|
2021-03-10 16:21:07 -05:00
|
|
|
result.push_str(grapheme);
|
2021-03-10 13:48:21 -05:00
|
|
|
}
|
|
|
|
|
2021-03-10 16:21:07 -05:00
|
|
|
self.len = length;
|
|
|
|
self.string = result;
|
2021-03-10 13:48:21 -05:00
|
|
|
}
|
|
|
|
|
2021-03-10 16:09:46 -05:00
|
|
|
#[allow(clippy::integer_arithmetic)]
|
2021-03-10 13:48:21 -05:00
|
|
|
pub fn delete(&mut self, at: usize) {
|
|
|
|
if at >= self.len() {
|
2021-03-12 16:29:19 -05:00
|
|
|
return;
|
2021-03-10 16:21:07 -05:00
|
|
|
}
|
2021-03-10 13:48:21 -05:00
|
|
|
|
2021-03-10 16:21:07 -05:00
|
|
|
let mut result = String::new();
|
|
|
|
let mut length = 0;
|
2021-03-10 13:48:21 -05:00
|
|
|
|
2021-03-10 16:21:07 -05:00
|
|
|
for (index, grapheme) in self.string[..].graphemes(true).enumerate() {
|
|
|
|
if index != at {
|
|
|
|
length += 1;
|
|
|
|
result.push_str(grapheme);
|
|
|
|
}
|
2021-03-10 13:48:21 -05:00
|
|
|
}
|
|
|
|
|
2021-03-10 16:21:07 -05:00
|
|
|
self.len = length;
|
|
|
|
self.string = result;
|
2021-03-10 13:48:21 -05:00
|
|
|
}
|
2021-03-10 14:37:58 -05:00
|
|
|
|
|
|
|
pub fn append(&mut self, new: &Self) {
|
|
|
|
self.string = format!("{}{}", self.string, new.string);
|
2021-03-10 16:21:07 -05:00
|
|
|
self.len += new.len;
|
2021-03-10 14:37:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn split(&mut self, at: usize) -> Self {
|
2021-03-10 16:21:07 -05:00
|
|
|
let mut row = String::new();
|
|
|
|
let mut length = 0;
|
|
|
|
let mut splitted_row = String::new();
|
|
|
|
let mut splittend_length = 0;
|
|
|
|
|
|
|
|
for (index, grapheme) in self.string[..].graphemes(true).enumerate() {
|
|
|
|
if index < at {
|
|
|
|
length += 1;
|
|
|
|
row.push_str(grapheme);
|
|
|
|
} else {
|
|
|
|
splittend_length += 1;
|
|
|
|
splitted_row.push_str(grapheme);
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 14:37:58 -05:00
|
|
|
|
2021-03-10 16:21:07 -05:00
|
|
|
self.string = row;
|
|
|
|
self.len = length;
|
2021-03-10 14:37:58 -05:00
|
|
|
|
2021-03-10 16:21:07 -05:00
|
|
|
Self {
|
|
|
|
string: splitted_row,
|
|
|
|
len: splittend_length,
|
2021-03-15 13:53:30 -04:00
|
|
|
highlighting: Vec::new(),
|
2021-03-10 16:21:07 -05:00
|
|
|
}
|
2021-03-10 14:37:58 -05:00
|
|
|
}
|
2021-03-10 14:51:11 -05:00
|
|
|
|
|
|
|
pub fn as_bytes(&self) -> &[u8] {
|
|
|
|
self.string.as_bytes()
|
|
|
|
}
|
2021-03-12 11:50:28 -05:00
|
|
|
|
2021-03-12 16:29:19 -05:00
|
|
|
pub fn find(&self, query: &str, at: usize, direction: SearchDirection) -> Option<usize> {
|
2021-03-15 14:53:08 -04:00
|
|
|
if at > self.len || query.is_empty() {
|
2021-03-12 16:29:19 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (start, end) = match direction {
|
|
|
|
SearchDirection::Forward => (at, self.len),
|
|
|
|
SearchDirection::Backward => (0, at),
|
|
|
|
};
|
|
|
|
|
|
|
|
let substring: String = self.string[..]
|
|
|
|
.graphemes(true)
|
|
|
|
.skip(start)
|
|
|
|
.take(end - start)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let matching_byte_index = if direction == SearchDirection::Forward {
|
|
|
|
substring.find(query)
|
|
|
|
} else {
|
|
|
|
substring.rfind(query)
|
|
|
|
};
|
2021-03-12 12:17:32 -05:00
|
|
|
|
2021-03-12 11:50:28 -05:00
|
|
|
if let Some(matching_byte_index) = matching_byte_index {
|
|
|
|
for (grapheme_index, (byte_index, _)) in
|
2021-03-12 12:17:32 -05:00
|
|
|
substring[..].grapheme_indices(true).enumerate()
|
2021-03-12 11:50:28 -05:00
|
|
|
{
|
|
|
|
if matching_byte_index == byte_index {
|
2021-03-12 16:29:19 -05:00
|
|
|
return Some(start + grapheme_index);
|
2021-03-12 11:50:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2021-03-15 13:53:30 -04:00
|
|
|
|
2021-03-16 11:39:13 -04:00
|
|
|
fn highlight_match(&mut self, word: Option<&str>) {
|
2021-03-15 14:53:08 -04:00
|
|
|
if let Some(word) = word {
|
2021-03-16 11:39:13 -04:00
|
|
|
if word.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-15 14:53:08 -04:00
|
|
|
|
2021-03-16 11:39:13 -04:00
|
|
|
let mut index = 0;
|
|
|
|
while let Some(search_match) = self.find(word, index, SearchDirection::Forward) {
|
2021-03-15 14:53:08 -04:00
|
|
|
if let Some(next_index) = search_match.checked_add(word[..].graphemes(true).count())
|
|
|
|
{
|
2021-03-16 11:39:13 -04:00
|
|
|
#[allow(clippy::indexing_slicing)]
|
|
|
|
for i in index.saturating_add(search_match)..next_index {
|
|
|
|
self.highlighting[i] = highlighting::Type::Match;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = next_index;
|
2021-03-15 14:53:08 -04:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 11:39:13 -04:00
|
|
|
}
|
2021-03-15 14:53:08 -04:00
|
|
|
|
2021-03-16 12:18:37 -04:00
|
|
|
fn highlight_str(
|
|
|
|
&mut self,
|
|
|
|
index: &mut usize,
|
|
|
|
substring: &str,
|
|
|
|
chars: &[char],
|
|
|
|
hl_type: highlighting::Type,
|
|
|
|
) -> bool {
|
|
|
|
if substring.is_empty() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (substring_index, c) in substring.chars().enumerate() {
|
|
|
|
if let Some(next_char) = chars.get(index.saturating_add(substring_index)) {
|
|
|
|
if *next_char != c {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _ in 0..substring.len() {
|
|
|
|
self.highlighting.push(hl_type);
|
|
|
|
*index += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-03-16 12:40:02 -04:00
|
|
|
fn highlight_keywords(
|
2021-03-16 12:18:37 -04:00
|
|
|
&mut self,
|
|
|
|
index: &mut usize,
|
|
|
|
chars: &[char],
|
2021-03-16 12:40:02 -04:00
|
|
|
keywords: &[String],
|
|
|
|
hl_type: highlighting::Type,
|
2021-03-16 12:18:37 -04:00
|
|
|
) -> bool {
|
|
|
|
if *index > 0 {
|
|
|
|
#[allow(clippy::indexing_slicing, clippy::integer_arithmetic)]
|
|
|
|
let prev_char = chars[*index - 1];
|
|
|
|
if !is_separator(prev_char) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 12:40:02 -04:00
|
|
|
for word in keywords {
|
2021-03-16 12:18:37 -04:00
|
|
|
if *index < chars.len().saturating_sub(word.len()) {
|
|
|
|
#[allow(clippy::indexing_slicing, clippy::integer_arithmetic)]
|
|
|
|
let next_char = chars[*index + word.len()];
|
|
|
|
if !is_separator(next_char) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 12:40:02 -04:00
|
|
|
if self.highlight_str(index, &word, chars, hl_type) {
|
2021-03-16 12:18:37 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2021-03-16 12:40:02 -04:00
|
|
|
fn highlight_primary_keywords(
|
|
|
|
&mut self,
|
|
|
|
index: &mut usize,
|
|
|
|
opts: &HighlightingOptions,
|
|
|
|
chars: &[char],
|
|
|
|
) -> bool {
|
|
|
|
self.highlight_keywords(
|
|
|
|
index,
|
|
|
|
chars,
|
|
|
|
opts.primary_keywords(),
|
|
|
|
highlighting::Type::PrimaryKeywords,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight_secondary_keywords(
|
|
|
|
&mut self,
|
|
|
|
index: &mut usize,
|
|
|
|
opts: &HighlightingOptions,
|
|
|
|
chars: &[char],
|
|
|
|
) -> bool {
|
|
|
|
self.highlight_keywords(
|
|
|
|
index,
|
|
|
|
chars,
|
|
|
|
opts.secondary_keywords(),
|
|
|
|
highlighting::Type::SecondaryKeywords,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-16 11:39:13 -04:00
|
|
|
fn highlight_char(
|
|
|
|
&mut self,
|
|
|
|
index: &mut usize,
|
2021-03-16 12:18:37 -04:00
|
|
|
opts: &HighlightingOptions,
|
2021-03-16 11:39:13 -04:00
|
|
|
c: char,
|
|
|
|
chars: &[char],
|
|
|
|
) -> bool {
|
|
|
|
if opts.characters() && c == '\'' {
|
|
|
|
if let Some(next_char) = chars.get(index.saturating_add(1)) {
|
|
|
|
let closing_index = if *next_char == '\\' {
|
|
|
|
index.saturating_add(3)
|
|
|
|
} else {
|
|
|
|
index.saturating_add(2)
|
|
|
|
};
|
2021-03-15 14:53:08 -04:00
|
|
|
|
2021-03-16 11:39:13 -04:00
|
|
|
if let Some(closing_char) = chars.get(closing_index) {
|
|
|
|
if *closing_char == '\'' {
|
|
|
|
for _ in 0..=closing_index.saturating_sub(*index) {
|
|
|
|
self.highlighting.push(highlighting::Type::Character);
|
|
|
|
*index += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-03-15 14:53:08 -04:00
|
|
|
}
|
|
|
|
}
|
2021-03-16 11:39:13 -04:00
|
|
|
}
|
2021-03-15 13:53:30 -04:00
|
|
|
|
2021-03-16 11:39:13 -04:00
|
|
|
false
|
|
|
|
}
|
2021-03-16 09:28:07 -04:00
|
|
|
|
2021-03-16 11:39:13 -04:00
|
|
|
fn highlight_comment(
|
|
|
|
&mut self,
|
|
|
|
index: &mut usize,
|
2021-03-16 12:18:37 -04:00
|
|
|
opts: &HighlightingOptions,
|
2021-03-16 11:39:13 -04:00
|
|
|
c: char,
|
|
|
|
chars: &[char],
|
|
|
|
) -> bool {
|
|
|
|
if opts.comments() && c == '/' && *index < chars.len() {
|
|
|
|
if let Some(next_char) = chars.get(index.saturating_add(1)) {
|
|
|
|
if *next_char == '/' {
|
|
|
|
for _ in *index..chars.len() {
|
|
|
|
self.highlighting.push(highlighting::Type::Comment);
|
|
|
|
*index += 1;
|
|
|
|
}
|
2021-03-16 10:57:15 -04:00
|
|
|
|
2021-03-16 11:39:13 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-03-16 10:57:15 -04:00
|
|
|
|
2021-03-16 11:39:13 -04:00
|
|
|
false
|
|
|
|
}
|
2021-03-16 10:57:15 -04:00
|
|
|
|
2021-03-16 11:39:13 -04:00
|
|
|
fn highlight_string(
|
|
|
|
&mut self,
|
|
|
|
index: &mut usize,
|
2021-03-16 12:18:37 -04:00
|
|
|
opts: &HighlightingOptions,
|
2021-03-16 11:39:13 -04:00
|
|
|
c: char,
|
|
|
|
chars: &[char],
|
|
|
|
) -> bool {
|
|
|
|
if opts.strings() && c == '"' {
|
|
|
|
loop {
|
|
|
|
self.highlighting.push(highlighting::Type::String);
|
|
|
|
*index += 1;
|
|
|
|
|
|
|
|
if let Some(next_char) = chars.get(*index) {
|
|
|
|
if *next_char == '"' {
|
|
|
|
break;
|
2021-03-16 10:57:15 -04:00
|
|
|
}
|
2021-03-16 11:39:13 -04:00
|
|
|
} else {
|
|
|
|
break;
|
2021-03-16 10:57:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 11:39:13 -04:00
|
|
|
self.highlighting.push(highlighting::Type::String);
|
|
|
|
*index += 1;
|
|
|
|
return true;
|
|
|
|
}
|
2021-03-16 10:45:19 -04:00
|
|
|
|
2021-03-16 11:39:13 -04:00
|
|
|
false
|
|
|
|
}
|
2021-03-16 10:45:19 -04:00
|
|
|
|
2021-03-16 11:39:13 -04:00
|
|
|
fn highlight_number(
|
|
|
|
&mut self,
|
|
|
|
index: &mut usize,
|
2021-03-16 12:18:37 -04:00
|
|
|
opts: &HighlightingOptions,
|
2021-03-16 11:39:13 -04:00
|
|
|
c: char,
|
|
|
|
chars: &[char],
|
|
|
|
) -> bool {
|
|
|
|
if opts.numbers() && c.is_ascii_digit() {
|
|
|
|
if *index > 0 {
|
|
|
|
#[allow(clippy::indexing_slicing, clippy::integer_arithmetic)]
|
|
|
|
let prev_char = chars[*index - 1];
|
2021-03-16 12:18:37 -04:00
|
|
|
if !is_separator(prev_char) {
|
2021-03-16 11:39:13 -04:00
|
|
|
return false;
|
2021-03-16 10:45:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 11:39:13 -04:00
|
|
|
loop {
|
|
|
|
self.highlighting.push(highlighting::Type::Number);
|
|
|
|
*index += 1;
|
|
|
|
if let Some(next_char) = chars.get(*index) {
|
|
|
|
if *next_char != '.' && !next_char.is_ascii_digit() {
|
2021-03-16 11:02:27 -04:00
|
|
|
break;
|
|
|
|
}
|
2021-03-16 10:38:26 -04:00
|
|
|
} else {
|
2021-03-16 11:39:13 -04:00
|
|
|
break;
|
2021-03-16 10:38:26 -04:00
|
|
|
}
|
2021-03-15 13:53:30 -04:00
|
|
|
}
|
2021-03-15 14:53:08 -04:00
|
|
|
|
2021-03-16 11:39:13 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2021-03-16 12:18:37 -04:00
|
|
|
pub fn highlight(&mut self, opts: &HighlightingOptions, word: Option<&str>) {
|
2021-03-16 11:39:13 -04:00
|
|
|
self.highlighting = Vec::new();
|
|
|
|
let chars: Vec<char> = self.string.chars().collect();
|
|
|
|
let mut index = 0;
|
|
|
|
|
|
|
|
while let Some(c) = chars.get(index) {
|
|
|
|
if self.highlight_char(&mut index, opts, *c, &chars)
|
|
|
|
|| self.highlight_comment(&mut index, opts, *c, &chars)
|
2021-03-16 12:18:37 -04:00
|
|
|
|| self.highlight_primary_keywords(&mut index, &opts, &chars)
|
2021-03-16 12:40:02 -04:00
|
|
|
|| self.highlight_secondary_keywords(&mut index, &opts, &chars)
|
2021-03-16 11:39:13 -04:00
|
|
|
|| self.highlight_string(&mut index, opts, *c, &chars)
|
|
|
|
|| self.highlight_number(&mut index, opts, *c, &chars)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.highlighting.push(highlighting::Type::None);
|
2021-03-15 14:53:08 -04:00
|
|
|
index += 1;
|
2021-03-15 13:53:30 -04:00
|
|
|
}
|
|
|
|
|
2021-03-16 11:39:13 -04:00
|
|
|
self.highlight_match(word);
|
2021-03-15 13:53:30 -04:00
|
|
|
}
|
2021-03-12 16:29:19 -05:00
|
|
|
}
|
2021-03-16 12:18:37 -04:00
|
|
|
|
|
|
|
fn is_separator(c: char) -> bool {
|
|
|
|
c.is_ascii_punctuation() || c.is_ascii_whitespace()
|
2021-03-16 12:40:02 -04:00
|
|
|
}
|