Highlight multi-line comments

This commit is contained in:
Timothy Warren 2021-03-16 14:07:39 -04:00
parent 62df35a751
commit 76e1b24855
2 changed files with 60 additions and 18 deletions

View File

@ -17,12 +17,13 @@ impl Document {
pub fn open(filename: &str) -> Result<Self, std::io::Error> {
let contents = fs::read_to_string(filename)?;
let file_type = FileType::from(filename);
let mut start_with_comment = false;
let mut rows = Vec::new();
for value in contents.lines() {
let mut row = Row::from(value);
row.highlight(&file_type.highlighting_options(), None);
start_with_comment = row.highlight(&file_type.highlighting_options(), None, start_with_comment);
rows.push(row);
}
@ -60,20 +61,17 @@ impl Document {
if c == '\n' {
self.insert_newline(at);
return;
}
if at.y == self.rows.len() {
} else if at.y == self.rows.len() {
let mut row = Row::default();
row.insert(0, c);
row.highlight(&self.file_type.highlighting_options(), None);
self.rows.push(row);
} else {
#[allow(clippy::indexing_slicing)]
let row = &mut self.rows[at.y];
row.insert(at.x, c);
row.highlight(&self.file_type.highlighting_options(), None);
}
self.highlight(None);
}
#[allow(clippy::integer_arithmetic, clippy::indexing_slicing)]
@ -90,23 +88,28 @@ impl Document {
let next_row = self.rows.remove(at.y + 1);
let row = &mut self.rows[at.y];
row.append(&next_row);
row.highlight(&self.file_type.highlighting_options(), None);
} else {
let row = &mut self.rows[at.y];
row.delete(at.x);
row.highlight(&self.file_type.highlighting_options(), None);
}
self.highlight(None);
}
pub fn save(&mut self) -> Result<(), Error> {
if let Some(file_name) = &self.file_name {
let mut file = fs::File::create(file_name)?;
self.file_type = FileType::from(file_name);
let mut start_with_comment = false;
for row in &mut self.rows {
file.write_all(row.as_bytes())?;
file.write_all(b"\n")?;
row.highlight(&self.file_type.highlighting_options(), None)
start_with_comment = row.highlight(
&self.file_type.highlighting_options(),
None,
start_with_comment,
);
}
// File has been cleaned! (Saved)
@ -117,8 +120,14 @@ impl Document {
}
pub fn highlight(&mut self, word: Option<&str>) {
let mut start_with_comment = false;
for row in &mut self.rows {
row.highlight(&self.file_type.highlighting_options(), word);
start_with_comment = row.highlight(
&self.file_type.highlighting_options(),
word,
start_with_comment,
);
}
}
@ -173,10 +182,7 @@ impl Document {
#[allow(clippy::indexing_slicing)]
let current_row = &mut self.rows[at.y];
let mut new_row = current_row.split(at.x);
current_row.highlight(&self.file_type.highlighting_options(), None);
new_row.highlight(&self.file_type.highlighting_options(), None);
let new_row = current_row.split(at.x);
#[allow(clippy::integer_arithmetic)]
self.rows.insert(at.y + 1, new_row);

View File

@ -376,7 +376,7 @@ impl Row {
for _ in *index..closing_index {
self.highlighting.push(highlighting::Type::MultilineComment);
*index +=1;
*index += 1;
}
return true;
@ -450,12 +450,42 @@ impl Row {
false
}
pub fn highlight(&mut self, opts: &HighlightingOptions, word: Option<&str>) {
#[allow(clippy::indexing_slicing, clippy::integer_arithmetic)]
pub fn highlight(
&mut self,
opts: &HighlightingOptions,
word: Option<&str>,
start_with_comment: bool,
) -> bool {
self.highlighting = Vec::new();
let chars: Vec<char> = self.string.chars().collect();
let mut index = 0;
let mut in_ml_comment = start_with_comment;
// Multi-line comments are needy
if in_ml_comment {
let closing_index = if let Some(closing_index) = self.string.find("*/") {
closing_index + 2
} else {
chars.len()
};
for _ in 0..closing_index {
self.highlighting.push(highlighting::Type::MultilineComment);
}
index = closing_index;
}
while let Some(c) = chars.get(index) {
if self.highlight_multiline_comment(&mut index, &opts, *c, &chars) {
in_ml_comment = true;
continue;
}
in_ml_comment = false;
if self.highlight_char(&mut index, opts, *c, &chars)
|| self.highlight_comment(&mut index, opts, *c, &chars)
|| self.highlight_multiline_comment(&mut index, opts, *c, &chars)
@ -472,6 +502,12 @@ impl Row {
}
self.highlight_match(word);
if in_ml_comment && &self.string[self.string.len().saturating_sub(2)..] != "*/" {
return true;
}
false
}
}