Highlight first line of multi-line comments

This commit is contained in:
Timothy Warren 2021-03-16 13:38:50 -04:00
parent 8169adcb2f
commit 62df35a751
3 changed files with 38 additions and 1 deletions

View File

@ -24,11 +24,15 @@ pub struct Position {
pub y: usize,
}
/** The message to display on the status line */
struct StatusMessage {
text: String,
time: Instant,
}
/**
* Add methods to the StatusMessage
*/
impl StatusMessage {
fn default() -> Self {
Self::from("")
@ -42,6 +46,7 @@ impl StatusMessage {
}
}
/// The main editor object
pub struct Editor {
should_quit: bool,
terminal: Terminal,

View File

@ -147,6 +147,6 @@ impl HighlightingOptions {
}
pub fn multiline_comments(&self) -> bool {
&self.multiline_comments
self.multiline_comments
}
}

View File

@ -356,6 +356,37 @@ impl Row {
false
}
#[allow(clippy::indexing_slicing, clippy::integer_arithmetic)]
fn highlight_multiline_comment(
&mut self,
index: &mut usize,
opts: &HighlightingOptions,
c: char,
chars: &[char],
) -> bool {
if opts.multiline_comments() && c == '/' && *index < chars.len() {
if let Some(next_char) = chars.get(index.saturating_add(1)) {
if *next_char == '*' {
let closing_index =
if let Some(closing_index) = self.string[*index + 2..].find("*/") {
*index + closing_index + 4
} else {
chars.len()
};
for _ in *index..closing_index {
self.highlighting.push(highlighting::Type::MultilineComment);
*index +=1;
}
return true;
}
}
}
false
}
fn highlight_string(
&mut self,
index: &mut usize,
@ -427,6 +458,7 @@ impl Row {
while let Some(c) = chars.get(index) {
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)
|| self.highlight_primary_keywords(&mut index, &opts, &chars)
|| self.highlight_secondary_keywords(&mut index, &opts, &chars)
|| self.highlight_string(&mut index, opts, *c, &chars)